Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -350,28 +350,30 @@ def process_file(file):
|
|
| 350 |
|
| 351 |
def process_question(question, history):
|
| 352 |
if not rag.thread_id:
|
| 353 |
-
return "Please upload a document first.", history
|
| 354 |
try:
|
| 355 |
response = rag.ask_question(question)
|
| 356 |
history.append({"role": "user", "content": question})
|
| 357 |
history.append({"role": "assistant", "content": response})
|
| 358 |
-
return "", history
|
| 359 |
except Exception as e:
|
| 360 |
-
return "", history + [{"role": "assistant", "content": f"Error: {str(e)}"}]
|
| 361 |
|
| 362 |
def process_audio(audio_file, history):
|
| 363 |
if audio_file is None:
|
| 364 |
-
return "Please record or upload an audio file.", history
|
| 365 |
try:
|
| 366 |
transcript = rag.transcribe_audio(audio_file)
|
| 367 |
if not rag.thread_id:
|
| 368 |
-
return "Please upload a document first.", history
|
| 369 |
response = rag.ask_question(transcript)
|
| 370 |
history.append({"role": "user", "content": f"🎤 {transcript}"})
|
| 371 |
history.append({"role": "assistant", "content": response})
|
| 372 |
-
return "", history
|
| 373 |
except Exception as e:
|
| 374 |
-
|
|
|
|
|
|
|
| 375 |
|
| 376 |
def process_audio_base64(audio_base64, history):
|
| 377 |
if not audio_base64:
|
|
@@ -417,33 +419,27 @@ with gr.Blocks(css=custom_css, title="Document Q&A System") as demo:
|
|
| 417 |
audio_input = gr.Audio(type="filepath", label="Record or Upload Audio", elem_classes="gradio-audio", visible=False)
|
| 418 |
audio_status = gr.Textbox(label="Audio Status", interactive=False, elem_classes="status-text", visible=False)
|
| 419 |
|
| 420 |
-
#
|
| 421 |
with gr.Row(elem_classes="centered-main"):
|
| 422 |
with gr.Column():
|
| 423 |
with gr.Group(elem_classes="compact-box"):
|
| 424 |
-
gr.Markdown("<div class='section-title'>
|
| 425 |
file_input = gr.File(label="Upload Document", file_types=[".pdf", ".txt", ".doc", ".docx"], file_count="single", type="binary", elem_classes="upload-btn")
|
| 426 |
reset_btn = gr.Button("Reset Chat & Upload New Document", elem_classes="reset-btn")
|
|
|
|
| 427 |
def reset_all():
|
| 428 |
rag.thread_id = None
|
| 429 |
-
return "", [], "", None
|
| 430 |
-
file_input.change(process_file, file_input, file_output)
|
| 431 |
reset_btn.click(reset_all, None, [file_output, chatbot, question, audio_input])
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
with gr.Row(elem_classes="centered-main"):
|
| 435 |
-
with gr.Column():
|
| 436 |
-
with gr.Group(elem_classes="compact-box"):
|
| 437 |
-
gr.Markdown("<div class='section-title'>2. Chat with your Document</div>")
|
| 438 |
with gr.Row():
|
|
|
|
| 439 |
send_btn = gr.Button("Send", elem_classes="send-btn")
|
| 440 |
mic_btn = gr.Button("🎤", elem_classes="audio-btn")
|
| 441 |
-
question.submit(process_question, [question, chatbot], [question, chatbot, question, audio_input])
|
| 442 |
send_btn.click(process_question, [question, chatbot], [question, chatbot, question, audio_input])
|
| 443 |
-
|
| 444 |
-
# Inline audio recorder, hidden by default
|
| 445 |
audio_input.change(process_audio, [audio_input, chatbot], [audio_input, chatbot, question, audio_input])
|
| 446 |
-
|
| 447 |
def show_audio():
|
| 448 |
return {audio_input: gr.update(visible=True), audio_status: gr.update(visible=True)}
|
| 449 |
mic_btn.click(show_audio, None, [audio_input, audio_status])
|
|
|
|
| 350 |
|
| 351 |
def process_question(question, history):
|
| 352 |
if not rag.thread_id:
|
| 353 |
+
return "Please upload a document first.", history, "", None
|
| 354 |
try:
|
| 355 |
response = rag.ask_question(question)
|
| 356 |
history.append({"role": "user", "content": question})
|
| 357 |
history.append({"role": "assistant", "content": response})
|
| 358 |
+
return "", history, "", None
|
| 359 |
except Exception as e:
|
| 360 |
+
return "", history + [{"role": "assistant", "content": f"Error: {str(e)}"}], "", None
|
| 361 |
|
| 362 |
def process_audio(audio_file, history):
|
| 363 |
if audio_file is None:
|
| 364 |
+
return "Please record or upload an audio file.", history, "", None
|
| 365 |
try:
|
| 366 |
transcript = rag.transcribe_audio(audio_file)
|
| 367 |
if not rag.thread_id:
|
| 368 |
+
return "Please upload a document first.", history, "", None
|
| 369 |
response = rag.ask_question(transcript)
|
| 370 |
history.append({"role": "user", "content": f"🎤 {transcript}"})
|
| 371 |
history.append({"role": "assistant", "content": response})
|
| 372 |
+
return "", history, "", None
|
| 373 |
except Exception as e:
|
| 374 |
+
history.append({"role": "user", "content": f"🎤 [Error transcribing audio: {str(e)}]"})
|
| 375 |
+
history.append({"role": "assistant", "content": "It seems there was an error while transcribing audio due to a technical issue. If there's anything specific from the document or any other questions you have regarding the content, please let me know, and I can assist you with that information."})
|
| 376 |
+
return "", history, "", None
|
| 377 |
|
| 378 |
def process_audio_base64(audio_base64, history):
|
| 379 |
if not audio_base64:
|
|
|
|
| 419 |
audio_input = gr.Audio(type="filepath", label="Record or Upload Audio", elem_classes="gradio-audio", visible=False)
|
| 420 |
audio_status = gr.Textbox(label="Audio Status", interactive=False, elem_classes="status-text", visible=False)
|
| 421 |
|
| 422 |
+
# Unified card for upload, chat, and voice
|
| 423 |
with gr.Row(elem_classes="centered-main"):
|
| 424 |
with gr.Column():
|
| 425 |
with gr.Group(elem_classes="compact-box"):
|
| 426 |
+
gr.Markdown("<div class='section-title'>Document Q&A</div>")
|
| 427 |
file_input = gr.File(label="Upload Document", file_types=[".pdf", ".txt", ".doc", ".docx"], file_count="single", type="binary", elem_classes="upload-btn")
|
| 428 |
reset_btn = gr.Button("Reset Chat & Upload New Document", elem_classes="reset-btn")
|
| 429 |
+
file_input.change(process_file, file_input, file_output)
|
| 430 |
def reset_all():
|
| 431 |
rag.thread_id = None
|
| 432 |
+
return "", [], "", None
|
|
|
|
| 433 |
reset_btn.click(reset_all, None, [file_output, chatbot, question, audio_input])
|
| 434 |
+
file_output.render()
|
| 435 |
+
chatbot.render()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
with gr.Row():
|
| 437 |
+
question.render()
|
| 438 |
send_btn = gr.Button("Send", elem_classes="send-btn")
|
| 439 |
mic_btn = gr.Button("🎤", elem_classes="audio-btn")
|
|
|
|
| 440 |
send_btn.click(process_question, [question, chatbot], [question, chatbot, question, audio_input])
|
| 441 |
+
question.submit(process_question, [question, chatbot], [question, chatbot, question, audio_input])
|
|
|
|
| 442 |
audio_input.change(process_audio, [audio_input, chatbot], [audio_input, chatbot, question, audio_input])
|
|
|
|
| 443 |
def show_audio():
|
| 444 |
return {audio_input: gr.update(visible=True), audio_status: gr.update(visible=True)}
|
| 445 |
mic_btn.click(show_audio, None, [audio_input, audio_status])
|