david-ar commited on
Commit
a0b41bb
·
verified ·
1 Parent(s): a0e7846

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. modeling_twentyq.py +22 -45
modeling_twentyq.py CHANGED
@@ -273,54 +273,31 @@ class TwentyQForCausalLM(PreTrainedModel, GenerationMixin):
273
  def play(self):
274
  """Interactive CLI mode — play 20Q in the terminal."""
275
  self._ensure_strings()
276
- self._rng = random.Random()
277
- print(f"\n Think of something and I'll try to guess it in 20 questions.\n")
278
  input(" Press Enter when ready... ")
279
- print()
280
 
281
- answers = []
282
- qnum = 0
283
-
284
- qnum += 1
285
- print(f" {qnum}. Is it Animal, Vegetable, Mineral, or Other?")
286
  while True:
287
- r = input(" > ").strip().lower()
288
- avm = AVM_WORDS.get(r) or AVM_WORDS.get(r[:1] if r else "", 0)
289
- if avm:
290
- answers.append((0, avm, False))
291
- break
292
- print()
293
-
294
- while qnum < 30:
295
- nc, best_t, best_s, cidx, cscores = self._rank_targets(answers)
296
- if nc == 0:
297
- print(" I'm stumped!")
298
  return
299
 
300
- if nc == 1 or qnum == 20 or qnum == 24 or qnum == 30 or (qnum >= 18 and nc <= 2):
301
- qnum += 1
302
- print(f" {qnum}. I'm guessing... {self.targets_str[best_t]}?")
303
- r = input(" (Y/N/Close) > ").strip().lower()
304
- if r.startswith("y"):
305
- print(f"\n I win! Got it in {qnum} questions.")
306
- return
307
- answers.append((best_t, 0, True))
308
- if qnum >= 25:
309
- break
310
- print()
311
- continue
312
-
313
- q = self._select_question(answers, nc, cidx)
314
- if q < 0:
315
- break
316
- qnum += 1
317
- print(f" {qnum}. {self.questions_str[q]}?")
318
- while True:
319
- r = input(" > ").strip().lower()
320
- ac = ANSWER_WORDS.get(r) or ANSWER_WORDS.get(r[:1] if r else "", 0)
321
- if ac and ac > 0:
322
- answers.append((q, ac, False))
323
- break
324
- print()
325
 
326
- print(f"\n I give up after {qnum} questions!")
 
 
 
 
273
  def play(self):
274
  """Interactive CLI mode — play 20Q in the terminal."""
275
  self._ensure_strings()
276
+ print("\n Think of something and I'll try to guess it in 20 questions.\n")
 
277
  input(" Press Enter when ready... ")
 
278
 
279
+ # Use the same generate() path as the pipeline
280
+ conversation = "Think of something and I'll try to guess it in 20 questions.\n"
 
 
 
281
  while True:
282
+ # Generate next response
283
+ conversation += "[A] "
284
+ ids = torch.tensor([list(conversation.encode("utf-8"))])
285
+ out = self.generate(ids)
286
+ response = bytes(out[0, ids.shape[1]:].tolist()).decode("utf-8", errors="replace")
287
+ conversation += response + "\n"
288
+
289
+ print(f"\n 20Q: {response}")
290
+ if "I win" in response or "stumped" in response:
 
 
291
  return
292
 
293
+ if "Animal, Vegetable, Mineral" in response:
294
+ hint = "(Animal/Vegetable/Mineral/Other)"
295
+ elif "guessing" in response.lower():
296
+ hint = "(Yes/No/Close)"
297
+ else:
298
+ hint = "(Yes/No/Probably/Doubtful/Maybe/Unknown)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299
 
300
+ reply = input(f" You {hint}: ").strip()
301
+ if not reply:
302
+ return
303
+ conversation += f"[U] {reply}\n"