Spaces:
Runtime error
Runtime error
gouravgujariya
commited on
Commit
โข
355ccec
1
Parent(s):
2692382
Update app.py
Browse files
app.py
CHANGED
@@ -4,46 +4,63 @@ from transformers import pipeline
|
|
4 |
# Load the text generation model
|
5 |
pipe = pipeline("text-generation", model="microsoft/Phi-3.5-mini-instruct", trust_remote_code=True)
|
6 |
|
7 |
-
# Initialize variables for points
|
8 |
points = 0
|
|
|
9 |
|
10 |
-
# Function to reset
|
11 |
def reset_game():
|
12 |
-
global points
|
13 |
points = 0
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
# Function to handle the game
|
17 |
def emoji_game(user_guess=""):
|
18 |
-
global points
|
19 |
|
20 |
-
|
|
|
21 |
return reset_game()
|
22 |
|
23 |
-
#
|
24 |
-
|
|
|
25 |
response_text = response[0]['generated_text'].lower()
|
26 |
-
|
27 |
-
#
|
28 |
-
if "correct" in response_text: # Correct
|
29 |
points += 1
|
30 |
if points >= 10:
|
31 |
-
return f"Correct!
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
34 |
points -= 1
|
35 |
if points <= -10:
|
36 |
-
return f"Incorrect!
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
interface = gr.Interface(
|
41 |
-
fn=emoji_game, #
|
42 |
-
inputs="text", #
|
43 |
-
outputs="text", # Text output
|
44 |
title="Guess the Word from Emojis Game",
|
45 |
-
description="Try to guess the word or phrase based on the emojis! ๐ฎ"
|
46 |
)
|
47 |
|
48 |
-
# Launch the interface
|
49 |
interface.launch()
|
|
|
4 |
# Load the text generation model
|
5 |
pipe = pipeline("text-generation", model="microsoft/Phi-3.5-mini-instruct", trust_remote_code=True)
|
6 |
|
7 |
+
# Initialize global variables for points and game status
|
8 |
points = 0
|
9 |
+
current_emoji = ""
|
10 |
|
11 |
+
# Function to reset the game and initialize introduction
|
12 |
def reset_game():
|
13 |
+
global points, current_emoji
|
14 |
points = 0
|
15 |
+
# LLM introduces the game and asks the first emoji puzzle
|
16 |
+
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"
|
17 |
+
|
18 |
+
# Ask LLM for the first emoji puzzle
|
19 |
+
emoji_prompt = "Give me an emoji-based puzzle for a word or phrase guessing game."
|
20 |
+
response = pipe([{"role": "user", "content": emoji_prompt}])
|
21 |
+
current_emoji = response[0]['generated_text']
|
22 |
+
|
23 |
+
return introduction + f"Here's your first puzzle: {current_emoji}"
|
24 |
|
25 |
+
# Function to handle the game logic
|
26 |
def emoji_game(user_guess=""):
|
27 |
+
global points, current_emoji
|
28 |
|
29 |
+
# If no user input (game start), LLM asks the question first
|
30 |
+
if user_guess == "":
|
31 |
return reset_game()
|
32 |
|
33 |
+
# Check if the user made a guess; LLM evaluates the guess
|
34 |
+
guess_prompt = f"User guessed '{user_guess}'. Was the guess correct for this emoji puzzle: {current_emoji}?"
|
35 |
+
response = pipe([{"role": "user", "content": guess_prompt}])
|
36 |
response_text = response[0]['generated_text'].lower()
|
37 |
+
|
38 |
+
# Process response: if it's correct, increase points, otherwise decrease points
|
39 |
+
if "correct" in response_text: # Correct guess
|
40 |
points += 1
|
41 |
if points >= 10:
|
42 |
+
return f"๐ Correct! You've reached 10 points! You win the game! ๐"
|
43 |
+
else:
|
44 |
+
# Get next emoji puzzle for the user
|
45 |
+
next_emoji_prompt = "Give me another emoji-based puzzle."
|
46 |
+
response = pipe([{"role": "user", "content": next_emoji_prompt}])
|
47 |
+
current_emoji = response[0]['generated_text']
|
48 |
+
return f"โ
Correct! Your current points: {points}. Here's your next puzzle: {current_emoji}"
|
49 |
+
else: # Incorrect guess
|
50 |
points -= 1
|
51 |
if points <= -10:
|
52 |
+
return f"โ Incorrect! You've dropped to -10 points. Game over! ๐ข"
|
53 |
+
else:
|
54 |
+
return f"โ Incorrect! Your current points: {points}. Try again: {current_emoji}"
|
55 |
+
|
56 |
+
# Create a Gradio interface for the game
|
57 |
interface = gr.Interface(
|
58 |
+
fn=emoji_game, # Game logic function
|
59 |
+
inputs="text", # Text input for user guess
|
60 |
+
outputs="text", # Text output for LLM responses
|
61 |
title="Guess the Word from Emojis Game",
|
62 |
+
description="Try to guess the word or phrase based on the emojis! ๐ฎ Reach 10 points to win or drop to -10 points to lose!"
|
63 |
)
|
64 |
|
65 |
+
# Launch the Gradio interface
|
66 |
interface.launch()
|