0Learn commited on
Commit
2c16c22
Β·
verified Β·
1 Parent(s): 04def06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -10
app.py CHANGED
@@ -1,20 +1,95 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
  import random
4
 
5
- # Initialize the language model
6
- generator = pipeline('text-generation', model='gpt2')
7
-
8
  # Initialize feedback storage
9
  feedback_store = []
10
 
11
  # List of emojis for the emoji selector
12
  emojis = ["πŸ˜€", "πŸ˜‚", "😊", "😍", "πŸ€”", "😎", "πŸ™Œ", "πŸ‘", "❀️", "πŸŽ‰"]
13
 
14
- # Function to generate a response using the language model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def get_bot_response(message):
16
- response = generator(message, max_length=50, num_return_sequences=1)[0]['generated_text']
17
- return response.split(message)[-1].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  # Function to handle user input and generate response
20
  def chat(message, history):
@@ -26,7 +101,7 @@ def chat(message, history):
26
  bot_message = get_bot_response(message)
27
 
28
  # Add bot message to chat history
29
- history[-1] = (message, bot_message)
30
 
31
  return history
32
 
@@ -78,8 +153,8 @@ with gr.Blocks() as demo:
78
  clear.click(lambda: None, None, chatbot, queue=False)
79
  emoji_dropdown.change(add_emoji, inputs=[emoji_dropdown, msg], outputs=msg)
80
  copy_button.click(copy_last_message, inputs=[chatbot], outputs=[copied_text])
81
- thumbs_up.click(handle_feedback, inputs=["positive", chatbot], outputs=[feedback_text])
82
- thumbs_down.click(handle_feedback, inputs=["negative", chatbot], outputs=[feedback_text])
83
 
84
  # For Hugging Face Spaces compatibility
85
  demo.launch()
 
1
  import gradio as gr
 
2
  import random
3
 
 
 
 
4
  # Initialize feedback storage
5
  feedback_store = []
6
 
7
  # List of emojis for the emoji selector
8
  emojis = ["πŸ˜€", "πŸ˜‚", "😊", "😍", "πŸ€”", "😎", "πŸ™Œ", "πŸ‘", "❀️", "πŸŽ‰"]
9
 
10
+ # Game state
11
+ game_state = {
12
+ "location": "start",
13
+ "inventory": [],
14
+ "game_started": False
15
+ }
16
+
17
+ # Game map
18
+ game_map = {
19
+ "start": {
20
+ "description": "You're at the entrance of a mysterious cave. Paths lead north and east.",
21
+ "exits": {"north": "dark_tunnel", "east": "forest_clearing"}
22
+ },
23
+ "dark_tunnel": {
24
+ "description": "You're in a dark, damp tunnel. There's a faint light to the north and the entrance is to the south.",
25
+ "exits": {"north": "underground_lake", "south": "start"}
26
+ },
27
+ "forest_clearing": {
28
+ "description": "You're in a peaceful forest clearing. There's a tall tree here and paths leading west and south.",
29
+ "exits": {"west": "start", "south": "ancient_ruins"},
30
+ "items": ["shiny_key"]
31
+ },
32
+ "underground_lake": {
33
+ "description": "You're at the edge of an underground lake. The water glows with an eerie blue light. The tunnel is to the south.",
34
+ "exits": {"south": "dark_tunnel"}
35
+ },
36
+ "ancient_ruins": {
37
+ "description": "You're standing amidst ancient ruins. There's a locked chest here and a path leading north.",
38
+ "exits": {"north": "forest_clearing"},
39
+ "items": ["locked_chest"]
40
+ }
41
+ }
42
+
43
  def get_bot_response(message):
44
+ message = message.lower().strip()
45
+
46
+ if not game_state["game_started"]:
47
+ if "start game" in message or "play game" in message:
48
+ game_state["game_started"] = True
49
+ return "Welcome to the Text Adventure! " + game_map[game_state["location"]]["description"] + "\nWhat would you like to do? (Type 'help' for commands)"
50
+ else:
51
+ return "I'm a text adventure game bot. Type 'start game' to begin playing!"
52
+
53
+ if message == "help":
54
+ return "Commands: 'look' to examine your surroundings, 'go [direction]' to move, 'inventory' to check your items, 'take [item]' to pick up an item, 'use [item]' to use an item."
55
+
56
+ if message == "look":
57
+ return game_map[game_state["location"]]["description"]
58
+
59
+ if message.startswith("go "):
60
+ direction = message.split()[1]
61
+ if direction in game_map[game_state["location"]]["exits"]:
62
+ game_state["location"] = game_map[game_state["location"]]["exits"][direction]
63
+ return game_map[game_state["location"]]["description"]
64
+ else:
65
+ return "You can't go that way."
66
+
67
+ if message == "inventory":
68
+ if game_state["inventory"]:
69
+ return "You are carrying: " + ", ".join(game_state["inventory"])
70
+ else:
71
+ return "Your inventory is empty."
72
+
73
+ if message.startswith("take "):
74
+ item = message.split()[1]
75
+ if "items" in game_map[game_state["location"]] and item in game_map[game_state["location"]]["items"]:
76
+ game_state["inventory"].append(item)
77
+ game_map[game_state["location"]]["items"].remove(item)
78
+ return f"You have taken the {item}."
79
+ else:
80
+ return "There's no such item here."
81
+
82
+ if message.startswith("use "):
83
+ item = message.split()[1]
84
+ if item in game_state["inventory"]:
85
+ if item == "shiny_key" and game_state["location"] == "ancient_ruins":
86
+ return "You use the shiny key to unlock the chest. Inside, you find a golden amulet. Congratulations, you've won the game!"
87
+ else:
88
+ return f"You can't use the {item} here."
89
+ else:
90
+ return f"You don't have a {item} in your inventory."
91
+
92
+ return "I don't understand that command. Type 'help' for a list of commands."
93
 
94
  # Function to handle user input and generate response
95
  def chat(message, history):
 
101
  bot_message = get_bot_response(message)
102
 
103
  # Add bot message to chat history
104
+ history.append((message, bot_message))
105
 
106
  return history
107
 
 
153
  clear.click(lambda: None, None, chatbot, queue=False)
154
  emoji_dropdown.change(add_emoji, inputs=[emoji_dropdown, msg], outputs=msg)
155
  copy_button.click(copy_last_message, inputs=[chatbot], outputs=[copied_text])
156
+ thumbs_up.click(handle_feedback, inputs=[gr.Textbox(value="positive"), chatbot], outputs=[feedback_text])
157
+ thumbs_down.click(handle_feedback, inputs=[gr.Textbox(value="negative"), chatbot], outputs=[feedback_text])
158
 
159
  # For Hugging Face Spaces compatibility
160
  demo.launch()