jjz5463 commited on
Commit
46757c3
1 Parent(s): 527aa63

try to implement session

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