Spaces:
Runtime error
Runtime error
Download app.py from SpaceNinja007/GuessBot: direct link, hf CLI and curl.
- Browser
- Download file 2.48 kB
-
https://huggingface.co/spaces/SpaceNinja007/GuessBot/resolve/main/app.py
- Command line
-
hf download hf://spaces/SpaceNinja007/GuessBot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SpaceNinja007/GuessBot/resolve/main/app.py
2.48 kB
| import json | |
| import os | |
| import random | |
| import gradio as gr | |
| # 🧠 Trait questions | |
| question_bank = { | |
| "alive": "Is it alive?", | |
| "animal": "Is it an animal?", | |
| "fly": "Can it fly?", | |
| "big": "Is it bigger than a car?", | |
| "daily": "Do you use it every day?", | |
| "indoors": "Is it found indoors?", | |
| "person": "Is it a person?", | |
| "famous": "Is it famous?" | |
| } | |
| # 📦 Load/save knowledge | |
| def load_knowledge(): | |
| if os.path.exists("guessbot_knowledge.json"): | |
| with open("guessbot_knowledge.json", "r") as f: | |
| return json.load(f) | |
| return {} | |
| def save_knowledge(knowledge): | |
| with open("guessbot_knowledge.json", "w") as f: | |
| json.dump(knowledge, f, indent=2) | |
| # 🔍 Confidence-based guessing | |
| def guess_with_confidence(traits, knowledge): | |
| best_match = None | |
| best_score = -1 | |
| for name, known_traits in knowledge.items(): | |
| score = sum(traits.get(k) == v for k, v in known_traits.items()) | |
| if score > best_score: | |
| best_score = score | |
| best_match = name | |
| return best_match | |
| # 🎮 Main interaction | |
| def guessbot_interface(alive, animal, fly, big, daily, indoors, person, famous, actual_object): | |
| traits = { | |
| "alive": alive, | |
| "animal": animal, | |
| "fly": fly, | |
| "big": big, | |
| "daily": daily, | |
| "indoors": indoors, | |
| "person": person, | |
| "famous": famous | |
| } | |
| knowledge = load_knowledge() | |
| guess = guess_with_confidence(traits, knowledge) | |
| if guess and guess.lower() == actual_object.lower(): | |
| return f"🎉 I guessed it! You were thinking of: {guess}" | |
| else: | |
| knowledge[actual_object] = traits | |
| save_knowledge(knowledge) | |
| return f"🤔 I guessed: {guess or 'nothing'}. But now I’ve learned: {actual_object}" | |
| demo = gr.Interface( | |
| fn=guessbot_interface, | |
| inputs=[ | |
| gr.Checkbox(label="Is it alive?"), | |
| gr.Checkbox(label="Is it an animal?"), | |
| gr.Checkbox(label="Can it fly?"), | |
| gr.Checkbox(label="Is it bigger than a car?"), | |
| gr.Checkbox(label="Do you use it every day?"), | |
| gr.Checkbox(label="Is it found indoors?"), | |
| gr.Checkbox(label="Is it a person?"), | |
| gr.Checkbox(label="Is it famous?"), | |
| gr.Textbox(label="What were you thinking of?") | |
| ], | |
| outputs="text", | |
| title="🧠 GuessBot: The Learning AI", | |
| description="Think of something and answer the questions. GuessBot will try to guess it—and learn if it’s wrong!" | |
| ) | |