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}]) current_emoji = response[0]['generated_text'] return introduction + f"Here's your first puzzle: {current_emoji}" # Function to handle the game logic def emoji_game(user_guess=""): global points, current_emoji # If no user input (game start), LLM asks the question first if user_guess == "": return reset_game() # Check if the user made a guess; LLM evaluates the guess guess_prompt = f"User guessed '{user_guess}'. Was the guess correct for this emoji puzzle: {current_emoji}?" response = pipe([{"role": "user", "content": guess_prompt}]) 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}]) 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 interface = gr.Interface( fn=emoji_game, # Game logic function inputs="text", # Text input for user guess outputs="text", # Text output for LLM responses title="Guess the Word from Emojis Game", description="Try to guess the word or phrase based on the emojis! 🎮 Reach 10 points to win or drop to -10 points to lose!" ) # Launch the Gradio interface interface.launch()