MickMick102 commited on
Commit
b8ce5f6
·
1 Parent(s): 9275ff7

fix: enhance session management and output handling in startup and response functions

Browse files
Files changed (2) hide show
  1. app.py +34 -8
  2. backend/systemprompt.py +2 -2
app.py CHANGED
@@ -25,13 +25,16 @@ if phone_waiting_sound.channels > 1:
25
  sound_samples = sound_samples.reshape((-1, phone_waiting_sound.channels)).mean(axis=1)
26
  sound_samples = sound_samples.astype(np.float32) / 32768.0 # Normalize to [-1,
27
  def startup(*arg):
28
-
29
 
30
  yield (phone_waiting_sound.frame_rate, sound_samples)
31
  STARTUP_MESSAGE = "สวัสดีค่ะ พลอย 1577Homeshopping ยินดีให้บริการค่ะ"
32
  yield from synthesize_text(STARTUP_MESSAGE)
33
  time.sleep(1)
34
- yield AdditionalOutputs([{"role": "assistant", "content": STARTUP_MESSAGE}])
 
 
 
35
 
36
  custom_css = """
37
  /* Overall Gradio page styling: hot pink background */
@@ -91,10 +94,15 @@ def response(
91
  conversation_history = conversation_history or []
92
  start_time = time.time()
93
  session_identifier = session_id or ""
 
94
  if not session_identifier:
95
  session_identifier = str(uuid.uuid4())
 
96
  print(f"[WARN] Missing session_id; generated temporary session {session_identifier}")
97
 
 
 
 
98
  if not audio or audio[1] is None or not np.any(audio[1]):
99
  print("No audio input detected; skipping response generation.")
100
  print(f"------------------------")
@@ -143,7 +151,7 @@ def response(
143
  print(f"User: {transcription}")
144
  if is_valid_turn(user_turn):
145
  conversation_history.append(user_turn)
146
- yield AdditionalOutputs(snapshot_history(conversation_history))
147
 
148
  # print("Conversation history:", conversation_history)
149
 
@@ -223,7 +231,10 @@ def response(
223
  first_chunk_sent = True
224
  text_buffer = ""
225
  delimiter_count = 0
226
- yield AdditionalOutputs(snapshot_history(conversation_history))
 
 
 
227
 
228
  i += 1
229
  if text_buffer.strip():
@@ -247,7 +258,10 @@ def response(
247
  text_buffer = ""
248
  delimiter_count = 0
249
 
250
- yield AdditionalOutputs(snapshot_history(conversation_history))
 
 
 
251
 
252
  except Exception as e:
253
  print(f"An error occurred during response generation or synthesis: {e}")
@@ -257,7 +271,10 @@ def response(
257
  except Exception as synth_error:
258
  print(f"Could not synthesize error message: {synth_error}")
259
  assistant_turn["content"] = (assistant_turn.get("content", "") + f" [Error: {e}]").strip()
260
- yield AdditionalOutputs(snapshot_history(conversation_history))
 
 
 
261
 
262
  total_latency = time.time() - start_time
263
  print(f"Total: {total_latency:.4f}s")
@@ -276,6 +293,12 @@ def initialize_session_id():
276
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="orange", secondary_hue="orange")) as demo:
277
  gr.HTML("""<h1 style='text-align: center'>1577 Voicebot Demo</h1>""")
278
  session_state = gr.State(value=None)
 
 
 
 
 
 
279
  with gr.Row():
280
  with gr.Column(scale=1, elem_classes=["phone-column"]):
281
  audio = WebRTC(
@@ -340,9 +363,12 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="orange", second
340
  time_limit=8192
341
  )
342
 
 
 
 
343
  audio.on_additional_outputs(
344
- lambda history: history,
345
- outputs=[conversation_history],
346
  queue=True,
347
  show_progress="hidden"
348
  )
 
25
  sound_samples = sound_samples.reshape((-1, phone_waiting_sound.channels)).mean(axis=1)
26
  sound_samples = sound_samples.astype(np.float32) / 32768.0 # Normalize to [-1,
27
  def startup(*arg):
28
+ session_identifier = arg[1] if len(arg) > 1 else None
29
 
30
  yield (phone_waiting_sound.frame_rate, sound_samples)
31
  STARTUP_MESSAGE = "สวัสดีค่ะ พลอย 1577Homeshopping ยินดีให้บริการค่ะ"
32
  yield from synthesize_text(STARTUP_MESSAGE)
33
  time.sleep(1)
34
+ yield AdditionalOutputs(
35
+ [{"role": "assistant", "content": STARTUP_MESSAGE}],
36
+ session_identifier,
37
+ )
38
 
39
  custom_css = """
40
  /* Overall Gradio page styling: hot pink background */
 
94
  conversation_history = conversation_history or []
95
  start_time = time.time()
96
  session_identifier = session_id or ""
97
+ generated_session_id = False
98
  if not session_identifier:
99
  session_identifier = str(uuid.uuid4())
100
+ generated_session_id = True
101
  print(f"[WARN] Missing session_id; generated temporary session {session_identifier}")
102
 
103
+ if generated_session_id:
104
+ yield AdditionalOutputs(snapshot_history(conversation_history), session_identifier)
105
+
106
  if not audio or audio[1] is None or not np.any(audio[1]):
107
  print("No audio input detected; skipping response generation.")
108
  print(f"------------------------")
 
151
  print(f"User: {transcription}")
152
  if is_valid_turn(user_turn):
153
  conversation_history.append(user_turn)
154
+ yield AdditionalOutputs(snapshot_history(conversation_history), session_identifier)
155
 
156
  # print("Conversation history:", conversation_history)
157
 
 
231
  first_chunk_sent = True
232
  text_buffer = ""
233
  delimiter_count = 0
234
+ yield AdditionalOutputs(
235
+ snapshot_history(conversation_history),
236
+ session_identifier,
237
+ )
238
 
239
  i += 1
240
  if text_buffer.strip():
 
258
  text_buffer = ""
259
  delimiter_count = 0
260
 
261
+ yield AdditionalOutputs(
262
+ snapshot_history(conversation_history),
263
+ session_identifier,
264
+ )
265
 
266
  except Exception as e:
267
  print(f"An error occurred during response generation or synthesis: {e}")
 
271
  except Exception as synth_error:
272
  print(f"Could not synthesize error message: {synth_error}")
273
  assistant_turn["content"] = (assistant_turn.get("content", "") + f" [Error: {e}]").strip()
274
+ yield AdditionalOutputs(
275
+ snapshot_history(conversation_history),
276
+ session_identifier,
277
+ )
278
 
279
  total_latency = time.time() - start_time
280
  print(f"Total: {total_latency:.4f}s")
 
293
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="orange", secondary_hue="orange")) as demo:
294
  gr.HTML("""<h1 style='text-align: center'>1577 Voicebot Demo</h1>""")
295
  session_state = gr.State(value=None)
296
+ session_display = gr.Textbox(
297
+ label="Session ID",
298
+ value="",
299
+ interactive=False,
300
+ )
301
+
302
  with gr.Row():
303
  with gr.Column(scale=1, elem_classes=["phone-column"]):
304
  audio = WebRTC(
 
363
  time_limit=8192
364
  )
365
 
366
+ def _sync_history_and_session(history, session_identifier):
367
+ return history, session_identifier, session_identifier
368
+
369
  audio.on_additional_outputs(
370
+ _sync_history_and_session,
371
+ outputs=[conversation_history, session_state, session_display],
372
  queue=True,
373
  show_progress="hidden"
374
  )
backend/systemprompt.py CHANGED
@@ -196,8 +196,8 @@ You are "Ploy" (พลอย), a friendly and professional sales expert for 1577
196
 
197
  **Step C: Address Collection (Strict Sequential Flow)**
198
  Collect data **one field at a time**. Confirm the user's input before moving to the next.
199
- 1. **ชื่อจริง:** (Verify spelling if unclear).|
200
- 2. **นามสกุล:** (Verify spelling if unclear).|
201
  3. **Province (จังหวัด):**|
202
  4. **District (เขต/อำเภอ):**|
203
  5. **Sub-district (แขวง/ตำบล):**|
 
196
 
197
  **Step C: Address Collection (Strict Sequential Flow)**
198
  Collect data **one field at a time**. Confirm the user's input before moving to the next.
199
+ 1. **ชื่อจริง[Assume Thai Name]:** (Ask user to speak clearly if unclear).|
200
+ 2. **นามสกุล[Assume Thai last name]:** (Ask user to speak clearly if unclear).|
201
  3. **Province (จังหวัด):**|
202
  4. **District (เขต/อำเภอ):**|
203
  5. **Sub-district (แขวง/ตำบล):**|