Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,14 @@ import gc
|
|
| 3 |
import re
|
| 4 |
import ast
|
| 5 |
import json
|
| 6 |
-
import
|
| 7 |
-
import
|
|
|
|
| 8 |
import base64
|
|
|
|
| 9 |
from io import BytesIO
|
| 10 |
from threading import Thread
|
|
|
|
| 11 |
|
| 12 |
import gradio as gr
|
| 13 |
import spaces
|
|
@@ -17,8 +20,8 @@ from PIL import Image, ImageOps
|
|
| 17 |
from transformers import (
|
| 18 |
Qwen2_5_VLForConditionalGeneration,
|
| 19 |
Qwen3VLForConditionalGeneration,
|
| 20 |
-
AutoProcessor,
|
| 21 |
AutoModelForVision2Seq,
|
|
|
|
| 22 |
TextIteratorStreamer,
|
| 23 |
)
|
| 24 |
|
|
@@ -28,6 +31,7 @@ from docling_core.types.doc import DoclingDocument, DocTagsDocument
|
|
| 28 |
MAX_MAX_NEW_TOKENS = 4096
|
| 29 |
DEFAULT_MAX_NEW_TOKENS = 2048
|
| 30 |
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
|
|
|
| 31 |
|
| 32 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 33 |
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
|
@@ -42,9 +46,87 @@ def safe_empty_cache():
|
|
| 42 |
torch.cuda.empty_cache()
|
| 43 |
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
def apply_gpu_duration(val: str):
|
| 46 |
try:
|
| 47 |
-
return int(
|
| 48 |
except Exception:
|
| 49 |
return 60
|
| 50 |
|
|
@@ -109,84 +191,99 @@ model_n = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
|
| 109 |
).to(device).eval()
|
| 110 |
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
padded_image = ImageOps.expand(image, border=(pad_w, pad_h, pad_w, pad_h), fill=corner_pixel)
|
| 121 |
-
return padded_image
|
| 122 |
|
| 123 |
|
| 124 |
-
def
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
def process_match(match):
|
| 130 |
-
num_list = ast.literal_eval(match.group(0))
|
| 131 |
-
normalized = normalize_list(num_list)
|
| 132 |
-
return "".join([f"<loc_{num}>" for num in normalized])
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
| 137 |
|
|
|
|
|
|
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
try:
|
| 143 |
return int(gpu_timeout)
|
| 144 |
except Exception:
|
| 145 |
return 60
|
| 146 |
|
| 147 |
|
| 148 |
-
def get_model_and_processor(model_name: str):
|
| 149 |
-
if model_name == "Nanonets-OCR-s":
|
| 150 |
-
return processor_m, model_m
|
| 151 |
-
elif model_name == "MonkeyOCR-Recognition":
|
| 152 |
-
return processor_g, model_g
|
| 153 |
-
elif model_name == "SmolDocling-256M-preview":
|
| 154 |
-
return processor_x, model_x
|
| 155 |
-
elif model_name == "Typhoon-OCR-7B":
|
| 156 |
-
return processor_l, model_l
|
| 157 |
-
elif model_name == "Thyme-RL":
|
| 158 |
-
return processor_n, model_n
|
| 159 |
-
elif model_name == "FireRed-OCR":
|
| 160 |
-
return processor_f, model_f
|
| 161 |
-
return None, None
|
| 162 |
-
|
| 163 |
-
|
| 164 |
@spaces.GPU(duration=calc_timeout_image)
|
| 165 |
-
def generate_image(
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
gc.collect()
|
| 173 |
safe_empty_cache()
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
-
if
|
| 181 |
-
|
| 182 |
-
return
|
| 183 |
|
| 184 |
-
if not text or not
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
if model_name == "SmolDocling-256M-preview":
|
| 192 |
if "OTSL" in text or "code" in text:
|
|
@@ -194,13 +291,14 @@ def generate_image(model_name: str, text: str, image: Image.Image,
|
|
| 194 |
if "OCR at text at" in text or "Identify element" in text or "formula" in text:
|
| 195 |
text = normalize_values(text, target_max=500)
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
try:
|
| 198 |
-
messages = [
|
| 199 |
-
{
|
| 200 |
-
"role": "user",
|
| 201 |
-
"content": [{"type": "image"} for _ in images] + [{"type": "text", "text": text}],
|
| 202 |
-
}
|
| 203 |
-
]
|
| 204 |
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 205 |
inputs = processor(text=prompt, images=images, return_tensors="pt").to(device)
|
| 206 |
|
|
@@ -228,290 +326,234 @@ def generate_image(model_name: str, text: str, image: Image.Image,
|
|
| 228 |
if any(tag in cleaned_output for tag in ["<doctag>", "<otsl>", "<code>", "<chart>", "<formula>"]):
|
| 229 |
if "<chart>" in cleaned_output:
|
| 230 |
cleaned_output = cleaned_output.replace("<chart>", "<otsl>").replace("</chart>", "</otsl>")
|
| 231 |
-
cleaned_output = re.sub(r
|
| 232 |
doctags_doc = DocTagsDocument.from_doctags_and_image_pairs([cleaned_output], images)
|
| 233 |
doc = DoclingDocument.load_from_doctags(doctags_doc, document_name="Document")
|
| 234 |
markdown_output = doc.export_to_markdown()
|
| 235 |
yield markdown_output
|
| 236 |
else:
|
| 237 |
yield cleaned_output
|
| 238 |
-
|
| 239 |
-
yield f"Error: {str(e)}"
|
| 240 |
finally:
|
| 241 |
gc.collect()
|
| 242 |
safe_empty_cache()
|
| 243 |
|
| 244 |
|
| 245 |
-
image_examples = [
|
| 246 |
-
["Generate Markdown", "examples/6.jpg"],
|
| 247 |
-
["Perform OCR on the image precisely.", "examples/5.jpg"],
|
| 248 |
-
["Run OCR on the image and ensure high accuracy.", "examples/4.jpg"],
|
| 249 |
-
["Conduct OCR on the image with exact text recognition.", "examples/2.jpg"],
|
| 250 |
-
["Perform precise OCR extraction on the image.", "examples/1.jpg"],
|
| 251 |
-
["Convert this page to docling", "examples/3.jpg"],
|
| 252 |
-
]
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
def encode_full_image(path):
|
| 256 |
-
if not os.path.exists(path):
|
| 257 |
-
return ""
|
| 258 |
-
try:
|
| 259 |
-
with open(path, "rb") as f:
|
| 260 |
-
data = f.read()
|
| 261 |
-
ext = path.rsplit(".", 1)[-1].lower()
|
| 262 |
-
mime = {"jpg": "image/jpeg", "jpeg": "image/jpeg", "png": "image/png", "webp": "image/webp"}.get(ext, "image/jpeg")
|
| 263 |
-
return f"data:{mime};base64,{base64.b64encode(data).decode()}"
|
| 264 |
-
except Exception as e:
|
| 265 |
-
print(f"Encode error for {path}: {e}")
|
| 266 |
-
return ""
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
def make_thumb_b64(path, max_dim=240):
|
| 270 |
-
if not os.path.exists(path):
|
| 271 |
-
return ""
|
| 272 |
-
try:
|
| 273 |
-
img = Image.open(path).convert("RGB")
|
| 274 |
-
img.thumbnail((max_dim, max_dim))
|
| 275 |
-
buf = BytesIO()
|
| 276 |
-
img.save(buf, format="JPEG", quality=70)
|
| 277 |
-
return f"data:image/jpeg;base64,{base64.b64encode(buf.getvalue()).decode()}"
|
| 278 |
-
except Exception as e:
|
| 279 |
-
print(f"Thumbnail error for {path}: {e}")
|
| 280 |
-
return ""
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
def build_example_cards_html():
|
| 284 |
-
cards = ""
|
| 285 |
-
for i, ex in enumerate(image_examples):
|
| 286 |
-
query, img_path = ex
|
| 287 |
-
thumb = make_thumb_b64(img_path)
|
| 288 |
-
thumb_html = f'<img src="{thumb}" alt="example">' if thumb else '<div class="example-thumb-placeholder">Preview</div>'
|
| 289 |
-
short_query = html_lib.escape(query[:80]) + ("..." if len(query) > 80 else "")
|
| 290 |
-
cards += f"""
|
| 291 |
-
<div class="example-card" data-idx="{i}">
|
| 292 |
-
<div class="example-thumbs">{thumb_html}</div>
|
| 293 |
-
<div class="example-meta"><span class="example-badge">Example {i+1}</span></div>
|
| 294 |
-
<div class="example-prompt-text">{short_query}</div>
|
| 295 |
-
</div>
|
| 296 |
-
"""
|
| 297 |
-
return cards
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
def load_example_data(idx_str):
|
| 301 |
-
try:
|
| 302 |
-
idx = int(float(idx_str)) if idx_str and str(idx_str).strip() else -1
|
| 303 |
-
except Exception:
|
| 304 |
-
idx = -1
|
| 305 |
-
|
| 306 |
-
if idx < 0 or idx >= len(image_examples):
|
| 307 |
-
return json.dumps({"image": "", "query": "", "status": "error"})
|
| 308 |
-
|
| 309 |
-
query, img_path = image_examples[idx]
|
| 310 |
-
b64 = encode_full_image(img_path)
|
| 311 |
-
if not b64:
|
| 312 |
-
return json.dumps({"image": "", "query": "", "status": "error"})
|
| 313 |
-
return json.dumps({"image": b64, "query": query, "status": "ok"})
|
| 314 |
-
|
| 315 |
-
|
| 316 |
EXAMPLE_CARDS_HTML = build_example_cards_html()
|
| 317 |
|
| 318 |
-
|
| 319 |
<svg viewBox="0 0 64 64" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
|
| 320 |
<defs>
|
| 321 |
<linearGradient id="visionGrad" x1="0%" y1="0%" x2="100%" y2="100%">
|
| 322 |
-
<stop offset="0%" stop-color="#
|
| 323 |
<stop offset="100%" stop-color="#FF4500"/>
|
| 324 |
</linearGradient>
|
| 325 |
</defs>
|
| 326 |
-
<
|
| 327 |
-
<
|
| 328 |
-
<circle cx="32" cy="
|
| 329 |
-
<
|
| 330 |
-
<path d="M18 52h28" stroke="#FF4500" stroke-width="3" stroke-linecap="round"/>
|
| 331 |
-
<path d="M18 58h20" stroke="#FF4500" stroke-width="3" stroke-linecap="round" opacity="0.8"/>
|
| 332 |
</svg>
|
| 333 |
"""
|
| 334 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
css = r"""
|
| 336 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&family=JetBrains+Mono:wght@400;500;600&display=swap');
|
| 337 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
*{box-sizing:border-box;margin:0;padding:0}
|
| 339 |
body,.gradio-container{
|
| 340 |
-
background:
|
| 341 |
font-family:'Inter',system-ui,-apple-system,sans-serif!important;
|
| 342 |
font-size:14px!important;
|
| 343 |
-
color:
|
| 344 |
min-height:100vh;
|
| 345 |
}
|
| 346 |
footer{display:none!important}
|
| 347 |
.hidden-input{display:none!important;height:0!important;overflow:hidden!important;margin:0!important;padding:0!important}
|
| 348 |
-
|
| 349 |
-
#gradio-run-btn{
|
| 350 |
-
position:absolute;left:-9999px;top:-9999px;width:1px;height:1px;
|
| 351 |
-
opacity:0.01;pointer-events:none;overflow:hidden;
|
| 352 |
-
}
|
| 353 |
-
#example-load-btn{
|
| 354 |
position:absolute!important;left:-9999px!important;top:-9999px!important;
|
| 355 |
-
width:1px!important;height:1px!important;opacity:
|
| 356 |
-
pointer-events:none!important;overflow:hidden!important;
|
| 357 |
}
|
| 358 |
|
| 359 |
.app-shell{
|
| 360 |
-
background:
|
| 361 |
-
border:1px solid
|
| 362 |
border-radius:18px;
|
| 363 |
margin:12px auto;
|
| 364 |
-
max-width:
|
| 365 |
overflow:hidden;
|
| 366 |
-
box-shadow:0 25px 50px -12px rgba(0,0,0,.6),0 0 0 1px rgba(255,255,255,.
|
| 367 |
}
|
| 368 |
.app-header{
|
| 369 |
-
background:linear-gradient(135deg,#18181b,#
|
| 370 |
-
border-bottom:1px solid
|
| 371 |
padding:14px 24px;
|
| 372 |
-
display:flex;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
}
|
| 374 |
.app-header-left{display:flex;align-items:center;gap:12px}
|
| 375 |
.app-logo{
|
| 376 |
-
width:
|
| 377 |
-
background:linear-gradient(135deg,#
|
| 378 |
-
|
| 379 |
-
display:flex;align-items:center;justify-content:center;
|
| 380 |
-
box-shadow:0 4px 12px rgba(255,69,0,.35);
|
| 381 |
}
|
| 382 |
-
.app-logo svg{width:24px;height:24px}
|
| 383 |
.app-title{
|
| 384 |
-
font-size:
|
| 385 |
-
background:linear-gradient(135deg,#
|
| 386 |
-webkit-background-clip:text;-webkit-text-fill-color:transparent;
|
| 387 |
-
letter-spacing:-.3px;
|
| 388 |
}
|
| 389 |
.app-badge{
|
| 390 |
-
font-size:11px;font-weight:700;padding:
|
| 391 |
-
background:
|
| 392 |
-
}
|
| 393 |
-
.app-badge.fast{
|
| 394 |
-
background:rgba(249,115,22,.12);color:#fb923c;border:1px solid rgba(249,115,22,.22)
|
| 395 |
}
|
|
|
|
| 396 |
|
| 397 |
.app-toolbar{
|
| 398 |
-
background:
|
| 399 |
-
|
|
|
|
|
|
|
| 400 |
}
|
| 401 |
-
.tb-sep{width:1px;height:28px;background:
|
| 402 |
.modern-tb-btn{
|
| 403 |
display:inline-flex;align-items:center;justify-content:center;gap:6px;
|
| 404 |
-
min-width:32px;height:
|
| 405 |
-
|
| 406 |
-
font-
|
|
|
|
| 407 |
}
|
| 408 |
-
.modern-tb-btn:hover{background:
|
| 409 |
-
.modern-tb-btn:active,.modern-tb-btn.active{background:
|
| 410 |
-
.modern-tb-btn .tb-label
|
| 411 |
-
.modern-tb-btn .tb-svg{width:15px;height:15px;flex-shrink:0;color:#fff!important}
|
| 412 |
.modern-tb-btn .tb-svg,.modern-tb-btn .tb-svg *{stroke:#fff!important;fill:none!important}
|
| 413 |
-
.tb-info{font-family:'JetBrains Mono',monospace;font-size:12px;color:
|
| 414 |
-
|
| 415 |
-
.app-main-row{display:flex;gap:0;flex:1;overflow:hidden}
|
| 416 |
-
.app-main-left{flex:1;display:flex;flex-direction:column;min-width:0;border-right:1px solid #292524}
|
| 417 |
-
.app-main-right{width:460px;display:flex;flex-direction:column;flex-shrink:0;background:#18181b}
|
| 418 |
|
| 419 |
-
|
| 420 |
-
|
|
|
|
|
|
|
|
|
|
| 421 |
}
|
| 422 |
-
#image-drop-zone.drag-over{outline:2px solid #FF4500;outline-offset:-2px;background:rgba(255,69,0,.04)}
|
| 423 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 424 |
.upload-prompt-modern{
|
| 425 |
-
position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);z-index:20
|
| 426 |
}
|
| 427 |
.upload-click-area{
|
| 428 |
display:flex;flex-direction:column;align-items:center;justify-content:center;
|
| 429 |
-
cursor:pointer;padding:36px 52px;border:2px dashed #
|
| 430 |
-
background:rgba(255,69,0,.03);transition:all .2s ease;gap:8px;
|
| 431 |
}
|
| 432 |
-
.upload-click-area:hover{background:rgba(255,69,0,.08);border-color:
|
| 433 |
.upload-click-area:active{background:rgba(255,69,0,.12);transform:scale(.98)}
|
| 434 |
-
.upload-click-area svg{width:
|
| 435 |
-
.upload-main-text{color:
|
| 436 |
-
.upload-sub-text{color:#
|
| 437 |
|
| 438 |
-
.image-
|
| 439 |
-
|
| 440 |
}
|
| 441 |
-
.image-
|
| 442 |
-
.image-preview-card{
|
| 443 |
position:relative;
|
| 444 |
-
width:100%;
|
| 445 |
-
|
| 446 |
-
|
| 447 |
}
|
| 448 |
-
.image-
|
| 449 |
-
|
| 450 |
}
|
| 451 |
-
.image-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
display:flex;align-items:center;justify-content:center;transition:all .15s ease;
|
| 455 |
-
border:1px solid rgba(255,255,255,.1);
|
| 456 |
}
|
| 457 |
-
.image-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
|
| 459 |
.hint-bar{
|
| 460 |
-
background:rgba(255,69,0,.
|
| 461 |
-
|
|
|
|
| 462 |
}
|
| 463 |
-
.hint-bar b{color:#
|
| 464 |
.hint-bar kbd{
|
| 465 |
-
display:inline-block;padding:1px 6px;background:#
|
| 466 |
-
border-radius:4px;font-family:'JetBrains Mono',monospace;font-size:11px;color:
|
| 467 |
}
|
| 468 |
|
| 469 |
-
.examples-section{border-top:1px solid
|
| 470 |
-
.examples-title{
|
| 471 |
-
font-size:12px;font-weight:700;color:
|
| 472 |
-
letter-spacing:.8px;margin-bottom:10px;
|
| 473 |
}
|
|
|
|
| 474 |
.examples-scroll{display:flex;gap:10px;overflow-x:auto;padding-bottom:8px}
|
| 475 |
.examples-scroll::-webkit-scrollbar{height:6px}
|
| 476 |
.examples-scroll::-webkit-scrollbar-track{background:#09090b;border-radius:3px}
|
| 477 |
-
.examples-scroll::-webkit-scrollbar-thumb{background:#
|
| 478 |
-
.examples-scroll::-webkit-scrollbar-thumb:hover{background:#44403c}
|
| 479 |
.example-card{
|
| 480 |
-
flex-shrink:0;width:
|
| 481 |
-
|
| 482 |
}
|
| 483 |
-
.example-card:hover{border-color:
|
| 484 |
-
.example-card.loading{opacity:.
|
| 485 |
-
.example-thumbs{display:flex;height:
|
| 486 |
-
.example-thumbs img{width:100%;height:100%;object-fit:cover
|
| 487 |
.example-thumb-placeholder{
|
| 488 |
-
flex:1;display:flex;align-items:center;justify-content:center;
|
| 489 |
-
background:#18181b;color:#57534e;font-size:11px;min-width:0;
|
| 490 |
}
|
| 491 |
.example-meta{padding:6px 10px;display:flex;align-items:center;gap:6px}
|
| 492 |
.example-badge{
|
| 493 |
display:inline-flex;padding:2px 7px;background:rgba(255,69,0,.1);border-radius:4px;
|
| 494 |
-
font-size:10px;font-weight:700;color:#
|
| 495 |
}
|
| 496 |
.example-prompt-text{
|
| 497 |
-
padding:0 10px 8px;font-size:11px;color:
|
| 498 |
display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden;
|
| 499 |
}
|
| 500 |
|
| 501 |
-
.panel-card{border-bottom:1px solid
|
| 502 |
-
.panel-card-
|
| 503 |
-
|
| 504 |
-
text-transform:uppercase;letter-spacing:.8px;border-bottom:1px solid rgba(41,37,36,.7);
|
| 505 |
-
}
|
| 506 |
-
.panel-card-body{padding:16px 20px;display:flex;flex-direction:column;gap:8px}
|
| 507 |
-
.modern-label{font-size:13px;font-weight:600;color:#d6d3d1;margin-bottom:4px;display:block}
|
| 508 |
.modern-textarea{
|
| 509 |
-
width:100%;background:#09090b;border:1px solid
|
| 510 |
-
padding:
|
| 511 |
-
resize:none;outline:none;min-height:90px;transition:border-color .2s
|
| 512 |
}
|
| 513 |
-
.modern-textarea:focus{border-color:
|
| 514 |
-
.modern-textarea::placeholder{color:#
|
| 515 |
.modern-textarea.error-flash{
|
| 516 |
border-color:#ef4444!important;box-shadow:0 0 0 3px rgba(239,68,68,.2)!important;animation:shake .4s ease;
|
| 517 |
}
|
|
@@ -526,51 +568,48 @@ footer{display:none!important}
|
|
| 526 |
}
|
| 527 |
.toast-notification.visible{transform:translateX(-50%) translateY(0);opacity:1;pointer-events:auto}
|
| 528 |
.toast-notification.error{background:linear-gradient(135deg,#dc2626,#b91c1c);color:#fff;border:1px solid rgba(255,255,255,.15)}
|
| 529 |
-
.toast-notification.warning{background:linear-gradient(135deg,#
|
| 530 |
-
.toast-notification.info{background:linear-gradient(135deg,#
|
| 531 |
-
.toast-notification .toast-icon{font-size:16px;line-height:1}
|
| 532 |
-
.toast-notification .toast-text{line-height:1.3}
|
| 533 |
|
| 534 |
.btn-run{
|
| 535 |
display:flex;align-items:center;justify-content:center;gap:8px;width:100%;
|
| 536 |
-
background:linear-gradient(135deg,#
|
| 537 |
padding:12px 24px;cursor:pointer;font-size:15px;font-weight:700;font-family:'Inter',sans-serif;
|
| 538 |
color:#fff!important;transition:all .2s ease;letter-spacing:-.2px;
|
| 539 |
-
box-shadow:0 4px 16px rgba(255,69,0,.
|
| 540 |
}
|
| 541 |
.btn-run:hover{
|
| 542 |
-
background:linear-gradient(135deg,#
|
| 543 |
-
box-shadow:0 6px 24px rgba(255,69,0,.
|
| 544 |
}
|
| 545 |
-
.btn-run:active{transform:translateY(0)
|
| 546 |
-
.btn-run svg{width:18px;height:18px}
|
| 547 |
-
.btn-run svg *{fill:#fff!important;stroke:#fff!important}
|
| 548 |
|
| 549 |
-
.output-frame{
|
|
|
|
|
|
|
|
|
|
| 550 |
.output-frame .out-title{
|
| 551 |
-
padding:10px 20px;font-size:13px;font-weight:
|
| 552 |
-
text-transform:uppercase;letter-spacing:.8px;
|
| 553 |
-
|
| 554 |
}
|
| 555 |
.output-frame .out-body{
|
| 556 |
flex:1;background:#09090b;display:flex;align-items:stretch;justify-content:stretch;
|
| 557 |
-
overflow:hidden;min-height:
|
| 558 |
}
|
| 559 |
-
.output-
|
| 560 |
-
width:100%;height:100%;
|
|
|
|
| 561 |
}
|
| 562 |
-
.output-
|
| 563 |
-
width:100%;height:100%;
|
| 564 |
-
|
| 565 |
-
padding:12px 14px;color:#f5f5f4;
|
| 566 |
font-family:'JetBrains Mono',monospace;font-size:13px;line-height:1.55;
|
| 567 |
-
white-space:pre-wrap;
|
| 568 |
}
|
| 569 |
-
.output-
|
| 570 |
-
.output-
|
| 571 |
-
.output-scroll::-webkit-scrollbar-track{background:#111113}
|
| 572 |
-
.output-scroll::-webkit-scrollbar-thumb{background:#292524;border-radius:4px}
|
| 573 |
-
.output-scroll::-webkit-scrollbar-thumb:hover{background:#44403c}
|
| 574 |
|
| 575 |
.modern-loader{
|
| 576 |
display:none;position:absolute;top:0;left:0;right:0;bottom:0;background:rgba(9,9,11,.92);
|
|
@@ -578,129 +617,116 @@ footer{display:none!important}
|
|
| 578 |
}
|
| 579 |
.modern-loader.active{display:flex}
|
| 580 |
.modern-loader .loader-spinner{
|
| 581 |
-
width:36px;height:36px;border:3px solid #
|
| 582 |
border-radius:50%;animation:spin .8s linear infinite;
|
| 583 |
}
|
| 584 |
@keyframes spin{to{transform:rotate(360deg)}}
|
| 585 |
-
.modern-loader .loader-text{font-size:13px;color:
|
| 586 |
-
.loader-bar-track{width:200px;height:4px;background:#
|
| 587 |
.loader-bar-fill{
|
| 588 |
-
height:100%;background:linear-gradient(90deg,#FF4500,#
|
| 589 |
background-size:200% 100%;animation:shimmer 1.5s ease-in-out infinite;border-radius:2px;
|
| 590 |
}
|
| 591 |
@keyframes shimmer{0%{background-position:200% 0}100%{background-position:-200% 0}}
|
| 592 |
|
| 593 |
-
.settings-group{border:1px solid
|
| 594 |
.settings-group-title{
|
| 595 |
-
font-size:12px;font-weight:700;color:
|
| 596 |
-
padding:10px 16px;border-bottom:1px solid
|
| 597 |
}
|
| 598 |
.settings-group-body{padding:14px 16px;display:flex;flex-direction:column;gap:12px}
|
| 599 |
.slider-row{display:flex;align-items:center;gap:10px;min-height:28px}
|
| 600 |
-
.slider-row label{font-size:13px;font-weight:
|
| 601 |
.slider-row input[type="range"]{
|
| 602 |
-
flex:1;-webkit-appearance:none;appearance:none;height:6px;background:#
|
| 603 |
-
border-radius:3px;outline:none;min-width:0;
|
| 604 |
}
|
| 605 |
.slider-row input[type="range"]::-webkit-slider-thumb{
|
| 606 |
-
-webkit-appearance:none;width:16px;height:16px;background:linear-gradient(135deg,#
|
| 607 |
-
border-radius:50%;cursor:pointer;box-shadow:0 2px 6px rgba(255,69,0,.
|
| 608 |
}
|
| 609 |
-
.slider-row input[type="range"]::-webkit-slider-thumb:hover{transform:scale(1.2)}
|
| 610 |
.slider-row input[type="range"]::-moz-range-thumb{
|
| 611 |
-
width:16px;height:16px;background:linear-gradient(135deg,#
|
| 612 |
-
border-radius:50%;cursor:pointer;border:none;box-shadow:0 2px 6px rgba(255,69,0,.
|
| 613 |
}
|
| 614 |
.slider-row .slider-val{
|
| 615 |
-
min-width:
|
| 616 |
-
font-weight:
|
| 617 |
-
border-radius:6px;color:#f5f5f4;flex-shrink:0;
|
| 618 |
-
}
|
| 619 |
-
|
| 620 |
-
.segmented-wrap{
|
| 621 |
-
display:flex;gap:8px;flex-wrap:wrap;
|
| 622 |
-
}
|
| 623 |
-
.segmented-btn{
|
| 624 |
-
display:inline-flex;align-items:center;justify-content:center;
|
| 625 |
-
padding:7px 12px;border-radius:999px;border:1px solid #44403c;background:#09090b;
|
| 626 |
-
color:#d6d3d1;font-size:12px;font-weight:700;font-family:'JetBrains Mono',monospace;
|
| 627 |
-
cursor:pointer;transition:all .15s ease;
|
| 628 |
}
|
| 629 |
-
.
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
color:#fff;border-color:#FF4500;box-shadow:0 2px 10px rgba(255,69,0,.25);
|
| 633 |
}
|
|
|
|
| 634 |
|
| 635 |
-
.
|
| 636 |
-
|
|
|
|
|
|
|
|
|
|
| 637 |
}
|
| 638 |
-
.
|
| 639 |
-
|
| 640 |
-
padding:8px 12px;
|
| 641 |
-
|
| 642 |
}
|
| 643 |
-
.
|
| 644 |
-
|
| 645 |
-
background:
|
| 646 |
-
color:#fff;border-color:#FF4500;box-shadow:0 2px 10px rgba(255,69,0,.25);
|
| 647 |
}
|
|
|
|
| 648 |
|
| 649 |
.app-statusbar{
|
| 650 |
-
background:
|
| 651 |
display:flex;gap:12px;height:34px;align-items:center;font-size:12px;
|
| 652 |
}
|
| 653 |
.app-statusbar .sb-section{
|
| 654 |
padding:0 12px;flex:1;display:flex;align-items:center;font-family:'JetBrains Mono',monospace;
|
| 655 |
-
font-size:12px;color:#
|
| 656 |
}
|
| 657 |
.app-statusbar .sb-section.sb-fixed{
|
| 658 |
flex:0 0 auto;min-width:120px;text-align:center;justify-content:center;
|
| 659 |
-
padding:3px 12px;background:rgba(255,69,0,.08);border-radius:6px;color:#
|
| 660 |
}
|
| 661 |
|
| 662 |
-
.exp-note{padding:10px 20px;font-size:12px;color:#
|
| 663 |
-
.exp-note a{color:#
|
| 664 |
.exp-note a:hover{text-decoration:underline}
|
| 665 |
|
| 666 |
-
@media(max-width:
|
| 667 |
.app-main-row{flex-direction:column}
|
| 668 |
-
.app-main-right{width:100%}
|
| 669 |
-
.app-main-left{border-right:none;border-bottom:1px solid
|
| 670 |
}
|
| 671 |
"""
|
| 672 |
|
| 673 |
-
UPLOAD_SVG = '<svg class="tb-svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>'
|
| 674 |
-
CLEAR_SVG = '<svg class="tb-svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/><line x1="10" y1="11" x2="10" y2="17"/><line x1="14" y1="11" x2="14" y2="17"/></svg>'
|
| 675 |
-
OCR_RUN_SVG = '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M12 5c-5.6 0-9.5 5-9.5 7s3.9 7 9.5 7 9.5-5 9.5-7-3.9-7-9.5-7zm0 11a4 4 0 1 1 0-8 4 4 0 0 1 0 8z"/><circle cx="12" cy="12" r="1.8"/></svg>'
|
| 676 |
-
|
| 677 |
gallery_js = r"""
|
| 678 |
() => {
|
| 679 |
function init() {
|
| 680 |
-
if (window.
|
| 681 |
|
| 682 |
-
const dropZone = document.getElementById('
|
| 683 |
const uploadPrompt = document.getElementById('upload-prompt');
|
| 684 |
const uploadClick = document.getElementById('upload-click-area');
|
| 685 |
const fileInput = document.getElementById('custom-file-input');
|
| 686 |
-
const
|
| 687 |
-
const
|
| 688 |
-
const removeBtn = document.getElementById('image-remove-btn');
|
| 689 |
const btnUpload = document.getElementById('tb-upload');
|
| 690 |
const btnClear = document.getElementById('tb-clear');
|
| 691 |
-
const
|
| 692 |
const runBtnEl = document.getElementById('custom-run-btn');
|
| 693 |
-
const
|
| 694 |
-
const
|
|
|
|
|
|
|
| 695 |
|
| 696 |
-
if (!dropZone || !fileInput || !
|
| 697 |
setTimeout(init, 250);
|
| 698 |
return;
|
| 699 |
}
|
| 700 |
|
| 701 |
-
window.
|
| 702 |
|
| 703 |
-
let
|
| 704 |
let toastTimer = null;
|
| 705 |
|
| 706 |
function showToast(message, type) {
|
|
@@ -726,10 +752,10 @@ function init() {
|
|
| 726 |
}
|
| 727 |
window.__showToast = showToast;
|
| 728 |
|
| 729 |
-
function
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
setTimeout(() =>
|
| 733 |
}
|
| 734 |
|
| 735 |
function setGradioValue(containerId, value) {
|
|
@@ -741,104 +767,138 @@ function init() {
|
|
| 741 |
const ns = Object.getOwnPropertyDescriptor(proto, 'value');
|
| 742 |
if (ns && ns.set) {
|
| 743 |
ns.set.call(el, value);
|
| 744 |
-
el.dispatchEvent(new Event('input', {bubbles:true, composed:true}));
|
| 745 |
-
el.dispatchEvent(new Event('change', {bubbles:true, composed:true}));
|
| 746 |
}
|
| 747 |
});
|
| 748 |
}
|
| 749 |
window.__setGradioValue = setGradioValue;
|
| 750 |
|
| 751 |
-
function
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
uploadPrompt.style.display = 'none';
|
| 755 |
-
previewWrap.classList.add('visible');
|
| 756 |
-
previewImg.src = currentImage.b64;
|
| 757 |
-
if (imgStatus) imgStatus.textContent = '1 image loaded';
|
| 758 |
-
if (sbImage) sbImage.textContent = currentImage.name ? currentImage.name : '1 image uploaded';
|
| 759 |
-
} else {
|
| 760 |
-
uploadPrompt.style.display = '';
|
| 761 |
-
previewWrap.classList.remove('visible');
|
| 762 |
-
previewImg.removeAttribute('src');
|
| 763 |
-
if (imgStatus) imgStatus.textContent = 'No image';
|
| 764 |
-
if (sbImage) sbImage.textContent = 'No image uploaded';
|
| 765 |
-
}
|
| 766 |
}
|
| 767 |
|
| 768 |
-
function
|
| 769 |
-
setGradioValue('
|
| 770 |
}
|
| 771 |
|
| 772 |
-
function
|
| 773 |
-
setGradioValue('
|
| 774 |
}
|
| 775 |
|
| 776 |
-
function
|
| 777 |
-
|
| 778 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 779 |
syncImageToGradio();
|
| 780 |
}
|
| 781 |
-
window.
|
| 782 |
|
| 783 |
function clearImage() {
|
| 784 |
-
|
| 785 |
-
|
| 786 |
syncImageToGradio();
|
| 787 |
}
|
| 788 |
-
window.
|
| 789 |
|
| 790 |
function processFiles(files) {
|
| 791 |
-
const file = Array.from(files
|
| 792 |
if (!file) {
|
| 793 |
showToast('Please upload a valid image file', 'error');
|
| 794 |
return;
|
| 795 |
}
|
| 796 |
const reader = new FileReader();
|
| 797 |
-
reader.onload = (e) =>
|
| 798 |
reader.readAsDataURL(file);
|
| 799 |
}
|
| 800 |
|
| 801 |
-
fileInput.addEventListener('change', (e) => {
|
| 802 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 803 |
if (btnUpload) btnUpload.addEventListener('click', () => fileInput.click());
|
| 804 |
if (btnClear) btnClear.addEventListener('click', clearImage);
|
| 805 |
-
if (removeBtn) removeBtn.addEventListener('click', clearImage);
|
| 806 |
|
| 807 |
-
dropZone.addEventListener('dragover', (e) => {
|
| 808 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 809 |
dropZone.addEventListener('drop', (e) => {
|
| 810 |
e.preventDefault();
|
| 811 |
dropZone.classList.remove('drag-over');
|
| 812 |
if (e.dataTransfer.files.length) processFiles(e.dataTransfer.files);
|
| 813 |
});
|
| 814 |
|
| 815 |
-
|
|
|
|
| 816 |
|
| 817 |
-
window.
|
| 818 |
-
|
| 819 |
-
|
| 820 |
};
|
| 821 |
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
}
|
| 828 |
|
| 829 |
-
document.querySelectorAll('.
|
| 830 |
-
|
| 831 |
-
|
|
|
|
|
|
|
|
|
|
| 832 |
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
btn.classList.toggle('active', btn.getAttribute('data-value') === value);
|
| 836 |
-
});
|
| 837 |
-
setGradioValue('gpu-duration-input', value);
|
| 838 |
-
}
|
| 839 |
|
| 840 |
-
|
| 841 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 842 |
});
|
| 843 |
|
| 844 |
function syncSlider(customId, gradioId) {
|
|
@@ -853,8 +913,8 @@ function init() {
|
|
| 853 |
const ns = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value');
|
| 854 |
if (ns && ns.set) {
|
| 855 |
ns.set.call(el, slider.value);
|
| 856 |
-
el.dispatchEvent(new Event('input', {bubbles:true, composed:true}));
|
| 857 |
-
el.dispatchEvent(new Event('change', {bubbles:true, composed:true}));
|
| 858 |
}
|
| 859 |
});
|
| 860 |
});
|
|
@@ -866,6 +926,20 @@ function init() {
|
|
| 866 |
syncSlider('custom-top-k', 'gradio-top-k');
|
| 867 |
syncSlider('custom-repetition-penalty', 'gradio-repetition-penalty');
|
| 868 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 869 |
function showLoader() {
|
| 870 |
const l = document.getElementById('output-loader');
|
| 871 |
if (l) l.classList.add('active');
|
|
@@ -873,81 +947,65 @@ function init() {
|
|
| 873 |
if (sb) sb.textContent = 'Processing...';
|
| 874 |
}
|
| 875 |
|
| 876 |
-
function hideLoader() {
|
| 877 |
const l = document.getElementById('output-loader');
|
| 878 |
if (l) l.classList.remove('active');
|
| 879 |
const sb = document.querySelector('.sb-fixed');
|
| 880 |
-
if (sb) sb.textContent = 'Done';
|
| 881 |
}
|
| 882 |
|
| 883 |
window.__showLoader = showLoader;
|
| 884 |
window.__hideLoader = hideLoader;
|
| 885 |
|
| 886 |
-
function
|
| 887 |
-
|
| 888 |
-
|
|
|
|
| 889 |
|
| 890 |
-
|
|
|
|
|
|
|
|
|
|
| 891 |
showToast('Please upload an image and enter a query', 'error');
|
| 892 |
-
|
| 893 |
return false;
|
| 894 |
}
|
| 895 |
if (!hasImage) {
|
| 896 |
showToast('Please upload an image', 'error');
|
| 897 |
return false;
|
| 898 |
}
|
| 899 |
-
if (!
|
| 900 |
showToast('Please enter a query', 'warning');
|
| 901 |
-
|
| 902 |
-
return false;
|
| 903 |
-
}
|
| 904 |
-
|
| 905 |
-
if (queryVal.length > 4000) {
|
| 906 |
-
showToast('Query is too long', 'warning');
|
| 907 |
-
flashQueryError();
|
| 908 |
return false;
|
| 909 |
}
|
| 910 |
-
|
| 911 |
return true;
|
| 912 |
}
|
| 913 |
|
| 914 |
window.__clickGradioRunBtn = function() {
|
| 915 |
if (!validateBeforeRun()) return;
|
| 916 |
-
|
| 917 |
syncImageToGradio();
|
|
|
|
|
|
|
| 918 |
showLoader();
|
| 919 |
setTimeout(() => {
|
| 920 |
const gradioBtn = document.getElementById('gradio-run-btn');
|
| 921 |
if (!gradioBtn) return;
|
| 922 |
const btn = gradioBtn.querySelector('button');
|
| 923 |
if (btn) btn.click(); else gradioBtn.click();
|
| 924 |
-
},
|
| 925 |
};
|
| 926 |
|
| 927 |
-
if (runBtnEl)
|
| 928 |
-
|
| 929 |
-
|
| 930 |
-
card.addEventListener('click', () => {
|
| 931 |
-
const idx = card.getAttribute('data-idx');
|
| 932 |
-
document.querySelectorAll('.example-card.loading').forEach(c => c.classList.remove('loading'));
|
| 933 |
-
card.classList.add('loading');
|
| 934 |
-
showToast('Loading example...', 'info');
|
| 935 |
-
setGradioValue('example-result-data', '');
|
| 936 |
-
setGradioValue('example-idx-input', idx);
|
| 937 |
-
setTimeout(() => {
|
| 938 |
-
const btn = document.getElementById('example-load-btn');
|
| 939 |
-
if (btn) {
|
| 940 |
-
const b = btn.querySelector('button');
|
| 941 |
-
if (b) b.click(); else btn.click();
|
| 942 |
-
}
|
| 943 |
-
}, 120);
|
| 944 |
-
setTimeout(() => card.classList.remove('loading'), 8000);
|
| 945 |
-
});
|
| 946 |
-
});
|
| 947 |
|
| 948 |
-
|
| 949 |
-
|
| 950 |
-
|
|
|
|
|
|
|
| 951 |
}
|
| 952 |
init();
|
| 953 |
}
|
|
@@ -955,37 +1013,48 @@ init();
|
|
| 955 |
|
| 956 |
wire_outputs_js = r"""
|
| 957 |
() => {
|
| 958 |
-
function
|
| 959 |
const resultContainer = document.getElementById('gradio-raw-output');
|
| 960 |
-
const
|
| 961 |
-
if (!resultContainer || !
|
|
|
|
|
|
|
|
|
|
| 962 |
|
| 963 |
-
function
|
| 964 |
-
const
|
| 965 |
-
const
|
| 966 |
-
const
|
| 967 |
-
if (!
|
| 968 |
-
|
| 969 |
-
|
| 970 |
-
|
| 971 |
-
|
| 972 |
-
|
| 973 |
-
|
| 974 |
-
|
| 975 |
-
|
| 976 |
-
|
| 977 |
}
|
| 978 |
}
|
| 979 |
|
| 980 |
-
const observer = new MutationObserver(
|
| 981 |
-
observer.observe(resultContainer, {
|
| 982 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 983 |
}
|
| 984 |
-
|
| 985 |
|
| 986 |
function watchExampleResults() {
|
| 987 |
const container = document.getElementById('example-result-data');
|
| 988 |
-
if (!container) {
|
|
|
|
|
|
|
|
|
|
| 989 |
|
| 990 |
let lastProcessed = '';
|
| 991 |
|
|
@@ -993,318 +1062,328 @@ function watchExampleResults() {
|
|
| 993 |
const el = container.querySelector('textarea') || container.querySelector('input');
|
| 994 |
if (!el) return;
|
| 995 |
const val = el.value;
|
| 996 |
-
if (!val || val === lastProcessed || val.length <
|
| 997 |
|
| 998 |
try {
|
| 999 |
const data = JSON.parse(val);
|
| 1000 |
if (data.status === 'ok' && data.image) {
|
| 1001 |
lastProcessed = val;
|
| 1002 |
-
|
| 1003 |
-
if (window.
|
| 1004 |
-
if (window.
|
|
|
|
|
|
|
| 1005 |
document.querySelectorAll('.example-card.loading').forEach(c => c.classList.remove('loading'));
|
| 1006 |
if (window.__showToast) window.__showToast('Example loaded', 'info');
|
| 1007 |
} else if (data.status === 'error') {
|
| 1008 |
document.querySelectorAll('.example-card.loading').forEach(c => c.classList.remove('loading'));
|
| 1009 |
-
if (window.__showToast) window.__showToast('Could not load example', 'error');
|
| 1010 |
}
|
| 1011 |
-
} catch(e) {
|
| 1012 |
console.error('Example parse error:', e);
|
| 1013 |
}
|
| 1014 |
}
|
| 1015 |
|
| 1016 |
const obs = new MutationObserver(checkResult);
|
| 1017 |
-
obs.observe(container, {childList:true, subtree:true, characterData:true, attributes:true});
|
| 1018 |
setInterval(checkResult, 500);
|
| 1019 |
}
|
| 1020 |
watchExampleResults();
|
| 1021 |
|
| 1022 |
-
function
|
| 1023 |
-
const
|
| 1024 |
-
|
| 1025 |
-
|
| 1026 |
-
|
| 1027 |
-
|
| 1028 |
-
['gradio-repetition-penalty', 'custom-repetition-penalty', 'custom-repetition-penalty-val']
|
| 1029 |
-
];
|
| 1030 |
-
|
| 1031 |
-
function syncOne(gradioId, customId, valId) {
|
| 1032 |
-
const src = document.getElementById(gradioId);
|
| 1033 |
-
const dst = document.getElementById(customId);
|
| 1034 |
-
const v = document.getElementById(valId);
|
| 1035 |
-
if (!src || !dst) return;
|
| 1036 |
-
const el = src.querySelector('input[type="range"],input[type="number"]');
|
| 1037 |
-
if (el && typeof el.value !== 'undefined') {
|
| 1038 |
-
dst.value = el.value;
|
| 1039 |
-
if (v) v.textContent = el.value;
|
| 1040 |
}
|
| 1041 |
-
}
|
| 1042 |
-
|
| 1043 |
-
setInterval(() => {
|
| 1044 |
-
pairs.forEach(p => syncOne(p[0], p[1], p[2]));
|
| 1045 |
-
}, 1200);
|
| 1046 |
}
|
| 1047 |
-
|
| 1048 |
}
|
| 1049 |
"""
|
| 1050 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1051 |
|
| 1052 |
-
|
| 1053 |
-
|
| 1054 |
-
return None
|
| 1055 |
-
try:
|
| 1056 |
-
if b64_str.startswith("data:image"):
|
| 1057 |
-
_, data = b64_str.split(",", 1)
|
| 1058 |
-
else:
|
| 1059 |
-
data = b64_str
|
| 1060 |
-
image_data = base64.b64decode(data)
|
| 1061 |
-
return Image.open(BytesIO(image_data)).convert("RGB")
|
| 1062 |
-
except Exception as e:
|
| 1063 |
-
raise gr.Error(f"Invalid image data: {e}")
|
| 1064 |
|
|
|
|
|
|
|
| 1065 |
|
| 1066 |
-
|
| 1067 |
-
|
| 1068 |
-
image = decode_b64_to_pil(image_b64)
|
| 1069 |
|
| 1070 |
-
|
| 1071 |
-
|
| 1072 |
-
if not query or not str(query).strip():
|
| 1073 |
-
raise gr.Error("Please enter a query.")
|
| 1074 |
-
if model_name not in ["FireRed-OCR", "Nanonets-OCR-s", "MonkeyOCR-Recognition", "Thyme-RL", "Typhoon-OCR-7B", "SmolDocling-256M-preview"]:
|
| 1075 |
-
raise gr.Error("Please select a valid model.")
|
| 1076 |
-
|
| 1077 |
-
stream = generate_image(
|
| 1078 |
-
model_name=model_name,
|
| 1079 |
-
text=str(query).strip(),
|
| 1080 |
-
image=image,
|
| 1081 |
-
max_new_tokens=int(max_new_tokens),
|
| 1082 |
-
temperature=float(temperature),
|
| 1083 |
-
top_p=float(top_p),
|
| 1084 |
-
top_k=int(top_k),
|
| 1085 |
-
repetition_penalty=float(repetition_penalty),
|
| 1086 |
-
gpu_timeout=int(gpu_duration),
|
| 1087 |
-
)
|
| 1088 |
|
| 1089 |
-
|
| 1090 |
-
|
| 1091 |
-
last = chunk
|
| 1092 |
-
yield last
|
| 1093 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1094 |
|
| 1095 |
-
|
| 1096 |
-
|
| 1097 |
-
|
| 1098 |
-
|
| 1099 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1100 |
|
| 1101 |
-
|
| 1102 |
-
|
| 1103 |
-
|
| 1104 |
-
|
| 1105 |
-
|
|
|
|
| 1106 |
|
| 1107 |
-
|
|
|
|
|
|
|
|
|
|
| 1108 |
|
| 1109 |
-
|
| 1110 |
-
|
| 1111 |
-
|
|
|
|
|
|
|
| 1112 |
|
| 1113 |
-
|
| 1114 |
-
|
| 1115 |
-
|
| 1116 |
-
|
| 1117 |
-
|
| 1118 |
-
|
| 1119 |
-
<span class="app-badge">Vision Based</span>
|
| 1120 |
-
<span class="app-badge fast">OCR Suite</span>
|
| 1121 |
-
</div>
|
| 1122 |
-
</div>
|
| 1123 |
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
| 1128 |
-
<
|
| 1129 |
-
|
| 1130 |
-
</
|
| 1131 |
-
<
|
| 1132 |
-
<span id="tb-image-status" class="tb-info">No image</span>
|
| 1133 |
</div>
|
|
|
|
| 1134 |
|
| 1135 |
-
|
| 1136 |
-
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
<span class="upload-main-text">Click or drag an image here</span>
|
| 1146 |
-
<span class="upload-sub-text">Upload one image for OCR, document parsing, or visual text extraction across multiple OCR models</span>
|
| 1147 |
-
</div>
|
| 1148 |
-
</div>
|
| 1149 |
-
|
| 1150 |
-
<input id="custom-file-input" type="file" accept="image/*" style="display:none;" />
|
| 1151 |
|
| 1152 |
-
|
| 1153 |
-
|
| 1154 |
-
|
| 1155 |
-
|
| 1156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1157 |
</div>
|
| 1158 |
</div>
|
| 1159 |
-
|
| 1160 |
-
<div class="
|
| 1161 |
-
<
|
| 1162 |
-
<b>Query:</b> Describe the OCR task you want ·
|
| 1163 |
-
<kbd>Clear</kbd> removes the image
|
| 1164 |
</div>
|
|
|
|
| 1165 |
|
| 1166 |
-
|
| 1167 |
-
|
| 1168 |
-
|
| 1169 |
-
|
| 1170 |
-
</div>
|
| 1171 |
-
</div>
|
| 1172 |
</div>
|
| 1173 |
|
| 1174 |
-
<div class="
|
| 1175 |
-
<div class="
|
| 1176 |
-
|
| 1177 |
-
|
| 1178 |
-
<label class="modern-label" for="custom-query-input">Query</label>
|
| 1179 |
-
<textarea id="custom-query-input" class="modern-textarea" rows="4" placeholder="e.g., Perform OCR on the image precisely."></textarea>
|
| 1180 |
-
</div>
|
| 1181 |
</div>
|
|
|
|
|
|
|
| 1182 |
|
| 1183 |
-
|
| 1184 |
-
|
| 1185 |
-
|
| 1186 |
-
|
| 1187 |
-
|
| 1188 |
-
|
| 1189 |
-
|
| 1190 |
-
|
| 1191 |
-
|
| 1192 |
-
|
| 1193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1194 |
</div>
|
| 1195 |
</div>
|
|
|
|
| 1196 |
|
| 1197 |
-
|
| 1198 |
-
|
| 1199 |
-
|
| 1200 |
-
|
| 1201 |
-
|
| 1202 |
-
|
| 1203 |
-
<button class="segmented-btn" data-value="120">120</button>
|
| 1204 |
-
<button class="segmented-btn" data-value="180">180</button>
|
| 1205 |
-
<button class="segmented-btn" data-value="240">240</button>
|
| 1206 |
-
<button class="segmented-btn" data-value="300">300</button>
|
| 1207 |
-
</div>
|
| 1208 |
-
<div style="font-size:12px;color:#78716c;">Higher GPU duration allows longer processing but uses more GPU quota.</div>
|
| 1209 |
-
</div>
|
| 1210 |
-
</div>
|
| 1211 |
|
| 1212 |
-
|
| 1213 |
-
|
| 1214 |
-
|
| 1215 |
-
<span id="run-btn-label">Run OCR</span>
|
| 1216 |
-
</button>
|
| 1217 |
</div>
|
| 1218 |
-
|
| 1219 |
-
|
| 1220 |
-
|
| 1221 |
-
<
|
|
|
|
| 1222 |
</div>
|
| 1223 |
-
<div class="
|
| 1224 |
-
<
|
| 1225 |
-
<div class="loader-spinner"></div>
|
| 1226 |
-
<div class="loader-text">Processing OCR...</div>
|
| 1227 |
-
<div class="loader-bar-track"><div class="loader-bar-fill"></div></div>
|
| 1228 |
-
</div>
|
| 1229 |
-
<div class="output-html-wrap">
|
| 1230 |
-
<div id="raw-output-box" class="output-scroll placeholder">Raw OCR output will appear here</div>
|
| 1231 |
-
</div>
|
| 1232 |
</div>
|
| 1233 |
</div>
|
|
|
|
| 1234 |
|
| 1235 |
-
|
| 1236 |
-
|
| 1237 |
-
|
| 1238 |
-
|
| 1239 |
-
|
| 1240 |
-
|
| 1241 |
-
|
| 1242 |
-
|
| 1243 |
-
|
| 1244 |
-
|
| 1245 |
-
|
| 1246 |
-
|
| 1247 |
-
|
| 1248 |
-
|
| 1249 |
-
|
| 1250 |
-
|
| 1251 |
-
|
| 1252 |
-
|
| 1253 |
-
|
| 1254 |
-
|
| 1255 |
-
|
| 1256 |
-
|
| 1257 |
-
|
| 1258 |
-
|
| 1259 |
-
|
| 1260 |
-
|
| 1261 |
-
|
| 1262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1263 |
</div>
|
| 1264 |
</div>
|
| 1265 |
</div>
|
| 1266 |
</div>
|
|
|
|
| 1267 |
|
| 1268 |
-
|
| 1269 |
-
|
| 1270 |
-
|
| 1271 |
|
| 1272 |
-
|
| 1273 |
-
|
| 1274 |
-
|
| 1275 |
-
</div>
|
| 1276 |
</div>
|
| 1277 |
-
|
|
|
|
| 1278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1279 |
run_btn = gr.Button("Run", elem_id="gradio-run-btn")
|
| 1280 |
|
|
|
|
|
|
|
|
|
|
| 1281 |
demo.load(fn=None, js=gallery_js)
|
| 1282 |
demo.load(fn=None, js=wire_outputs_js)
|
| 1283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1284 |
run_btn.click(
|
| 1285 |
-
fn=
|
| 1286 |
inputs=[
|
| 1287 |
model_choice,
|
| 1288 |
-
|
| 1289 |
hidden_image_b64,
|
| 1290 |
max_new_tokens,
|
| 1291 |
temperature,
|
| 1292 |
top_p,
|
| 1293 |
top_k,
|
| 1294 |
repetition_penalty,
|
| 1295 |
-
|
| 1296 |
],
|
| 1297 |
-
outputs=[
|
| 1298 |
-
js=r"""(m,
|
| 1299 |
-
const
|
| 1300 |
-
const
|
| 1301 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1302 |
let imgVal = img;
|
| 1303 |
-
|
| 1304 |
-
|
| 1305 |
-
|
|
|
|
| 1306 |
}
|
| 1307 |
-
return [
|
| 1308 |
}""",
|
| 1309 |
)
|
| 1310 |
|
|
|
|
| 3 |
import re
|
| 4 |
import ast
|
| 5 |
import json
|
| 6 |
+
import time
|
| 7 |
+
import uuid
|
| 8 |
+
import html
|
| 9 |
import base64
|
| 10 |
+
import random
|
| 11 |
from io import BytesIO
|
| 12 |
from threading import Thread
|
| 13 |
+
from typing import Iterable
|
| 14 |
|
| 15 |
import gradio as gr
|
| 16 |
import spaces
|
|
|
|
| 20 |
from transformers import (
|
| 21 |
Qwen2_5_VLForConditionalGeneration,
|
| 22 |
Qwen3VLForConditionalGeneration,
|
|
|
|
| 23 |
AutoModelForVision2Seq,
|
| 24 |
+
AutoProcessor,
|
| 25 |
TextIteratorStreamer,
|
| 26 |
)
|
| 27 |
|
|
|
|
| 31 |
MAX_MAX_NEW_TOKENS = 4096
|
| 32 |
DEFAULT_MAX_NEW_TOKENS = 2048
|
| 33 |
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
| 34 |
+
MAX_SEED = 2147483647
|
| 35 |
|
| 36 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 37 |
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
|
|
|
| 46 |
torch.cuda.empty_cache()
|
| 47 |
|
| 48 |
|
| 49 |
+
def pil_to_data_url(img: Image.Image, fmt="PNG"):
|
| 50 |
+
buf = BytesIO()
|
| 51 |
+
img.save(buf, format=fmt)
|
| 52 |
+
mime = "image/png" if fmt.upper() == "PNG" else "image/jpeg"
|
| 53 |
+
return f"data:{mime};base64,{base64.b64encode(buf.getvalue()).decode()}"
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def encode_full_image(path):
|
| 57 |
+
if not os.path.exists(path):
|
| 58 |
+
return ""
|
| 59 |
+
try:
|
| 60 |
+
with open(path, "rb") as f:
|
| 61 |
+
data = f.read()
|
| 62 |
+
ext = path.rsplit(".", 1)[-1].lower()
|
| 63 |
+
mime = {"jpg": "image/jpeg", "jpeg": "image/jpeg", "png": "image/png", "webp": "image/webp"}.get(ext, "image/jpeg")
|
| 64 |
+
return f"data:{mime};base64,{base64.b64encode(data).decode()}"
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"Encode error for {path}: {e}")
|
| 67 |
+
return ""
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def make_thumb_b64(path, max_dim=220):
|
| 71 |
+
if not os.path.exists(path):
|
| 72 |
+
return ""
|
| 73 |
+
try:
|
| 74 |
+
img = Image.open(path).convert("RGB")
|
| 75 |
+
resampling = getattr(Image, "Resampling", Image)
|
| 76 |
+
img.thumbnail((max_dim, max_dim), resampling.LANCZOS)
|
| 77 |
+
buf = BytesIO()
|
| 78 |
+
img.save(buf, format="JPEG", quality=70)
|
| 79 |
+
return f"data:image/jpeg;base64,{base64.b64encode(buf.getvalue()).decode()}"
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"Thumbnail error for {path}: {e}")
|
| 82 |
+
return ""
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def b64_to_pil(data_url):
|
| 86 |
+
if not data_url:
|
| 87 |
+
return None
|
| 88 |
+
try:
|
| 89 |
+
if data_url.startswith("data:image"):
|
| 90 |
+
_, data = data_url.split(",", 1)
|
| 91 |
+
else:
|
| 92 |
+
data = data_url
|
| 93 |
+
image_data = base64.b64decode(data)
|
| 94 |
+
return Image.open(BytesIO(image_data)).convert("RGB")
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print(f"Decode error: {e}")
|
| 97 |
+
return None
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def add_random_padding(image, min_percent=0.1, max_percent=0.10):
|
| 101 |
+
image = image.convert("RGB")
|
| 102 |
+
width, height = image.size
|
| 103 |
+
pad_w_percent = random.uniform(min_percent, max_percent)
|
| 104 |
+
pad_h_percent = random.uniform(min_percent, max_percent)
|
| 105 |
+
pad_w = int(width * pad_w_percent)
|
| 106 |
+
pad_h = int(height * pad_h_percent)
|
| 107 |
+
corner_pixel = image.getpixel((0, 0))
|
| 108 |
+
padded_image = ImageOps.expand(image, border=(pad_w, pad_h, pad_w, pad_h), fill=corner_pixel)
|
| 109 |
+
return padded_image
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def normalize_values(text, target_max=500):
|
| 113 |
+
def normalize_list(values):
|
| 114 |
+
max_value = max(values) if values else 1
|
| 115 |
+
return [round((v / max_value) * target_max) for v in values]
|
| 116 |
+
|
| 117 |
+
def process_match(match):
|
| 118 |
+
num_list = ast.literal_eval(match.group(0))
|
| 119 |
+
normalized = normalize_list(num_list)
|
| 120 |
+
return "".join([f"<loc_{num}>" for num in normalized])
|
| 121 |
+
|
| 122 |
+
pattern = r"\[([\d\.\s,]+)\]"
|
| 123 |
+
normalized_text = re.sub(pattern, process_match, text)
|
| 124 |
+
return normalized_text
|
| 125 |
+
|
| 126 |
+
|
| 127 |
def apply_gpu_duration(val: str):
|
| 128 |
try:
|
| 129 |
+
return int(val)
|
| 130 |
except Exception:
|
| 131 |
return 60
|
| 132 |
|
|
|
|
| 191 |
).to(device).eval()
|
| 192 |
|
| 193 |
|
| 194 |
+
EXAMPLES_CONFIG = [
|
| 195 |
+
{"prompt": "Generate Markdown", "image": "examples/6.jpg"},
|
| 196 |
+
{"prompt": "Perform OCR on the image precisely.", "image": "examples/5.jpg"},
|
| 197 |
+
{"prompt": "Run OCR on the image and ensure high accuracy.", "image": "examples/4.jpg"},
|
| 198 |
+
{"prompt": "Conduct OCR on the image with exact text recognition.", "image": "examples/2.jpg"},
|
| 199 |
+
{"prompt": "Perform precise OCR extraction on the image.", "image": "examples/1.jpg"},
|
| 200 |
+
{"prompt": "Convert this page to docling", "image": "examples/3.jpg"},
|
| 201 |
+
]
|
|
|
|
|
|
|
| 202 |
|
| 203 |
|
| 204 |
+
def build_example_cards_html():
|
| 205 |
+
cards = ""
|
| 206 |
+
for i, ex in enumerate(EXAMPLES_CONFIG):
|
| 207 |
+
thumb = make_thumb_b64(ex["image"])
|
| 208 |
+
prompt_short = html.escape(ex["prompt"][:90])
|
| 209 |
+
if len(ex["prompt"]) > 90:
|
| 210 |
+
prompt_short += "..."
|
| 211 |
+
image_html = f'<img src="{thumb}" alt="">' if thumb else '<div class="example-thumb-placeholder">Preview</div>'
|
| 212 |
+
cards += f"""
|
| 213 |
+
<div class="example-card" data-idx="{i}">
|
| 214 |
+
<div class="example-thumbs">{image_html}</div>
|
| 215 |
+
<div class="example-meta"><span class="example-badge">Example</span></div>
|
| 216 |
+
<div class="example-prompt-text">{prompt_short}</div>
|
| 217 |
+
</div>
|
| 218 |
+
"""
|
| 219 |
+
return cards
|
| 220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
+
def load_example_data(idx_str):
|
| 223 |
+
try:
|
| 224 |
+
idx = int(float(idx_str)) if idx_str and str(idx_str).strip() else -1
|
| 225 |
+
except Exception:
|
| 226 |
+
idx = -1
|
| 227 |
|
| 228 |
+
if idx < 0 or idx >= len(EXAMPLES_CONFIG):
|
| 229 |
+
return json.dumps({"image": "", "prompt": "", "status": "error"})
|
| 230 |
|
| 231 |
+
ex = EXAMPLES_CONFIG[idx]
|
| 232 |
+
b64 = encode_full_image(ex["image"])
|
| 233 |
+
if not b64:
|
| 234 |
+
return json.dumps({"image": "", "prompt": "", "status": "error"})
|
| 235 |
+
|
| 236 |
+
return json.dumps({"image": b64, "prompt": ex["prompt"], "status": "ok"})
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def calc_timeout_image(model_name, text, image_b64, max_new_tokens, temperature, top_p, top_k, repetition_penalty, gpu_timeout):
|
| 240 |
try:
|
| 241 |
return int(gpu_timeout)
|
| 242 |
except Exception:
|
| 243 |
return 60
|
| 244 |
|
| 245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
@spaces.GPU(duration=calc_timeout_image)
|
| 247 |
+
def generate_image(
|
| 248 |
+
model_name: str,
|
| 249 |
+
text: str,
|
| 250 |
+
image_b64: str,
|
| 251 |
+
max_new_tokens: int = 1024,
|
| 252 |
+
temperature: float = 0.6,
|
| 253 |
+
top_p: float = 0.9,
|
| 254 |
+
top_k: int = 50,
|
| 255 |
+
repetition_penalty: float = 1.2,
|
| 256 |
+
gpu_timeout: int = 60,
|
| 257 |
+
):
|
| 258 |
gc.collect()
|
| 259 |
safe_empty_cache()
|
| 260 |
|
| 261 |
+
model_map = {
|
| 262 |
+
"FireRed-OCR": (processor_f, model_f),
|
| 263 |
+
"Nanonets-OCR-s": (processor_m, model_m),
|
| 264 |
+
"MonkeyOCR-Recognition": (processor_g, model_g),
|
| 265 |
+
"Thyme-RL": (processor_n, model_n),
|
| 266 |
+
"Typhoon-OCR-7B": (processor_l, model_l),
|
| 267 |
+
"SmolDocling-256M-preview": (processor_x, model_x),
|
| 268 |
+
}
|
| 269 |
|
| 270 |
+
if model_name not in model_map:
|
| 271 |
+
raise gr.Error("Invalid model selected.")
|
|
|
|
| 272 |
|
| 273 |
+
if not text or not text.strip():
|
| 274 |
+
raise gr.Error("Please enter a query.")
|
| 275 |
+
if len(text.strip()) > MAX_INPUT_TOKEN_LENGTH * 8:
|
| 276 |
+
raise gr.Error("Your query is too long.")
|
| 277 |
+
if not image_b64 or not str(image_b64).strip():
|
| 278 |
+
raise gr.Error("Please upload an image.")
|
| 279 |
|
| 280 |
+
image = b64_to_pil(image_b64)
|
| 281 |
+
if image is None:
|
| 282 |
+
raise gr.Error("Uploaded image is invalid or could not be decoded.")
|
| 283 |
+
|
| 284 |
+
processor, model = model_map[model_name]
|
| 285 |
+
images = [image]
|
| 286 |
+
text = text.strip()
|
| 287 |
|
| 288 |
if model_name == "SmolDocling-256M-preview":
|
| 289 |
if "OTSL" in text or "code" in text:
|
|
|
|
| 291 |
if "OCR at text at" in text or "Identify element" in text or "formula" in text:
|
| 292 |
text = normalize_values(text, target_max=500)
|
| 293 |
|
| 294 |
+
messages = [
|
| 295 |
+
{
|
| 296 |
+
"role": "user",
|
| 297 |
+
"content": [{"type": "image"} for _ in images] + [{"type": "text", "text": text}],
|
| 298 |
+
}
|
| 299 |
+
]
|
| 300 |
+
|
| 301 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 303 |
inputs = processor(text=prompt, images=images, return_tensors="pt").to(device)
|
| 304 |
|
|
|
|
| 326 |
if any(tag in cleaned_output for tag in ["<doctag>", "<otsl>", "<code>", "<chart>", "<formula>"]):
|
| 327 |
if "<chart>" in cleaned_output:
|
| 328 |
cleaned_output = cleaned_output.replace("<chart>", "<otsl>").replace("</chart>", "</otsl>")
|
| 329 |
+
cleaned_output = re.sub(r"(<loc_500>)(?!.*<loc_500>)<[^>]+>", r"\1", cleaned_output)
|
| 330 |
doctags_doc = DocTagsDocument.from_doctags_and_image_pairs([cleaned_output], images)
|
| 331 |
doc = DoclingDocument.load_from_doctags(doctags_doc, document_name="Document")
|
| 332 |
markdown_output = doc.export_to_markdown()
|
| 333 |
yield markdown_output
|
| 334 |
else:
|
| 335 |
yield cleaned_output
|
| 336 |
+
|
|
|
|
| 337 |
finally:
|
| 338 |
gc.collect()
|
| 339 |
safe_empty_cache()
|
| 340 |
|
| 341 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
EXAMPLE_CARDS_HTML = build_example_cards_html()
|
| 343 |
|
| 344 |
+
FIRE_VISION_LOGO = """
|
| 345 |
<svg viewBox="0 0 64 64" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
|
| 346 |
<defs>
|
| 347 |
<linearGradient id="visionGrad" x1="0%" y1="0%" x2="100%" y2="100%">
|
| 348 |
+
<stop offset="0%" stop-color="#FF7A45"/>
|
| 349 |
<stop offset="100%" stop-color="#FF4500"/>
|
| 350 |
</linearGradient>
|
| 351 |
</defs>
|
| 352 |
+
<path d="M32 10C18 10 8.2 19 4 32c4.2 13 14 22 28 22s23.8-9 28-22C55.8 19 46 10 32 10Z" fill="none" stroke="url(#visionGrad)" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/>
|
| 353 |
+
<circle cx="32" cy="32" r="10" fill="none" stroke="url(#visionGrad)" stroke-width="4"/>
|
| 354 |
+
<circle cx="32" cy="32" r="4" fill="#FF4500"/>
|
| 355 |
+
<path d="M49 15l4-4M15 49l-4 4M49 49l4 4M15 15l-4-4" stroke="#FFA07A" stroke-width="3" stroke-linecap="round"/>
|
|
|
|
|
|
|
| 356 |
</svg>
|
| 357 |
"""
|
| 358 |
|
| 359 |
+
DOWNLOAD_SVG = '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M12 16l-5-5h3V4h4v7h3l-5 5z"/><path d="M20 18H4v2h16v-2z"/></svg>'
|
| 360 |
+
UPLOAD_SVG = '<svg class="tb-svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>'
|
| 361 |
+
CLEAR_SVG = '<svg class="tb-svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>'
|
| 362 |
+
RUN_SVG = '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8 5v14l11-7z"/></svg>'
|
| 363 |
+
|
| 364 |
css = r"""
|
| 365 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&family=JetBrains+Mono:wght@400;500;600&display=swap');
|
| 366 |
|
| 367 |
+
:root{
|
| 368 |
+
--accent:#FF4500;
|
| 369 |
+
--accent-2:#FF6A33;
|
| 370 |
+
--accent-soft:rgba(255,69,0,.12);
|
| 371 |
+
--accent-soft-2:rgba(255,69,0,.18);
|
| 372 |
+
--bg:#0f0f13;
|
| 373 |
+
--panel:#18181b;
|
| 374 |
+
--panel-2:#09090b;
|
| 375 |
+
--border:#27272a;
|
| 376 |
+
--muted:#a1a1aa;
|
| 377 |
+
--muted-2:#71717a;
|
| 378 |
+
--text:#e4e4e7;
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
*{box-sizing:border-box;margin:0;padding:0}
|
| 382 |
body,.gradio-container{
|
| 383 |
+
background:var(--bg)!important;
|
| 384 |
font-family:'Inter',system-ui,-apple-system,sans-serif!important;
|
| 385 |
font-size:14px!important;
|
| 386 |
+
color:var(--text)!important;
|
| 387 |
min-height:100vh;
|
| 388 |
}
|
| 389 |
footer{display:none!important}
|
| 390 |
.hidden-input{display:none!important;height:0!important;overflow:hidden!important;margin:0!important;padding:0!important}
|
| 391 |
+
#gradio-run-btn,#example-load-btn{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
position:absolute!important;left:-9999px!important;top:-9999px!important;
|
| 393 |
+
width:1px!important;height:1px!important;opacity:.01!important;pointer-events:none!important;overflow:hidden!important;
|
|
|
|
| 394 |
}
|
| 395 |
|
| 396 |
.app-shell{
|
| 397 |
+
background:var(--panel);
|
| 398 |
+
border:1px solid var(--border);
|
| 399 |
border-radius:18px;
|
| 400 |
margin:12px auto;
|
| 401 |
+
max-width:1450px;
|
| 402 |
overflow:hidden;
|
| 403 |
+
box-shadow:0 25px 50px -12px rgba(0,0,0,.6),0 0 0 1px rgba(255,255,255,.03);
|
| 404 |
}
|
| 405 |
.app-header{
|
| 406 |
+
background:linear-gradient(135deg,#18181b,#1e1e24);
|
| 407 |
+
border-bottom:1px solid var(--border);
|
| 408 |
padding:14px 24px;
|
| 409 |
+
display:flex;
|
| 410 |
+
align-items:center;
|
| 411 |
+
justify-content:space-between;
|
| 412 |
+
gap:12px;
|
| 413 |
+
flex-wrap:wrap;
|
| 414 |
}
|
| 415 |
.app-header-left{display:flex;align-items:center;gap:12px}
|
| 416 |
.app-logo{
|
| 417 |
+
width:40px;height:40px;border-radius:12px;display:flex;align-items:center;justify-content:center;
|
| 418 |
+
background:linear-gradient(135deg,#FF7A45,#FF4500);
|
| 419 |
+
box-shadow:0 4px 14px rgba(255,69,0,.35);
|
|
|
|
|
|
|
| 420 |
}
|
| 421 |
+
.app-logo svg{width:24px;height:24px;display:block}
|
| 422 |
.app-title{
|
| 423 |
+
font-size:18px;font-weight:800;letter-spacing:-.3px;
|
| 424 |
+
background:linear-gradient(135deg,#fff,#cfcfd4);
|
| 425 |
-webkit-background-clip:text;-webkit-text-fill-color:transparent;
|
|
|
|
| 426 |
}
|
| 427 |
.app-badge{
|
| 428 |
+
font-size:11px;font-weight:700;padding:4px 10px;border-radius:999px;
|
| 429 |
+
background:var(--accent-soft);color:#FF8A5C;border:1px solid rgba(255,69,0,.28);letter-spacing:.3px;
|
|
|
|
|
|
|
|
|
|
| 430 |
}
|
| 431 |
+
.app-badge.fast{background:rgba(34,197,94,.12);color:#4ade80;border:1px solid rgba(34,197,94,.25)}
|
| 432 |
|
| 433 |
.app-toolbar{
|
| 434 |
+
background:var(--panel);
|
| 435 |
+
border-bottom:1px solid var(--border);
|
| 436 |
+
padding:8px 16px;
|
| 437 |
+
display:flex;gap:6px;align-items:center;flex-wrap:wrap;
|
| 438 |
}
|
| 439 |
+
.tb-sep{width:1px;height:28px;background:var(--border);margin:0 8px}
|
| 440 |
.modern-tb-btn{
|
| 441 |
display:inline-flex;align-items:center;justify-content:center;gap:6px;
|
| 442 |
+
min-width:32px;height:34px;
|
| 443 |
+
background:transparent;border:1px solid transparent;border-radius:8px;
|
| 444 |
+
cursor:pointer;font-size:13px;font-weight:600;padding:0 12px;
|
| 445 |
+
color:#fff!important;transition:all .15s ease;
|
| 446 |
}
|
| 447 |
+
.modern-tb-btn:hover{background:var(--accent-soft);border-color:rgba(255,69,0,.35)}
|
| 448 |
+
.modern-tb-btn:active,.modern-tb-btn.active{background:var(--accent-soft-2);border-color:rgba(255,69,0,.45)}
|
| 449 |
+
.modern-tb-btn .tb-label,.modern-tb-btn .tb-svg{color:#fff!important}
|
|
|
|
| 450 |
.modern-tb-btn .tb-svg,.modern-tb-btn .tb-svg *{stroke:#fff!important;fill:none!important}
|
| 451 |
+
.tb-info{font-family:'JetBrains Mono',monospace;font-size:12px;color:var(--muted-2);padding:0 8px;display:flex;align-items:center}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
|
| 453 |
+
.app-main-row{display:flex;gap:0;flex:1;overflow:hidden;min-height:760px}
|
| 454 |
+
.app-main-left{flex:1;display:flex;flex-direction:column;min-width:0;border-right:1px solid var(--border)}
|
| 455 |
+
.app-main-right{
|
| 456 |
+
width:460px;display:flex;flex-direction:column;flex-shrink:0;background:var(--panel);
|
| 457 |
+
max-height:calc(100vh - 120px);overflow:hidden;
|
| 458 |
}
|
|
|
|
| 459 |
|
| 460 |
+
#upload-drop-zone{
|
| 461 |
+
position:relative;background:var(--panel-2);min-height:360px;overflow:auto;
|
| 462 |
+
}
|
| 463 |
+
#upload-drop-zone.drag-over{
|
| 464 |
+
outline:2px solid var(--accent);outline-offset:-2px;background:rgba(255,69,0,.04);
|
| 465 |
+
}
|
| 466 |
.upload-prompt-modern{
|
| 467 |
+
position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);z-index:20;
|
| 468 |
}
|
| 469 |
.upload-click-area{
|
| 470 |
display:flex;flex-direction:column;align-items:center;justify-content:center;
|
| 471 |
+
cursor:pointer;padding:36px 52px;border:2px dashed #3f3f46;border-radius:16px;
|
| 472 |
+
background:rgba(255,69,0,.03);transition:all .2s ease;gap:8px;text-align:center;
|
| 473 |
}
|
| 474 |
+
.upload-click-area:hover{background:rgba(255,69,0,.08);border-color:var(--accent);transform:scale(1.03)}
|
| 475 |
.upload-click-area:active{background:rgba(255,69,0,.12);transform:scale(.98)}
|
| 476 |
+
.upload-click-area svg{width:80px;height:80px}
|
| 477 |
+
.upload-main-text{color:var(--muted);font-size:14px;font-weight:500;margin-top:4px}
|
| 478 |
+
.upload-sub-text{color:#52525b;font-size:12px}
|
| 479 |
|
| 480 |
+
.single-image-stage{
|
| 481 |
+
display:none;padding:16px;height:100%;align-items:flex-start;justify-content:center;
|
| 482 |
}
|
| 483 |
+
.single-image-card{
|
|
|
|
| 484 |
position:relative;
|
| 485 |
+
width:min(100%,780px);
|
| 486 |
+
background:#111114;border:1px solid var(--border);border-radius:14px;
|
| 487 |
+
overflow:hidden;
|
| 488 |
}
|
| 489 |
+
.single-image-card img{
|
| 490 |
+
display:block;width:100%;max-height:460px;object-fit:contain;background:#0b0b0e;
|
| 491 |
}
|
| 492 |
+
.single-image-meta{
|
| 493 |
+
display:flex;align-items:center;justify-content:space-between;gap:8px;
|
| 494 |
+
padding:10px 12px;border-top:1px solid var(--border);
|
|
|
|
|
|
|
| 495 |
}
|
| 496 |
+
.single-image-name{
|
| 497 |
+
color:var(--muted);font-size:12px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;
|
| 498 |
+
}
|
| 499 |
+
.single-image-remove{
|
| 500 |
+
display:inline-flex;align-items:center;justify-content:center;
|
| 501 |
+
height:30px;padding:0 12px;border-radius:8px;background:rgba(255,69,0,.08);
|
| 502 |
+
border:1px solid rgba(255,69,0,.18);color:#ff9b73;font-size:12px;font-weight:600;cursor:pointer;
|
| 503 |
+
}
|
| 504 |
+
.single-image-remove:hover{background:rgba(255,69,0,.16);color:#fff;border-color:rgba(255,69,0,.35)}
|
| 505 |
|
| 506 |
.hint-bar{
|
| 507 |
+
background:rgba(255,69,0,.06);
|
| 508 |
+
border-top:1px solid var(--border);border-bottom:1px solid var(--border);
|
| 509 |
+
padding:10px 20px;font-size:13px;color:var(--muted);line-height:1.7;
|
| 510 |
}
|
| 511 |
+
.hint-bar b{color:#ff9b73;font-weight:600}
|
| 512 |
.hint-bar kbd{
|
| 513 |
+
display:inline-block;padding:1px 6px;background:#27272a;border:1px solid #3f3f46;
|
| 514 |
+
border-radius:4px;font-family:'JetBrains Mono',monospace;font-size:11px;color:var(--muted);
|
| 515 |
}
|
| 516 |
|
| 517 |
+
.examples-section{border-top:1px solid var(--border);padding:12px 16px}
|
| 518 |
+
.examples-title,.suggestions-title,.panel-card-title{
|
| 519 |
+
font-size:12px;font-weight:700;color:var(--muted-2);text-transform:uppercase;letter-spacing:.8px;
|
|
|
|
| 520 |
}
|
| 521 |
+
.examples-title,.suggestions-title{margin-bottom:10px}
|
| 522 |
.examples-scroll{display:flex;gap:10px;overflow-x:auto;padding-bottom:8px}
|
| 523 |
.examples-scroll::-webkit-scrollbar{height:6px}
|
| 524 |
.examples-scroll::-webkit-scrollbar-track{background:#09090b;border-radius:3px}
|
| 525 |
+
.examples-scroll::-webkit-scrollbar-thumb{background:#27272a;border-radius:3px}
|
|
|
|
| 526 |
.example-card{
|
| 527 |
+
flex-shrink:0;width:210px;background:#09090b;border:1px solid var(--border);border-radius:10px;
|
| 528 |
+
overflow:hidden;cursor:pointer;transition:all .2s ease;
|
| 529 |
}
|
| 530 |
+
.example-card:hover{border-color:var(--accent);transform:translateY(-2px);box-shadow:0 4px 12px rgba(255,69,0,.15)}
|
| 531 |
+
.example-card.loading{opacity:.55;pointer-events:none}
|
| 532 |
+
.example-thumbs{display:flex;height:110px;overflow:hidden;background:#18181b}
|
| 533 |
+
.example-thumbs img{width:100%;height:100%;object-fit:cover}
|
| 534 |
.example-thumb-placeholder{
|
| 535 |
+
flex:1;display:flex;align-items:center;justify-content:center;background:#18181b;color:#3f3f46;font-size:11px;
|
|
|
|
| 536 |
}
|
| 537 |
.example-meta{padding:6px 10px;display:flex;align-items:center;gap:6px}
|
| 538 |
.example-badge{
|
| 539 |
display:inline-flex;padding:2px 7px;background:rgba(255,69,0,.1);border-radius:4px;
|
| 540 |
+
font-size:10px;font-weight:700;color:#ff8a5c;font-family:'JetBrains Mono',monospace;
|
| 541 |
}
|
| 542 |
.example-prompt-text{
|
| 543 |
+
padding:0 10px 8px;font-size:11px;color:var(--muted);line-height:1.4;
|
| 544 |
display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden;
|
| 545 |
}
|
| 546 |
|
| 547 |
+
.panel-card{border-bottom:1px solid var(--border)}
|
| 548 |
+
.panel-card-body{padding:16px 20px;display:flex;flex-direction:column;gap:10px}
|
| 549 |
+
.modern-label{font-size:13px;font-weight:500;color:var(--muted);margin-bottom:4px;display:block}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
.modern-textarea{
|
| 551 |
+
width:100%;background:#09090b;border:1px solid var(--border);border-radius:8px;
|
| 552 |
+
padding:10px 14px;font-family:'Inter',sans-serif;font-size:14px;color:var(--text);
|
| 553 |
+
resize:none;outline:none;min-height:90px;transition:border-color .2s;
|
| 554 |
}
|
| 555 |
+
.modern-textarea:focus{border-color:var(--accent);box-shadow:0 0 0 3px rgba(255,69,0,.15)}
|
| 556 |
+
.modern-textarea::placeholder{color:#3f3f46}
|
| 557 |
.modern-textarea.error-flash{
|
| 558 |
border-color:#ef4444!important;box-shadow:0 0 0 3px rgba(239,68,68,.2)!important;animation:shake .4s ease;
|
| 559 |
}
|
|
|
|
| 568 |
}
|
| 569 |
.toast-notification.visible{transform:translateX(-50%) translateY(0);opacity:1;pointer-events:auto}
|
| 570 |
.toast-notification.error{background:linear-gradient(135deg,#dc2626,#b91c1c);color:#fff;border:1px solid rgba(255,255,255,.15)}
|
| 571 |
+
.toast-notification.warning{background:linear-gradient(135deg,#d97706,#b45309);color:#fff;border:1px solid rgba(255,255,255,.15)}
|
| 572 |
+
.toast-notification.info{background:linear-gradient(135deg,#ea580c,#c2410c);color:#fff;border:1px solid rgba(255,255,255,.15)}
|
|
|
|
|
|
|
| 573 |
|
| 574 |
.btn-run{
|
| 575 |
display:flex;align-items:center;justify-content:center;gap:8px;width:100%;
|
| 576 |
+
background:linear-gradient(135deg,#FF4500,#E03E00);border:none;border-radius:10px;
|
| 577 |
padding:12px 24px;cursor:pointer;font-size:15px;font-weight:700;font-family:'Inter',sans-serif;
|
| 578 |
color:#fff!important;transition:all .2s ease;letter-spacing:-.2px;
|
| 579 |
+
box-shadow:0 4px 16px rgba(255,69,0,.3),inset 0 1px 0 rgba(255,255,255,.1);
|
| 580 |
}
|
| 581 |
.btn-run:hover{
|
| 582 |
+
background:linear-gradient(135deg,#FF6A33,#FF4500);transform:translateY(-1px);
|
| 583 |
+
box-shadow:0 6px 24px rgba(255,69,0,.42),inset 0 1px 0 rgba(255,255,255,.15);
|
| 584 |
}
|
| 585 |
+
.btn-run:active{transform:translateY(0)}
|
| 586 |
+
.btn-run svg{width:18px;height:18px;fill:#fff!important}
|
|
|
|
| 587 |
|
| 588 |
+
.output-frame{
|
| 589 |
+
border-bottom:1px solid var(--border);
|
| 590 |
+
display:flex;flex-direction:column;position:relative;min-height:0;flex:1;
|
| 591 |
+
}
|
| 592 |
.output-frame .out-title{
|
| 593 |
+
padding:10px 20px;font-size:13px;font-weight:700;color:#fff!important;
|
| 594 |
+
text-transform:uppercase;letter-spacing:.8px;border-bottom:1px solid rgba(39,39,42,.6);
|
| 595 |
+
display:flex;align-items:center;justify-content:space-between;
|
| 596 |
}
|
| 597 |
.output-frame .out-body{
|
| 598 |
flex:1;background:#09090b;display:flex;align-items:stretch;justify-content:stretch;
|
| 599 |
+
overflow:hidden;min-height:280px;position:relative;padding:14px;
|
| 600 |
}
|
| 601 |
+
.output-text-shell{
|
| 602 |
+
width:100%;height:100%;min-height:280px;max-height:420px;background:#0d0d10;border:1px solid var(--border);
|
| 603 |
+
border-radius:12px;overflow:hidden;display:flex;flex-direction:column;
|
| 604 |
}
|
| 605 |
+
.output-text-area{
|
| 606 |
+
width:100%;height:100%;min-height:280px;max-height:420px;
|
| 607 |
+
padding:14px 16px;border:none;outline:none;background:transparent;color:var(--text);
|
|
|
|
| 608 |
font-family:'JetBrains Mono',monospace;font-size:13px;line-height:1.55;
|
| 609 |
+
white-space:pre-wrap;overflow:auto;resize:none;
|
| 610 |
}
|
| 611 |
+
.output-text-area::placeholder{color:#52525b}
|
| 612 |
+
.output-text-area.error{color:#fca5a5}
|
|
|
|
|
|
|
|
|
|
| 613 |
|
| 614 |
.modern-loader{
|
| 615 |
display:none;position:absolute;top:0;left:0;right:0;bottom:0;background:rgba(9,9,11,.92);
|
|
|
|
| 617 |
}
|
| 618 |
.modern-loader.active{display:flex}
|
| 619 |
.modern-loader .loader-spinner{
|
| 620 |
+
width:36px;height:36px;border:3px solid #27272a;border-top-color:var(--accent);
|
| 621 |
border-radius:50%;animation:spin .8s linear infinite;
|
| 622 |
}
|
| 623 |
@keyframes spin{to{transform:rotate(360deg)}}
|
| 624 |
+
.modern-loader .loader-text{font-size:13px;color:var(--muted);font-weight:500}
|
| 625 |
+
.loader-bar-track{width:200px;height:4px;background:#27272a;border-radius:2px;overflow:hidden}
|
| 626 |
.loader-bar-fill{
|
| 627 |
+
height:100%;background:linear-gradient(90deg,#FF4500,#FF6A33,#FF4500);
|
| 628 |
background-size:200% 100%;animation:shimmer 1.5s ease-in-out infinite;border-radius:2px;
|
| 629 |
}
|
| 630 |
@keyframes shimmer{0%{background-position:200% 0}100%{background-position:-200% 0}}
|
| 631 |
|
| 632 |
+
.settings-group{border:1px solid var(--border);border-radius:10px;margin:12px 16px;padding:0;overflow:hidden}
|
| 633 |
.settings-group-title{
|
| 634 |
+
font-size:12px;font-weight:700;color:var(--muted-2);text-transform:uppercase;letter-spacing:.8px;
|
| 635 |
+
padding:10px 16px;border-bottom:1px solid var(--border);background:rgba(24,24,27,.5);
|
| 636 |
}
|
| 637 |
.settings-group-body{padding:14px 16px;display:flex;flex-direction:column;gap:12px}
|
| 638 |
.slider-row{display:flex;align-items:center;gap:10px;min-height:28px}
|
| 639 |
+
.slider-row label{font-size:13px;font-weight:500;color:var(--muted);min-width:86px;flex-shrink:0}
|
| 640 |
.slider-row input[type="range"]{
|
| 641 |
+
flex:1;-webkit-appearance:none;appearance:none;height:6px;background:#27272a;border-radius:3px;outline:none;min-width:0;
|
|
|
|
| 642 |
}
|
| 643 |
.slider-row input[type="range"]::-webkit-slider-thumb{
|
| 644 |
+
-webkit-appearance:none;width:16px;height:16px;background:linear-gradient(135deg,#FF4500,#E03E00);
|
| 645 |
+
border-radius:50%;cursor:pointer;box-shadow:0 2px 6px rgba(255,69,0,.4);
|
| 646 |
}
|
|
|
|
| 647 |
.slider-row input[type="range"]::-moz-range-thumb{
|
| 648 |
+
width:16px;height:16px;background:linear-gradient(135deg,#FF4500,#E03E00);
|
| 649 |
+
border-radius:50%;cursor:pointer;border:none;box-shadow:0 2px 6px rgba(255,69,0,.4);
|
| 650 |
}
|
| 651 |
.slider-row .slider-val{
|
| 652 |
+
min-width:64px;text-align:right;font-family:'JetBrains Mono',monospace;font-size:12px;
|
| 653 |
+
font-weight:500;padding:3px 8px;background:#09090b;border:1px solid var(--border);border-radius:6px;color:var(--muted);flex-shrink:0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
}
|
| 655 |
+
.select-row select{
|
| 656 |
+
width:100%;background:#09090b;border:1px solid var(--border);border-radius:8px;color:var(--text);
|
| 657 |
+
padding:10px 12px;outline:none;
|
|
|
|
| 658 |
}
|
| 659 |
+
.select-row select:focus{border-color:var(--accent);box-shadow:0 0 0 3px rgba(255,69,0,.15)}
|
| 660 |
|
| 661 |
+
.ra-wrap{width:100%}
|
| 662 |
+
.ra-inner{
|
| 663 |
+
position:relative;display:inline-flex;align-items:center;gap:0;padding:6px;
|
| 664 |
+
background:#111114;border-radius:9999px;overflow:hidden;border:1px solid var(--border);
|
| 665 |
+
width:100%;
|
| 666 |
}
|
| 667 |
+
.ra-input{display:none}
|
| 668 |
+
.ra-label{
|
| 669 |
+
position:relative;z-index:2;padding:8px 12px;font-size:13px;font-weight:700;color:#a1a1aa;
|
| 670 |
+
cursor:pointer;transition:color .2s;white-space:nowrap;flex:1;text-align:center;
|
| 671 |
}
|
| 672 |
+
.ra-highlight{
|
| 673 |
+
position:absolute;z-index:1;top:6px;left:6px;height:calc(100% - 12px);border-radius:9999px;
|
| 674 |
+
background:#fff1eb;box-shadow:0 2px 4px rgba(0,0,0,.1);transition:transform .2s,width .2s;
|
|
|
|
| 675 |
}
|
| 676 |
+
.ra-input:checked + .ra-label{color:#2b1207}
|
| 677 |
|
| 678 |
.app-statusbar{
|
| 679 |
+
background:var(--panel);border-top:1px solid var(--border);padding:6px 20px;
|
| 680 |
display:flex;gap:12px;height:34px;align-items:center;font-size:12px;
|
| 681 |
}
|
| 682 |
.app-statusbar .sb-section{
|
| 683 |
padding:0 12px;flex:1;display:flex;align-items:center;font-family:'JetBrains Mono',monospace;
|
| 684 |
+
font-size:12px;color:#52525b;overflow:hidden;white-space:nowrap;
|
| 685 |
}
|
| 686 |
.app-statusbar .sb-section.sb-fixed{
|
| 687 |
flex:0 0 auto;min-width:120px;text-align:center;justify-content:center;
|
| 688 |
+
padding:3px 12px;background:rgba(255,69,0,.08);border-radius:6px;color:#ff8a5c;font-weight:600;
|
| 689 |
}
|
| 690 |
|
| 691 |
+
.exp-note{padding:10px 20px;font-size:12px;color:#52525b;border-top:1px solid var(--border);text-align:center}
|
| 692 |
+
.exp-note a{color:#ff8a5c;text-decoration:none}
|
| 693 |
.exp-note a:hover{text-decoration:underline}
|
| 694 |
|
| 695 |
+
@media(max-width:960px){
|
| 696 |
.app-main-row{flex-direction:column}
|
| 697 |
+
.app-main-right{width:100%;max-height:none}
|
| 698 |
+
.app-main-left{border-right:none;border-bottom:1px solid var(--border)}
|
| 699 |
}
|
| 700 |
"""
|
| 701 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 702 |
gallery_js = r"""
|
| 703 |
() => {
|
| 704 |
function init() {
|
| 705 |
+
if (window.__ocrInitDone) return;
|
| 706 |
|
| 707 |
+
const dropZone = document.getElementById('upload-drop-zone');
|
| 708 |
const uploadPrompt = document.getElementById('upload-prompt');
|
| 709 |
const uploadClick = document.getElementById('upload-click-area');
|
| 710 |
const fileInput = document.getElementById('custom-file-input');
|
| 711 |
+
const imageStage = document.getElementById('single-image-stage');
|
| 712 |
+
const imagePreviewWrap = document.getElementById('single-image-preview');
|
|
|
|
| 713 |
const btnUpload = document.getElementById('tb-upload');
|
| 714 |
const btnClear = document.getElementById('tb-clear');
|
| 715 |
+
const promptInput = document.getElementById('custom-prompt-input');
|
| 716 |
const runBtnEl = document.getElementById('custom-run-btn');
|
| 717 |
+
const countTb = document.getElementById('tb-image-count');
|
| 718 |
+
const countSb = document.getElementById('sb-image-count');
|
| 719 |
+
const modelSelect = document.getElementById('custom-model');
|
| 720 |
+
const rawOut = document.getElementById('raw-output-view');
|
| 721 |
|
| 722 |
+
if (!dropZone || !fileInput || !promptInput) {
|
| 723 |
setTimeout(init, 250);
|
| 724 |
return;
|
| 725 |
}
|
| 726 |
|
| 727 |
+
window.__ocrInitDone = true;
|
| 728 |
|
| 729 |
+
let uploadedImage = null;
|
| 730 |
let toastTimer = null;
|
| 731 |
|
| 732 |
function showToast(message, type) {
|
|
|
|
| 752 |
}
|
| 753 |
window.__showToast = showToast;
|
| 754 |
|
| 755 |
+
function flashPromptError() {
|
| 756 |
+
promptInput.classList.add('error-flash');
|
| 757 |
+
promptInput.focus();
|
| 758 |
+
setTimeout(() => promptInput.classList.remove('error-flash'), 800);
|
| 759 |
}
|
| 760 |
|
| 761 |
function setGradioValue(containerId, value) {
|
|
|
|
| 767 |
const ns = Object.getOwnPropertyDescriptor(proto, 'value');
|
| 768 |
if (ns && ns.set) {
|
| 769 |
ns.set.call(el, value);
|
| 770 |
+
el.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
|
| 771 |
+
el.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
|
| 772 |
}
|
| 773 |
});
|
| 774 |
}
|
| 775 |
window.__setGradioValue = setGradioValue;
|
| 776 |
|
| 777 |
+
function syncImageToGradio() {
|
| 778 |
+
setGradioValue('hidden-image-b64', uploadedImage ? uploadedImage.b64 : '');
|
| 779 |
+
updateCounts();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 780 |
}
|
| 781 |
|
| 782 |
+
function syncPromptToGradio() {
|
| 783 |
+
setGradioValue('prompt-gradio-input', promptInput.value || '');
|
| 784 |
}
|
| 785 |
|
| 786 |
+
function syncModelToGradio() {
|
| 787 |
+
setGradioValue('model-choice-input', modelSelect ? modelSelect.value : 'FireRed-OCR');
|
| 788 |
}
|
| 789 |
|
| 790 |
+
function updateCounts() {
|
| 791 |
+
const txt = uploadedImage ? '1 image' : 'No image';
|
| 792 |
+
if (countTb) countTb.textContent = txt;
|
| 793 |
+
if (countSb) countSb.textContent = uploadedImage ? '1 image uploaded' : 'No image uploaded';
|
| 794 |
+
}
|
| 795 |
+
|
| 796 |
+
function renderImage() {
|
| 797 |
+
if (!uploadedImage) {
|
| 798 |
+
if (uploadPrompt) uploadPrompt.style.display = '';
|
| 799 |
+
if (imageStage) imageStage.style.display = 'none';
|
| 800 |
+
if (imagePreviewWrap) imagePreviewWrap.innerHTML = '';
|
| 801 |
+
return;
|
| 802 |
+
}
|
| 803 |
+
if (uploadPrompt) uploadPrompt.style.display = 'none';
|
| 804 |
+
if (imageStage) imageStage.style.display = 'flex';
|
| 805 |
+
|
| 806 |
+
imagePreviewWrap.innerHTML = ''
|
| 807 |
+
+ '<div class="single-image-card">'
|
| 808 |
+
+ ' <img src="' + uploadedImage.b64 + '" alt="' + (uploadedImage.name || 'image') + '">'
|
| 809 |
+
+ ' <div class="single-image-meta">'
|
| 810 |
+
+ ' <div class="single-image-name">' + (uploadedImage.name || 'uploaded_image') + '</div>'
|
| 811 |
+
+ ' <button class="single-image-remove" id="single-image-remove-btn">Remove</button>'
|
| 812 |
+
+ ' </div>'
|
| 813 |
+
+ '</div>';
|
| 814 |
+
|
| 815 |
+
const removeBtn = document.getElementById('single-image-remove-btn');
|
| 816 |
+
if (removeBtn) removeBtn.addEventListener('click', clearImage);
|
| 817 |
+
}
|
| 818 |
+
|
| 819 |
+
function setImage(b64, name) {
|
| 820 |
+
uploadedImage = { b64, name: name || 'uploaded_image' };
|
| 821 |
+
renderImage();
|
| 822 |
syncImageToGradio();
|
| 823 |
}
|
| 824 |
+
window.__setSingleImage = setImage;
|
| 825 |
|
| 826 |
function clearImage() {
|
| 827 |
+
uploadedImage = null;
|
| 828 |
+
renderImage();
|
| 829 |
syncImageToGradio();
|
| 830 |
}
|
| 831 |
+
window.__clearSingleImage = clearImage;
|
| 832 |
|
| 833 |
function processFiles(files) {
|
| 834 |
+
const file = Array.from(files).find(f => f.type && f.type.startsWith('image/'));
|
| 835 |
if (!file) {
|
| 836 |
showToast('Please upload a valid image file', 'error');
|
| 837 |
return;
|
| 838 |
}
|
| 839 |
const reader = new FileReader();
|
| 840 |
+
reader.onload = (e) => setImage(e.target.result, file.name);
|
| 841 |
reader.readAsDataURL(file);
|
| 842 |
}
|
| 843 |
|
| 844 |
+
fileInput.addEventListener('change', (e) => {
|
| 845 |
+
processFiles(e.target.files);
|
| 846 |
+
e.target.value = '';
|
| 847 |
+
});
|
| 848 |
+
|
| 849 |
+
if (uploadClick) uploadClick.addEventListener('click', () => fileInput.click());
|
| 850 |
if (btnUpload) btnUpload.addEventListener('click', () => fileInput.click());
|
| 851 |
if (btnClear) btnClear.addEventListener('click', clearImage);
|
|
|
|
| 852 |
|
| 853 |
+
dropZone.addEventListener('dragover', (e) => {
|
| 854 |
+
e.preventDefault();
|
| 855 |
+
dropZone.classList.add('drag-over');
|
| 856 |
+
});
|
| 857 |
+
dropZone.addEventListener('dragleave', (e) => {
|
| 858 |
+
e.preventDefault();
|
| 859 |
+
dropZone.classList.remove('drag-over');
|
| 860 |
+
});
|
| 861 |
dropZone.addEventListener('drop', (e) => {
|
| 862 |
e.preventDefault();
|
| 863 |
dropZone.classList.remove('drag-over');
|
| 864 |
if (e.dataTransfer.files.length) processFiles(e.dataTransfer.files);
|
| 865 |
});
|
| 866 |
|
| 867 |
+
promptInput.addEventListener('input', syncPromptToGradio);
|
| 868 |
+
if (modelSelect) modelSelect.addEventListener('change', syncModelToGradio);
|
| 869 |
|
| 870 |
+
window.__setPrompt = function(text) {
|
| 871 |
+
promptInput.value = text || '';
|
| 872 |
+
syncPromptToGradio();
|
| 873 |
};
|
| 874 |
|
| 875 |
+
window.__setModel = function(val) {
|
| 876 |
+
if (modelSelect) {
|
| 877 |
+
modelSelect.value = val;
|
| 878 |
+
syncModelToGradio();
|
| 879 |
+
}
|
| 880 |
+
};
|
| 881 |
|
| 882 |
+
document.querySelectorAll('.example-card[data-idx]').forEach(card => {
|
| 883 |
+
card.addEventListener('click', () => {
|
| 884 |
+
const idx = card.getAttribute('data-idx');
|
| 885 |
+
document.querySelectorAll('.example-card.loading').forEach(c => c.classList.remove('loading'));
|
| 886 |
+
card.classList.add('loading');
|
| 887 |
+
showToast('Loading example...', 'info');
|
| 888 |
|
| 889 |
+
setGradioValue('example-result-data', '');
|
| 890 |
+
setGradioValue('example-idx-input', idx);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 891 |
|
| 892 |
+
setTimeout(() => {
|
| 893 |
+
const btn = document.getElementById('example-load-btn');
|
| 894 |
+
if (btn) {
|
| 895 |
+
const b = btn.querySelector('button');
|
| 896 |
+
if (b) b.click(); else btn.click();
|
| 897 |
+
}
|
| 898 |
+
}, 120);
|
| 899 |
+
|
| 900 |
+
setTimeout(() => card.classList.remove('loading'), 12000);
|
| 901 |
+
});
|
| 902 |
});
|
| 903 |
|
| 904 |
function syncSlider(customId, gradioId) {
|
|
|
|
| 913 |
const ns = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value');
|
| 914 |
if (ns && ns.set) {
|
| 915 |
ns.set.call(el, slider.value);
|
| 916 |
+
el.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
|
| 917 |
+
el.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
|
| 918 |
}
|
| 919 |
});
|
| 920 |
});
|
|
|
|
| 926 |
syncSlider('custom-top-k', 'gradio-top-k');
|
| 927 |
syncSlider('custom-repetition-penalty', 'gradio-repetition-penalty');
|
| 928 |
|
| 929 |
+
function syncGpuDurationToGradio(val) {
|
| 930 |
+
setGradioValue('gpu-duration-input', String(val));
|
| 931 |
+
const hidden = document.getElementById('gpu-duration-state');
|
| 932 |
+
if (hidden) setGradioValue('gpu-duration-state', String(val));
|
| 933 |
+
}
|
| 934 |
+
|
| 935 |
+
const gpuWrap = document.getElementById('gpu-duration-radio');
|
| 936 |
+
if (gpuWrap) {
|
| 937 |
+
const inputs = gpuWrap.querySelectorAll('.ra-input');
|
| 938 |
+
inputs.forEach(inp => {
|
| 939 |
+
inp.addEventListener('change', () => syncGpuDurationToGradio(inp.value));
|
| 940 |
+
});
|
| 941 |
+
}
|
| 942 |
+
|
| 943 |
function showLoader() {
|
| 944 |
const l = document.getElementById('output-loader');
|
| 945 |
if (l) l.classList.add('active');
|
|
|
|
| 947 |
if (sb) sb.textContent = 'Processing...';
|
| 948 |
}
|
| 949 |
|
| 950 |
+
function hideLoader(statusText) {
|
| 951 |
const l = document.getElementById('output-loader');
|
| 952 |
if (l) l.classList.remove('active');
|
| 953 |
const sb = document.querySelector('.sb-fixed');
|
| 954 |
+
if (sb) sb.textContent = statusText || 'Done';
|
| 955 |
}
|
| 956 |
|
| 957 |
window.__showLoader = showLoader;
|
| 958 |
window.__hideLoader = hideLoader;
|
| 959 |
|
| 960 |
+
function clearOutput() {
|
| 961 |
+
if (rawOut) rawOut.value = '';
|
| 962 |
+
}
|
| 963 |
+
window.__clearOutput = clearOutput;
|
| 964 |
|
| 965 |
+
function validateBeforeRun() {
|
| 966 |
+
const promptVal = (promptInput.value || '').trim();
|
| 967 |
+
const hasImage = !!uploadedImage;
|
| 968 |
+
if (!hasImage && !promptVal) {
|
| 969 |
showToast('Please upload an image and enter a query', 'error');
|
| 970 |
+
flashPromptError();
|
| 971 |
return false;
|
| 972 |
}
|
| 973 |
if (!hasImage) {
|
| 974 |
showToast('Please upload an image', 'error');
|
| 975 |
return false;
|
| 976 |
}
|
| 977 |
+
if (!promptVal) {
|
| 978 |
showToast('Please enter a query', 'warning');
|
| 979 |
+
flashPromptError();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 980 |
return false;
|
| 981 |
}
|
|
|
|
| 982 |
return true;
|
| 983 |
}
|
| 984 |
|
| 985 |
window.__clickGradioRunBtn = function() {
|
| 986 |
if (!validateBeforeRun()) return;
|
| 987 |
+
syncPromptToGradio();
|
| 988 |
syncImageToGradio();
|
| 989 |
+
syncModelToGradio();
|
| 990 |
+
clearOutput();
|
| 991 |
showLoader();
|
| 992 |
setTimeout(() => {
|
| 993 |
const gradioBtn = document.getElementById('gradio-run-btn');
|
| 994 |
if (!gradioBtn) return;
|
| 995 |
const btn = gradioBtn.querySelector('button');
|
| 996 |
if (btn) btn.click(); else gradioBtn.click();
|
| 997 |
+
}, 150);
|
| 998 |
};
|
| 999 |
|
| 1000 |
+
if (runBtnEl) {
|
| 1001 |
+
runBtnEl.addEventListener('click', () => window.__clickGradioRunBtn());
|
| 1002 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1003 |
|
| 1004 |
+
renderImage();
|
| 1005 |
+
updateCounts();
|
| 1006 |
+
syncPromptToGradio();
|
| 1007 |
+
syncModelToGradio();
|
| 1008 |
+
syncGpuDurationToGradio('60');
|
| 1009 |
}
|
| 1010 |
init();
|
| 1011 |
}
|
|
|
|
| 1013 |
|
| 1014 |
wire_outputs_js = r"""
|
| 1015 |
() => {
|
| 1016 |
+
function watchRawOutput() {
|
| 1017 |
const resultContainer = document.getElementById('gradio-raw-output');
|
| 1018 |
+
const outputView = document.getElementById('raw-output-view');
|
| 1019 |
+
if (!resultContainer || !outputView) {
|
| 1020 |
+
setTimeout(watchRawOutput, 500);
|
| 1021 |
+
return;
|
| 1022 |
+
}
|
| 1023 |
|
| 1024 |
+
function sync() {
|
| 1025 |
+
const ta = resultContainer.querySelector('textarea');
|
| 1026 |
+
const inp = resultContainer.querySelector('input');
|
| 1027 |
+
const source = ta || inp;
|
| 1028 |
+
if (!source) return;
|
| 1029 |
+
|
| 1030 |
+
const val = source.value || '';
|
| 1031 |
+
if (outputView.value !== val) {
|
| 1032 |
+
outputView.value = val;
|
| 1033 |
+
outputView.scrollTop = outputView.scrollHeight;
|
| 1034 |
+
}
|
| 1035 |
+
|
| 1036 |
+
if (window.__hideLoader && val && val.trim().length > 0) {
|
| 1037 |
+
window.__hideLoader('Streaming...');
|
| 1038 |
}
|
| 1039 |
}
|
| 1040 |
|
| 1041 |
+
const observer = new MutationObserver(sync);
|
| 1042 |
+
observer.observe(resultContainer, {
|
| 1043 |
+
childList: true,
|
| 1044 |
+
subtree: true,
|
| 1045 |
+
attributes: true,
|
| 1046 |
+
characterData: true
|
| 1047 |
+
});
|
| 1048 |
+
setInterval(sync, 400);
|
| 1049 |
}
|
| 1050 |
+
watchRawOutput();
|
| 1051 |
|
| 1052 |
function watchExampleResults() {
|
| 1053 |
const container = document.getElementById('example-result-data');
|
| 1054 |
+
if (!container) {
|
| 1055 |
+
setTimeout(watchExampleResults, 500);
|
| 1056 |
+
return;
|
| 1057 |
+
}
|
| 1058 |
|
| 1059 |
let lastProcessed = '';
|
| 1060 |
|
|
|
|
| 1062 |
const el = container.querySelector('textarea') || container.querySelector('input');
|
| 1063 |
if (!el) return;
|
| 1064 |
const val = el.value;
|
| 1065 |
+
if (!val || val === lastProcessed || val.length < 20) return;
|
| 1066 |
|
| 1067 |
try {
|
| 1068 |
const data = JSON.parse(val);
|
| 1069 |
if (data.status === 'ok' && data.image) {
|
| 1070 |
lastProcessed = val;
|
| 1071 |
+
|
| 1072 |
+
if (window.__clearSingleImage) window.__clearSingleImage();
|
| 1073 |
+
if (window.__setPrompt && data.prompt) window.__setPrompt(data.prompt);
|
| 1074 |
+
if (window.__setSingleImage) window.__setSingleImage(data.image, 'example_image');
|
| 1075 |
+
|
| 1076 |
document.querySelectorAll('.example-card.loading').forEach(c => c.classList.remove('loading'));
|
| 1077 |
if (window.__showToast) window.__showToast('Example loaded', 'info');
|
| 1078 |
} else if (data.status === 'error') {
|
| 1079 |
document.querySelectorAll('.example-card.loading').forEach(c => c.classList.remove('loading'));
|
| 1080 |
+
if (window.__showToast) window.__showToast('Could not load example image', 'error');
|
| 1081 |
}
|
| 1082 |
+
} catch (e) {
|
| 1083 |
console.error('Example parse error:', e);
|
| 1084 |
}
|
| 1085 |
}
|
| 1086 |
|
| 1087 |
const obs = new MutationObserver(checkResult);
|
| 1088 |
+
obs.observe(container, { childList: true, subtree: true, characterData: true, attributes: true });
|
| 1089 |
setInterval(checkResult, 500);
|
| 1090 |
}
|
| 1091 |
watchExampleResults();
|
| 1092 |
|
| 1093 |
+
function watchErrors() {
|
| 1094 |
+
const root = document.body;
|
| 1095 |
+
const observer = new MutationObserver(() => {
|
| 1096 |
+
const err = document.querySelector('.gradio-container .toast, .gradio-container [role="alert"]');
|
| 1097 |
+
if (err && window.__hideLoader) {
|
| 1098 |
+
window.__hideLoader('Ready');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1099 |
}
|
| 1100 |
+
});
|
| 1101 |
+
observer.observe(root, { childList: true, subtree: true });
|
|
|
|
|
|
|
|
|
|
| 1102 |
}
|
| 1103 |
+
watchErrors();
|
| 1104 |
}
|
| 1105 |
"""
|
| 1106 |
|
| 1107 |
+
radio_gpu_html = """
|
| 1108 |
+
<div class="ra-wrap" id="gpu-duration-radio">
|
| 1109 |
+
<div class="ra-inner">
|
| 1110 |
+
<div class="ra-highlight"></div>
|
| 1111 |
|
| 1112 |
+
<input class="ra-input" type="radio" name="gpu-duration" id="gpu-60" value="60" checked>
|
| 1113 |
+
<label class="ra-label" for="gpu-60">60</label>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1114 |
|
| 1115 |
+
<input class="ra-input" type="radio" name="gpu-duration" id="gpu-90" value="90">
|
| 1116 |
+
<label class="ra-label" for="gpu-90">90</label>
|
| 1117 |
|
| 1118 |
+
<input class="ra-input" type="radio" name="gpu-duration" id="gpu-120" value="120">
|
| 1119 |
+
<label class="ra-label" for="gpu-120">120</label>
|
|
|
|
| 1120 |
|
| 1121 |
+
<input class="ra-input" type="radio" name="gpu-duration" id="gpu-180" value="180">
|
| 1122 |
+
<label class="ra-label" for="gpu-180">180</label>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1123 |
|
| 1124 |
+
<input class="ra-input" type="radio" name="gpu-duration" id="gpu-240" value="240">
|
| 1125 |
+
<label class="ra-label" for="gpu-240">240</label>
|
|
|
|
|
|
|
| 1126 |
|
| 1127 |
+
<input class="ra-input" type="radio" name="gpu-duration" id="gpu-300" value="300">
|
| 1128 |
+
<label class="ra-label" for="gpu-300">300</label>
|
| 1129 |
+
</div>
|
| 1130 |
+
</div>
|
| 1131 |
+
"""
|
| 1132 |
|
| 1133 |
+
radio_gpu_js = r"""
|
| 1134 |
+
() => {
|
| 1135 |
+
function initRadio() {
|
| 1136 |
+
const wrap = document.getElementById('gpu-duration-radio');
|
| 1137 |
+
if (!wrap || wrap.dataset.bound === '1') return;
|
| 1138 |
+
|
| 1139 |
+
const inner = wrap.querySelector('.ra-inner');
|
| 1140 |
+
const highlight = wrap.querySelector('.ra-highlight');
|
| 1141 |
+
const inputs = Array.from(wrap.querySelectorAll('.ra-input'));
|
| 1142 |
+
if (!inner || !highlight || !inputs.length) {
|
| 1143 |
+
setTimeout(initRadio, 300);
|
| 1144 |
+
return;
|
| 1145 |
+
}
|
| 1146 |
|
| 1147 |
+
function setHighlight(idx) {
|
| 1148 |
+
const n = inputs.length;
|
| 1149 |
+
const pct = 100 / n;
|
| 1150 |
+
highlight.style.width = `calc(${pct}% - 6px)`;
|
| 1151 |
+
highlight.style.transform = `translateX(${idx * 100}%)`;
|
| 1152 |
+
}
|
| 1153 |
|
| 1154 |
+
function syncFromChecked() {
|
| 1155 |
+
const idx = Math.max(0, inputs.findIndex(i => i.checked));
|
| 1156 |
+
setHighlight(idx);
|
| 1157 |
+
}
|
| 1158 |
|
| 1159 |
+
inputs.forEach((inp, idx) => {
|
| 1160 |
+
inp.addEventListener('change', () => {
|
| 1161 |
+
if (inp.checked) setHighlight(idx);
|
| 1162 |
+
});
|
| 1163 |
+
});
|
| 1164 |
|
| 1165 |
+
syncFromChecked();
|
| 1166 |
+
wrap.dataset.bound = '1';
|
| 1167 |
+
}
|
| 1168 |
+
initRadio();
|
| 1169 |
+
}
|
| 1170 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1171 |
|
| 1172 |
+
app_html = f"""
|
| 1173 |
+
<div class="app-shell">
|
| 1174 |
+
<div class="app-header">
|
| 1175 |
+
<div class="app-header-left">
|
| 1176 |
+
<div class="app-logo">{FIRE_VISION_LOGO}</div>
|
| 1177 |
+
<span class="app-title">Multimodal OCR 2</span>
|
| 1178 |
+
<span class="app-badge">Vision Based</span>
|
| 1179 |
+
<span class="app-badge fast">Streaming OCR</span>
|
|
|
|
| 1180 |
</div>
|
| 1181 |
+
</div>
|
| 1182 |
|
| 1183 |
+
<div class="app-toolbar">
|
| 1184 |
+
<button id="tb-upload" class="modern-tb-btn" title="Upload image">
|
| 1185 |
+
{UPLOAD_SVG}<span class="tb-label">Upload</span>
|
| 1186 |
+
</button>
|
| 1187 |
+
<button id="tb-clear" class="modern-tb-btn" title="Clear image">
|
| 1188 |
+
{CLEAR_SVG}<span class="tb-label">Clear</span>
|
| 1189 |
+
</button>
|
| 1190 |
+
<div class="tb-sep"></div>
|
| 1191 |
+
<span id="tb-image-count" class="tb-info">No image</span>
|
| 1192 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1193 |
|
| 1194 |
+
<div class="app-main-row">
|
| 1195 |
+
<div class="app-main-left">
|
| 1196 |
+
<div id="upload-drop-zone">
|
| 1197 |
+
<div id="upload-prompt" class="upload-prompt-modern">
|
| 1198 |
+
<div id="upload-click-area" class="upload-click-area">
|
| 1199 |
+
<svg viewBox="0 0 80 80" fill="none" xmlns="http://www.w3.org/2000/svg">
|
| 1200 |
+
<rect x="8" y="14" width="64" height="52" rx="6" fill="none" stroke="#FF4500" stroke-width="2" stroke-dasharray="4 3"/>
|
| 1201 |
+
<polygon points="12,62 30,40 42,50 54,34 68,62" fill="rgba(255,69,0,0.15)" stroke="#FF4500" stroke-width="1.5"/>
|
| 1202 |
+
<circle cx="28" cy="30" r="6" fill="rgba(255,69,0,0.2)" stroke="#FF4500" stroke-width="1.5"/>
|
| 1203 |
+
</svg>
|
| 1204 |
+
<span class="upload-main-text">Click or drag an image here</span>
|
| 1205 |
+
<span class="upload-sub-text">Upload one image for OCR, document parsing, and vision-language extraction</span>
|
| 1206 |
</div>
|
| 1207 |
</div>
|
| 1208 |
+
<input id="custom-file-input" type="file" accept="image/*" style="display:none;" />
|
| 1209 |
+
<div id="single-image-stage" class="single-image-stage">
|
| 1210 |
+
<div id="single-image-preview" style="width:100%"></div>
|
|
|
|
|
|
|
| 1211 |
</div>
|
| 1212 |
+
</div>
|
| 1213 |
|
| 1214 |
+
<div class="hint-bar">
|
| 1215 |
+
<b>Upload:</b> Click or drag to add one image ·
|
| 1216 |
+
<b>Run OCR:</b> Enter a query and press <kbd>Submit</kbd> ·
|
| 1217 |
+
<kbd>Clear</kbd> removes the current image
|
|
|
|
|
|
|
| 1218 |
</div>
|
| 1219 |
|
| 1220 |
+
<div class="examples-section">
|
| 1221 |
+
<div class="examples-title">Quick Examples</div>
|
| 1222 |
+
<div class="examples-scroll">
|
| 1223 |
+
{EXAMPLE_CARDS_HTML}
|
|
|
|
|
|
|
|
|
|
| 1224 |
</div>
|
| 1225 |
+
</div>
|
| 1226 |
+
</div>
|
| 1227 |
|
| 1228 |
+
<div class="app-main-right">
|
| 1229 |
+
<div class="panel-card">
|
| 1230 |
+
<div class="panel-card-title" style="padding:12px 20px;border-bottom:1px solid rgba(39,39,42,.6);">OCR Instruction</div>
|
| 1231 |
+
<div class="panel-card-body">
|
| 1232 |
+
<label class="modern-label" for="custom-prompt-input">Query</label>
|
| 1233 |
+
<textarea id="custom-prompt-input" class="modern-textarea" rows="4" placeholder="e.g., Perform OCR on the image precisely."></textarea>
|
| 1234 |
+
|
| 1235 |
+
<div class="modern-label" style="margin-top:4px;">Select Model</div>
|
| 1236 |
+
<div class="select-row">
|
| 1237 |
+
<select id="custom-model">
|
| 1238 |
+
<option value="FireRed-OCR" selected>FireRed-OCR</option>
|
| 1239 |
+
<option value="Nanonets-OCR-s">Nanonets-OCR-s</option>
|
| 1240 |
+
<option value="MonkeyOCR-Recognition">MonkeyOCR-Recognition</option>
|
| 1241 |
+
<option value="Thyme-RL">Thyme-RL</option>
|
| 1242 |
+
<option value="Typhoon-OCR-7B">Typhoon-OCR-7B</option>
|
| 1243 |
+
<option value="SmolDocling-256M-preview">SmolDocling-256M-preview</option>
|
| 1244 |
+
</select>
|
| 1245 |
</div>
|
| 1246 |
</div>
|
| 1247 |
+
</div>
|
| 1248 |
|
| 1249 |
+
<div style="padding:12px 20px;">
|
| 1250 |
+
<button id="custom-run-btn" class="btn-run">
|
| 1251 |
+
{RUN_SVG}
|
| 1252 |
+
<span>Submit</span>
|
| 1253 |
+
</button>
|
| 1254 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1255 |
|
| 1256 |
+
<div class="output-frame">
|
| 1257 |
+
<div class="out-title">
|
| 1258 |
+
<span>Raw Output Stream</span>
|
|
|
|
|
|
|
| 1259 |
</div>
|
| 1260 |
+
<div class="out-body">
|
| 1261 |
+
<div class="modern-loader" id="output-loader">
|
| 1262 |
+
<div class="loader-spinner"></div>
|
| 1263 |
+
<div class="loader-text">Processing OCR...</div>
|
| 1264 |
+
<div class="loader-bar-track"><div class="loader-bar-fill"></div></div>
|
| 1265 |
</div>
|
| 1266 |
+
<div class="output-text-shell">
|
| 1267 |
+
<textarea id="raw-output-view" class="output-text-area" spellcheck="false" placeholder="OCR output will appear here..." readonly></textarea>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1268 |
</div>
|
| 1269 |
</div>
|
| 1270 |
+
</div>
|
| 1271 |
|
| 1272 |
+
<div class="settings-group">
|
| 1273 |
+
<div class="settings-group-title">Advanced Settings</div>
|
| 1274 |
+
<div class="settings-group-body">
|
| 1275 |
+
<div class="slider-row">
|
| 1276 |
+
<label>Max Tokens</label>
|
| 1277 |
+
<input type="range" id="custom-max-new-tokens" min="1" max="{MAX_MAX_NEW_TOKENS}" step="1" value="{DEFAULT_MAX_NEW_TOKENS}">
|
| 1278 |
+
<span class="slider-val" id="custom-max-new-tokens-val">{DEFAULT_MAX_NEW_TOKENS}</span>
|
| 1279 |
+
</div>
|
| 1280 |
+
<div class="slider-row">
|
| 1281 |
+
<label>Temperature</label>
|
| 1282 |
+
<input type="range" id="custom-temperature" min="0.1" max="4" step="0.1" value="0.6">
|
| 1283 |
+
<span class="slider-val" id="custom-temperature-val">0.6</span>
|
| 1284 |
+
</div>
|
| 1285 |
+
<div class="slider-row">
|
| 1286 |
+
<label>Top-P</label>
|
| 1287 |
+
<input type="range" id="custom-top-p" min="0.05" max="1" step="0.05" value="0.9">
|
| 1288 |
+
<span class="slider-val" id="custom-top-p-val">0.9</span>
|
| 1289 |
+
</div>
|
| 1290 |
+
<div class="slider-row">
|
| 1291 |
+
<label>Top-K</label>
|
| 1292 |
+
<input type="range" id="custom-top-k" min="1" max="1000" step="1" value="50">
|
| 1293 |
+
<span class="slider-val" id="custom-top-k-val">50</span>
|
| 1294 |
+
</div>
|
| 1295 |
+
<div class="slider-row">
|
| 1296 |
+
<label>Repetition</label>
|
| 1297 |
+
<input type="range" id="custom-repetition-penalty" min="1" max="2" step="0.05" value="1.2">
|
| 1298 |
+
<span class="slider-val" id="custom-repetition-penalty-val">1.2</span>
|
| 1299 |
+
</div>
|
| 1300 |
+
|
| 1301 |
+
<div class="modern-label" style="margin-top:4px;">GPU Duration (seconds)</div>
|
| 1302 |
+
{radio_gpu_html}
|
| 1303 |
+
<div style="font-size:12px;color:#71717a;margin-top:4px;">
|
| 1304 |
+
Higher GPU duration allows longer processing but consumes more GPU quota.
|
| 1305 |
</div>
|
| 1306 |
</div>
|
| 1307 |
</div>
|
| 1308 |
</div>
|
| 1309 |
+
</div>
|
| 1310 |
|
| 1311 |
+
<div class="exp-note">
|
| 1312 |
+
Vision-based OCR interface for multimodal document understanding
|
| 1313 |
+
</div>
|
| 1314 |
|
| 1315 |
+
<div class="app-statusbar">
|
| 1316 |
+
<div class="sb-section" id="sb-image-count">No image uploaded</div>
|
| 1317 |
+
<div class="sb-section sb-fixed">Ready</div>
|
|
|
|
| 1318 |
</div>
|
| 1319 |
+
</div>
|
| 1320 |
+
"""
|
| 1321 |
|
| 1322 |
+
|
| 1323 |
+
with gr.Blocks() as demo:
|
| 1324 |
+
hidden_image_b64 = gr.Textbox(value="", elem_id="hidden-image-b64", elem_classes="hidden-input", container=False)
|
| 1325 |
+
prompt = gr.Textbox(value="", elem_id="prompt-gradio-input", elem_classes="hidden-input", container=False)
|
| 1326 |
+
model_choice = gr.Textbox(value="FireRed-OCR", elem_id="model-choice-input", elem_classes="hidden-input", container=False)
|
| 1327 |
+
|
| 1328 |
+
max_new_tokens = gr.Slider(minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS, elem_id="gradio-max-new-tokens", elem_classes="hidden-input", container=False)
|
| 1329 |
+
temperature = gr.Slider(minimum=0.1, maximum=4.0, step=0.1, value=0.6, elem_id="gradio-temperature", elem_classes="hidden-input", container=False)
|
| 1330 |
+
top_p = gr.Slider(minimum=0.05, maximum=1.0, step=0.05, value=0.9, elem_id="gradio-top-p", elem_classes="hidden-input", container=False)
|
| 1331 |
+
top_k = gr.Slider(minimum=1, maximum=1000, step=1, value=50, elem_id="gradio-top-k", elem_classes="hidden-input", container=False)
|
| 1332 |
+
repetition_penalty = gr.Slider(minimum=1.0, maximum=2.0, step=0.05, value=1.2, elem_id="gradio-repetition-penalty", elem_classes="hidden-input", container=False)
|
| 1333 |
+
|
| 1334 |
+
gpu_duration_input = gr.Textbox(value="60", elem_id="gpu-duration-input", elem_classes="hidden-input", container=False)
|
| 1335 |
+
gpu_duration_state = gr.Number(value=60, visible=False, elem_id="gpu-duration-state")
|
| 1336 |
+
|
| 1337 |
+
raw_output = gr.Textbox(elem_id="gradio-raw-output", elem_classes="hidden-input", container=False, lines=10)
|
| 1338 |
+
|
| 1339 |
+
example_idx = gr.Textbox(value="", elem_id="example-idx-input", elem_classes="hidden-input", container=False)
|
| 1340 |
+
example_result = gr.Textbox(value="", elem_id="example-result-data", elem_classes="hidden-input", container=False)
|
| 1341 |
+
example_load_btn = gr.Button("Load Example", elem_id="example-load-btn")
|
| 1342 |
run_btn = gr.Button("Run", elem_id="gradio-run-btn")
|
| 1343 |
|
| 1344 |
+
gr.HTML(app_html)
|
| 1345 |
+
gr.HTML(radio_gpu_js)
|
| 1346 |
+
|
| 1347 |
demo.load(fn=None, js=gallery_js)
|
| 1348 |
demo.load(fn=None, js=wire_outputs_js)
|
| 1349 |
|
| 1350 |
+
gpu_duration_input.change(
|
| 1351 |
+
fn=apply_gpu_duration,
|
| 1352 |
+
inputs=[gpu_duration_input],
|
| 1353 |
+
outputs=[gpu_duration_state],
|
| 1354 |
+
api_visibility="private",
|
| 1355 |
+
queue=False,
|
| 1356 |
+
)
|
| 1357 |
+
|
| 1358 |
run_btn.click(
|
| 1359 |
+
fn=generate_image,
|
| 1360 |
inputs=[
|
| 1361 |
model_choice,
|
| 1362 |
+
prompt,
|
| 1363 |
hidden_image_b64,
|
| 1364 |
max_new_tokens,
|
| 1365 |
temperature,
|
| 1366 |
top_p,
|
| 1367 |
top_k,
|
| 1368 |
repetition_penalty,
|
| 1369 |
+
gpu_duration_state,
|
| 1370 |
],
|
| 1371 |
+
outputs=[raw_output],
|
| 1372 |
+
js=r"""(m, p, img, mnt, t, tp, tk, rp, gpu) => {
|
| 1373 |
+
const promptEl = document.getElementById('custom-prompt-input');
|
| 1374 |
+
const modelEl = document.getElementById('custom-model');
|
| 1375 |
+
const imageContainer = window.__ocrInitDone ? (document.getElementById('hidden-image-b64')) : null;
|
| 1376 |
+
|
| 1377 |
+
const promptVal = promptEl ? promptEl.value : p;
|
| 1378 |
+
const modelVal = modelEl ? modelEl.value : m;
|
| 1379 |
+
|
| 1380 |
let imgVal = img;
|
| 1381 |
+
const hiddenImgWrap = document.getElementById('hidden-image-b64');
|
| 1382 |
+
if (hiddenImgWrap) {
|
| 1383 |
+
const inner = hiddenImgWrap.querySelector('textarea, input');
|
| 1384 |
+
if (inner) imgVal = inner.value || imgVal;
|
| 1385 |
}
|
| 1386 |
+
return [modelVal, promptVal, imgVal, mnt, t, tp, tk, rp, gpu];
|
| 1387 |
}""",
|
| 1388 |
)
|
| 1389 |
|