import gradio as gr from transformers import pipeline # Load the text generation model pipe = pipeline("text-generation", model="microsoft/Phi-3.5-mini-instruct", trust_remote_code=True) # Initialize global variables for points and game status points = 0 current_emoji = "" # Function to reset the game and initialize introduction def reset_game(): global points, current_emoji points = 0 # LLM introduces the game and asks the first emoji puzzle introduction = "Welcome to 'Guess the Word from Emojis'! 🎮 Here's how it works:\n\n- You'll get an emoji-based puzzle.\n- For each correct guess, you'll earn 1 point.\n- Wrong guesses will deduct 1 point.\n- Win by reaching 10 points, or lose if your score drops to -10 points.\n\nLet's begin!\n" # Ask LLM for the first emoji puzzle emoji_prompt = "Give me an emoji-based puzzle for a word or phrase guessing game." response = pipe([{"role": "user", "content": emoji_prompt}], max_new_tokens=30) current_emoji = response[0]['generated_text'] return introduction + f"Here's your first puzzle: {current_emoji}" # Function to handle the game logic and user interactions def emoji_game(user_input=""): global points, current_emoji # Normalize the user input user_input = user_input.strip().lower() # Handle specific phrases from the user if user_input in ["hi", "hello"]: return "Hello! Ready to play 'Guess the Word from Emojis'? Type your guess or ask for a hint!" elif user_input in ["how does the game work?", "can you explain the rules?", "how do I play?"]: return "In this game, you'll guess the word or phrase based on emojis. For each correct guess, you earn 1 point. If you guess wrong, you lose 1 point. Reach 10 points to win or drop to -10 points to lose!" elif user_input in ["give me a hint", "i need a hint"]: # Provide a hint (a simple example could be the length of the answer) hint = f"The word or phrase has {len(current_emoji)} characters." return f"Hint: {hint}" # If no user input (game start), LLM asks the question first if user_input == "": return reset_game() # Check if the user made a guess; LLM evaluates the guess guess_prompt = f"User guessed '{user_input}'. Was the guess correct for this emoji puzzle: {current_emoji}?" response = pipe([{"role": "user", "content": guess_prompt}], max_new_tokens=30) response_text = response[0]['generated_text'].lower() # Process response: if it's correct, increase points; otherwise, decrease points if "correct" in response_text: # Correct guess points += 1 if points >= 10: return f"🎉 Correct! You've reached 10 points! You win the game! 🏆" else: # Get next emoji puzzle for the user next_emoji_prompt = "Give me another emoji-based puzzle." response = pipe([{"role": "user", "content": next_emoji_prompt}], max_new_tokens=30) current_emoji = response[0]['generated_text'] return f"✅ Correct! Your current points: {points}. Here's your next puzzle: {current_emoji}" else: # Incorrect guess points -= 1 if points <= -10: return f"❌ Incorrect! You've dropped to -10 points. Game over! 😢" else: return f"❌ Incorrect! Your current points: {points}. Try again: {current_emoji}" # Create a Gradio interface for the game with horizontal layout and buttons with gr.Blocks() as interface: gr.Markdown("### Guess the Word from Emojis Game 🎮") output = gr.Textbox(label="Game Output", interactive=False) input_box = gr.Textbox(label="Your Guess or Command") with gr.Row(): start_button = gr.Button("Start Game") next_button = gr.Button("Next Puzzle") input_box.submit(emoji_game, inputs=input_box, outputs=output) start_button.click(reset_game, outputs=output) next_button.click(lambda: emoji_game(), outputs=output) # Launch the Gradio interface interface.launch()