pratikshahp commited on
Commit
494792a
·
verified ·
1 Parent(s): 60a5388

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -95,18 +95,26 @@ def initialize_game_state():
95
 
96
  # Initialize the game state
97
  game_state = initialize_game_state()
 
98
 
99
  # Function to process user input and actions
100
  def run_action(message, history):
101
- global game_state # Access the global game state
 
 
 
102
 
103
  if message.lower() == "start game":
104
  return game_state["start"]
105
 
106
- if message.lower() == "start again":
107
  game_state = initialize_game_state()
108
  return "Game restarted! " + game_state["start"]
109
 
 
 
 
 
110
  system_prompt = """You are an AI Game master. Your job is to write what \
111
  happens next in a player's adventure game.\
112
  Instructions: \
@@ -143,12 +151,15 @@ def run_action(message, history):
143
 
144
  return model_output.choices[0].message.content
145
 
146
- # Gradio ChatInterface with the custom "start again" logic
 
 
 
147
  demo = gr.ChatInterface(
148
- run_action,
149
  chatbot=gr.Chatbot(
150
  height=250,
151
- placeholder="Type 'start game' to begin or 'start again' to restart the adventure",
152
  type="messages",
153
  ),
154
  textbox=gr.Textbox(
@@ -164,4 +175,3 @@ demo = gr.ChatInterface(
164
 
165
  # Launch the game
166
  demo.launch(share=True, server_name="0.0.0.0")
167
-
 
95
 
96
  # Initialize the game state
97
  game_state = initialize_game_state()
98
+ game_running = True # Flag to manage game status
99
 
100
  # Function to process user input and actions
101
  def run_action(message, history):
102
+ global game_state, game_running # Access the global game state and game status
103
+
104
+ if not game_running:
105
+ return "The game has ended. Type 'restart the game' to play again."
106
 
107
  if message.lower() == "start game":
108
  return game_state["start"]
109
 
110
+ if message.lower() == "restart the game":
111
  game_state = initialize_game_state()
112
  return "Game restarted! " + game_state["start"]
113
 
114
+ if message.lower() == "exit":
115
+ game_running = False
116
+ return "The game has ended. Type 'restart the game' to play again."
117
+
118
  system_prompt = """You are an AI Game master. Your job is to write what \
119
  happens next in a player's adventure game.\
120
  Instructions: \
 
151
 
152
  return model_output.choices[0].message.content
153
 
154
+
155
+ def main_loop(message, history):
156
+ return run_action(message, history)
157
+
158
  demo = gr.ChatInterface(
159
+ main_loop,
160
  chatbot=gr.Chatbot(
161
  height=250,
162
+ placeholder="Type 'start game' to begin, 'restart the game' to restart, or 'exit' to end the game.",
163
  type="messages",
164
  ),
165
  textbox=gr.Textbox(
 
175
 
176
  # Launch the game
177
  demo.launch(share=True, server_name="0.0.0.0")