Spaces:
Running
on
Zero
Running
on
Zero
initial commit (#1)
Browse files- initial commit (f32c616c2efd2941b1ed6d239721513531329842)
- .gitattributes +5 -0
- app.py +383 -0
- images/1.png +3 -0
- images/2.jpg +3 -0
- images/3.png +3 -0
- images/4.png +0 -0
- images/5.jpg +0 -0
- images/6.jpg +0 -0
- images/7.jpg +0 -0
- requirements.txt +15 -0
- videos/1.mp4 +3 -0
- videos/2.mp4 +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,8 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
images/1.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
images/2.jpg filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
images/3.png filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
videos/1.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
videos/2.mp4 filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,383 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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random
|
| 3 |
+
import uuid
|
| 4 |
+
import json
|
| 5 |
+
import time
|
| 6 |
+
import asyncio
|
| 7 |
+
from threading import Thread
|
| 8 |
+
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import spaces
|
| 11 |
+
import torch
|
| 12 |
+
import numpy as np
|
| 13 |
+
from PIL import Image, ImageOps
|
| 14 |
+
import cv2
|
| 15 |
+
|
| 16 |
+
from transformers import (
|
| 17 |
+
Qwen2VLForConditionalGeneration,
|
| 18 |
+
Qwen2_5_VLForConditionalGeneration,
|
| 19 |
+
AutoModelForVision2Seq,
|
| 20 |
+
AutoProcessor,
|
| 21 |
+
TextIteratorStreamer,
|
| 22 |
+
)
|
| 23 |
+
from transformers.image_utils import load_image
|
| 24 |
+
|
| 25 |
+
from docling_core.types.doc import DoclingDocument, DocTagsDocument
|
| 26 |
+
|
| 27 |
+
import re
|
| 28 |
+
import ast
|
| 29 |
+
import html
|
| 30 |
+
|
| 31 |
+
# Constants for text generation
|
| 32 |
+
MAX_MAX_NEW_TOKENS = 2048
|
| 33 |
+
DEFAULT_MAX_NEW_TOKENS = 1024
|
| 34 |
+
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
| 35 |
+
|
| 36 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 37 |
+
|
| 38 |
+
# Load Nanonets-OCR-s
|
| 39 |
+
MODEL_ID_M = "nanonets/Nanonets-OCR-s"
|
| 40 |
+
processor_m = AutoProcessor.from_pretrained(MODEL_ID_M, trust_remote_code=True)
|
| 41 |
+
model_m = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 42 |
+
MODEL_ID_M,
|
| 43 |
+
trust_remote_code=True,
|
| 44 |
+
torch_dtype=torch.float16
|
| 45 |
+
).to(device).eval()
|
| 46 |
+
|
| 47 |
+
# Load MonkeyOCR
|
| 48 |
+
MODEL_ID_G = "echo840/MonkeyOCR"
|
| 49 |
+
SUBFOLDER = "Recognition"
|
| 50 |
+
processor_g = AutoProcessor.from_pretrained(
|
| 51 |
+
MODEL_ID_G,
|
| 52 |
+
trust_remote_code=True,
|
| 53 |
+
subfolder=SUBFOLDER
|
| 54 |
+
)
|
| 55 |
+
model_g = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 56 |
+
MODEL_ID_G,
|
| 57 |
+
trust_remote_code=True,
|
| 58 |
+
subfolder=SUBFOLDER,
|
| 59 |
+
torch_dtype=torch.float16
|
| 60 |
+
).to(device).eval()
|
| 61 |
+
|
| 62 |
+
# Load typhoon-ocr-7b
|
| 63 |
+
MODEL_ID_L = "scb10x/typhoon-ocr-7b"
|
| 64 |
+
processor_l = AutoProcessor.from_pretrained(MODEL_ID_L, trust_remote_code=True)
|
| 65 |
+
model_l = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 66 |
+
MODEL_ID_L,
|
| 67 |
+
trust_remote_code=True,
|
| 68 |
+
torch_dtype=torch.float16
|
| 69 |
+
).to(device).eval()
|
| 70 |
+
|
| 71 |
+
#--------------------------------------------------#
|
| 72 |
+
# Load SmolDocling-256M-preview
|
| 73 |
+
MODEL_ID_X = "ds4sd/SmolDocling-256M-preview"
|
| 74 |
+
processor_x = AutoProcessor.from_pretrained(MODEL_ID_X, trust_remote_code=True)
|
| 75 |
+
model_x = AutoModelForVision2Seq.from_pretrained(
|
| 76 |
+
MODEL_ID_X,
|
| 77 |
+
trust_remote_code=True,
|
| 78 |
+
torch_dtype=torch.float16
|
| 79 |
+
).to(device).eval()
|
| 80 |
+
#--------------------------------------------------#
|
| 81 |
+
|
| 82 |
+
# Preprocessing functions for SmolDocling-256M
|
| 83 |
+
def add_random_padding(image, min_percent=0.1, max_percent=0.10):
|
| 84 |
+
"""Add random padding to an image based on its size."""
|
| 85 |
+
image = image.convert("RGB")
|
| 86 |
+
width, height = image.size
|
| 87 |
+
pad_w_percent = random.uniform(min_percent, max_percent)
|
| 88 |
+
pad_h_percent = random.uniform(min_percent, max_percent)
|
| 89 |
+
pad_w = int(width * pad_w_percent)
|
| 90 |
+
pad_h = int(height * pad_h_percent)
|
| 91 |
+
corner_pixel = image.getpixel((0, 0)) # Top-left corner
|
| 92 |
+
padded_image = ImageOps.expand(image, border=(pad_w, pad_h, pad_w, pad_h), fill=corner_pixel)
|
| 93 |
+
return padded_image
|
| 94 |
+
|
| 95 |
+
def normalize_values(text, target_max=500):
|
| 96 |
+
"""Normalize numerical values in text to a target maximum."""
|
| 97 |
+
def normalize_list(values):
|
| 98 |
+
max_value = max(values) if values else 1
|
| 99 |
+
return [round((v / max_value) * target_max) for v in values]
|
| 100 |
+
|
| 101 |
+
def process_match(match):
|
| 102 |
+
num_list = ast.literal_eval(match.group(0))
|
| 103 |
+
normalized = normalize_list(num_list)
|
| 104 |
+
return "".join([f"<loc_{num}>" for num in normalized])
|
| 105 |
+
|
| 106 |
+
pattern = r"\[([\d\.\s,]+)\]"
|
| 107 |
+
normalized_text = re.sub(pattern, process_match, text)
|
| 108 |
+
return normalized_text
|
| 109 |
+
|
| 110 |
+
def downsample_video(video_path):
|
| 111 |
+
"""Downsample a video to evenly spaced frames, returning PIL images with timestamps."""
|
| 112 |
+
vidcap = cv2.VideoCapture(video_path)
|
| 113 |
+
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 114 |
+
fps = vidcap.get(cv2.CAP_PROP_FPS)
|
| 115 |
+
frames = []
|
| 116 |
+
frame_indices = np.linspace(0, total_frames - 1, 10, dtype=int)
|
| 117 |
+
for i in frame_indices:
|
| 118 |
+
vidcap.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 119 |
+
success, image = vidcap.read()
|
| 120 |
+
if success:
|
| 121 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 122 |
+
pil_image = Image.fromarray(image)
|
| 123 |
+
timestamp = round(i / fps, 2)
|
| 124 |
+
frames.append((pil_image, timestamp))
|
| 125 |
+
vidcap.release()
|
| 126 |
+
return frames
|
| 127 |
+
|
| 128 |
+
@spaces.GPU
|
| 129 |
+
def generate_image(model_name: str, text: str, image: Image.Image,
|
| 130 |
+
max_new_tokens: int = 1024,
|
| 131 |
+
temperature: float = 0.6,
|
| 132 |
+
top_p: float = 0.9,
|
| 133 |
+
top_k: int = 50,
|
| 134 |
+
repetition_penalty: float = 1.2):
|
| 135 |
+
"""Generate responses for image input using the selected model."""
|
| 136 |
+
# Model selection
|
| 137 |
+
if model_name == "Nanonets-OCR-s":
|
| 138 |
+
processor = processor_m
|
| 139 |
+
model = model_m
|
| 140 |
+
elif model_name == "MonkeyOCR-Recognition":
|
| 141 |
+
processor = processor_g
|
| 142 |
+
model = model_g
|
| 143 |
+
elif model_name == "SmolDocling-256M-preview":
|
| 144 |
+
processor = processor_x
|
| 145 |
+
model = model_x
|
| 146 |
+
elif model_name == "Typhoon-OCR-7B":
|
| 147 |
+
processor = processor_l
|
| 148 |
+
model = model_l
|
| 149 |
+
else:
|
| 150 |
+
yield "Invalid model selected."
|
| 151 |
+
return
|
| 152 |
+
|
| 153 |
+
if image is None:
|
| 154 |
+
yield "Please upload an image."
|
| 155 |
+
return
|
| 156 |
+
|
| 157 |
+
# Prepare images as a list (single image for image inference)
|
| 158 |
+
images = [image]
|
| 159 |
+
|
| 160 |
+
# SmolDocling-256M specific preprocessing
|
| 161 |
+
if model_name == "SmolDocling-256M-preview":
|
| 162 |
+
if "OTSL" in text or "code" in text:
|
| 163 |
+
images = [add_random_padding(img) for img in images]
|
| 164 |
+
if "OCR at text at" in text or "Identify element" in text or "formula" in text:
|
| 165 |
+
text = normalize_values(text, target_max=500)
|
| 166 |
+
|
| 167 |
+
# Unified message structure for all models
|
| 168 |
+
messages = [
|
| 169 |
+
{
|
| 170 |
+
"role": "user",
|
| 171 |
+
"content": [{"type": "image"} for _ in images] + [
|
| 172 |
+
{"type": "text", "text": text}
|
| 173 |
+
]
|
| 174 |
+
}
|
| 175 |
+
]
|
| 176 |
+
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 177 |
+
inputs = processor(text=prompt, images=images, return_tensors="pt").to(device)
|
| 178 |
+
|
| 179 |
+
# Generation with streaming
|
| 180 |
+
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
| 181 |
+
generation_kwargs = {
|
| 182 |
+
**inputs,
|
| 183 |
+
"streamer": streamer,
|
| 184 |
+
"max_new_tokens": max_new_tokens,
|
| 185 |
+
"temperature": temperature,
|
| 186 |
+
"top_p": top_p,
|
| 187 |
+
"top_k": top_k,
|
| 188 |
+
"repetition_penalty": repetition_penalty,
|
| 189 |
+
}
|
| 190 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 191 |
+
thread.start()
|
| 192 |
+
|
| 193 |
+
# Stream output and collect full response
|
| 194 |
+
buffer = ""
|
| 195 |
+
full_output = ""
|
| 196 |
+
for new_text in streamer:
|
| 197 |
+
full_output += new_text
|
| 198 |
+
buffer += new_text.replace("<|im_end|>", "")
|
| 199 |
+
yield buffer
|
| 200 |
+
|
| 201 |
+
# SmolDocling-256M specific postprocessing
|
| 202 |
+
if model_name == "SmolDocling-256M-preview":
|
| 203 |
+
cleaned_output = full_output.replace("<end_of_utterance>", "").strip()
|
| 204 |
+
if any(tag in cleaned_output for tag in ["<doctag>", "<otsl>", "<code>", "<chart>", "<formula>"]):
|
| 205 |
+
if "<chart>" in cleaned_output:
|
| 206 |
+
cleaned_output = cleaned_output.replace("<chart>", "<otsl>").replace("</chart>", "</otsl>")
|
| 207 |
+
cleaned_output = re.sub(r'(<loc_500>)(?!.*<loc_500>)<[^>]+>', r'\1', cleaned_output)
|
| 208 |
+
doctags_doc = DocTagsDocument.from_doctags_and_image_pairs([cleaned_output], images)
|
| 209 |
+
doc = DoclingDocument.load_from_doctags(doctags_doc, document_name="Document")
|
| 210 |
+
markdown_output = doc.export_to_markdown()
|
| 211 |
+
yield f"**MD Output:**\n\n{markdown_output}"
|
| 212 |
+
else:
|
| 213 |
+
yield cleaned_output
|
| 214 |
+
|
| 215 |
+
@spaces.GPU
|
| 216 |
+
def generate_video(model_name: str, text: str, video_path: str,
|
| 217 |
+
max_new_tokens: int = 1024,
|
| 218 |
+
temperature: float = 0.6,
|
| 219 |
+
top_p: float = 0.9,
|
| 220 |
+
top_k: int = 50,
|
| 221 |
+
repetition_penalty: float = 1.2):
|
| 222 |
+
"""Generate responses for video input using the selected model."""
|
| 223 |
+
# Model selection
|
| 224 |
+
if model_name == "Nanonets-OCR-s":
|
| 225 |
+
processor = processor_m
|
| 226 |
+
model = model_m
|
| 227 |
+
elif model_name == "MonkeyOCR-Recognition":
|
| 228 |
+
processor = processor_g
|
| 229 |
+
model = model_g
|
| 230 |
+
elif model_name == "SmolDocling-256M-preview":
|
| 231 |
+
processor = processor_x
|
| 232 |
+
model = model_x
|
| 233 |
+
elif model_name == "Typhoon-OCR-7B":
|
| 234 |
+
processor = processor_l
|
| 235 |
+
model = model_l
|
| 236 |
+
else:
|
| 237 |
+
yield "Invalid model selected."
|
| 238 |
+
return
|
| 239 |
+
|
| 240 |
+
if video_path is None:
|
| 241 |
+
yield "Please upload a video."
|
| 242 |
+
return
|
| 243 |
+
|
| 244 |
+
# Extract frames from video
|
| 245 |
+
frames = downsample_video(video_path)
|
| 246 |
+
images = [frame for frame, _ in frames]
|
| 247 |
+
|
| 248 |
+
# SmolDocling-256M specific preprocessing
|
| 249 |
+
if model_name == "SmolDocling-256M-preview":
|
| 250 |
+
if "OTSL" in text or "code" in text:
|
| 251 |
+
images = [add_random_padding(img) for img in images]
|
| 252 |
+
if "OCR at text at" in text or "Identify element" in text or "formula" in text:
|
| 253 |
+
text = normalize_values(text, target_max=500)
|
| 254 |
+
|
| 255 |
+
# Unified message structure for all models
|
| 256 |
+
messages = [
|
| 257 |
+
{
|
| 258 |
+
"role": "user",
|
| 259 |
+
"content": [{"type": "image"} for _ in images] + [
|
| 260 |
+
{"type": "text", "text": text}
|
| 261 |
+
]
|
| 262 |
+
}
|
| 263 |
+
]
|
| 264 |
+
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 265 |
+
inputs = processor(text=prompt, images=images, return_tensors="pt").to(device)
|
| 266 |
+
|
| 267 |
+
# Generation with streaming
|
| 268 |
+
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
| 269 |
+
generation_kwargs = {
|
| 270 |
+
**inputs,
|
| 271 |
+
"streamer": streamer,
|
| 272 |
+
"max_new_tokens": max_new_tokens,
|
| 273 |
+
"temperature": temperature,
|
| 274 |
+
"top_p": top_p,
|
| 275 |
+
"top_k": top_k,
|
| 276 |
+
"repetition_penalty": repetition_penalty,
|
| 277 |
+
}
|
| 278 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 279 |
+
thread.start()
|
| 280 |
+
|
| 281 |
+
# Stream output and collect full response
|
| 282 |
+
buffer = ""
|
| 283 |
+
full_output = ""
|
| 284 |
+
for new_text in streamer:
|
| 285 |
+
full_output += new_text
|
| 286 |
+
buffer += new_text.replace("<|im_end|>", "")
|
| 287 |
+
yield buffer
|
| 288 |
+
|
| 289 |
+
# SmolDocling-256M specific postprocessing
|
| 290 |
+
if model_name == "SmolDocling-256M-preview":
|
| 291 |
+
cleaned_output = full_output.replace("<end_of_utterance>", "").strip()
|
| 292 |
+
if any(tag in cleaned_output for tag in ["<doctag>", "<otsl>", "<code>", "<chart>", "<formula>"]):
|
| 293 |
+
if "<chart>" in cleaned_output:
|
| 294 |
+
cleaned_output = cleaned_output.replace("<chart>", "<otsl>").replace("</chart>", "</otsl>")
|
| 295 |
+
cleaned_output = re.sub(r'(<loc_500>)(?!.*<loc_500>)<[^>]+>', r'\1', cleaned_output)
|
| 296 |
+
doctags_doc = DocTagsDocument.from_doctags_and_image_pairs([cleaned_output], images)
|
| 297 |
+
doc = DoclingDocument.load_from_doctags(doctags_doc, document_name="Document")
|
| 298 |
+
markdown_output = doc.export_to_markdown()
|
| 299 |
+
yield f"**MD Output:**\n\n{markdown_output}"
|
| 300 |
+
else:
|
| 301 |
+
yield cleaned_output
|
| 302 |
+
|
| 303 |
+
# Define examples for image and video inference
|
| 304 |
+
image_examples = [
|
| 305 |
+
["OCR the image", "images/2.jpg"],
|
| 306 |
+
["Convert this page to docling", "images/1.png"],
|
| 307 |
+
["Convert this page to docling", "images/3.png"],
|
| 308 |
+
["Convert chart to OTSL.", "images/4.png"],
|
| 309 |
+
["Convert code to text", "images/5.jpg"],
|
| 310 |
+
["Convert this table to OTSL.", "images/6.jpg"],
|
| 311 |
+
["Convert formula to late.", "images/7.jpg"],
|
| 312 |
+
]
|
| 313 |
+
|
| 314 |
+
video_examples = [
|
| 315 |
+
["Explain the video in detail.", "videos/1.mp4"],
|
| 316 |
+
["Explain the video in detail.", "videos/2.mp4"]
|
| 317 |
+
]
|
| 318 |
+
|
| 319 |
+
css = """
|
| 320 |
+
.submit-btn {
|
| 321 |
+
background-color: #2980b9 !important;
|
| 322 |
+
color: white !important;
|
| 323 |
+
}
|
| 324 |
+
.submit-btn:hover {
|
| 325 |
+
background-color: #3498db !important;
|
| 326 |
+
}
|
| 327 |
+
"""
|
| 328 |
+
|
| 329 |
+
# Create the Gradio Interface
|
| 330 |
+
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
| 331 |
+
gr.Markdown("# **[Multimodal OCR2](https://huggingface.co/collections/prithivMLmods/multimodal-implementations-67c9982ea04b39f0608badb0)**")
|
| 332 |
+
with gr.Row():
|
| 333 |
+
with gr.Column():
|
| 334 |
+
with gr.Tabs():
|
| 335 |
+
with gr.TabItem("Image Inference"):
|
| 336 |
+
image_query = gr.Textbox(label="Query Input", placeholder="Enter your query here...")
|
| 337 |
+
image_upload = gr.Image(type="pil", label="Image")
|
| 338 |
+
image_submit = gr.Button("Submit", elem_classes="submit-btn")
|
| 339 |
+
gr.Examples(
|
| 340 |
+
examples=image_examples,
|
| 341 |
+
inputs=[image_query, image_upload]
|
| 342 |
+
)
|
| 343 |
+
with gr.TabItem("Video Inference"):
|
| 344 |
+
video_query = gr.Textbox(label="Query Input", placeholder="Enter your query here...")
|
| 345 |
+
video_upload = gr.Video(label="Video")
|
| 346 |
+
video_submit = gr.Button("Submit", elem_classes="submit-btn")
|
| 347 |
+
gr.Examples(
|
| 348 |
+
examples=video_examples,
|
| 349 |
+
inputs=[video_query, video_upload]
|
| 350 |
+
)
|
| 351 |
+
with gr.Accordion("Advanced options", open=False):
|
| 352 |
+
max_new_tokens = gr.Slider(label="Max new tokens", minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS)
|
| 353 |
+
temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=4.0, step=0.1, value=0.6)
|
| 354 |
+
top_p = gr.Slider(label="Top-p (nucleus sampling)", minimum=0.05, maximum=1.0, step=0.05, value=0.9)
|
| 355 |
+
top_k = gr.Slider(label="Top-k", minimum=1, maximum=1000, step=1, value=50)
|
| 356 |
+
repetition_penalty = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2)
|
| 357 |
+
with gr.Column():
|
| 358 |
+
output = gr.Textbox(label="Output", interactive=False, lines=3, scale=2)
|
| 359 |
+
model_choice = gr.Radio(
|
| 360 |
+
choices=["SmolDocling-256M-preview", "Nanonets-OCR-s", "MonkeyOCR-Recognition", "Typhoon-OCR-7B"],
|
| 361 |
+
label="Select Model",
|
| 362 |
+
value="Nanonets-OCR-s"
|
| 363 |
+
)
|
| 364 |
+
|
| 365 |
+
gr.Markdown("**Model Info 💻**")
|
| 366 |
+
gr.Markdown("> [SmolDocling-256M](https://huggingface.co/ds4sd/SmolDocling-256M-preview): SmolDocling is a multimodal Image-Text-to-Text model designed for efficient document conversion. It retains Docling's most popular features while ensuring full compatibility with Docling through seamless support for DoclingDocuments.")
|
| 367 |
+
gr.Markdown("> [Nanonets-OCR-s](https://huggingface.co/nanonets/Nanonets-OCR-s): nanonets-ocr-s is a powerful, state-of-the-art image-to-markdown ocr model that goes far beyond traditional text extraction. it transforms documents into structured markdown with intelligent content recognition and semantic tagging.")
|
| 368 |
+
gr.Markdown("> [MonkeyOCR-Recognition](https://huggingface.co/echo840/MonkeyOCR): MonkeyOCR adopts a Structure-Recognition-Relation (SRR) triplet paradigm, which simplifies the multi-tool pipeline of modular approaches while avoiding the inefficiency of using large multimodal models for full-page document processing.")
|
| 369 |
+
gr.Markdown("> [Typhoon-OCR-7B](https://huggingface.co/scb10x/typhoon-ocr-7b): A bilingual document parsing model built specifically for real-world documents in Thai and English inspired by models like olmOCR based on Qwen2.5-VL-Instruction. Extracts and interprets embedded text (e.g., chart labels, captions) in Thai or English.")
|
| 370 |
+
|
| 371 |
+
image_submit.click(
|
| 372 |
+
fn=generate_image,
|
| 373 |
+
inputs=[model_choice, image_query, image_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
|
| 374 |
+
outputs=output
|
| 375 |
+
)
|
| 376 |
+
video_submit.click(
|
| 377 |
+
fn=generate_video,
|
| 378 |
+
inputs=[model_choice, video_query, video_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
|
| 379 |
+
outputs=output
|
| 380 |
+
)
|
| 381 |
+
|
| 382 |
+
if __name__ == "__main__":
|
| 383 |
+
demo.queue(max_size=40).launch(share=True, mcp_server=True, ssr_mode=False, show_error=True)
|
images/1.png
ADDED
|
Git LFS Details
|
images/2.jpg
ADDED
|
Git LFS Details
|
images/3.png
ADDED
|
Git LFS Details
|
images/4.png
ADDED
|
images/5.jpg
ADDED
|
images/6.jpg
ADDED
|
images/7.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
transformers-stream-generator
|
| 4 |
+
qwen-vl-utils
|
| 5 |
+
torchvision
|
| 6 |
+
docling-core
|
| 7 |
+
torch
|
| 8 |
+
requests
|
| 9 |
+
huggingface_hub
|
| 10 |
+
albumentations
|
| 11 |
+
spaces
|
| 12 |
+
accelerate
|
| 13 |
+
pillow
|
| 14 |
+
opencv-python
|
| 15 |
+
av
|
videos/1.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9127aaafccef6f02fce6812bc9c89e1e4026832cf133492481952cc4b94cb595
|
| 3 |
+
size 791367
|
videos/2.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bdf85ced4e76f2afd1a66b2c41e93868ccd9f928a02105de5e7db3c8651c692e
|
| 3 |
+
size 1040341
|