guess_emoji / app.py
gouravgujariya's picture
Update app.py
48857de verified
raw
history blame
4.29 kB
import gradio as gr
from transformers import pipeline
import emoji
import random
# 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 = ""
# Predefined emoji puzzles
emoji_puzzles = [
emoji.emojize(":snake:") + emoji.emojize(":wizard:") + emoji.emojize(":lightning:"),
emoji.emojize(":apple:") + emoji.emojize(":woman_farmer:") + emoji.emojize(":door:") + emoji.emojize(":mirror:"),
emoji.emojize(":dog:") + emoji.emojize(":house:"),
emoji.emojize(":pizza:") + emoji.emojize(":slice:"),
]
# 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"
# Randomly select an emoji puzzle
current_emoji = random.choice(emoji_puzzles)
# Return introduction and the emoji puzzle
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)
# Ensure to get the generated text correctly
response_text = response[0]['generated_text'] if response and isinstance(response, list) and len(response) > 0 else "โ“"
# Process response: if it's correct, increase points; otherwise, decrease points
if "correct" in response_text.lower(): # 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
current_emoji = random.choice(emoji_puzzles)
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()