Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -32,11 +32,14 @@ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
|
32 |
|
33 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
34 |
|
35 |
-
# ---
|
|
|
|
|
|
|
36 |
|
37 |
# Load DREX-062225-exp
|
38 |
MODEL_ID_X = "prithivMLmods/DREX-062225-exp"
|
39 |
-
processor_x = AutoProcessor.from_pretrained(MODEL_ID_X, trust_remote_code=True)
|
40 |
model_x = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
41 |
MODEL_ID_X,
|
42 |
trust_remote_code=True,
|
@@ -45,7 +48,7 @@ model_x = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
|
45 |
|
46 |
# Load typhoon-ocr-3b
|
47 |
MODEL_ID_T = "scb10x/typhoon-ocr-3b"
|
48 |
-
processor_t = AutoProcessor.from_pretrained(MODEL_ID_T, trust_remote_code=True)
|
49 |
model_t = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
50 |
MODEL_ID_T,
|
51 |
trust_remote_code=True,
|
@@ -54,7 +57,7 @@ model_t = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
|
54 |
|
55 |
# Load olmOCR-7B-0225-preview
|
56 |
MODEL_ID_O = "allenai/olmOCR-7B-0225-preview"
|
57 |
-
processor_o = AutoProcessor.from_pretrained(MODEL_ID_O, trust_remote_code=True)
|
58 |
model_o = Qwen2VLForConditionalGeneration.from_pretrained(
|
59 |
MODEL_ID_O,
|
60 |
trust_remote_code=True,
|
@@ -64,7 +67,7 @@ model_o = Qwen2VLForConditionalGeneration.from_pretrained(
|
|
64 |
# Load Lumian-VLR-7B-Thinking
|
65 |
MODEL_ID_J = "prithivMLmods/Lumian-VLR-7B-Thinking"
|
66 |
SUBFOLDER = "think-preview"
|
67 |
-
processor_j = AutoProcessor.from_pretrained(MODEL_ID_J, trust_remote_code=True, subfolder=SUBFOLDER)
|
68 |
model_j = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
69 |
MODEL_ID_J,
|
70 |
trust_remote_code=True,
|
@@ -72,7 +75,7 @@ model_j = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
|
72 |
torch_dtype=torch.float16
|
73 |
).to(device).eval()
|
74 |
|
75 |
-
#
|
76 |
MODEL_ID_V4 = 'openbmb/MiniCPM-V-4'
|
77 |
model_v4 = AutoModel.from_pretrained(
|
78 |
MODEL_ID_V4,
|
@@ -80,7 +83,16 @@ model_v4 = AutoModel.from_pretrained(
|
|
80 |
torch_dtype=torch.bfloat16,
|
81 |
attn_implementation='sdpa'
|
82 |
).eval().to(device)
|
83 |
-
tokenizer_v4 = AutoTokenizer.from_pretrained(MODEL_ID_V4, trust_remote_code=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
|
86 |
def downsample_video(video_path):
|
@@ -119,36 +131,25 @@ def generate_image(model_name: str, text: str, image: Image.Image,
|
|
119 |
yield "Please upload an image.", "Please upload an image."
|
120 |
return
|
121 |
|
122 |
-
# Handle
|
123 |
if model_name == "openbmb/MiniCPM-V-4":
|
124 |
msgs = [{'role': 'user', 'content': [image, text]}]
|
125 |
try:
|
126 |
answer = model_v4.chat(
|
127 |
-
image=image.convert('RGB'),
|
128 |
-
|
129 |
-
|
130 |
-
max_new_tokens=max_new_tokens,
|
131 |
-
temperature=temperature,
|
132 |
-
top_p=top_p,
|
133 |
-
repetition_penalty=repetition_penalty,
|
134 |
)
|
135 |
yield answer, answer
|
136 |
except Exception as e:
|
137 |
yield f"Error: {e}", f"Error: {e}"
|
138 |
return
|
139 |
|
140 |
-
#
|
141 |
-
if model_name
|
142 |
-
processor, model = processor_x, model_x
|
143 |
-
elif model_name == "olmOCR-7B-0225-preview":
|
144 |
-
processor, model = processor_o, model_o
|
145 |
-
elif model_name == "Typhoon-OCR":
|
146 |
-
processor, model = processor_t, model_t
|
147 |
-
elif model_name == "Lumian-VLR-7B-Thinking":
|
148 |
-
processor, model = processor_j, model_j
|
149 |
-
else:
|
150 |
yield "Invalid model selected.", "Invalid model selected."
|
151 |
return
|
|
|
152 |
|
153 |
messages = [{"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": text}]}]
|
154 |
prompt_full = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
@@ -185,46 +186,38 @@ def generate_video(model_name: str, text: str, video_path: str,
|
|
185 |
yield "Could not process video.", "Could not process video."
|
186 |
return
|
187 |
|
188 |
-
# Handle
|
189 |
if model_name == "openbmb/MiniCPM-V-4":
|
190 |
images = [frame for frame, ts in frames_with_ts]
|
|
|
191 |
content = [text] + images
|
192 |
msgs = [{'role': 'user', 'content': content}]
|
193 |
try:
|
|
|
194 |
answer = model_v4.chat(
|
195 |
-
image=images[0].convert('RGB'),
|
196 |
-
|
197 |
-
|
198 |
-
max_new_tokens=max_new_tokens,
|
199 |
-
temperature=temperature,
|
200 |
-
top_p=top_p,
|
201 |
-
repetition_penalty=repetition_penalty,
|
202 |
)
|
203 |
yield answer, answer
|
204 |
except Exception as e:
|
205 |
yield f"Error: {e}", f"Error: {e}"
|
206 |
return
|
207 |
|
208 |
-
#
|
209 |
-
if model_name
|
210 |
-
processor, model = processor_x, model_x
|
211 |
-
elif model_name == "olmOCR-7B-0225-preview":
|
212 |
-
processor, model = processor_o, model_o
|
213 |
-
elif model_name == "Typhoon-OCR":
|
214 |
-
processor, model = processor_t, model_t
|
215 |
-
elif model_name == "Lumian-VLR-7B-Thinking":
|
216 |
-
processor, model = processor_j, model_j
|
217 |
-
else:
|
218 |
yield "Invalid model selected.", "Invalid model selected."
|
219 |
return
|
|
|
220 |
|
221 |
# Prepare messages for Qwen-style models
|
222 |
messages = [{"role": "user", "content": [{"type": "text", "text": text}]}]
|
|
|
223 |
for frame, timestamp in frames_with_ts:
|
224 |
messages[0]["content"].append({"type": "image", "image": frame})
|
|
|
225 |
|
226 |
prompt_full = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
227 |
-
images_for_processor = [frame for frame, ts in frames_with_ts]
|
228 |
inputs = processor(
|
229 |
text=[prompt_full], images=images_for_processor, return_tensors="pt", padding=True,
|
230 |
truncation=True, max_length=MAX_INPUT_TOKEN_LENGTH
|
@@ -260,7 +253,6 @@ video_examples = [
|
|
260 |
["Explain the ad in detail.", "videos/1.mp4"]
|
261 |
]
|
262 |
|
263 |
-
# Added CSS to style the output area as a "Canvas"
|
264 |
css = """
|
265 |
.submit-btn { background-color: #2980b9 !important; color: white !important; }
|
266 |
.submit-btn:hover { background-color: #3498db !important; }
|
@@ -298,14 +290,16 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
298 |
with gr.Accordion("(Result.md)", open=False):
|
299 |
markdown_output = gr.Markdown(label="(Result.Md)")
|
300 |
model_choice = gr.Radio(
|
301 |
-
choices=[
|
302 |
label="Select Model",
|
303 |
value="openbmb/MiniCPM-V-4"
|
304 |
)
|
305 |
gr.Markdown("**Model Info 💻** | [Report Bug](https://huggingface.co/spaces/prithivMLmods/Multimodal-VLM-Thinking/discussions)")
|
306 |
-
gr.Markdown("> MiniCPM-V 4.0 is
|
307 |
-
gr.Markdown(">
|
308 |
-
gr.Markdown(">
|
|
|
|
|
309 |
gr.Markdown("> ⚠️ Note: Video inference performance can vary significantly between models.")
|
310 |
|
311 |
image_submit.click(
|
@@ -320,4 +314,4 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
320 |
)
|
321 |
|
322 |
if __name__ == "__main__":
|
323 |
-
demo.queue(max_size=50).launch(share=True,
|
|
|
32 |
|
33 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
34 |
|
35 |
+
# --- Model Loading ---
|
36 |
+
|
37 |
+
# To address the warnings, we add `use_fast=False` to ensure we use the
|
38 |
+
# processor version the model was originally saved with.
|
39 |
|
40 |
# Load DREX-062225-exp
|
41 |
MODEL_ID_X = "prithivMLmods/DREX-062225-exp"
|
42 |
+
processor_x = AutoProcessor.from_pretrained(MODEL_ID_X, trust_remote_code=True, use_fast=False)
|
43 |
model_x = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
44 |
MODEL_ID_X,
|
45 |
trust_remote_code=True,
|
|
|
48 |
|
49 |
# Load typhoon-ocr-3b
|
50 |
MODEL_ID_T = "scb10x/typhoon-ocr-3b"
|
51 |
+
processor_t = AutoProcessor.from_pretrained(MODEL_ID_T, trust_remote_code=True, use_fast=False)
|
52 |
model_t = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
53 |
MODEL_ID_T,
|
54 |
trust_remote_code=True,
|
|
|
57 |
|
58 |
# Load olmOCR-7B-0225-preview
|
59 |
MODEL_ID_O = "allenai/olmOCR-7B-0225-preview"
|
60 |
+
processor_o = AutoProcessor.from_pretrained(MODEL_ID_O, trust_remote_code=True, use_fast=False)
|
61 |
model_o = Qwen2VLForConditionalGeneration.from_pretrained(
|
62 |
MODEL_ID_O,
|
63 |
trust_remote_code=True,
|
|
|
67 |
# Load Lumian-VLR-7B-Thinking
|
68 |
MODEL_ID_J = "prithivMLmods/Lumian-VLR-7B-Thinking"
|
69 |
SUBFOLDER = "think-preview"
|
70 |
+
processor_j = AutoProcessor.from_pretrained(MODEL_ID_J, trust_remote_code=True, subfolder=SUBFOLDER, use_fast=False)
|
71 |
model_j = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
72 |
MODEL_ID_J,
|
73 |
trust_remote_code=True,
|
|
|
75 |
torch_dtype=torch.float16
|
76 |
).to(device).eval()
|
77 |
|
78 |
+
# Load openbmb/MiniCPM-V-4
|
79 |
MODEL_ID_V4 = 'openbmb/MiniCPM-V-4'
|
80 |
model_v4 = AutoModel.from_pretrained(
|
81 |
MODEL_ID_V4,
|
|
|
83 |
torch_dtype=torch.bfloat16,
|
84 |
attn_implementation='sdpa'
|
85 |
).eval().to(device)
|
86 |
+
tokenizer_v4 = AutoTokenizer.from_pretrained(MODEL_ID_V4, trust_remote_code=True, use_fast=False)
|
87 |
+
|
88 |
+
# --- Refactored Model Dictionary ---
|
89 |
+
# This simplifies model selection in the generation functions.
|
90 |
+
MODELS = {
|
91 |
+
"DREX-062225-7B-exp": (processor_x, model_x),
|
92 |
+
"Typhoon-OCR-3B": (processor_t, model_t),
|
93 |
+
"olmOCR-7B-0225-preview": (processor_o, model_o),
|
94 |
+
"Lumian-VLR-7B-Thinking": (processor_j, model_j),
|
95 |
+
}
|
96 |
|
97 |
|
98 |
def downsample_video(video_path):
|
|
|
131 |
yield "Please upload an image.", "Please upload an image."
|
132 |
return
|
133 |
|
134 |
+
# Handle MiniCPM-V-4 separately due to its different API
|
135 |
if model_name == "openbmb/MiniCPM-V-4":
|
136 |
msgs = [{'role': 'user', 'content': [image, text]}]
|
137 |
try:
|
138 |
answer = model_v4.chat(
|
139 |
+
image=image.convert('RGB'), msgs=msgs, tokenizer=tokenizer_v4,
|
140 |
+
max_new_tokens=max_new_tokens, temperature=temperature,
|
141 |
+
top_p=top_p, repetition_penalty=repetition_penalty,
|
|
|
|
|
|
|
|
|
142 |
)
|
143 |
yield answer, answer
|
144 |
except Exception as e:
|
145 |
yield f"Error: {e}", f"Error: {e}"
|
146 |
return
|
147 |
|
148 |
+
# Use the dictionary for other models
|
149 |
+
if model_name not in MODELS:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
yield "Invalid model selected.", "Invalid model selected."
|
151 |
return
|
152 |
+
processor, model = MODELS[model_name]
|
153 |
|
154 |
messages = [{"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": text}]}]
|
155 |
prompt_full = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
|
|
186 |
yield "Could not process video.", "Could not process video."
|
187 |
return
|
188 |
|
189 |
+
# Handle MiniCPM-V-4 separately
|
190 |
if model_name == "openbmb/MiniCPM-V-4":
|
191 |
images = [frame for frame, ts in frames_with_ts]
|
192 |
+
# For video, the prompt includes the text and then all the image frames
|
193 |
content = [text] + images
|
194 |
msgs = [{'role': 'user', 'content': content}]
|
195 |
try:
|
196 |
+
# The .chat API still takes a single image argument, typically the first frame
|
197 |
answer = model_v4.chat(
|
198 |
+
image=images[0].convert('RGB'), msgs=msgs, tokenizer=tokenizer_v4,
|
199 |
+
max_new_tokens=max_new_tokens, temperature=temperature,
|
200 |
+
top_p=top_p, repetition_penalty=repetition_penalty,
|
|
|
|
|
|
|
|
|
201 |
)
|
202 |
yield answer, answer
|
203 |
except Exception as e:
|
204 |
yield f"Error: {e}", f"Error: {e}"
|
205 |
return
|
206 |
|
207 |
+
# Use the dictionary for other models
|
208 |
+
if model_name not in MODELS:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
yield "Invalid model selected.", "Invalid model selected."
|
210 |
return
|
211 |
+
processor, model = MODELS[model_name]
|
212 |
|
213 |
# Prepare messages for Qwen-style models
|
214 |
messages = [{"role": "user", "content": [{"type": "text", "text": text}]}]
|
215 |
+
images_for_processor = []
|
216 |
for frame, timestamp in frames_with_ts:
|
217 |
messages[0]["content"].append({"type": "image", "image": frame})
|
218 |
+
images_for_processor.append(frame)
|
219 |
|
220 |
prompt_full = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
|
|
221 |
inputs = processor(
|
222 |
text=[prompt_full], images=images_for_processor, return_tensors="pt", padding=True,
|
223 |
truncation=True, max_length=MAX_INPUT_TOKEN_LENGTH
|
|
|
253 |
["Explain the ad in detail.", "videos/1.mp4"]
|
254 |
]
|
255 |
|
|
|
256 |
css = """
|
257 |
.submit-btn { background-color: #2980b9 !important; color: white !important; }
|
258 |
.submit-btn:hover { background-color: #3498db !important; }
|
|
|
290 |
with gr.Accordion("(Result.md)", open=False):
|
291 |
markdown_output = gr.Markdown(label="(Result.Md)")
|
292 |
model_choice = gr.Radio(
|
293 |
+
choices=["openbmb/MiniCPM-V-4", "Lumian-VLR-7B-Thinking", "Typhoon-OCR-3B", "DREX-062225-7B-exp", "olmOCR-7B-0225-preview"],
|
294 |
label="Select Model",
|
295 |
value="openbmb/MiniCPM-V-4"
|
296 |
)
|
297 |
gr.Markdown("**Model Info 💻** | [Report Bug](https://huggingface.co/spaces/prithivMLmods/Multimodal-VLM-Thinking/discussions)")
|
298 |
+
gr.Markdown("> **MiniCPM-V 4.0** is an efficient open-source multimodal model with strong performance in single/multi-image and video understanding, inheriting and improving upon the MiniCPM-V series.")
|
299 |
+
gr.Markdown("> **Lumian-VLR-7B-Thinking** is a high-fidelity vision-language reasoning model for fine-grained multimodal understanding, video reasoning, and document comprehension.")
|
300 |
+
gr.Markdown("> **olmOCR-7B-0225-preview** is a 7B parameter model designed for robust text extraction in complex OCR tasks.")
|
301 |
+
gr.Markdown("> **Typhoon-OCR-3B** is a 3B parameter OCR model optimized for efficient and accurate character recognition.")
|
302 |
+
gr.Markdown("> **DREX-062225-exp** is an experimental model emphasizing strong document reading, extraction, and vision-language understanding.")
|
303 |
gr.Markdown("> ⚠️ Note: Video inference performance can vary significantly between models.")
|
304 |
|
305 |
image_submit.click(
|
|
|
314 |
)
|
315 |
|
316 |
if __name__ == "__main__":
|
317 |
+
demo.queue(max_size=50).launch(share=True, show_error=True)
|