Upload folder using huggingface_hub
Browse files- 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 |
-
|
| 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 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
qnum += 1
|
| 285 |
-
print(f" {qnum}. Is it Animal, Vegetable, Mineral, or Other?")
|
| 286 |
while True:
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
if nc == 0:
|
| 297 |
-
print(" I'm stumped!")
|
| 298 |
return
|
| 299 |
|
| 300 |
-
if
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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"
|