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!" )