Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -17,6 +17,10 @@ import requests
|
|
17 |
import torch
|
18 |
from PIL import Image
|
19 |
import fitz
|
|
|
|
|
|
|
|
|
20 |
|
21 |
from transformers import (
|
22 |
Qwen2_5_VLForConditionalGeneration,
|
@@ -32,7 +36,7 @@ from transformers import (
|
|
32 |
LlavaOnevisionProcessor,
|
33 |
)
|
34 |
|
35 |
-
from transformers.image_utils import load_image
|
36 |
|
37 |
from reportlab.lib.pagesizes import A4
|
38 |
from reportlab.lib.styles import getSampleStyleSheet
|
@@ -54,6 +58,75 @@ if torch.cuda.is_available():
|
|
54 |
|
55 |
print("Using device:", device)
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
# --- Model Loading ---
|
58 |
MODEL_ID_M = "LiquidAI/LFM2-VL-450M"
|
59 |
processor_m = AutoProcessor.from_pretrained(MODEL_ID_M, trust_remote_code=True)
|
@@ -100,12 +173,6 @@ model_x = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
|
100 |
MODEL_ID_X, trust_remote_code=True, torch_dtype=torch.float16
|
101 |
).to(device).eval()
|
102 |
|
103 |
-
MODEL_ID_Z = "Vchitect/ShotVL-3B"
|
104 |
-
processor_z = AutoProcessor.from_pretrained(MODEL_ID_Z, trust_remote_code=True)
|
105 |
-
model_z = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
106 |
-
MODEL_ID_Z, trust_remote_code=True, torch_dtype=torch.float16
|
107 |
-
).to(device).eval()
|
108 |
-
|
109 |
# --- Moondream2 Model Loading ---
|
110 |
MODEL_ID_MD = "vikhyatk/moondream2"
|
111 |
REVISION_MD = "2025-06-21"
|
@@ -160,6 +227,15 @@ model_lo = LlavaOnevisionForConditionalGeneration.from_pretrained(
|
|
160 |
torch_dtype=torch.float16
|
161 |
).to(device).eval()
|
162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
# --- PDF Generation and Preview Utility Function ---
|
165 |
def generate_and_preview_pdf(image: Image.Image, text_content: str, font_size: int, line_spacing: float, alignment: str, image_size: str):
|
@@ -261,6 +337,23 @@ def process_document_stream(
|
|
261 |
)
|
262 |
yield answer, answer
|
263 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
|
265 |
processor = None
|
266 |
model = None
|
@@ -274,7 +367,6 @@ def process_document_stream(
|
|
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
|
@@ -343,7 +435,7 @@ def create_gradio_interface():
|
|
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)",
|
346 |
-
"
|
347 |
"VLAA-Thinker-Qwen2VL-2B(reason)", "MonkeyOCR-pro-1.2B(ocr)",
|
348 |
"Qwen2.5-VL-3B-Abliterated-Caption-it(caption)", "Nanonets-OCR-s(ocr)",
|
349 |
"LMM-R1-MGT-PerceReason(reason)", "OCRFlux-3B(ocr)", "TBAC-VLR1-3B(open-r1)",
|
|
|
17 |
import torch
|
18 |
from PIL import Image
|
19 |
import fitz
|
20 |
+
import numpy as np
|
21 |
+
import torchvision.transforms as T
|
22 |
+
from torchvision.transforms.functional import InterpolationMode
|
23 |
+
|
24 |
|
25 |
from transformers import (
|
26 |
Qwen2_5_VLForConditionalGeneration,
|
|
|
36 |
LlavaOnevisionProcessor,
|
37 |
)
|
38 |
|
39 |
+
from transformers.image_utils import load_image as hf_load_image
|
40 |
|
41 |
from reportlab.lib.pagesizes import A4
|
42 |
from reportlab.lib.styles import getSampleStyleSheet
|
|
|
58 |
|
59 |
print("Using device:", device)
|
60 |
|
61 |
+
# --- InternVL3_5-2B-MPO Preprocessing Functions ---
|
62 |
+
IMAGENET_MEAN = (0.485, 0.456, 0.406)
|
63 |
+
IMAGENET_STD = (0.229, 0.224, 0.225)
|
64 |
+
|
65 |
+
def build_transform(input_size):
|
66 |
+
MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
|
67 |
+
transform = T.Compose([
|
68 |
+
T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
|
69 |
+
T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
|
70 |
+
T.ToTensor(),
|
71 |
+
T.Normalize(mean=MEAN, std=STD)
|
72 |
+
])
|
73 |
+
return transform
|
74 |
+
|
75 |
+
def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
|
76 |
+
best_ratio_diff = float('inf')
|
77 |
+
best_ratio = (1, 1)
|
78 |
+
area = width * height
|
79 |
+
for ratio in target_ratios:
|
80 |
+
target_aspect_ratio = ratio[0] / ratio[1]
|
81 |
+
ratio_diff = abs(aspect_ratio - target_aspect_ratio)
|
82 |
+
if ratio_diff < best_ratio_diff:
|
83 |
+
best_ratio_diff = ratio_diff
|
84 |
+
best_ratio = ratio
|
85 |
+
elif ratio_diff == best_ratio_diff:
|
86 |
+
if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
|
87 |
+
best_ratio = ratio
|
88 |
+
return best_ratio
|
89 |
+
|
90 |
+
def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
|
91 |
+
orig_width, orig_height = image.size
|
92 |
+
aspect_ratio = orig_width / orig_height
|
93 |
+
|
94 |
+
target_ratios = set(
|
95 |
+
(i, j) for n in range(min_num, max_num + 1) for i in range(1, n + 1) for j in range(1, n + 1) if
|
96 |
+
i * j <= max_num and i * j >= min_num)
|
97 |
+
target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
|
98 |
+
|
99 |
+
target_aspect_ratio = find_closest_aspect_ratio(
|
100 |
+
aspect_ratio, target_ratios, orig_width, orig_height, image_size)
|
101 |
+
|
102 |
+
target_width = image_size * target_aspect_ratio[0]
|
103 |
+
target_height = image_size * target_aspect_ratio[1]
|
104 |
+
blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
|
105 |
+
|
106 |
+
resized_img = image.resize((target_width, target_height))
|
107 |
+
processed_images = []
|
108 |
+
for i in range(blocks):
|
109 |
+
box = (
|
110 |
+
(i % (target_width // image_size)) * image_size,
|
111 |
+
(i // (target_width // image_size)) * image_size,
|
112 |
+
((i % (target_width // image_size)) + 1) * image_size,
|
113 |
+
((i // (target_width // image_size)) + 1) * image_size
|
114 |
+
)
|
115 |
+
split_img = resized_img.crop(box)
|
116 |
+
processed_images.append(split_img)
|
117 |
+
assert len(processed_images) == blocks
|
118 |
+
if use_thumbnail and len(processed_images) != 1:
|
119 |
+
thumbnail_img = image.resize((image_size, image_size))
|
120 |
+
processed_images.append(thumbnail_img)
|
121 |
+
return processed_images
|
122 |
+
|
123 |
+
def load_image_internvl(image, input_size=448, max_num=12):
|
124 |
+
transform = build_transform(input_size=input_size)
|
125 |
+
images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
|
126 |
+
pixel_values = [transform(img) for img in images]
|
127 |
+
pixel_values = torch.stack(pixel_values)
|
128 |
+
return pixel_values
|
129 |
+
|
130 |
# --- Model Loading ---
|
131 |
MODEL_ID_M = "LiquidAI/LFM2-VL-450M"
|
132 |
processor_m = AutoProcessor.from_pretrained(MODEL_ID_M, trust_remote_code=True)
|
|
|
173 |
MODEL_ID_X, trust_remote_code=True, torch_dtype=torch.float16
|
174 |
).to(device).eval()
|
175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
# --- Moondream2 Model Loading ---
|
177 |
MODEL_ID_MD = "vikhyatk/moondream2"
|
178 |
REVISION_MD = "2025-06-21"
|
|
|
227 |
torch_dtype=torch.float16
|
228 |
).to(device).eval()
|
229 |
|
230 |
+
# --- New Model: OpenGVLab/InternVL3_5-2B-MPO ---
|
231 |
+
MODEL_ID_IV = 'OpenGVLab/InternVL3_5-2B-MPO'
|
232 |
+
model_iv = AutoModel.from_pretrained(
|
233 |
+
MODEL_ID_IV,
|
234 |
+
torch_dtype=torch.bfloat16,
|
235 |
+
trust_remote_code=True,
|
236 |
+
device_map="auto").eval()
|
237 |
+
tokenizer_iv = AutoTokenizer.from_pretrained(MODEL_ID_IV, trust_remote_code=True, use_fast=False)
|
238 |
+
|
239 |
|
240 |
# --- PDF Generation and Preview Utility Function ---
|
241 |
def generate_and_preview_pdf(image: Image.Image, text_content: str, font_size: int, line_spacing: float, alignment: str, image_size: str):
|
|
|
337 |
)
|
338 |
yield answer, answer
|
339 |
return
|
340 |
+
|
341 |
+
# --- Special Handling for InternVL ---
|
342 |
+
if model_name == "OpenGVLab/InternVL3_5-2B-MPO(reason)":
|
343 |
+
pixel_values = load_image_internvl(image, max_num=12).to(torch.bfloat16).to(device)
|
344 |
+
generation_config = dict(
|
345 |
+
max_new_tokens=max_new_tokens,
|
346 |
+
do_sample=True if temperature > 0 else False,
|
347 |
+
temperature=temperature,
|
348 |
+
top_p=top_p,
|
349 |
+
top_k=top_k,
|
350 |
+
repetition_penalty=repetition_penalty,
|
351 |
+
)
|
352 |
+
question = f"<image>\n{prompt_input}"
|
353 |
+
response = model_iv.chat(tokenizer_iv, pixel_values, question, generation_config)
|
354 |
+
yield response, response
|
355 |
+
return
|
356 |
+
|
357 |
|
358 |
processor = None
|
359 |
model = None
|
|
|
367 |
else:
|
368 |
if model_name == "LFM2-VL-450M(fast)": processor, model = processor_m, model_m
|
369 |
elif model_name == "LFM2-VL-1.6B(fast)": processor, model = processor_t, model_t
|
|
|
370 |
elif model_name == "SmolVLM-Instruct-250M(smol)": processor, model = processor_c, model_c
|
371 |
elif model_name == "MonkeyOCR-pro-1.2B(ocr)": processor, model = processor_g, model_g
|
372 |
elif model_name == "VLAA-Thinker-Qwen2VL-2B(reason)": processor, model = processor_i, model_i
|
|
|
435 |
with gr.Column(scale=1):
|
436 |
model_choice = gr.Dropdown(
|
437 |
choices=["LFM2-VL-450M(fast)", "LFM2-VL-1.6B(fast)", "SmolVLM-Instruct-250M(smol)", "Moondream2(vision)",
|
438 |
+
"OpenGVLab/InternVL3_5-2B-MPO(reason)", "Megalodon-OCR-Sync-0713(ocr)",
|
439 |
"VLAA-Thinker-Qwen2VL-2B(reason)", "MonkeyOCR-pro-1.2B(ocr)",
|
440 |
"Qwen2.5-VL-3B-Abliterated-Caption-it(caption)", "Nanonets-OCR-s(ocr)",
|
441 |
"LMM-R1-MGT-PerceReason(reason)", "OCRFlux-3B(ocr)", "TBAC-VLR1-3B(open-r1)",
|