File size: 4,293 Bytes
1e7d879
 
48857de
 
1e7d879
 
 
 
355ccec
2692382
355ccec
2692382
48857de
 
 
 
 
 
 
 
355ccec
2692382
355ccec
2692382
355ccec
 
 
48857de
 
e92b92d
4f48e66
355ccec
2692382
7952d76
 
355ccec
2692382
7952d76
 
 
 
 
 
 
 
 
 
 
 
 
355ccec
7952d76
2692382
 
355ccec
7952d76
286a0cd
355ccec
e92b92d
 
 
7952d76
e92b92d
2692382
 
355ccec
 
 
48857de
355ccec
 
2692382
 
355ccec
 
 
 
286a0cd
 
 
 
 
 
 
 
 
 
 
 
 
1e7d879
355ccec
1e7d879
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
84
85
86
87
88
89
90
91
92
93
94
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()