Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -349,6 +349,9 @@ def process_file(file):
|
|
| 349 |
return f"Error uploading file: {str(e)}"
|
| 350 |
|
| 351 |
def process_question(question, history):
|
|
|
|
|
|
|
|
|
|
| 352 |
if not rag.thread_id:
|
| 353 |
return "Please upload a document first.", history, "", None
|
| 354 |
try:
|
|
@@ -357,13 +360,18 @@ def process_question(question, history):
|
|
| 357 |
history.append({"role": "assistant", "content": response})
|
| 358 |
return "", history, "", None
|
| 359 |
except Exception as e:
|
| 360 |
-
|
|
|
|
| 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)
|
|
@@ -419,9 +427,9 @@ with gr.Blocks(css=custom_css, title="Document Q&A System") as demo:
|
|
| 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 |
-
|
| 423 |
-
|
| 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")
|
|
@@ -432,6 +440,9 @@ with gr.Blocks(css=custom_css, title="Document Q&A System") as demo:
|
|
| 432 |
return "", [], "", None
|
| 433 |
reset_btn.click(reset_all, None, [file_output, chatbot, question, audio_input])
|
| 434 |
file_output
|
|
|
|
|
|
|
|
|
|
| 435 |
chatbot
|
| 436 |
with gr.Row():
|
| 437 |
question
|
|
|
|
| 349 |
return f"Error uploading file: {str(e)}"
|
| 350 |
|
| 351 |
def process_question(question, history):
|
| 352 |
+
# Prevent sending empty messages
|
| 353 |
+
if not question or not question.strip():
|
| 354 |
+
return "", history, "", None
|
| 355 |
if not rag.thread_id:
|
| 356 |
return "Please upload a document first.", history, "", None
|
| 357 |
try:
|
|
|
|
| 360 |
history.append({"role": "assistant", "content": response})
|
| 361 |
return "", history, "", None
|
| 362 |
except Exception as e:
|
| 363 |
+
history.append({"role": "assistant", "content": f"Error: {str(e)}"})
|
| 364 |
+
return "", history, "", None
|
| 365 |
|
| 366 |
def process_audio(audio_file, history):
|
| 367 |
if audio_file is None:
|
| 368 |
return "Please record or upload an audio file.", history, "", None
|
| 369 |
try:
|
| 370 |
transcript = rag.transcribe_audio(audio_file)
|
| 371 |
+
if not transcript or not str(transcript).strip():
|
| 372 |
+
history.append({"role": "user", "content": "🎤 [No audio detected or transcription failed]"})
|
| 373 |
+
history.append({"role": "assistant", "content": "Sorry, I couldn't understand the audio. Please try again."})
|
| 374 |
+
return "", history, "", None
|
| 375 |
if not rag.thread_id:
|
| 376 |
return "Please upload a document first.", history, "", None
|
| 377 |
response = rag.ask_question(transcript)
|
|
|
|
| 427 |
audio_input = gr.Audio(type="filepath", label="Record or Upload Audio", elem_classes="gradio-audio", visible=False)
|
| 428 |
audio_status = gr.Textbox(label="Audio Status", interactive=False, elem_classes="status-text", visible=False)
|
| 429 |
|
| 430 |
+
with gr.Row():
|
| 431 |
+
# Sidebar on the left
|
| 432 |
+
with gr.Column(scale=1, min_width=280):
|
| 433 |
with gr.Group(elem_classes="compact-box"):
|
| 434 |
gr.Markdown("<div class='section-title'>Document Q&A</div>")
|
| 435 |
file_input = gr.File(label="Upload Document", file_types=[".pdf", ".txt", ".doc", ".docx"], file_count="single", type="binary", elem_classes="upload-btn")
|
|
|
|
| 440 |
return "", [], "", None
|
| 441 |
reset_btn.click(reset_all, None, [file_output, chatbot, question, audio_input])
|
| 442 |
file_output
|
| 443 |
+
# Main chat area
|
| 444 |
+
with gr.Column(scale=3, min_width=400):
|
| 445 |
+
with gr.Group(elem_classes="compact-box"):
|
| 446 |
chatbot
|
| 447 |
with gr.Row():
|
| 448 |
question
|