DreamStream-1 commited on
Commit
6b9dfec
·
verified ·
1 Parent(s): 98238c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -379,7 +379,12 @@ def process_voice_note(audio_file, history):
379
  if audio_file is None:
380
  return "Please record or upload an audio file.", history, "", None, None
381
  try:
382
- transcript = rag.transcribe_audio(audio_file)
 
 
 
 
 
383
  if not transcript or not str(transcript).strip():
384
  history.append({"role": "user", "content": "🎤 [No audio detected or transcription failed]"})
385
  history.append({"role": "assistant", "content": "Sorry, I couldn't understand the audio. Please try again."})
@@ -403,19 +408,16 @@ with gr.Blocks(css=custom_css, title="Document Q&A System") as demo:
403
  <div style='text-align:center; color:#1976D2; margin-bottom:18px;'>Upload a document, record your voice, and chat!</div>
404
  """)
405
 
406
- # Define shared components at the top
407
  chatbot = gr.Chatbot(height=400, elem_classes="gradio-chatbot", label=None, type="messages")
408
  file_output = gr.Textbox(label="Upload Status", interactive=False, elem_classes="textbox")
409
  question = gr.Textbox(label="Type your question and press Enter", placeholder="Ask a question about your document...", elem_classes="textbox")
410
  audio_input = gr.Audio(type="filepath", label="Record or Upload Audio", elem_classes="gradio-audio", visible=False)
411
- audio_status = gr.Textbox(label="Audio Status", interactive=False, elem_classes="status-text", visible=False)
412
  tts_output = gr.Audio(label="Assistant Voice Reply", interactive=False, visible=False)
413
 
414
  with gr.Row():
415
- # Controls on the top left
416
- with gr.Column(scale=1, min_width=320):
417
  with gr.Group(elem_classes="compact-box"):
418
- gr.Markdown("<div class='section-title'>Upload & Voice</div>")
419
  file_input = gr.File(label="Upload Document", file_types=[".pdf", ".txt", ".doc", ".docx"], file_count="single", type="binary", elem_classes="upload-btn")
420
  mic_btn = gr.Button("🎤 Record Voice", elem_classes="audio-btn")
421
  audio_input
@@ -435,8 +437,7 @@ with gr.Blocks(css=custom_css, title="Document Q&A System") as demo:
435
  send_voice_btn.click(process_voice_note, [audio_input, chatbot], [file_output, chatbot, question, audio_input, tts_output])
436
  send_voice_btn.click(hide_audio, None, [audio_input, send_voice_btn])
437
  tts_output
438
- # Chatbot/chat area to the right
439
- with gr.Column(scale=3, min_width=400):
440
  with gr.Group(elem_classes="compact-box"):
441
  chatbot
442
  with gr.Row():
 
379
  if audio_file is None:
380
  return "Please record or upload an audio file.", history, "", None, None
381
  try:
382
+ # If audio_file is a string (filepath), open it as a file
383
+ if isinstance(audio_file, str):
384
+ with open(audio_file, "rb") as f:
385
+ transcript = rag.transcribe_audio(f)
386
+ else:
387
+ transcript = rag.transcribe_audio(audio_file)
388
  if not transcript or not str(transcript).strip():
389
  history.append({"role": "user", "content": "🎤 [No audio detected or transcription failed]"})
390
  history.append({"role": "assistant", "content": "Sorry, I couldn't understand the audio. Please try again."})
 
408
  <div style='text-align:center; color:#1976D2; margin-bottom:18px;'>Upload a document, record your voice, and chat!</div>
409
  """)
410
 
 
411
  chatbot = gr.Chatbot(height=400, elem_classes="gradio-chatbot", label=None, type="messages")
412
  file_output = gr.Textbox(label="Upload Status", interactive=False, elem_classes="textbox")
413
  question = gr.Textbox(label="Type your question and press Enter", placeholder="Ask a question about your document...", elem_classes="textbox")
414
  audio_input = gr.Audio(type="filepath", label="Record or Upload Audio", elem_classes="gradio-audio", visible=False)
 
415
  tts_output = gr.Audio(label="Assistant Voice Reply", interactive=False, visible=False)
416
 
417
  with gr.Row():
418
+ with gr.Column(scale=1, min_width=350):
 
419
  with gr.Group(elem_classes="compact-box"):
420
+ gr.Markdown("<div class='section-title'>Document Q&A Controls</div>")
421
  file_input = gr.File(label="Upload Document", file_types=[".pdf", ".txt", ".doc", ".docx"], file_count="single", type="binary", elem_classes="upload-btn")
422
  mic_btn = gr.Button("🎤 Record Voice", elem_classes="audio-btn")
423
  audio_input
 
437
  send_voice_btn.click(process_voice_note, [audio_input, chatbot], [file_output, chatbot, question, audio_input, tts_output])
438
  send_voice_btn.click(hide_audio, None, [audio_input, send_voice_btn])
439
  tts_output
440
+ with gr.Column(scale=3, min_width=500):
 
441
  with gr.Group(elem_classes="compact-box"):
442
  chatbot
443
  with gr.Row():