iajitpanday commited on
Commit
279f152
·
verified ·
1 Parent(s): 8b77663

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -38
app.py CHANGED
@@ -534,51 +534,58 @@ def build_gradio_interface():
534
  # Conversation history display
535
  conversation_display = gr.Markdown("*Conversation will appear here*")
536
 
537
- # Real-time audio input
538
  audio_input = gr.Audio(
539
  label="Speak",
540
-
541
- type="numpy",
542
- streaming=True
543
  )
544
 
545
-
546
  # State to store conversation history
547
  conversation_state = gr.State([])
548
 
549
  # Function to process audio and update conversation
550
- # Updates to the process_and_update function in the test interface section
551
-
552
- def process_and_update(audio, api_key, caller_id, conversation_history):
553
- # Check for required parameters
554
- if not api_key:
555
- return "**Error:** API key is required.", conversation_history
556
-
557
- if audio is None:
558
- return "*Conversation will appear here*", conversation_history
559
-
560
- # Process the audio
561
- result = process_voice_interaction(audio, api_key, caller_id)
562
-
563
- # Update conversation history
564
- if result.get("success") and "transcription" in result and "response" in result:
565
- # Add new conversation turn
566
- conversation_history.append({
567
- "user": result["transcription"],
568
- "bot": result["response"]
569
- })
570
-
571
- # Format the conversation as markdown
572
- markdown = "## Conversation\n\n"
573
- for turn in conversation_history:
574
- markdown += f"**You:** {turn['user']}\n\n"
575
- markdown += f"**Bot:** {turn['bot']}\n\n"
576
 
577
- return markdown, conversation_history
578
- else:
579
- # If there was an error
580
- error_msg = result.get("error", "Unknown error")
581
- return f"**Error:** {error_msg}", conversation_history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
582
 
583
  def clear_conversation():
584
  return "*Conversation will appear here*", []
@@ -592,13 +599,14 @@ def process_and_update(audio, api_key, caller_id, conversation_history):
592
  # Debug interface
593
  with gr.Blocks() as debug_interface:
594
  gr.Markdown("# Debug Interface")
595
- audio_input = gr.Audio(label="Test Audio Input")
 
596
  debug_btn = gr.Button("Debug Audio Format")
597
  output_json = gr.JSON(label="Debug Info")
598
 
599
  debug_btn.click(
600
  debug_audio,
601
- inputs=audio_input,
602
  outputs=output_json
603
  )
604
 
 
534
  # Conversation history display
535
  conversation_display = gr.Markdown("*Conversation will appear here*")
536
 
537
+ # Real-time audio input - compatible with older Gradio versions
538
  audio_input = gr.Audio(
539
  label="Speak",
540
+ type="numpy"
 
 
541
  )
542
 
 
543
  # State to store conversation history
544
  conversation_state = gr.State([])
545
 
546
  # Function to process audio and update conversation
547
+ def process_and_update(audio, api_key, caller_id, conversation_history):
548
+ if not api_key:
549
+ return "**Error:** API key is required.", conversation_history
550
+
551
+ if audio is None:
552
+ return "*Conversation will appear here*", conversation_history
553
+
554
+ # Process the audio
555
+ result = process_voice_interaction(audio, api_key, caller_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
556
 
557
+ # Update conversation history
558
+ if "transcription" in result and "response" in result:
559
+ # Add new conversation turn
560
+ conversation_history.append({
561
+ "user": result["transcription"],
562
+ "bot": result["response"]
563
+ })
564
+
565
+ # Format the conversation as markdown
566
+ markdown = "## Conversation\n\n"
567
+ for turn in conversation_history:
568
+ markdown += f"**You:** {turn['user']}\n\n"
569
+ markdown += f"**Bot:** {turn['bot']}\n\n"
570
+
571
+ return markdown, conversation_history
572
+ else:
573
+ # If there was an error
574
+ error_msg = result.get("error", "Unknown error")
575
+ return f"**Error:** {error_msg}", conversation_history
576
+
577
+ # Submit button for audio processing
578
+ submit_btn = gr.Button("Process Audio")
579
+
580
+ # Event handler for submit button
581
+ submit_btn.click(
582
+ process_and_update,
583
+ inputs=[audio_input, api_key_input, caller_id_input, conversation_state],
584
+ outputs=[conversation_display, conversation_state]
585
+ )
586
+
587
+ # Clear conversation button
588
+ clear_btn = gr.Button("Clear Conversation")
589
 
590
  def clear_conversation():
591
  return "*Conversation will appear here*", []
 
599
  # Debug interface
600
  with gr.Blocks() as debug_interface:
601
  gr.Markdown("# Debug Interface")
602
+
603
+ audio_input_debug = gr.Audio(label="Test Audio Input")
604
  debug_btn = gr.Button("Debug Audio Format")
605
  output_json = gr.JSON(label="Debug Info")
606
 
607
  debug_btn.click(
608
  debug_audio,
609
+ inputs=audio_input_debug,
610
  outputs=output_json
611
  )
612