Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import mimetypes
|
| 3 |
from typing import List, Optional
|
| 4 |
|
|
@@ -7,7 +8,7 @@ import gradio as gr
|
|
| 7 |
import numpy as np
|
| 8 |
from PIL import Image
|
| 9 |
import torch
|
| 10 |
-
from transformers import AutoProcessor, AutoModelForVision2Seq
|
| 11 |
|
| 12 |
# =========================
|
| 13 |
# Config
|
|
@@ -19,11 +20,11 @@ MODEL_ID = "Qwen/Qwen2-VL-2B-Instruct"
|
|
| 19 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 20 |
DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
|
| 21 |
|
| 22 |
-
MAX_VIDEO_SECONDS = 15
|
| 23 |
-
MAX_FILE_MB = 60
|
| 24 |
-
MAX_SIDE = 768
|
| 25 |
-
MAX_FRAMES = 4
|
| 26 |
-
MAX_NEW_TOKENS = 256
|
| 27 |
|
| 28 |
# =========================
|
| 29 |
# Load model & processor
|
|
@@ -36,17 +37,10 @@ model = AutoModelForVision2Seq.from_pretrained(
|
|
| 36 |
trust_remote_code=True,
|
| 37 |
).to(DEVICE)
|
| 38 |
|
| 39 |
-
|
| 40 |
# =========================
|
| 41 |
# Helpers
|
| 42 |
# =========================
|
| 43 |
def get_path(file_obj) -> Optional[str]:
|
| 44 |
-
"""
|
| 45 |
-
Gradio <File> may arrive as:
|
| 46 |
-
- str (tmp path) -> return as-is
|
| 47 |
-
- dict {"name": "..."} -> return ["name"] or ["path"]
|
| 48 |
-
- SimpleNamespace with .name -> return .name
|
| 49 |
-
"""
|
| 50 |
if file_obj is None:
|
| 51 |
return None
|
| 52 |
if isinstance(file_obj, str):
|
|
@@ -55,17 +49,14 @@ def get_path(file_obj) -> Optional[str]:
|
|
| 55 |
return file_obj.get("name") or file_obj.get("path")
|
| 56 |
return getattr(file_obj, "name", None)
|
| 57 |
|
| 58 |
-
|
| 59 |
def is_image_file(path: str) -> bool:
|
| 60 |
kind, _ = mimetypes.guess_type(path)
|
| 61 |
return (kind or "").startswith("image/")
|
| 62 |
|
| 63 |
-
|
| 64 |
def is_video_file(path: str) -> bool:
|
| 65 |
kind, _ = mimetypes.guess_type(path)
|
| 66 |
return (kind or "").startswith("video/")
|
| 67 |
|
| 68 |
-
|
| 69 |
def resize_max_side(img: Image.Image, max_side: int = MAX_SIDE) -> Image.Image:
|
| 70 |
w, h = img.size
|
| 71 |
m = max(w, h)
|
|
@@ -74,27 +65,20 @@ def resize_max_side(img: Image.Image, max_side: int = MAX_SIDE) -> Image.Image:
|
|
| 74 |
s = max_side / float(m)
|
| 75 |
return img.resize((int(w * s), int(h * s)), Image.LANCZOS)
|
| 76 |
|
| 77 |
-
|
| 78 |
def sample_video_frames(video_path: str, max_seconds=MAX_VIDEO_SECONDS, k=MAX_FRAMES) -> List[Image.Image]:
|
| 79 |
-
"""Take K evenly spaced frames within the first `max_seconds`."""
|
| 80 |
cap = cv2.VideoCapture(video_path)
|
| 81 |
if not cap.isOpened():
|
| 82 |
return []
|
| 83 |
-
|
| 84 |
fps = cap.get(cv2.CAP_PROP_FPS) or 25.0
|
| 85 |
total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT) or 0)
|
| 86 |
duration = total / max(fps, 1)
|
| 87 |
-
|
| 88 |
-
# Only consider frames within MAX_VIDEO_SECONDS (or all if shorter)
|
| 89 |
limit_frames = int(min(duration, max_seconds) * fps) if total else 0
|
| 90 |
nframes = min(limit_frames or total, total) if total > 0 else 0
|
| 91 |
if nframes == 0:
|
| 92 |
nframes = int(min(duration, max_seconds) * fps) or 1
|
| 93 |
-
|
| 94 |
k = max(1, min(k, nframes))
|
| 95 |
idxs = [int(i * nframes / k + nframes / (2 * k)) for i in range(k)]
|
| 96 |
idxs = sorted(set(max(0, min(nframes - 1, x)) for x in idxs))
|
| 97 |
-
|
| 98 |
frames: List[Image.Image] = []
|
| 99 |
for target in idxs:
|
| 100 |
cap.set(cv2.CAP_PROP_POS_FRAMES, target)
|
|
@@ -107,9 +91,7 @@ def sample_video_frames(video_path: str, max_seconds=MAX_VIDEO_SECONDS, k=MAX_FR
|
|
| 107 |
cap.release()
|
| 108 |
return frames
|
| 109 |
|
| 110 |
-
|
| 111 |
def build_messages(prompt: str) -> list:
|
| 112 |
-
"""Qwen2-VL chat template expects <image> tag in user turn."""
|
| 113 |
return [{
|
| 114 |
"role": "user",
|
| 115 |
"content": [
|
|
@@ -118,32 +100,33 @@ def build_messages(prompt: str) -> list:
|
|
| 118 |
]
|
| 119 |
}]
|
| 120 |
|
| 121 |
-
|
| 122 |
# =========================
|
| 123 |
-
# Chat
|
| 124 |
# =========================
|
| 125 |
-
def
|
| 126 |
history = history or []
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
path = get_path(file_obj)
|
| 131 |
if not path or not os.path.exists(path):
|
| 132 |
history[-1][1] = "⚠️ Please upload an image or a short video first."
|
| 133 |
-
yield history
|
| 134 |
return
|
| 135 |
|
| 136 |
-
# Size guard
|
| 137 |
try:
|
| 138 |
if os.path.getsize(path) > MAX_FILE_MB * 1024 * 1024:
|
| 139 |
history[-1][1] = f"⚠️ File is larger than {MAX_FILE_MB} MB."
|
| 140 |
-
yield history
|
| 141 |
return
|
| 142 |
except Exception:
|
| 143 |
pass
|
| 144 |
|
| 145 |
-
#
|
| 146 |
-
images: List[Image.Image] = []
|
| 147 |
try:
|
| 148 |
if is_image_file(path):
|
| 149 |
images = [resize_max_side(Image.open(path).convert("RGB"))]
|
|
@@ -151,48 +134,77 @@ def chat_fn(history, file_obj, prompt):
|
|
| 151 |
images = sample_video_frames(path, MAX_VIDEO_SECONDS, MAX_FRAMES)
|
| 152 |
if not images:
|
| 153 |
history[-1][1] = "⚠️ Could not read frames from the video."
|
| 154 |
-
yield history
|
| 155 |
return
|
| 156 |
else:
|
| 157 |
history[-1][1] = "⚠️ Unsupported file type."
|
| 158 |
-
yield history
|
| 159 |
return
|
| 160 |
|
| 161 |
-
# Build chat input
|
| 162 |
messages = build_messages(prompt)
|
| 163 |
-
chat_str = processor.apply_chat_template(
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
)
|
| 166 |
|
| 167 |
-
#
|
| 168 |
-
enc = processor(
|
| 169 |
-
text=chat_str,
|
| 170 |
-
images=images, # ALWAYS a list
|
| 171 |
-
return_tensors="pt"
|
| 172 |
-
).to(model.device)
|
| 173 |
-
|
| 174 |
-
# Inference (one-shot; more robust than streaming for Spaces CPU)
|
| 175 |
with torch.no_grad():
|
| 176 |
out_ids = model.generate(
|
| 177 |
**enc,
|
| 178 |
max_new_tokens=MAX_NEW_TOKENS,
|
| 179 |
do_sample=False,
|
|
|
|
| 180 |
)
|
| 181 |
|
| 182 |
-
#
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
-
history
|
| 189 |
-
yield history
|
| 190 |
|
| 191 |
except Exception as e:
|
| 192 |
-
# Show clean error in the bubble
|
| 193 |
history[-1][1] = f"⚠️ Error: {str(e)}"
|
| 194 |
-
yield history
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
# =========================
|
| 198 |
# Gradio UI
|
|
@@ -223,7 +235,6 @@ with gr.Blocks(title=TITLE, css=CSS, theme=gr.themes.Soft()) as demo:
|
|
| 223 |
)
|
| 224 |
send = gr.Button("Submit", variant="primary")
|
| 225 |
|
| 226 |
-
# Preview on file change
|
| 227 |
def on_file(file_obj):
|
| 228 |
path = get_path(file_obj)
|
| 229 |
if not path or not os.path.exists(path):
|
|
@@ -236,7 +247,7 @@ with gr.Blocks(title=TITLE, css=CSS, theme=gr.themes.Soft()) as demo:
|
|
| 236 |
return None
|
| 237 |
|
| 238 |
uploader.change(on_file, inputs=[uploader], outputs=[preview])
|
| 239 |
-
send.click(
|
| 240 |
|
| 241 |
if __name__ == "__main__":
|
| 242 |
demo.queue().launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
import mimetypes
|
| 4 |
from typing import List, Optional
|
| 5 |
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
import torch
|
| 11 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq, TextStreamer
|
| 12 |
|
| 13 |
# =========================
|
| 14 |
# Config
|
|
|
|
| 20 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
|
| 22 |
|
| 23 |
+
MAX_VIDEO_SECONDS = 15
|
| 24 |
+
MAX_FILE_MB = 60
|
| 25 |
+
MAX_SIDE = 768
|
| 26 |
+
MAX_FRAMES = 4
|
| 27 |
+
MAX_NEW_TOKENS = 256
|
| 28 |
|
| 29 |
# =========================
|
| 30 |
# Load model & processor
|
|
|
|
| 37 |
trust_remote_code=True,
|
| 38 |
).to(DEVICE)
|
| 39 |
|
|
|
|
| 40 |
# =========================
|
| 41 |
# Helpers
|
| 42 |
# =========================
|
| 43 |
def get_path(file_obj) -> Optional[str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if file_obj is None:
|
| 45 |
return None
|
| 46 |
if isinstance(file_obj, str):
|
|
|
|
| 49 |
return file_obj.get("name") or file_obj.get("path")
|
| 50 |
return getattr(file_obj, "name", None)
|
| 51 |
|
|
|
|
| 52 |
def is_image_file(path: str) -> bool:
|
| 53 |
kind, _ = mimetypes.guess_type(path)
|
| 54 |
return (kind or "").startswith("image/")
|
| 55 |
|
|
|
|
| 56 |
def is_video_file(path: str) -> bool:
|
| 57 |
kind, _ = mimetypes.guess_type(path)
|
| 58 |
return (kind or "").startswith("video/")
|
| 59 |
|
|
|
|
| 60 |
def resize_max_side(img: Image.Image, max_side: int = MAX_SIDE) -> Image.Image:
|
| 61 |
w, h = img.size
|
| 62 |
m = max(w, h)
|
|
|
|
| 65 |
s = max_side / float(m)
|
| 66 |
return img.resize((int(w * s), int(h * s)), Image.LANCZOS)
|
| 67 |
|
|
|
|
| 68 |
def sample_video_frames(video_path: str, max_seconds=MAX_VIDEO_SECONDS, k=MAX_FRAMES) -> List[Image.Image]:
|
|
|
|
| 69 |
cap = cv2.VideoCapture(video_path)
|
| 70 |
if not cap.isOpened():
|
| 71 |
return []
|
|
|
|
| 72 |
fps = cap.get(cv2.CAP_PROP_FPS) or 25.0
|
| 73 |
total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT) or 0)
|
| 74 |
duration = total / max(fps, 1)
|
|
|
|
|
|
|
| 75 |
limit_frames = int(min(duration, max_seconds) * fps) if total else 0
|
| 76 |
nframes = min(limit_frames or total, total) if total > 0 else 0
|
| 77 |
if nframes == 0:
|
| 78 |
nframes = int(min(duration, max_seconds) * fps) or 1
|
|
|
|
| 79 |
k = max(1, min(k, nframes))
|
| 80 |
idxs = [int(i * nframes / k + nframes / (2 * k)) for i in range(k)]
|
| 81 |
idxs = sorted(set(max(0, min(nframes - 1, x)) for x in idxs))
|
|
|
|
| 82 |
frames: List[Image.Image] = []
|
| 83 |
for target in idxs:
|
| 84 |
cap.set(cv2.CAP_PROP_POS_FRAMES, target)
|
|
|
|
| 91 |
cap.release()
|
| 92 |
return frames
|
| 93 |
|
|
|
|
| 94 |
def build_messages(prompt: str) -> list:
|
|
|
|
| 95 |
return [{
|
| 96 |
"role": "user",
|
| 97 |
"content": [
|
|
|
|
| 100 |
]
|
| 101 |
}]
|
| 102 |
|
|
|
|
| 103 |
# =========================
|
| 104 |
+
# Chat (streaming)
|
| 105 |
# =========================
|
| 106 |
+
def chat_stream(history, file_obj, prompt):
|
| 107 |
history = history or []
|
| 108 |
+
# 1) Add user turn
|
| 109 |
+
history.append([prompt, ""])
|
| 110 |
+
# 2) Add assistant placeholder
|
| 111 |
+
history.append(["", "⏳ processing…"])
|
| 112 |
+
# Immediately show placeholder and disable inputs
|
| 113 |
+
yield history, gr.update(interactive=False), gr.update(interactive=False)
|
| 114 |
|
| 115 |
path = get_path(file_obj)
|
| 116 |
if not path or not os.path.exists(path):
|
| 117 |
history[-1][1] = "⚠️ Please upload an image or a short video first."
|
| 118 |
+
yield history, gr.update(interactive=True), gr.update(interactive=True)
|
| 119 |
return
|
| 120 |
|
|
|
|
| 121 |
try:
|
| 122 |
if os.path.getsize(path) > MAX_FILE_MB * 1024 * 1024:
|
| 123 |
history[-1][1] = f"⚠️ File is larger than {MAX_FILE_MB} MB."
|
| 124 |
+
yield history, gr.update(interactive=True), gr.update(interactive=True)
|
| 125 |
return
|
| 126 |
except Exception:
|
| 127 |
pass
|
| 128 |
|
| 129 |
+
# Prepare images list
|
|
|
|
| 130 |
try:
|
| 131 |
if is_image_file(path):
|
| 132 |
images = [resize_max_side(Image.open(path).convert("RGB"))]
|
|
|
|
| 134 |
images = sample_video_frames(path, MAX_VIDEO_SECONDS, MAX_FRAMES)
|
| 135 |
if not images:
|
| 136 |
history[-1][1] = "⚠️ Could not read frames from the video."
|
| 137 |
+
yield history, gr.update(interactive=True), gr.update(interactive=True)
|
| 138 |
return
|
| 139 |
else:
|
| 140 |
history[-1][1] = "⚠️ Unsupported file type."
|
| 141 |
+
yield history, gr.update(interactive=True), gr.update(interactive=True)
|
| 142 |
return
|
| 143 |
|
|
|
|
| 144 |
messages = build_messages(prompt)
|
| 145 |
+
chat_str = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
|
| 146 |
+
enc = processor(text=chat_str, images=images, return_tensors="pt").to(model.device)
|
| 147 |
+
|
| 148 |
+
# Streaming callback
|
| 149 |
+
acc = []
|
| 150 |
+
last_push = time.time()
|
| 151 |
+
|
| 152 |
+
def on_text(chunk: str):
|
| 153 |
+
nonlocal acc, last_push, history
|
| 154 |
+
acc.append(chunk)
|
| 155 |
+
# throttle UI updates to avoid spamming the frontend
|
| 156 |
+
if time.time() - last_push > 0.06:
|
| 157 |
+
history[-1][1] = "".join(acc)
|
| 158 |
+
last_push = time.time()
|
| 159 |
+
# Keep inputs disabled while streaming
|
| 160 |
+
yield_state = (history, gr.update(interactive=False), gr.update(interactive=False))
|
| 161 |
+
# Gradio requires generator; hack by storing to function attr
|
| 162 |
+
chat_stream._yield = yield_state # type: ignore
|
| 163 |
+
|
| 164 |
+
streamer = TextStreamer(
|
| 165 |
+
processor.tokenizer,
|
| 166 |
+
skip_prompt=True,
|
| 167 |
+
skip_special_tokens=True,
|
| 168 |
+
callback=on_text,
|
| 169 |
)
|
| 170 |
|
| 171 |
+
# Kick off generation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
with torch.no_grad():
|
| 173 |
out_ids = model.generate(
|
| 174 |
**enc,
|
| 175 |
max_new_tokens=MAX_NEW_TOKENS,
|
| 176 |
do_sample=False,
|
| 177 |
+
streamer=streamer,
|
| 178 |
)
|
| 179 |
|
| 180 |
+
# Flush final text
|
| 181 |
+
if acc:
|
| 182 |
+
history[-1][1] = "".join(acc).strip()
|
| 183 |
+
else:
|
| 184 |
+
# Fallback (non-stream)
|
| 185 |
+
cut = enc["input_ids"].shape[-1]
|
| 186 |
+
text = processor.batch_decode(out_ids[:, cut:], skip_special_tokens=True)[0].strip()
|
| 187 |
+
history[-1][1] = text if text else "(no output)"
|
| 188 |
|
| 189 |
+
yield history, gr.update(interactive=True), gr.update(interactive=True)
|
|
|
|
| 190 |
|
| 191 |
except Exception as e:
|
|
|
|
| 192 |
history[-1][1] = f"⚠️ Error: {str(e)}"
|
| 193 |
+
yield history, gr.update(interactive=True), gr.update(interactive=True)
|
| 194 |
+
|
| 195 |
+
# A tiny wrapper to surface mid-stream yields from callback
|
| 196 |
+
def _pump_stream(*args, **kwargs):
|
| 197 |
+
gen = chat_stream(*args, **kwargs)
|
| 198 |
+
while True:
|
| 199 |
+
# Check if callback parked a mid-step yield
|
| 200 |
+
ys = getattr(chat_stream, "_yield", None)
|
| 201 |
+
if ys is not None:
|
| 202 |
+
setattr(chat_stream, "_yield", None)
|
| 203 |
+
yield ys
|
| 204 |
+
try:
|
| 205 |
+
yield next(gen)
|
| 206 |
+
except StopIteration:
|
| 207 |
+
break
|
| 208 |
|
| 209 |
# =========================
|
| 210 |
# Gradio UI
|
|
|
|
| 235 |
)
|
| 236 |
send = gr.Button("Submit", variant="primary")
|
| 237 |
|
|
|
|
| 238 |
def on_file(file_obj):
|
| 239 |
path = get_path(file_obj)
|
| 240 |
if not path or not os.path.exists(path):
|
|
|
|
| 247 |
return None
|
| 248 |
|
| 249 |
uploader.change(on_file, inputs=[uploader], outputs=[preview])
|
| 250 |
+
send.click(_pump_stream, inputs=[chat, uploader, prompt_in], outputs=[chat, send, prompt_in])
|
| 251 |
|
| 252 |
if __name__ == "__main__":
|
| 253 |
demo.queue().launch()
|