saa231 commited on
Commit
1057b7a
·
verified ·
1 Parent(s): 30f2e8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -51
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
- if image and audio: # Handle the initial question with image and audio
9
- message, answer_audio = process_inputs(session, image=image, audio_path=audio)
10
-
11
- # Save images
12
- original_path = "uploaded_image.png"
13
- annotated_path = "annotated_image.png"
14
-
15
- image.save(original_path)
16
- if session.annotated_image:
17
- session.annotated_image.save(annotated_path)
18
-
19
- # Build markdown reply for initial question
20
- markdown_reply = (
21
- f"**{message}**\n\n"
22
- #f"**Original Image:**\n\n"
23
- #f"![Original Image](file/{original_path})\n\n"
24
- #f"**Detected Objects:**\n\n"
25
- #f"![Annotated Image](file/{annotated_path})\n\n"
26
- #f"**🔊 Audio Response:**\n\n"
27
- #f"<audio controls autoplay>\n"
28
- #f" <source src='file/{answer_audio}' type='audio/wav'>\n"
29
- #f"Your browser does not support the audio element.\n"
30
- #f"</audio>"
31
- )
32
- elif audio: # Handle follow-up with just audio
33
-
34
- if not session.current_image:
35
- return "No initial image found for the follow-up question.", None
 
 
 
 
 
36
 
37
- message, answer_audio = process_inputs(session, image=session.current_image, audio_path=audio)
38
-
39
- # Save the current image if it exists
40
- original_path = "uploaded_image.png"
41
- annotated_path = "annotated_image.png"
42
-
43
- if session.current_image:
44
- session.current_image.save(original_path)
45
- if session.annotated_image:
46
- session.annotated_image.save(annotated_path)
47
-
48
- # Build markdown reply for follow-up question
49
- markdown_reply = (
50
- f"**{message}**\n\n"
51
- #f"**Original Image:**\n\n"
52
- #f"![Original Image](file/{original_path})\n\n"
53
- #f"**Detected Objects:**\n\n"
54
- #f"![Annotated Image](file/{annotated_path})"
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"![Original Image](file/{original_path})\n\n"
27
+ #f"**Detected Objects:**\n\n"
28
+ #f"![Annotated Image](file/{annotated_path})\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"![Original Image](file/{original_path})\n\n"
49
+ #f"**Detected Objects:**\n\n"
50
+ #f"![Annotated Image](file/{annotated_path})"
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 ---