Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,61 @@
|
|
| 1 |
from PIL import Image
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
|
|
|
| 4 |
from project_model import process_inputs, session
|
| 5 |
|
| 6 |
|
| 7 |
def handle_question(image, audio):
|
| 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 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
markdown_reply
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
#f"**Detected Objects:**\n\n"
|
| 54 |
-
#f""
|
| 55 |
-
)
|
| 56 |
-
else:
|
| 57 |
-
return "Please upload an image and/or record an audio clip.", None
|
| 58 |
-
|
| 59 |
-
return markdown_reply, answer_audio
|
| 60 |
|
| 61 |
|
| 62 |
# --- Gradio App ---
|
|
|
|
| 1 |
from PIL import Image
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
+
import uuid
|
| 5 |
from project_model import process_inputs, session
|
| 6 |
|
| 7 |
|
| 8 |
def handle_question(image, audio):
|
| 9 |
+
try:
|
| 10 |
+
if image and audio: # Handle the initial question with image and audio
|
| 11 |
+
message, answer_audio = process_inputs(session, image=image, audio_path=audio)
|
| 12 |
+
|
| 13 |
+
# Save images
|
| 14 |
+
unique_id = uuid.uuid4().hex
|
| 15 |
+
original_path = f"uploaded_image_{unique_id}.png"
|
| 16 |
+
annotated_path = f"annotated_image_{unique_id}.png"
|
| 17 |
+
|
| 18 |
+
image.save(original_path)
|
| 19 |
+
if session.annotated_image:
|
| 20 |
+
session.annotated_image.save(annotated_path)
|
| 21 |
+
|
| 22 |
+
# Build markdown reply for initial question
|
| 23 |
+
markdown_reply = (
|
| 24 |
+
f"**{message}**\n\n"
|
| 25 |
+
#f"**Original Image:**\n\n"
|
| 26 |
+
#f"\n\n"
|
| 27 |
+
#f"**Detected Objects:**\n\n"
|
| 28 |
+
#f"\n\n"
|
| 29 |
+
#f"**🔊 Audio Response:**\n\n"
|
| 30 |
+
#f"<audio controls autoplay>\n"
|
| 31 |
+
#f" <source src='file/{answer_audio}' type='audio/wav'>\n"
|
| 32 |
+
#f"Your browser does not support the audio element.\n"
|
| 33 |
+
#f"</audio>"
|
| 34 |
+
)
|
| 35 |
+
elif audio: # Handle follow-up with just audio
|
| 36 |
+
|
| 37 |
+
if not session.current_image:
|
| 38 |
+
return "No initial image found for the follow-up question.", None
|
| 39 |
+
|
| 40 |
+
message, answer_audio = process_inputs(session, image=session.current_image, audio_path=audio)
|
| 41 |
+
|
| 42 |
|
| 43 |
+
|
| 44 |
+
# Build markdown reply for follow-up question
|
| 45 |
+
markdown_reply = (
|
| 46 |
+
f"**{message}**\n\n"
|
| 47 |
+
#f"**Original Image:**\n\n"
|
| 48 |
+
#f"\n\n"
|
| 49 |
+
#f"**Detected Objects:**\n\n"
|
| 50 |
+
#f""
|
| 51 |
+
)
|
| 52 |
+
else:
|
| 53 |
+
return "Please upload an image and/or record an audio clip.", None
|
| 54 |
+
|
| 55 |
+
return markdown_reply, answer_audio
|
| 56 |
+
except: ValueError as e:
|
| 57 |
+
return f"❗Error: {str(e)}", None
|
| 58 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
# --- Gradio App ---
|