Update app.py
Browse files
app.py
CHANGED
|
@@ -3,16 +3,16 @@ import gradio as gr
|
|
| 3 |
import os
|
| 4 |
from project_model import process_inputs, session
|
| 5 |
|
| 6 |
-
# --- Handle both initial
|
| 7 |
-
def handle_submit(image, audio
|
| 8 |
"""
|
| 9 |
-
Handles both initial
|
| 10 |
-
|
| 11 |
-
- If new image + audio => initial
|
| 12 |
-
- If only
|
| 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
|
|
@@ -22,19 +22,20 @@ def handle_submit(image, audio, followup_audio):
|
|
| 22 |
markdown_reply = f"**{message}**\n\n"
|
| 23 |
return markdown_reply, answer_audio
|
| 24 |
|
| 25 |
-
elif
|
| 26 |
-
# Follow-up case
|
| 27 |
-
message, answer_audio = process_inputs(session, audio_path=
|
| 28 |
|
| 29 |
-
#
|
| 30 |
image_save_path = "uploaded_image.png"
|
| 31 |
-
session.current_image
|
|
|
|
| 32 |
|
| 33 |
markdown_reply = f"**{message}**\n\n"
|
| 34 |
return markdown_reply, answer_audio
|
| 35 |
|
| 36 |
else:
|
| 37 |
-
return "❗ Please upload image/
|
| 38 |
|
| 39 |
# --- Gradio App ---
|
| 40 |
with gr.Blocks() as demo:
|
|
@@ -42,21 +43,19 @@ with gr.Blocks() as demo:
|
|
| 42 |
|
| 43 |
with gr.Row():
|
| 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="
|
| 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
|
| 51 |
|
| 52 |
with gr.Column():
|
| 53 |
status_output = gr.Markdown(label="Response")
|
| 54 |
-
audio_output = gr.Audio(label="🔊 Response
|
| 55 |
|
| 56 |
-
#
|
| 57 |
submit_btn.click(
|
| 58 |
fn=handle_submit,
|
| 59 |
-
inputs=[image_input, audio_input
|
| 60 |
outputs=[status_output, audio_output]
|
| 61 |
)
|
| 62 |
|
|
|
|
| 3 |
import os
|
| 4 |
from project_model import process_inputs, session
|
| 5 |
|
| 6 |
+
# --- Handle submission for both initial and follow-up ---
|
| 7 |
+
def handle_submit(image, audio):
|
| 8 |
"""
|
| 9 |
+
Handles both initial upload and follow-up.
|
| 10 |
+
Logic:
|
| 11 |
+
- If new image + audio => initial interaction
|
| 12 |
+
- If only audio => follow-up
|
| 13 |
"""
|
| 14 |
if image is not None and audio is not None:
|
| 15 |
+
# Initial case: reset with image and audio
|
| 16 |
message, answer_audio = process_inputs(session, image=image, audio_path=audio)
|
| 17 |
|
| 18 |
# Save uploaded image
|
|
|
|
| 22 |
markdown_reply = f"**{message}**\n\n"
|
| 23 |
return markdown_reply, answer_audio
|
| 24 |
|
| 25 |
+
elif audio is not None:
|
| 26 |
+
# Follow-up case: use previous image, but new audio (question)
|
| 27 |
+
message, answer_audio = process_inputs(session, image=None, audio_path=audio)
|
| 28 |
|
| 29 |
+
# Still display existing image
|
| 30 |
image_save_path = "uploaded_image.png"
|
| 31 |
+
if session.current_image:
|
| 32 |
+
session.current_image.save(image_save_path)
|
| 33 |
|
| 34 |
markdown_reply = f"**{message}**\n\n"
|
| 35 |
return markdown_reply, answer_audio
|
| 36 |
|
| 37 |
else:
|
| 38 |
+
return "❗ Please upload an image and/or record audio.", None
|
| 39 |
|
| 40 |
# --- Gradio App ---
|
| 41 |
with gr.Blocks() as demo:
|
|
|
|
| 43 |
|
| 44 |
with gr.Row():
|
| 45 |
with gr.Column():
|
| 46 |
+
image_input = gr.Image(label="Upload or Capture Image (only first time)", sources=["upload", "webcam"], type="pil")
|
| 47 |
+
audio_input = gr.Audio(label="Speak Your Question", sources=["microphone"], type="filepath")
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
submit_btn = gr.Button("Submit Question")
|
| 50 |
|
| 51 |
with gr.Column():
|
| 52 |
status_output = gr.Markdown(label="Response")
|
| 53 |
+
audio_output = gr.Audio(label="🔊 Listen to Response", interactive=False)
|
| 54 |
|
| 55 |
+
# Connect submit button
|
| 56 |
submit_btn.click(
|
| 57 |
fn=handle_submit,
|
| 58 |
+
inputs=[image_input, audio_input],
|
| 59 |
outputs=[status_output, audio_output]
|
| 60 |
)
|
| 61 |
|