developer0hye commited on
Commit
e8afa26
·
verified ·
1 Parent(s): 54febb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -1
app.py CHANGED
@@ -55,6 +55,25 @@ def identify_and_save_blob(blob_path):
55
  raise ValueError(f"An error occurred while processing the file: {e}")
56
 
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  @spaces.GPU
59
  def qwen_inference(media_input, text_input=None):
60
  if isinstance(media_input, str): # If it's a filepath
@@ -130,13 +149,20 @@ with gr.Blocks(css=css) as demo:
130
  with gr.Row():
131
  with gr.Column():
132
  input_media = gr.File(
133
- label="Upload Image or Video", type="filepath"
134
  )
 
135
  text_input = gr.Textbox(label="Question")
136
  submit_btn = gr.Button(value="Submit")
137
  with gr.Column():
138
  output_text = gr.Textbox(label="Output Text")
139
 
 
 
 
 
 
 
140
  submit_btn.click(
141
  qwen_inference, [input_media, text_input], [output_text]
142
  )
 
55
  raise ValueError(f"An error occurred while processing the file: {e}")
56
 
57
 
58
+ def process_file_upload(file_path):
59
+ """Process uploaded file and return the file path and image if applicable"""
60
+ if isinstance(file_path, str):
61
+ if file_path.endswith(tuple([i for i, f in image_extensions.items()])):
62
+ return file_path, Image.open(file_path)
63
+ elif file_path.endswith(video_extensions):
64
+ return file_path, None
65
+ else:
66
+ try:
67
+ media_path, media_type = identify_and_save_blob(file_path)
68
+ if media_type == "image":
69
+ return media_path, Image.open(media_path)
70
+ return media_path, None
71
+ except Exception as e:
72
+ print(e)
73
+ raise ValueError("Unsupported media type. Please upload an image or video.")
74
+ return None, None
75
+
76
+
77
  @spaces.GPU
78
  def qwen_inference(media_input, text_input=None):
79
  if isinstance(media_input, str): # If it's a filepath
 
149
  with gr.Row():
150
  with gr.Column():
151
  input_media = gr.File(
152
+ label="Upload Image or Video", type="filepath"
153
  )
154
+ preview_image = gr.Image(label="Preview", visible=True)
155
  text_input = gr.Textbox(label="Question")
156
  submit_btn = gr.Button(value="Submit")
157
  with gr.Column():
158
  output_text = gr.Textbox(label="Output Text")
159
 
160
+ input_media.change(
161
+ fn=process_file_upload,
162
+ inputs=[input_media],
163
+ outputs=[input_media, preview_image]
164
+ )
165
+
166
  submit_btn.click(
167
  qwen_inference, [input_media, text_input], [output_text]
168
  )