Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ import subprocess
|
|
8 |
import numpy as np
|
9 |
import os
|
10 |
from threading import Thread
|
|
|
11 |
|
12 |
# Model and Processor Loading (Done once at startup)
|
13 |
MODEL_ID = "Qwen/Qwen2-VL-7B-Instruct"
|
@@ -24,17 +25,38 @@ image_extensions = Image.registered_extensions()
|
|
24 |
video_extensions = ("avi", "mp4", "mov", "mkv", "flv", "wmv", "mjpeg", "wav", "gif", "webm", "m4v", "3gp")
|
25 |
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
31 |
media_type = "image"
|
32 |
-
|
|
|
33 |
media_type = "video"
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
messages = [
|
40 |
{
|
@@ -75,7 +97,6 @@ def qwen_inference(media_path, text_input=None):
|
|
75 |
buffer += new_text
|
76 |
yield buffer
|
77 |
|
78 |
-
|
79 |
css = """
|
80 |
#output {
|
81 |
height: 500px;
|
@@ -91,7 +112,7 @@ with gr.Blocks(css=css) as demo:
|
|
91 |
with gr.Row():
|
92 |
with gr.Column():
|
93 |
input_media = gr.File(
|
94 |
-
label="Upload Image or Video", type="filepath"
|
95 |
)
|
96 |
text_input = gr.Textbox(label="Question")
|
97 |
submit_btn = gr.Button(value="Submit")
|
|
|
8 |
import numpy as np
|
9 |
import os
|
10 |
from threading import Thread
|
11 |
+
import io
|
12 |
|
13 |
# Model and Processor Loading (Done once at startup)
|
14 |
MODEL_ID = "Qwen/Qwen2-VL-7B-Instruct"
|
|
|
25 |
video_extensions = ("avi", "mp4", "mov", "mkv", "flv", "wmv", "mjpeg", "wav", "gif", "webm", "m4v", "3gp")
|
26 |
|
27 |
|
28 |
+
def identify_and_save_blob(blob):
|
29 |
+
"""Identifies if the blob is an image or video and saves it accordingly."""
|
30 |
+
try:
|
31 |
+
Image.open(io.BytesIO(blob)).verify() # Check if it's a valid image
|
32 |
+
extension = ".png"
|
33 |
media_type = "image"
|
34 |
+
except:
|
35 |
+
extension = ".mp4"
|
36 |
media_type = "video"
|
37 |
+
|
38 |
+
filename = f"temp_media{extension}"
|
39 |
+
with open(filename, "wb") as f:
|
40 |
+
f.write(blob)
|
41 |
+
return filename, media_type
|
42 |
+
|
43 |
+
|
44 |
+
@spaces.GPU
|
45 |
+
def qwen_inference(media_input, text_input=None):
|
46 |
+
if isinstance(media_input, str): # If it's a filepath
|
47 |
+
media_path = media_input
|
48 |
+
if media_path.endswith(tuple([i for i, f in image_extensions.items()])):
|
49 |
+
media_type = "image"
|
50 |
+
elif media_path.endswith(video_extensions):
|
51 |
+
media_type = "video"
|
52 |
+
else:
|
53 |
+
raise ValueError(
|
54 |
+
"Unsupported media type. Please upload an image or video."
|
55 |
+
)
|
56 |
+
else: # If it's a blob
|
57 |
+
media_path, media_type = identify_and_save_blob(media_input)
|
58 |
+
|
59 |
+
print(media_path)
|
60 |
|
61 |
messages = [
|
62 |
{
|
|
|
97 |
buffer += new_text
|
98 |
yield buffer
|
99 |
|
|
|
100 |
css = """
|
101 |
#output {
|
102 |
height: 500px;
|
|
|
112 |
with gr.Row():
|
113 |
with gr.Column():
|
114 |
input_media = gr.File(
|
115 |
+
label="Upload Image or Video", type="filepath"
|
116 |
)
|
117 |
text_input = gr.Textbox(label="Question")
|
118 |
submit_btn = gr.Button(value="Submit")
|