Spaces:
Runtime error
Runtime error
Create guessbot_app.py
Browse files- guessbot_app.py +82 -0
guessbot_app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import random
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# 🧠 Trait questions
|
| 7 |
+
question_bank = {
|
| 8 |
+
"alive": "Is it alive?",
|
| 9 |
+
"animal": "Is it an animal?",
|
| 10 |
+
"fly": "Can it fly?",
|
| 11 |
+
"big": "Is it bigger than a car?",
|
| 12 |
+
"daily": "Do you use it every day?",
|
| 13 |
+
"indoors": "Is it found indoors?",
|
| 14 |
+
"person": "Is it a person?",
|
| 15 |
+
"famous": "Is it famous?"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
# 📦 Load/save knowledge
|
| 19 |
+
def load_knowledge():
|
| 20 |
+
if os.path.exists("guessbot_knowledge.json"):
|
| 21 |
+
with open("guessbot_knowledge.json", "r") as f:
|
| 22 |
+
return json.load(f)
|
| 23 |
+
return {}
|
| 24 |
+
|
| 25 |
+
def save_knowledge(knowledge):
|
| 26 |
+
with open("guessbot_knowledge.json", "w") as f:
|
| 27 |
+
json.dump(knowledge, f, indent=2)
|
| 28 |
+
|
| 29 |
+
# 🔍 Confidence-based guessing
|
| 30 |
+
def guess_with_confidence(traits, knowledge):
|
| 31 |
+
best_match = None
|
| 32 |
+
best_score = -1
|
| 33 |
+
for name, known_traits in knowledge.items():
|
| 34 |
+
score = sum(traits.get(k) == v for k, v in known_traits.items())
|
| 35 |
+
if score > best_score:
|
| 36 |
+
best_score = score
|
| 37 |
+
best_match = name
|
| 38 |
+
return best_match
|
| 39 |
+
|
| 40 |
+
# 🎮 Main interaction
|
| 41 |
+
def guessbot_interface(alive, animal, fly, big, daily, indoors, person, famous, actual_object):
|
| 42 |
+
traits = {
|
| 43 |
+
"alive": alive,
|
| 44 |
+
"animal": animal,
|
| 45 |
+
"fly": fly,
|
| 46 |
+
"big": big,
|
| 47 |
+
"daily": daily,
|
| 48 |
+
"indoors": indoors,
|
| 49 |
+
"person": person,
|
| 50 |
+
"famous": famous
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
knowledge = load_knowledge()
|
| 54 |
+
guess = guess_with_confidence(traits, knowledge)
|
| 55 |
+
|
| 56 |
+
if guess and guess.lower() == actual_object.lower():
|
| 57 |
+
return f"🎉 I guessed it! You were thinking of: {guess}"
|
| 58 |
+
else:
|
| 59 |
+
knowledge[actual_object] = traits
|
| 60 |
+
save_knowledge(knowledge)
|
| 61 |
+
return f"🤔 I guessed: {guess or 'nothing'}. But now I’ve learned: {actual_object}"
|
| 62 |
+
|
| 63 |
+
# 🖼️ Gradio UI
|
| 64 |
+
iface = gr.Interface(
|
| 65 |
+
fn=guessbot_interface,
|
| 66 |
+
inputs=[
|
| 67 |
+
gr.Checkbox(label="Is it alive?"),
|
| 68 |
+
gr.Checkbox(label="Is it an animal?"),
|
| 69 |
+
gr.Checkbox(label="Can it fly?"),
|
| 70 |
+
gr.Checkbox(label="Is it bigger than a car?"),
|
| 71 |
+
gr.Checkbox(label="Do you use it every day?"),
|
| 72 |
+
gr.Checkbox(label="Is it found indoors?"),
|
| 73 |
+
gr.Checkbox(label="Is it a person?"),
|
| 74 |
+
gr.Checkbox(label="Is it famous?"),
|
| 75 |
+
gr.Textbox(label="What were you thinking of?")
|
| 76 |
+
],
|
| 77 |
+
outputs="text",
|
| 78 |
+
title="🧠 GuessBot: The Learning AI",
|
| 79 |
+
description="Think of something and answer the questions. GuessBot will try to guess it—and learn if it’s wrong!"
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
iface.launch()
|