gouravgujariya commited on
Commit
355ccec
โ€ข
1 Parent(s): 2692382

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -24
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 points when the game starts or restarts
11
  def reset_game():
12
- global points
13
  points = 0
14
- return "Welcome to 'Guess the Word from Emojis'! You'll get emoji-based puzzles, and for each correct guess, you'll earn points. Wrong guesses will deduct points. Win by reaching 10 points, or lose if your score drops to -10. Let's get started! ๐Ÿ๐Ÿง™โšก = ???"
 
 
 
 
 
 
 
 
15
 
16
- # Function to handle the game flow
17
  def emoji_game(user_guess=""):
18
- global points
19
 
20
- if user_guess == "": # Introduction step, LLM starts the conversation
 
21
  return reset_game()
22
 
23
- # LLM generates a response based on the user's guess
24
- response = pipe([{"role": "user", "content": f"User guessed '{user_guess}'. Is it correct?"}])
 
25
  response_text = response[0]['generated_text'].lower()
26
-
27
- # Simple logic to simulate whether the LLM says the answer is right or wrong
28
- if "correct" in response_text: # Correct answer
29
  points += 1
30
  if points >= 10:
31
- return f"Correct! ๐ŸŽ‰ You've reached 10 points. You win the game!"
32
- return f"Correct! Your current points: {points}. Keep going!"
33
- else: # Wrong answer
 
 
 
 
 
34
  points -= 1
35
  if points <= -10:
36
- return f"Incorrect! ๐Ÿ˜ข You've dropped to -10 points. Game over!"
37
- return f"Incorrect! Your current points: {points}. Try again!"
38
-
39
- # Gradio interface
 
40
  interface = gr.Interface(
41
- fn=emoji_game, # The game logic function
42
- inputs="text", # User input (guess)
43
- outputs="text", # Text output (LLM response)
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()