Spaces:
Running on Zero
Running on Zero
File size: 6,887 Bytes
24f91c1 df4e947 91b63e4 df4e947 91b63e4 40a46cd 24f91c1 92113bf 24f91c1 92113bf 4760ee8 24f91c1 77a7547 24f91c1 77a7547 24f91c1 77a7547 24f91c1 77a7547 24f91c1 ee2dfd6 4760ee8 ee2dfd6 24f91c1 77a7547 24f91c1 92113bf df4e947 f85fdb1 40a46cd 4760ee8 24f91c1 77a7547 4760ee8 77a7547 4760ee8 77a7547 4760ee8 ee2dfd6 77a7547 4760ee8 24f91c1 91b63e4 4760ee8 24f91c1 b9569b2 e0e030d 77a7547 b9569b2 77a7547 24f91c1 4760ee8 77a7547 24f91c1 d91fdb2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | import gradio as gr
import torch
from transformers import AutoProcessor, AutoModelForImageTextToText
import spaces
from molmo_utils import process_vision_info
from typing import Iterable
from gradio.themes import Soft
from gradio.themes.utils import colors, fonts, sizes
colors.orange_red = colors.Color(
name="orange_red",
c50="#FFF0E5",
c100="#FFE0CC",
c200="#FFC299",
c300="#FFA366",
c400="#FF8533",
c500="#FF4500",
c600="#E63E00",
c700="#CC3700",
c800="#B33000",
c900="#992900",
c950="#802200",
)
class OrangeRedTheme(Soft):
def __init__(
self,
*,
primary_hue: colors.Color | str = colors.gray,
secondary_hue: colors.Color | str = colors.orange_red,
neutral_hue: colors.Color | str = colors.slate,
text_size: sizes.Size | str = sizes.text_lg,
font: fonts.Font | str | Iterable[fonts.Font | str] = (
fonts.GoogleFont("Outfit"), "Arial", "sans-serif",
),
font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace",
),
):
super().__init__(
primary_hue=primary_hue,
secondary_hue=secondary_hue,
neutral_hue=neutral_hue,
text_size=text_size,
font=font,
font_mono=font_mono,
)
super().set(
background_fill_primary="*primary_50",
background_fill_primary_dark="*primary_900",
body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)",
body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)",
button_primary_text_color="white",
button_primary_text_color_hover="white",
button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
button_primary_background_fill_dark="linear-gradient(90deg, *secondary_600, *secondary_700)",
button_primary_background_fill_hover_dark="linear-gradient(90deg, *secondary_500, *secondary_600)",
button_secondary_text_color="black",
button_secondary_text_color_hover="white",
button_secondary_background_fill="linear-gradient(90deg, *primary_300, *primary_300)",
button_secondary_background_fill_hover="linear-gradient(90deg, *primary_400, *primary_400)",
button_secondary_background_fill_dark="linear-gradient(90deg, *primary_500, *primary_600)",
button_secondary_background_fill_hover_dark="linear-gradient(90deg, *primary_500, *primary_500)",
slider_color="*secondary_500",
slider_color_dark="*secondary_600",
block_title_text_weight="600",
block_border_width="3px",
block_shadow="*shadow_drop_lg",
button_primary_shadow="*shadow_drop_lg",
button_large_padding="11px",
color_accent_soft="*primary_100",
block_label_background_fill="*primary_200",
)
orange_red_theme = OrangeRedTheme()
css = """
#main-title h1 {font-size: 2.4em !important;}
"""
MODEL_ID = "allenai/SAGE-MM-Qwen3-VL-4B-SFT_RL"
print(f"Loading {MODEL_ID}...")
processor = AutoProcessor.from_pretrained(
MODEL_ID,
attn_implementation="kernels-community/flash-attn2",
trust_remote_code=True,
dtype="auto",
device_map="auto"
)
model = AutoModelForImageTextToText.from_pretrained(
MODEL_ID,
trust_remote_code=True,
dtype="auto",
device_map="auto"
)
print("Model loaded successfully.")
@spaces.GPU(duration=180)
def process_video(user_text, video_path, max_new_tokens):
if not video_path:
return "Please upload a video."
if not user_text.strip():
user_text = "Describe this video in detail."
messages = [
{
"role": "user",
"content": [
dict(type="text", text=user_text),
dict(type="video", video=video_path),
],
}
]
try:
_, videos, video_kwargs = process_vision_info(messages)
videos, video_metadatas = zip(*videos)
videos, video_metadatas = list(videos), list(video_metadatas)
except Exception as e:
return f"Error processing video frames: {e}"
# Apply chat template
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
# Prepare inputs
inputs = processor(
videos=videos,
video_metadata=video_metadatas,
text=text,
padding=True,
return_tensors="pt",
**video_kwargs,
)
inputs = {k: v.to(model.device) for k, v in inputs.items()}
# Generate
with torch.inference_mode():
generated_ids = model.generate(
**inputs,
max_new_tokens=max_new_tokens
)
generated_tokens = generated_ids[0, inputs['input_ids'].size(1):]
generated_text = processor.tokenizer.decode(generated_tokens, skip_special_tokens=True)
return generated_text
with gr.Blocks() as demo:
gr.Markdown("# **SAGE-MM-Video-Reasoning**", elem_id="main-title")
gr.Markdown("Upload a video to get a detailed explanation or ask specific questions using [SAGE-MM-Qwen3-VL](https://huggingface.co/allenai/SAGE-MM-Qwen3-VL-4B-SFT_RL).")
with gr.Row():
with gr.Column():
vid_input = gr.Video(label="Input Video", format="mp4", height=350)
vid_prompt = gr.Textbox(
label="Prompt",
value="Describe this video in detail.",
placeholder="Type your question here..."
)
with gr.Accordion("Advanced Settings", open=False):
max_tokens_slider = gr.Slider(
minimum=128,
maximum=4096,
value=1024,
step=128,
label="Max New Tokens",
info="Controls the length of the generated text."
)
vid_btn = gr.Button("Analyze Video", variant="primary")
with gr.Column():
vid_text_out = gr.Textbox(label="Model Response", interactive=True, lines=18)
gr.Examples(
examples=[
["example-videos/1.mp4"],
["example-videos/2.mp4"],
["example-videos/3.mp4"],
["example-videos/4.mp4"],
["example-videos/5.mp4"],
],
inputs=[vid_input],
label="Video Examples"
)
vid_btn.click(
fn=process_video,
inputs=[vid_prompt, vid_input, max_tokens_slider],
outputs=[vid_text_out]
)
if __name__ == "__main__":
demo.launch(theme=orange_red_theme, css=css, mcp_server=True, ssr_mode=False) |