aysan98 commited on
Commit
b92709d
Β·
verified Β·
1 Parent(s): a16ed82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -36
app.py CHANGED
@@ -48,7 +48,7 @@ def create_app():
48
 
49
  save_btn = gr.Button("Save & Create Link", variant="primary")
50
  status = gr.Textbox(label="Status", interactive=False)
51
- link_box = gr.Textbox(label="Interview Link", visible=False, interactive=True)
52
 
53
  query_state = gr.State("")
54
  questions_state = gr.State([])
@@ -108,7 +108,7 @@ def create_app():
108
  session_input = gr.Textbox(label="Session ID", placeholder="Enter ID from researcher")
109
  load_btn = gr.Button("Load Interview", variant="primary")
110
 
111
- chatbot = gr.Chatbot(label="Interview", height=400, visible=False)
112
  answer = gr.Textbox(label="Your Answer", lines=3, visible=False)
113
  next_btn = gr.Button("Next", visible=False)
114
  submit_btn = gr.Button("Submit Interview", variant="primary", visible=False)
@@ -122,69 +122,152 @@ def create_app():
122
  def load_interview(sid):
123
  session = get_session(sid)
124
  if not session:
125
- return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), "❌ Invalid ID", [], 0, [], ""
 
 
 
 
 
 
 
 
 
 
 
126
  q1 = session['questions'][0]
127
- h = [(f"Q1: {q1['question']}", None)]
128
- return gr.update(visible=True, value=h), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), "βœ… Loaded", h, 0, [], sid
 
 
 
 
 
 
 
 
 
 
 
129
 
130
  def next_q(ans, sid, idx, hist, resp):
131
- if not ans:
132
- return hist, "❌ Provide answer", idx, resp, ""
 
133
  session = get_session(sid)
134
  if not session:
135
- return hist, "❌ Invalid", idx, resp, ""
136
 
137
  qs = session['questions']
138
- if hist and hist[-1][1] is None:
139
- hist[-1] = (hist[-1][0], ans)
140
 
141
- resp.append({'q': qs[idx]['question'], 'a': ans, 'section': qs[idx]['section']})
 
 
 
 
 
 
 
 
 
142
  idx += 1
143
 
 
144
  if idx < len(qs):
145
- hist.append((f"Q{idx+1}: {qs[idx]['question']}", None))
146
- return hist, f"βœ… Q{idx+1}/{len(qs)}", idx, resp, ""
147
  else:
148
- return hist, "βœ… Done! Click Submit", idx, resp, ""
149
 
150
  def submit_int(sid, resp):
151
  if not resp:
152
- return "❌ No responses"
153
- add_response(sid, resp)
 
 
 
 
154
  try:
155
  s = get_session(sid)
156
  syn = SynthesisAgent(s['api_key'])
157
  all_int = [r['data'] for r in s['responses']]
158
  persona = syn.aggregate_interviews(all_int, s['query'])
159
  update_persona(sid, persona)
160
- return f"βœ… Submitted! Total: {len(s['responses'])}"
161
- except:
162
- return "βœ… Submitted!"
163
-
164
- load_btn.click(load_interview, [session_input], [chatbot, answer, next_btn, submit_btn, interview_status, history, idx, responses, loaded_id])
165
- answer.submit(next_q, [answer, loaded_id, idx, history, responses], [history, interview_status, idx, responses, answer]).then(lambda h: gr.update(value=h), [history], [chatbot])
166
- next_btn.click(next_q, [answer, loaded_id, idx, history, responses], [history, interview_status, idx, responses, answer]).then(lambda h: gr.update(value=h), [history], [chatbot])
167
- submit_btn.click(submit_int, [loaded_id, responses], [interview_status])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
  with gr.Tab("πŸ“Š Results"):
170
- gr.Markdown("### View Analysis")
171
- result_sid = gr.Textbox(label="Session ID")
172
- load_result_btn = gr.Button("Load", variant="primary")
 
 
173
 
174
- responses_json = gr.JSON(label="Responses")
175
- persona_json = gr.JSON(label="Persona")
176
- refresh_btn = gr.Button("Regenerate Persona")
 
 
 
 
 
 
 
177
 
178
  def load_res(sid):
179
  s = get_session(sid)
180
  if not s:
181
- return {"error": "Invalid"}, {"error": "Invalid"}
182
- return {"session": sid, "total": len(s['responses']), "data": s['responses']}, s.get('latest_persona', {"msg": "No persona yet"})
 
 
 
 
 
 
 
 
 
 
183
 
184
  def regen(sid):
185
  s = get_session(sid)
186
- if not s or not s['responses']:
187
- return {"error": "No data"}
 
 
188
  try:
189
  syn = SynthesisAgent(s['api_key'])
190
  persona = syn.aggregate_interviews([r['data'] for r in s['responses']], s['query'])
@@ -193,8 +276,8 @@ def create_app():
193
  except Exception as e:
194
  return {"error": str(e)}
195
 
196
- load_result_btn.click(load_res, [result_sid], [responses_json, persona_json])
197
- refresh_btn.click(regen, [result_sid], [persona_json])
198
 
199
  return app
200
 
 
48
 
49
  save_btn = gr.Button("Save & Create Link", variant="primary")
50
  status = gr.Textbox(label="Status", interactive=False)
51
+ link_box = gr.Textbox(label="Interview Link (Session ID)", visible=False, interactive=True)
52
 
53
  query_state = gr.State("")
54
  questions_state = gr.State([])
 
108
  session_input = gr.Textbox(label="Session ID", placeholder="Enter ID from researcher")
109
  load_btn = gr.Button("Load Interview", variant="primary")
110
 
111
+ chatbot = gr.Chatbot(label="Interview", height=400, visible=False, type="messages")
112
  answer = gr.Textbox(label="Your Answer", lines=3, visible=False)
113
  next_btn = gr.Button("Next", visible=False)
114
  submit_btn = gr.Button("Submit Interview", variant="primary", visible=False)
 
122
  def load_interview(sid):
123
  session = get_session(sid)
124
  if not session:
125
+ return (
126
+ gr.update(visible=False),
127
+ gr.update(visible=False),
128
+ gr.update(visible=False),
129
+ gr.update(visible=False),
130
+ "❌ Invalid ID",
131
+ [],
132
+ 0,
133
+ [],
134
+ ""
135
+ )
136
+
137
  q1 = session['questions'][0]
138
+ h = [{"role": "assistant", "content": f"Question 1 of {len(session['questions'])}\n\n{q1['question']}"}]
139
+
140
+ return (
141
+ gr.update(visible=True, value=h),
142
+ gr.update(visible=True),
143
+ gr.update(visible=True),
144
+ gr.update(visible=True),
145
+ "βœ… Interview loaded. Please answer the questions.",
146
+ h,
147
+ 0,
148
+ [],
149
+ sid
150
+ )
151
 
152
  def next_q(ans, sid, idx, hist, resp):
153
+ if not ans or not ans.strip():
154
+ return hist, "❌ Please provide an answer", idx, resp, ""
155
+
156
  session = get_session(sid)
157
  if not session:
158
+ return hist, "❌ Invalid session", idx, resp, ""
159
 
160
  qs = session['questions']
 
 
161
 
162
+ # Add user answer to history
163
+ hist.append({"role": "user", "content": ans})
164
+
165
+ # Save response
166
+ resp.append({
167
+ 'q': qs[idx]['question'],
168
+ 'a': ans,
169
+ 'section': qs[idx]['section']
170
+ })
171
+
172
  idx += 1
173
 
174
+ # Check if more questions
175
  if idx < len(qs):
176
+ hist.append({"role": "assistant", "content": f"Question {idx+1} of {len(qs)}\n\n{qs[idx]['question']}"})
177
+ return hist, f"βœ… Answer recorded. Question {idx+1}/{len(qs)}", idx, resp, ""
178
  else:
179
+ return hist, "βœ… All questions answered! Click 'Submit Interview' to finish.", idx, resp, ""
180
 
181
  def submit_int(sid, resp):
182
  if not resp:
183
+ return "❌ No responses to submit"
184
+
185
+ success = add_response(sid, resp)
186
+ if not success:
187
+ return "❌ Error saving interview"
188
+
189
  try:
190
  s = get_session(sid)
191
  syn = SynthesisAgent(s['api_key'])
192
  all_int = [r['data'] for r in s['responses']]
193
  persona = syn.aggregate_interviews(all_int, s['query'])
194
  update_persona(sid, persona)
195
+ return f"βœ… Interview submitted successfully! Thank you.\n\nTotal responses for this session: {len(s['responses'])}\n\nPersona has been automatically updated!"
196
+ except Exception as e:
197
+ return f"βœ… Interview submitted successfully! Thank you.\n\n(Automated analysis encountered an error, but your responses are saved.)"
198
+
199
+ load_btn.click(
200
+ load_interview,
201
+ inputs=[session_input],
202
+ outputs=[chatbot, answer, next_btn, submit_btn, interview_status, history, idx, responses, loaded_id]
203
+ )
204
+
205
+ answer.submit(
206
+ next_q,
207
+ inputs=[answer, loaded_id, idx, history, responses],
208
+ outputs=[history, interview_status, idx, responses, answer]
209
+ ).then(
210
+ lambda h: gr.update(value=h),
211
+ inputs=[history],
212
+ outputs=[chatbot]
213
+ )
214
+
215
+ next_btn.click(
216
+ next_q,
217
+ inputs=[answer, loaded_id, idx, history, responses],
218
+ outputs=[history, interview_status, idx, responses, answer]
219
+ ).then(
220
+ lambda h: gr.update(value=h),
221
+ inputs=[history],
222
+ outputs=[chatbot]
223
+ )
224
+
225
+ submit_btn.click(
226
+ submit_int,
227
+ inputs=[loaded_id, responses],
228
+ outputs=[interview_status]
229
+ )
230
 
231
  with gr.Tab("πŸ“Š Results"):
232
+ gr.Markdown("### View Analysis & Results")
233
+ result_sid = gr.Textbox(label="Session ID", placeholder="Enter session ID")
234
+ load_result_btn = gr.Button("Load Results", variant="primary")
235
+
236
+ gr.Markdown("---")
237
 
238
+ with gr.Row():
239
+ with gr.Column():
240
+ gr.Markdown("#### πŸ“‹ Interview Responses")
241
+ responses_json = gr.JSON(label="All Responses")
242
+
243
+ with gr.Column():
244
+ gr.Markdown("#### 🎯 Customer Persona")
245
+ persona_json = gr.JSON(label="Empathy Map")
246
+
247
+ refresh_btn = gr.Button("πŸ”„ Regenerate Persona", variant="secondary")
248
 
249
  def load_res(sid):
250
  s = get_session(sid)
251
  if not s:
252
+ return {"error": "Invalid session ID"}, {"error": "Invalid session ID"}
253
+
254
+ responses_data = {
255
+ "session_id": sid,
256
+ "query": s['query'],
257
+ "total_responses": len(s['responses']),
258
+ "responses": s['responses']
259
+ }
260
+
261
+ persona_data = s.get('latest_persona', {"message": "No persona generated yet. Submit interviews first."})
262
+
263
+ return responses_data, persona_data
264
 
265
  def regen(sid):
266
  s = get_session(sid)
267
+ if not s:
268
+ return {"error": "Invalid session ID"}
269
+ if not s['responses']:
270
+ return {"error": "No responses yet. Submit interviews first."}
271
  try:
272
  syn = SynthesisAgent(s['api_key'])
273
  persona = syn.aggregate_interviews([r['data'] for r in s['responses']], s['query'])
 
276
  except Exception as e:
277
  return {"error": str(e)}
278
 
279
+ load_result_btn.click(load_res, inputs=[result_sid], outputs=[responses_json, persona_json])
280
+ refresh_btn.click(regen, inputs=[result_sid], outputs=[persona_json])
281
 
282
  return app
283