Update app.py
Browse files
app.py
CHANGED
|
@@ -3,37 +3,38 @@ import gradio as gr
|
|
| 3 |
import os
|
| 4 |
from project_model import process_inputs, session
|
| 5 |
|
| 6 |
-
# --- Handle
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
# --- Gradio App ---
|
| 39 |
with gr.Blocks() as demo:
|
|
@@ -43,25 +44,19 @@ with gr.Blocks() as demo:
|
|
| 43 |
with gr.Column():
|
| 44 |
image_input = gr.Image(label="Upload or Capture Image", sources=["upload", "webcam"], type="pil")
|
| 45 |
audio_input = gr.Audio(label="Initial Question (Voice)", sources=["microphone"], type="filepath")
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
gr.
|
| 49 |
-
followup_audio_input = gr.Audio(label="Follow-up Question", sources=["microphone"], type="filepath")
|
| 50 |
-
followup_btn = gr.Button("Ask Follow-up")
|
| 51 |
|
| 52 |
with gr.Column():
|
| 53 |
-
status_output = gr.Markdown(label="Response")
|
| 54 |
audio_output = gr.Audio(label="π Response Audio", interactive=False)
|
| 55 |
|
|
|
|
| 56 |
submit_btn.click(
|
| 57 |
-
fn=
|
| 58 |
-
inputs=[image_input, audio_input],
|
| 59 |
-
outputs=[status_output, audio_output]
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
followup_btn.click(
|
| 63 |
-
fn=handle_followup,
|
| 64 |
-
inputs=[followup_audio_input],
|
| 65 |
outputs=[status_output, audio_output]
|
| 66 |
)
|
| 67 |
|
|
|
|
| 3 |
import os
|
| 4 |
from project_model import process_inputs, session
|
| 5 |
|
| 6 |
+
# --- Handle both initial upload and follow-up ---
|
| 7 |
+
def handle_submit(image, audio, followup_audio):
|
| 8 |
+
"""
|
| 9 |
+
Handles both initial question and follow-up.
|
| 10 |
+
Priority:
|
| 11 |
+
- If new image + audio => initial upload
|
| 12 |
+
- If only followup audio => follow-up
|
| 13 |
+
"""
|
| 14 |
+
if image is not None and audio is not None:
|
| 15 |
+
# Initial case
|
| 16 |
+
message, answer_audio = process_inputs(session, image=image, audio_path=audio)
|
| 17 |
+
|
| 18 |
+
# Save uploaded image
|
| 19 |
+
image_save_path = "uploaded_image.png"
|
| 20 |
+
image.save(image_save_path)
|
| 21 |
+
|
| 22 |
+
markdown_reply = f"**{message}**\n\n"
|
| 23 |
+
return markdown_reply, answer_audio
|
| 24 |
+
|
| 25 |
+
elif followup_audio is not None:
|
| 26 |
+
# Follow-up case
|
| 27 |
+
message, answer_audio = process_inputs(session, audio_path=followup_audio)
|
| 28 |
+
|
| 29 |
+
# Reuse saved image
|
| 30 |
+
image_save_path = "uploaded_image.png"
|
| 31 |
+
session.current_image.save(image_save_path)
|
| 32 |
+
|
| 33 |
+
markdown_reply = f"**{message}**\n\n"
|
| 34 |
+
return markdown_reply, answer_audio
|
| 35 |
+
|
| 36 |
+
else:
|
| 37 |
+
return "β Please upload image/audio or record a follow-up.", None
|
| 38 |
|
| 39 |
# --- Gradio App ---
|
| 40 |
with gr.Blocks() as demo:
|
|
|
|
| 44 |
with gr.Column():
|
| 45 |
image_input = gr.Image(label="Upload or Capture Image", sources=["upload", "webcam"], type="pil")
|
| 46 |
audio_input = gr.Audio(label="Initial Question (Voice)", sources=["microphone"], type="filepath")
|
| 47 |
+
gr.Markdown("### π€ OR Ask a Follow-up Question")
|
| 48 |
+
followup_audio_input = gr.Audio(label="Follow-up Question (Voice)", sources=["microphone"], type="filepath")
|
| 49 |
|
| 50 |
+
submit_btn = gr.Button("Submit Question / Follow-up") # <== π₯ Single button
|
|
|
|
|
|
|
| 51 |
|
| 52 |
with gr.Column():
|
| 53 |
+
status_output = gr.Markdown(label="Response")
|
| 54 |
audio_output = gr.Audio(label="π Response Audio", interactive=False)
|
| 55 |
|
| 56 |
+
# Hook up the single submit button
|
| 57 |
submit_btn.click(
|
| 58 |
+
fn=handle_submit,
|
| 59 |
+
inputs=[image_input, audio_input, followup_audio_input],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
outputs=[status_output, audio_output]
|
| 61 |
)
|
| 62 |
|