Spaces:
Running
on
Zero
Running
on
Zero
update app
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import json
|
|
| 5 |
import time
|
| 6 |
import asyncio
|
| 7 |
from threading import Thread
|
|
|
|
| 8 |
|
| 9 |
import gradio as gr
|
| 10 |
import spaces
|
|
@@ -15,14 +16,75 @@ import cv2
|
|
| 15 |
|
| 16 |
from transformers import (
|
| 17 |
Qwen2_5_VLForConditionalGeneration,
|
| 18 |
-
|
| 19 |
AutoTokenizer,
|
| 20 |
AutoProcessor,
|
| 21 |
TextIteratorStreamer,
|
| 22 |
)
|
| 23 |
from transformers.image_utils import load_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# Constants for text generation
|
| 26 |
MAX_MAX_NEW_TOKENS = 2048
|
| 27 |
DEFAULT_MAX_NEW_TOKENS = 1024
|
| 28 |
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
|
@@ -47,19 +109,19 @@ model_x = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
|
| 47 |
torch_dtype=torch.float16
|
| 48 |
).to(device).eval()
|
| 49 |
|
| 50 |
-
# Load
|
| 51 |
-
MODEL_ID_Q = "
|
| 52 |
processor_q = AutoProcessor.from_pretrained(MODEL_ID_Q, trust_remote_code=True)
|
| 53 |
-
model_q =
|
| 54 |
MODEL_ID_Q,
|
| 55 |
trust_remote_code=True,
|
| 56 |
torch_dtype=torch.float16
|
| 57 |
).to(device).eval()
|
| 58 |
|
| 59 |
-
# Load
|
| 60 |
-
MODEL_ID_Y = "
|
| 61 |
processor_y = AutoProcessor.from_pretrained(MODEL_ID_Y, trust_remote_code=True)
|
| 62 |
-
model_y =
|
| 63 |
MODEL_ID_Y,
|
| 64 |
trust_remote_code=True,
|
| 65 |
torch_dtype=torch.float16
|
|
@@ -74,7 +136,8 @@ def downsample_video(video_path):
|
|
| 74 |
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 75 |
fps = vidcap.get(cv2.CAP_PROP_FPS)
|
| 76 |
frames = []
|
| 77 |
-
|
|
|
|
| 78 |
for i in frame_indices:
|
| 79 |
vidcap.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 80 |
success, image = vidcap.read()
|
|
@@ -95,20 +158,15 @@ def generate_image(model_name: str, text: str, image: Image.Image,
|
|
| 95 |
repetition_penalty: float = 1.2):
|
| 96 |
"""
|
| 97 |
Generates responses using the selected model for image input.
|
| 98 |
-
Yields raw text and Markdown-formatted text.
|
| 99 |
"""
|
| 100 |
if model_name == "Qwen2.5-VL-7B-Instruct":
|
| 101 |
-
processor = processor_m
|
| 102 |
-
model = model_m
|
| 103 |
elif model_name == "Qwen2.5-VL-3B-Instruct":
|
| 104 |
-
processor = processor_x
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
model =
|
| 109 |
-
elif model_name == "Lumian2-VLR-7B-Thinking":
|
| 110 |
-
processor = processor_y
|
| 111 |
-
model = model_y
|
| 112 |
else:
|
| 113 |
yield "Invalid model selected.", "Invalid model selected."
|
| 114 |
return
|
|
@@ -117,21 +175,11 @@ def generate_image(model_name: str, text: str, image: Image.Image,
|
|
| 117 |
yield "Please upload an image.", "Please upload an image."
|
| 118 |
return
|
| 119 |
|
| 120 |
-
messages = [{
|
| 121 |
-
"role": "user",
|
| 122 |
-
"content": [
|
| 123 |
-
{"type": "image", "image": image},
|
| 124 |
-
{"type": "text", "text": text},
|
| 125 |
-
]
|
| 126 |
-
}]
|
| 127 |
prompt_full = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 128 |
inputs = processor(
|
| 129 |
-
text=[prompt_full],
|
| 130 |
-
|
| 131 |
-
return_tensors="pt",
|
| 132 |
-
padding=True,
|
| 133 |
-
truncation=False,
|
| 134 |
-
max_length=MAX_INPUT_TOKEN_LENGTH
|
| 135 |
).to(device)
|
| 136 |
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
| 137 |
generation_kwargs = {**inputs, "streamer": streamer, "max_new_tokens": max_new_tokens}
|
|
@@ -152,20 +200,15 @@ def generate_video(model_name: str, text: str, video_path: str,
|
|
| 152 |
repetition_penalty: float = 1.2):
|
| 153 |
"""
|
| 154 |
Generates responses using the selected model for video input.
|
| 155 |
-
Yields raw text and Markdown-formatted text.
|
| 156 |
"""
|
| 157 |
if model_name == "Qwen2.5-VL-7B-Instruct":
|
| 158 |
-
processor = processor_m
|
| 159 |
-
model = model_m
|
| 160 |
elif model_name == "Qwen2.5-VL-3B-Instruct":
|
| 161 |
-
processor = processor_x
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
model =
|
| 166 |
-
elif model_name == "Lumian2-VLR-7B-Thinking":
|
| 167 |
-
processor = processor_y
|
| 168 |
-
model = model_y
|
| 169 |
else:
|
| 170 |
yield "Invalid model selected.", "Invalid model selected."
|
| 171 |
return
|
|
@@ -174,43 +217,38 @@ def generate_video(model_name: str, text: str, video_path: str,
|
|
| 174 |
yield "Please upload a video.", "Please upload a video."
|
| 175 |
return
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
messages[
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
truncation=False,
|
| 193 |
-
max_length=MAX_INPUT_TOKEN_LENGTH
|
| 194 |
).to(device)
|
| 195 |
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
| 196 |
generation_kwargs = {
|
| 197 |
-
**inputs,
|
| 198 |
-
"
|
| 199 |
-
"
|
| 200 |
-
"do_sample": True,
|
| 201 |
-
"temperature": temperature,
|
| 202 |
-
"top_p": top_p,
|
| 203 |
-
"top_k": top_k,
|
| 204 |
-
"repetition_penalty": repetition_penalty,
|
| 205 |
}
|
| 206 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 207 |
thread.start()
|
| 208 |
buffer = ""
|
| 209 |
for new_text in streamer:
|
| 210 |
buffer += new_text
|
|
|
|
| 211 |
time.sleep(0.01)
|
| 212 |
yield buffer, buffer
|
| 213 |
|
|
|
|
| 214 |
# Define examples for image and video inference
|
| 215 |
image_examples = [
|
| 216 |
["Explain the content in detail.", "images/D.jpg"],
|
|
@@ -228,42 +266,32 @@ video_examples = [
|
|
| 228 |
]
|
| 229 |
|
| 230 |
css = """
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
color: white !important;
|
| 234 |
-
}
|
| 235 |
-
.submit-btn:hover {
|
| 236 |
-
background-color: #3498db !important;
|
| 237 |
}
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
border-radius: 10px;
|
| 241 |
-
padding: 20px;
|
| 242 |
}
|
| 243 |
"""
|
| 244 |
|
| 245 |
# Create the Gradio Interface
|
| 246 |
-
with gr.Blocks(css=css, theme=
|
| 247 |
-
gr.Markdown("# **
|
| 248 |
with gr.Row():
|
| 249 |
-
with gr.Column():
|
| 250 |
with gr.Tabs():
|
| 251 |
with gr.TabItem("Image Inference"):
|
| 252 |
image_query = gr.Textbox(label="Query Input", placeholder="Enter your query here...")
|
| 253 |
image_upload = gr.Image(type="pil", label="Image", height=290)
|
| 254 |
-
image_submit = gr.Button("Submit",
|
| 255 |
-
gr.Examples(
|
| 256 |
-
|
| 257 |
-
inputs=[image_query, image_upload]
|
| 258 |
-
)
|
| 259 |
with gr.TabItem("Video Inference"):
|
| 260 |
video_query = gr.Textbox(label="Query Input", placeholder="✦︎ Enter your query here...")
|
| 261 |
video_upload = gr.Video(label="Video", height=290)
|
| 262 |
-
video_submit = gr.Button("Submit",
|
| 263 |
-
gr.Examples(
|
| 264 |
-
|
| 265 |
-
inputs=[video_query, video_upload]
|
| 266 |
-
)
|
| 267 |
with gr.Accordion("Advanced options", open=False):
|
| 268 |
max_new_tokens = gr.Slider(label="Max new tokens", minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS)
|
| 269 |
temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=4.0, step=0.1, value=0.6)
|
|
@@ -271,33 +299,18 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
| 271 |
top_k = gr.Slider(label="Top-k", minimum=1, maximum=1000, step=1, value=50)
|
| 272 |
repetition_penalty = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2)
|
| 273 |
|
| 274 |
-
with gr.Column():
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
with gr.Accordion("(Result.md)", open=False):
|
| 280 |
-
markdown_output = gr.Markdown()
|
| 281 |
|
| 282 |
model_choice = gr.Radio(
|
| 283 |
-
choices=["
|
| 284 |
label="Select Model",
|
| 285 |
-
value="
|
| 286 |
-
)
|
| 287 |
-
gr.Markdown("**Model Info 💻** | [Report Bug](https://huggingface.co/spaces/prithivMLmods/Qwen2.5-VL/discussions)")
|
| 288 |
-
|
| 289 |
-
gr.Markdown(
|
| 290 |
-
"""
|
| 291 |
-
> [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct): The Qwen2.5-VL-7B-Instruct model is a multimodal AI model developed by Alibaba Cloud that excels at understanding both text and images. It's a Vision-Language Model (VLM) designed to handle various visual understanding tasks, including image understanding, video analysis, and even multilingual support.
|
| 292 |
-
>
|
| 293 |
-
> [Qwen2.5-VL-7B-Abliterated-Caption-it](prithivMLmods/Qwen2.5-VL-7B-Abliterated-Caption-it): Qwen2.5-VL-7B-Abliterated-Caption-it is a fine-tuned version of Qwen2.5-VL-7B-Instruct, optimized for Abliterated Captioning / Uncensored Captioning. This model excels at generating detailed, context-rich, and high-fidelity captions across diverse image categories and variational aspect ratios, offering robust visual understanding without filtering or censorship.
|
| 294 |
-
"""
|
| 295 |
)
|
| 296 |
|
| 297 |
-
gr.Markdown("> [Lumian2-VLR-7B-Thinking](https://huggingface.co/prithivMLmods/Lumian2-VLR-7B-Thinking): The Lumian2-VLR-7B-Thinking model is a high-fidelity vision-language reasoning (experimental model) system designed for fine-grained multimodal understanding. Built on Qwen2.5-VL-7B-Instruct, this model enhances image captioning, sampled video reasoning, and document comprehension through explicit grounded reasoning. It produces structured reasoning traces aligned with visual coordinates, enabling explainable multimodal reasoning.")
|
| 298 |
-
|
| 299 |
-
gr.Markdown(">⚠️note: all the models in space are not guaranteed to perform well in video inference use cases.")
|
| 300 |
-
|
| 301 |
image_submit.click(
|
| 302 |
fn=generate_image,
|
| 303 |
inputs=[model_choice, image_query, image_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
|
|
@@ -310,4 +323,4 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
| 310 |
)
|
| 311 |
|
| 312 |
if __name__ == "__main__":
|
| 313 |
-
demo.queue(max_size=50).launch(
|
|
|
|
| 5 |
import time
|
| 6 |
import asyncio
|
| 7 |
from threading import Thread
|
| 8 |
+
from typing import Iterable
|
| 9 |
|
| 10 |
import gradio as gr
|
| 11 |
import spaces
|
|
|
|
| 16 |
|
| 17 |
from transformers import (
|
| 18 |
Qwen2_5_VLForConditionalGeneration,
|
| 19 |
+
Qwen3VLForConditionalGeneration,
|
| 20 |
AutoTokenizer,
|
| 21 |
AutoProcessor,
|
| 22 |
TextIteratorStreamer,
|
| 23 |
)
|
| 24 |
from transformers.image_utils import load_image
|
| 25 |
+
from gradio.themes import Soft
|
| 26 |
+
from gradio.themes.utils import colors, fonts, sizes
|
| 27 |
+
|
| 28 |
+
colors.thistle = colors.Color(
|
| 29 |
+
name="thistle",
|
| 30 |
+
c50="#F9F5F9", c100="#F0E8F1", c200="#E7DBE8", c300="#DECEE0",
|
| 31 |
+
c400="#D2BFD8", c500="#D8BFD8", c600="#B59CB7", c700="#927996",
|
| 32 |
+
c800="#6F5675", c900="#4C3454", c950="#291233",
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
class ThistleTheme(Soft):
|
| 36 |
+
def __init__(
|
| 37 |
+
self,
|
| 38 |
+
*,
|
| 39 |
+
primary_hue: colors.Color | str = colors.gray,
|
| 40 |
+
secondary_hue: colors.Color | str = colors.thistle,
|
| 41 |
+
neutral_hue: colors.Color | str = colors.slate,
|
| 42 |
+
text_size: sizes.Size | str = sizes.text_lg,
|
| 43 |
+
font: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 44 |
+
fonts.GoogleFont("Outfit"), "Arial", "sans-serif",
|
| 45 |
+
),
|
| 46 |
+
font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 47 |
+
fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace",
|
| 48 |
+
),
|
| 49 |
+
):
|
| 50 |
+
super().__init__(
|
| 51 |
+
primary_hue=primary_hue,
|
| 52 |
+
secondary_hue=secondary_hue,
|
| 53 |
+
neutral_hue=neutral_hue,
|
| 54 |
+
text_size=text_size,
|
| 55 |
+
font=font,
|
| 56 |
+
font_mono=font_mono,
|
| 57 |
+
)
|
| 58 |
+
super().set(
|
| 59 |
+
background_fill_primary="*primary_50",
|
| 60 |
+
background_fill_primary_dark="*primary_900",
|
| 61 |
+
body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)",
|
| 62 |
+
body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)",
|
| 63 |
+
button_primary_text_color="black",
|
| 64 |
+
button_primary_text_color_hover="white",
|
| 65 |
+
button_primary_background_fill="linear-gradient(90deg, *secondary_400, *secondary_400)",
|
| 66 |
+
button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_600)",
|
| 67 |
+
button_primary_background_fill_dark="linear-gradient(90deg, *secondary_600, *secondary_800)",
|
| 68 |
+
button_primary_background_fill_hover_dark="linear-gradient(90deg, *secondary_500, *secondary_500)",
|
| 69 |
+
button_secondary_text_color="black",
|
| 70 |
+
button_secondary_text_color_hover="white",
|
| 71 |
+
button_secondary_background_fill="linear-gradient(90deg, *primary_300, *primary_300)",
|
| 72 |
+
button_secondary_background_fill_hover="linear-gradient(90deg, *primary_400, *primary_400)",
|
| 73 |
+
button_secondary_background_fill_dark="linear-gradient(90deg, *primary_500, *primary_600)",
|
| 74 |
+
button_secondary_background_fill_hover_dark="linear-gradient(90deg, *primary_500, *primary_500)",
|
| 75 |
+
slider_color="*secondary_300",
|
| 76 |
+
slider_color_dark="*secondary_600",
|
| 77 |
+
block_title_text_weight="600",
|
| 78 |
+
block_border_width="3px",
|
| 79 |
+
block_shadow="*shadow_drop_lg",
|
| 80 |
+
button_primary_shadow="*shadow_drop_lg",
|
| 81 |
+
button_large_padding="11px",
|
| 82 |
+
color_accent_soft="*primary_100",
|
| 83 |
+
block_label_background_fill="*primary_200",
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
thistle_theme = ThistleTheme()
|
| 87 |
|
|
|
|
| 88 |
MAX_MAX_NEW_TOKENS = 2048
|
| 89 |
DEFAULT_MAX_NEW_TOKENS = 1024
|
| 90 |
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
|
|
|
| 109 |
torch_dtype=torch.float16
|
| 110 |
).to(device).eval()
|
| 111 |
|
| 112 |
+
# Load Qwen3-VL-4B-Instruct
|
| 113 |
+
MODEL_ID_Q = "Qwen/Qwen3-VL-4B-Instruct"
|
| 114 |
processor_q = AutoProcessor.from_pretrained(MODEL_ID_Q, trust_remote_code=True)
|
| 115 |
+
model_q = Qwen3VLForConditionalGeneration.from_pretrained(
|
| 116 |
MODEL_ID_Q,
|
| 117 |
trust_remote_code=True,
|
| 118 |
torch_dtype=torch.float16
|
| 119 |
).to(device).eval()
|
| 120 |
|
| 121 |
+
# Load Qwen3-VL-8B-Instruct
|
| 122 |
+
MODEL_ID_Y = "Qwen/Qwen3-VL-8B-Instruct"
|
| 123 |
processor_y = AutoProcessor.from_pretrained(MODEL_ID_Y, trust_remote_code=True)
|
| 124 |
+
model_y = Qwen3VLForConditionalGeneration.from_pretrained(
|
| 125 |
MODEL_ID_Y,
|
| 126 |
trust_remote_code=True,
|
| 127 |
torch_dtype=torch.float16
|
|
|
|
| 136 |
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 137 |
fps = vidcap.get(cv2.CAP_PROP_FPS)
|
| 138 |
frames = []
|
| 139 |
+
# Use a maximum of 10 frames to avoid excessive memory usage
|
| 140 |
+
frame_indices = np.linspace(0, total_frames - 1, min(total_frames, 10), dtype=int)
|
| 141 |
for i in frame_indices:
|
| 142 |
vidcap.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 143 |
success, image = vidcap.read()
|
|
|
|
| 158 |
repetition_penalty: float = 1.2):
|
| 159 |
"""
|
| 160 |
Generates responses using the selected model for image input.
|
|
|
|
| 161 |
"""
|
| 162 |
if model_name == "Qwen2.5-VL-7B-Instruct":
|
| 163 |
+
processor, model = processor_m, model_m
|
|
|
|
| 164 |
elif model_name == "Qwen2.5-VL-3B-Instruct":
|
| 165 |
+
processor, model = processor_x, model_x
|
| 166 |
+
elif model_name == "Qwen3-VL-4B-Instruct":
|
| 167 |
+
processor, model = processor_q, model_q
|
| 168 |
+
elif model_name == "Qwen3-VL-8B-Instruct":
|
| 169 |
+
processor, model = processor_y, model_y
|
|
|
|
|
|
|
|
|
|
| 170 |
else:
|
| 171 |
yield "Invalid model selected.", "Invalid model selected."
|
| 172 |
return
|
|
|
|
| 175 |
yield "Please upload an image.", "Please upload an image."
|
| 176 |
return
|
| 177 |
|
| 178 |
+
messages = [{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": text}]}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
prompt_full = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 180 |
inputs = processor(
|
| 181 |
+
text=[prompt_full], images=[image], return_tensors="pt", padding=True,
|
| 182 |
+
truncation=True, max_length=MAX_INPUT_TOKEN_LENGTH
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
).to(device)
|
| 184 |
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
| 185 |
generation_kwargs = {**inputs, "streamer": streamer, "max_new_tokens": max_new_tokens}
|
|
|
|
| 200 |
repetition_penalty: float = 1.2):
|
| 201 |
"""
|
| 202 |
Generates responses using the selected model for video input.
|
|
|
|
| 203 |
"""
|
| 204 |
if model_name == "Qwen2.5-VL-7B-Instruct":
|
| 205 |
+
processor, model = processor_m, model_m
|
|
|
|
| 206 |
elif model_name == "Qwen2.5-VL-3B-Instruct":
|
| 207 |
+
processor, model = processor_x, model_x
|
| 208 |
+
elif model_name == "Qwen3-VL-4B-Instruct":
|
| 209 |
+
processor, model = processor_q, model_q
|
| 210 |
+
elif model_name == "Qwen3-VL-8B-Instruct":
|
| 211 |
+
processor, model = processor_y, model_y
|
|
|
|
|
|
|
|
|
|
| 212 |
else:
|
| 213 |
yield "Invalid model selected.", "Invalid model selected."
|
| 214 |
return
|
|
|
|
| 217 |
yield "Please upload a video.", "Please upload a video."
|
| 218 |
return
|
| 219 |
|
| 220 |
+
frames_with_ts = downsample_video(video_path)
|
| 221 |
+
if not frames_with_ts:
|
| 222 |
+
yield "Could not process video.", "Could not process video."
|
| 223 |
+
return
|
| 224 |
+
|
| 225 |
+
messages = [{"role": "user", "content": [{"type": "text", "text": text}]}]
|
| 226 |
+
images_for_processor = []
|
| 227 |
+
for frame, timestamp in frames_with_ts:
|
| 228 |
+
messages[0]["content"].append({"type": "image"})
|
| 229 |
+
images_for_processor.append(frame)
|
| 230 |
+
|
| 231 |
+
prompt_full = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 232 |
+
inputs = processor(
|
| 233 |
+
text=[prompt_full], images=images_for_processor, return_tensors="pt", padding=True,
|
| 234 |
+
truncation=True, max_length=MAX_INPUT_TOKEN_LENGTH
|
|
|
|
|
|
|
| 235 |
).to(device)
|
| 236 |
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
| 237 |
generation_kwargs = {
|
| 238 |
+
**inputs, "streamer": streamer, "max_new_tokens": max_new_tokens,
|
| 239 |
+
"do_sample": True, "temperature": temperature, "top_p": top_p,
|
| 240 |
+
"top_k": top_k, "repetition_penalty": repetition_penalty,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
}
|
| 242 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 243 |
thread.start()
|
| 244 |
buffer = ""
|
| 245 |
for new_text in streamer:
|
| 246 |
buffer += new_text
|
| 247 |
+
buffer = buffer.replace("<|im_end|>", "")
|
| 248 |
time.sleep(0.01)
|
| 249 |
yield buffer, buffer
|
| 250 |
|
| 251 |
+
|
| 252 |
# Define examples for image and video inference
|
| 253 |
image_examples = [
|
| 254 |
["Explain the content in detail.", "images/D.jpg"],
|
|
|
|
| 266 |
]
|
| 267 |
|
| 268 |
css = """
|
| 269 |
+
#main-title h1 {
|
| 270 |
+
font-size: 2.3em !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
}
|
| 272 |
+
#output-title h2 {
|
| 273 |
+
font-size: 2.1em !important;
|
|
|
|
|
|
|
| 274 |
}
|
| 275 |
"""
|
| 276 |
|
| 277 |
# Create the Gradio Interface
|
| 278 |
+
with gr.Blocks(css=css, theme=thistle_theme) as demo:
|
| 279 |
+
gr.Markdown("# **Qwen3-VL-Outpost**", elem_id="main-title")
|
| 280 |
with gr.Row():
|
| 281 |
+
with gr.Column(scale=2):
|
| 282 |
with gr.Tabs():
|
| 283 |
with gr.TabItem("Image Inference"):
|
| 284 |
image_query = gr.Textbox(label="Query Input", placeholder="Enter your query here...")
|
| 285 |
image_upload = gr.Image(type="pil", label="Image", height=290)
|
| 286 |
+
image_submit = gr.Button("Submit", variant="primary")
|
| 287 |
+
gr.Examples(examples=image_examples, inputs=[image_query, image_upload])
|
| 288 |
+
|
|
|
|
|
|
|
| 289 |
with gr.TabItem("Video Inference"):
|
| 290 |
video_query = gr.Textbox(label="Query Input", placeholder="✦︎ Enter your query here...")
|
| 291 |
video_upload = gr.Video(label="Video", height=290)
|
| 292 |
+
video_submit = gr.Button("Submit", variant="primary")
|
| 293 |
+
gr.Examples(examples=video_examples, inputs=[video_query, video_upload])
|
| 294 |
+
|
|
|
|
|
|
|
| 295 |
with gr.Accordion("Advanced options", open=False):
|
| 296 |
max_new_tokens = gr.Slider(label="Max new tokens", minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS)
|
| 297 |
temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=4.0, step=0.1, value=0.6)
|
|
|
|
| 299 |
top_k = gr.Slider(label="Top-k", minimum=1, maximum=1000, step=1, value=50)
|
| 300 |
repetition_penalty = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2)
|
| 301 |
|
| 302 |
+
with gr.Column(scale=3):
|
| 303 |
+
gr.Markdown("## Output", elem_id="output-title")
|
| 304 |
+
output = gr.Textbox(label="Raw Output", interactive=False, lines=14, show_copy_button=True)
|
| 305 |
+
with gr.Accordion("(Result.md)", open=False):
|
| 306 |
+
markdown_output = gr.Markdown()
|
|
|
|
|
|
|
| 307 |
|
| 308 |
model_choice = gr.Radio(
|
| 309 |
+
choices=["Qwen3-VL-4B-Instruct", "Qwen3-VL-8B-Instruct", "Qwen2.5-VL-3B-Instruct", "Qwen2.5-VL-7B-Instruct"],
|
| 310 |
label="Select Model",
|
| 311 |
+
value="Qwen3-VL-4B-Instruct"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
)
|
| 313 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
image_submit.click(
|
| 315 |
fn=generate_image,
|
| 316 |
inputs=[model_choice, image_query, image_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
|
|
|
|
| 323 |
)
|
| 324 |
|
| 325 |
if __name__ == "__main__":
|
| 326 |
+
demo.queue(max_size=50).launch(mcp_server=True, ssr_mode=False, show_error=True)
|