jjz5463 commited on
Commit
527aa63
1 Parent(s): 42b2eba

try to implement session

Browse files
Files changed (1) hide show
  1. app.py +32 -33
app.py CHANGED
@@ -107,77 +107,76 @@ class AppSimulator:
107
  return self.simulation.one_conversation_round(user_input)
108
 
109
 
110
- # Initialize the simulator
111
- simulator_app = AppSimulator(openai_api_key=openai_api_key)
112
-
113
-
114
- def chat(user_input, history):
115
  """Chat handler that validates input and interacts with the simulator."""
116
- response = simulator_app.chat_interaction(user_input, history)
117
 
118
  # Initialize variables for task completion and steps
119
-
120
- # Define the pattern for matching the response
121
  pattern = r"Task completed! You took (\d+) steps\."
122
  match = re.match(pattern, response)
123
 
124
  if match:
125
  task_complete = 1
126
  task_completed_step = int(match.group(1))
127
- app_name = simulator_app.app_name
128
- idx = simulator_app.smallest_index
129
- task = simulator_app.task
130
  write_task_data(app_name, idx, task, task_complete, task_completed_step)
131
 
132
- return response
133
 
 
 
 
134
 
135
- def give_up():
136
  """Handle the Give-Up action by marking the first incomplete task as abandoned."""
137
  task_completed = 0
138
  task_completed_steps = 0
139
 
140
- app_name = simulator_app.app_name
141
- idx = simulator_app.smallest_index
142
- task = simulator_app.task
143
 
144
  write_task_data(app_name, idx, task, task_completed, task_completed_steps)
145
-
146
  return "Task marked as abandoned (Give-Up action)."
147
 
148
-
149
- # Gradio Interface using ChatInterface
150
  with gr.Blocks(fill_height=True) as demo:
151
  gr.Markdown("## Simulator Setup")
152
 
153
  # Input fields for initialization
154
- sitemap_input = gr.Textbox(label="Sitemap", placeholder="Enter the Hugging Face link to sitemap... (eg.jjz5463/AppStore_synthetic_sitemap)")
155
  initialize_button = gr.Button("Initialize Simulator")
156
 
157
- # Status block to display initialization progress with elapsed time
158
- status = gr.Textbox(label="Status", interactive=False)
159
-
160
- # Chat interface to handle user interactions
161
  chat_interface = gr.ChatInterface(fn=chat, type='messages')
162
-
163
  give_up_button = gr.Button("Give Up")
164
 
165
- # Define the callback function to initialize the simulator and update status
166
- def initialize_and_start_chat(sitemap):
167
- return simulator_app.initialize_simulator(sitemap) # Use progress tracking
 
 
168
 
169
- # Set up the button click to initialize simulator and update status only
170
  initialize_button.click(
171
  fn=initialize_and_start_chat,
172
- inputs=[sitemap_input],
173
  outputs=status # Update only the status block
174
  )
175
 
176
- # Set up the Give-Up button click to update the dataset
 
 
 
 
 
177
  give_up_button.click(
178
  fn=give_up,
179
- inputs=[],
180
- outputs=status # Update the status with the give-up message
181
  )
182
 
183
  # Launch the app
 
107
  return self.simulation.one_conversation_round(user_input)
108
 
109
 
110
+ # Updated chat function
111
+ def chat(user_input, history, simulator):
 
 
 
112
  """Chat handler that validates input and interacts with the simulator."""
113
+ response = simulator.chat_interaction(user_input, history)
114
 
115
  # Initialize variables for task completion and steps
 
 
116
  pattern = r"Task completed! You took (\d+) steps\."
117
  match = re.match(pattern, response)
118
 
119
  if match:
120
  task_complete = 1
121
  task_completed_step = int(match.group(1))
122
+ app_name = simulator.app_name
123
+ idx = simulator.smallest_index
124
+ task = simulator.task
125
  write_task_data(app_name, idx, task, task_complete, task_completed_step)
126
 
127
+ return response, history
128
 
129
+ def initialize_and_start_chat(sitemap, simulator):
130
+ """Initialize the simulator for the current session."""
131
+ return simulator.initialize_simulator(sitemap)
132
 
133
+ def give_up(simulator):
134
  """Handle the Give-Up action by marking the first incomplete task as abandoned."""
135
  task_completed = 0
136
  task_completed_steps = 0
137
 
138
+ app_name = simulator.app_name
139
+ idx = simulator.smallest_index
140
+ task = simulator.task
141
 
142
  write_task_data(app_name, idx, task, task_completed, task_completed_steps)
 
143
  return "Task marked as abandoned (Give-Up action)."
144
 
145
+ # Gradio Interface with gr.State for session-specific simulator
 
146
  with gr.Blocks(fill_height=True) as demo:
147
  gr.Markdown("## Simulator Setup")
148
 
149
  # Input fields for initialization
150
+ sitemap_input = gr.Textbox(label="Sitemap", placeholder="Enter the Hugging Face link to sitemap... (eg. jjz5463/AppStore_synthetic_sitemap)")
151
  initialize_button = gr.Button("Initialize Simulator")
152
 
153
+ # Chat interface and give-up button
 
 
 
154
  chat_interface = gr.ChatInterface(fn=chat, type='messages')
 
155
  give_up_button = gr.Button("Give Up")
156
 
157
+ # Status block to display initialization progress
158
+ status = gr.Textbox(label="Status", interactive=False)
159
+
160
+ # Simulator instance for each session
161
+ simulator = gr.State(AppSimulator(openai_api_key=openai_api_key))
162
 
163
+ # Define button clicks with gr.State
164
  initialize_button.click(
165
  fn=initialize_and_start_chat,
166
+ inputs=[sitemap_input, simulator],
167
  outputs=status # Update only the status block
168
  )
169
 
170
+ chat_interface.submit(
171
+ fn=chat,
172
+ inputs=["user_input", "history", simulator],
173
+ outputs=["chat_interface", "history"]
174
+ )
175
+
176
  give_up_button.click(
177
  fn=give_up,
178
+ inputs=[simulator],
179
+ outputs=status
180
  )
181
 
182
  # Launch the app