Text Generation
Transformers
Safetensors
English
twentyq
neural-network
2-bit
quantized
game
conversational
vintage-ai
twenty-questions
custom_code
8-bit precision
Instructions to use david-ar/20q with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use david-ar/20q with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="david-ar/20q", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("david-ar/20q", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use david-ar/20q with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "david-ar/20q" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "david-ar/20q", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/david-ar/20q
- SGLang
How to use david-ar/20q with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "david-ar/20q" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "david-ar/20q", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "david-ar/20q" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "david-ar/20q", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use david-ar/20q with Docker Model Runner:
docker model run hf.co/david-ar/20q
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"
|