Spaces:
Running
on
Zero
Running
on
Zero
update app (#15)
Browse files- update app (69d5c6df8b9fd0236d1a25e519150d82e46685fb)
app.py
CHANGED
|
@@ -28,6 +28,8 @@ from transformers import (
|
|
| 28 |
AutoProcessor,
|
| 29 |
TextIteratorStreamer,
|
| 30 |
AutoTokenizer,
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
from transformers.image_utils import load_image
|
|
@@ -150,10 +152,13 @@ model_o = AutoModelForVision2Seq.from_pretrained(
|
|
| 150 |
MODEL_ID_O, trust_remote_code=True, torch_dtype=torch.float16, _attn_implementation="flash_attention_2"
|
| 151 |
).to(device).eval()
|
| 152 |
|
| 153 |
-
# ---
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
|
| 159 |
# --- PDF Generation and Preview Utility Function ---
|
|
@@ -246,6 +251,7 @@ def process_document_stream(
|
|
| 246 |
yield "Please enter a prompt.", ""
|
| 247 |
return
|
| 248 |
|
|
|
|
| 249 |
if model_name == "Moondream2(vision)":
|
| 250 |
image_embeds = moondream.encode_image(image)
|
| 251 |
answer = moondream.answer_question(
|
|
@@ -255,61 +261,38 @@ def process_document_stream(
|
|
| 255 |
)
|
| 256 |
yield answer, answer
|
| 257 |
return
|
| 258 |
-
|
| 259 |
-
if model_name == "Florence-2-Flux-Large(caption)":
|
| 260 |
-
processor, model = processor_fl, model_fl
|
| 261 |
-
task_prompt = "<DESCRIPTION>"
|
| 262 |
-
if any(keyword in prompt_input.lower() for keyword in ["ocr", "read", "text", "extract"]):
|
| 263 |
-
task_prompt = "<OCR>"
|
| 264 |
-
|
| 265 |
-
prompt = task_prompt + prompt_input
|
| 266 |
-
if image.mode != "RGB":
|
| 267 |
-
image = image.convert("RGB")
|
| 268 |
-
|
| 269 |
-
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
|
| 270 |
-
|
| 271 |
-
generated_ids = model.generate(
|
| 272 |
-
input_ids=inputs["input_ids"],
|
| 273 |
-
pixel_values=inputs["pixel_values"],
|
| 274 |
-
max_new_tokens=max_new_tokens,
|
| 275 |
-
do_sample=True,
|
| 276 |
-
temperature=temperature,
|
| 277 |
-
top_p=top_p,
|
| 278 |
-
top_k=top_k,
|
| 279 |
-
repetition_penalty=repetition_penalty,
|
| 280 |
-
)
|
| 281 |
-
|
| 282 |
-
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 283 |
-
parsed_answer = processor.post_process_generation(generated_text, task=task_prompt, image_size=(image.width, image.height))
|
| 284 |
-
final_answer = parsed_answer.get(task_prompt, "Error: Could not parse model output.")
|
| 285 |
-
|
| 286 |
-
if isinstance(final_answer, dict) and 'labels' in final_answer:
|
| 287 |
-
final_answer = " ".join(final_answer['labels'])
|
| 288 |
-
|
| 289 |
-
yield final_answer, final_answer
|
| 290 |
-
return
|
| 291 |
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
elif model_name == "Qwen2.5-VL-3B-Abliterated-Caption-it(caption)": processor, model = processor_n, model_n
|
| 302 |
-
elif model_name == "LMM-R1-MGT-PerceReason(reason)": processor, model = processor_f, model_f
|
| 303 |
-
elif model_name == "TBAC-VLR1-3B(open-r1)": processor, model = processor_g, model_g
|
| 304 |
-
elif model_name == "OCRFlux-3B(ocr)": processor, model = processor_v, model_v
|
| 305 |
-
elif model_name == "SmolVLM-500M-Instruct(smol)": processor, model = processor_o, model_o
|
| 306 |
else:
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
| 314 |
|
| 315 |
generation_kwargs = {
|
|
@@ -359,10 +342,9 @@ def create_gradio_interface():
|
|
| 359 |
# Left Column (Inputs)
|
| 360 |
with gr.Column(scale=1):
|
| 361 |
model_choice = gr.Dropdown(
|
| 362 |
-
choices=["LFM2-VL-450M(fast)", "LFM2-VL-1.6B(fast)", "SmolVLM-Instruct-250M(smol)", "Moondream2(vision)",
|
| 363 |
-
"Florence-2-Flux-Large(caption)", "ShotVL-3B(cinematic)", "Megalodon-OCR-Sync-0713(ocr)",
|
| 364 |
"VLAA-Thinker-Qwen2VL-2B(reason)", "MonkeyOCR-pro-1.2B(ocr)", "Qwen2.5-VL-3B-Abliterated-Caption-it(caption)", "Nanonets-OCR-s(ocr)",
|
| 365 |
-
"LMM-R1-MGT-PerceReason(reason)", "OCRFlux-3B(ocr)", "TBAC-VLR1-3B(open-r1)", "SmolVLM-500M-Instruct(smol)"],
|
| 366 |
label="Select Model", value= "LFM2-VL-450M(fast)"
|
| 367 |
)
|
| 368 |
prompt_input = gr.Textbox(label="Query Input", placeholder="✦︎ Enter your query", value="Describe the image!")
|
|
|
|
| 28 |
AutoProcessor,
|
| 29 |
TextIteratorStreamer,
|
| 30 |
AutoTokenizer,
|
| 31 |
+
LlavaOnevisionForConditionalGeneration,
|
| 32 |
+
LlavaOnevisionProcessor,
|
| 33 |
)
|
| 34 |
|
| 35 |
from transformers.image_utils import load_image
|
|
|
|
| 152 |
MODEL_ID_O, trust_remote_code=True, torch_dtype=torch.float16, _attn_implementation="flash_attention_2"
|
| 153 |
).to(device).eval()
|
| 154 |
|
| 155 |
+
# --- New Model: llava-onevision ---
|
| 156 |
+
MODEL_ID_LO = "llava-hf/llava-onevision-qwen2-0.5b-ov-hf"
|
| 157 |
+
processor_lo = LlavaOnevisionProcessor.from_pretrained(MODEL_ID_LO)
|
| 158 |
+
model_lo = LlavaOnevisionForConditionalGeneration.from_pretrained(
|
| 159 |
+
MODEL_ID_LO,
|
| 160 |
+
torch_dtype=torch.float16
|
| 161 |
+
).to(device).eval()
|
| 162 |
|
| 163 |
|
| 164 |
# --- PDF Generation and Preview Utility Function ---
|
|
|
|
| 251 |
yield "Please enter a prompt.", ""
|
| 252 |
return
|
| 253 |
|
| 254 |
+
# --- Special Handling for Moondream2 ---
|
| 255 |
if model_name == "Moondream2(vision)":
|
| 256 |
image_embeds = moondream.encode_image(image)
|
| 257 |
answer = moondream.answer_question(
|
|
|
|
| 261 |
)
|
| 262 |
yield answer, answer
|
| 263 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
+
processor = None
|
| 266 |
+
model = None
|
| 267 |
+
|
| 268 |
+
# --- Special Handling for Llava-OneVision ---
|
| 269 |
+
if model_name == "llava-onevision-qwen2-0.5b-ov-hf(mini)":
|
| 270 |
+
processor, model = processor_lo, model_lo
|
| 271 |
+
prompt = f"<|im_start|>user <image>\n{prompt_input}<|im_end|><|im_start|>assistant"
|
| 272 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch.float16)
|
| 273 |
+
# --- Generic Handling for all other models ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
else:
|
| 275 |
+
if model_name == "LFM2-VL-450M(fast)": processor, model = processor_m, model_m
|
| 276 |
+
elif model_name == "LFM2-VL-1.6B(fast)": processor, model = processor_t, model_t
|
| 277 |
+
elif model_name == "ShotVL-3B(cinematic)": processor, model = processor_z, model_z
|
| 278 |
+
elif model_name == "SmolVLM-Instruct-250M(smol)": processor, model = processor_c, model_c
|
| 279 |
+
elif model_name == "MonkeyOCR-pro-1.2B(ocr)": processor, model = processor_g, model_g
|
| 280 |
+
elif model_name == "VLAA-Thinker-Qwen2VL-2B(reason)": processor, model = processor_i, model_i
|
| 281 |
+
elif model_name == "Nanonets-OCR-s(ocr)": processor, model = processor_a, model_a
|
| 282 |
+
elif model_name == "Megalodon-OCR-Sync-0713(ocr)": processor, model = processor_x, model_x
|
| 283 |
+
elif model_name == "Qwen2.5-VL-3B-Abliterated-Caption-it(caption)": processor, model = processor_n, model_n
|
| 284 |
+
elif model_name == "LMM-R1-MGT-PerceReason(reason)": processor, model = processor_f, model_f
|
| 285 |
+
elif model_name == "TBAC-VLR1-3B(open-r1)": processor, model = processor_g, model_g
|
| 286 |
+
elif model_name == "OCRFlux-3B(ocr)": processor, model = processor_v, model_v
|
| 287 |
+
elif model_name == "SmolVLM-500M-Instruct(smol)": processor, model = processor_o, model_o
|
| 288 |
+
else:
|
| 289 |
+
yield "Invalid model selected.", ""
|
| 290 |
+
return
|
| 291 |
+
|
| 292 |
+
messages = [{"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": prompt_input}]}]
|
| 293 |
+
prompt_full = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 294 |
+
inputs = processor(text=[prompt_full], images=[image], return_tensors="pt", padding=True, truncation=True, max_length=MAX_INPUT_TOKEN_LENGTH).to(device)
|
| 295 |
+
|
| 296 |
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
| 297 |
|
| 298 |
generation_kwargs = {
|
|
|
|
| 342 |
# Left Column (Inputs)
|
| 343 |
with gr.Column(scale=1):
|
| 344 |
model_choice = gr.Dropdown(
|
| 345 |
+
choices=["LFM2-VL-450M(fast)", "LFM2-VL-1.6B(fast)", "SmolVLM-Instruct-250M(smol)", "Moondream2(vision)", "ShotVL-3B(cinematic)", "Megalodon-OCR-Sync-0713(ocr)",
|
|
|
|
| 346 |
"VLAA-Thinker-Qwen2VL-2B(reason)", "MonkeyOCR-pro-1.2B(ocr)", "Qwen2.5-VL-3B-Abliterated-Caption-it(caption)", "Nanonets-OCR-s(ocr)",
|
| 347 |
+
"LMM-R1-MGT-PerceReason(reason)", "OCRFlux-3B(ocr)", "TBAC-VLR1-3B(open-r1)", "SmolVLM-500M-Instruct(smol)", "llava-onevision-qwen2-0.5b-ov-hf(mini)"],
|
| 348 |
label="Select Model", value= "LFM2-VL-450M(fast)"
|
| 349 |
)
|
| 350 |
prompt_input = gr.Textbox(label="Query Input", placeholder="✦︎ Enter your query", value="Describe the image!")
|