Spaces:
Runtime error
Runtime error
File size: 2,484 Bytes
dfdf94e 7364cd3 dfdf94e 54657ca 7364cd3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | 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!"
)
|