saa231 commited on
Commit
23013ca
Β·
verified Β·
1 Parent(s): 61a213b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -44
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 Initial Upload ---
7
- def handle_initial(image, audio):
8
- if image is None or audio is None:
9
- return "❗ Please upload both an image and an audio clip.", None
10
-
11
- message, answer_audio = process_inputs(session, image=image, audio_path=audio)
12
-
13
- # Save uploaded image
14
- image_save_path = "uploaded_image.png"
15
- image.save(image_save_path)
16
-
17
- # Markdown text
18
- markdown_reply = f"**{message}**\n\n![Context Image](file/{image_save_path})"
19
-
20
- return markdown_reply, answer_audio
21
-
22
- # --- Handle Follow-up ---
23
- def handle_followup(followup_audio):
24
- if followup_audio is None:
25
- return "❗ Please record a follow-up question.", None
26
-
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 text
34
- markdown_reply = f"**{message}**\n\n![Context Image](file/{image_save_path})"
35
-
36
- return markdown_reply, answer_audio
 
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
- submit_btn = gr.Button("Submit Initial Q&A")
 
47
 
48
- gr.Markdown("### 🎀 Ask a Follow-up Question")
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") # <== πŸ”₯ Changed to Markdown
54
  audio_output = gr.Audio(label="πŸ”Š Response Audio", interactive=False)
55
 
 
56
  submit_btn.click(
57
- fn=handle_initial,
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![Context Image](file/{image_save_path})"
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![Context Image](file/{image_save_path})"
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