prithivMLmods commited on
Commit
27bc33d
·
verified ·
1 Parent(s): ed758dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -426
app.py CHANGED
@@ -326,432 +326,6 @@ label {
326
  }
327
  """
328
 
329
- with gr.Blocks(css=css, title="GLM-OCR", theme=gr.themes.Soft()) as demo:
330
-
331
- gr.HTML("""
332
- <div class="main-wrapper">
333
- <div class="header-card">
334
- <h1 class="header-title">✦ GLM-OCR ✦</h1>
335
- <p class="header-subtitle">Document parsing & text recognition powered by AI</p>
336
- </div>
337
- </div>
338
- """)
339
-
340
- with gr.Row(elem_classes=["main-wrapper"]):
341
- with gr.Column(scale=1, elem_classes=["content-card"]):
342
- gr.HTML('<div class="section-label">📷 Upload Image</div>')
343
-
344
- image_input = gr.Image(
345
- type="pil",
346
- label="",
347
- sources=["upload", "clipboard"],
348
- elem_classes=["upload-box"],
349
- height=250
350
- )
351
-
352
- gr.HTML('<div class="section-label" style="margin-top: 20px;">🎯 Recognition Type</div>')
353
-
354
- task = gr.Radio(
355
- choices=list(TASK_PROMPTS.keys()),
356
- value="Text",
357
- label="",
358
- elem_classes=["task-buttons"]
359
- )
360
-
361
- btn = gr.Button(
362
- "Recognize →",
363
- variant="primary",
364
- elem_classes=["go-button"]
365
- )
366
-
367
- gr.HTML('<div class="section-label" style="margin-top: 20px;">📁 Examples</div>')
368
-
369
- examples = gr.Examples(
370
- examples=[
371
- "examples/1.jpg",
372
- "examples/2.jpg",
373
- "examples/3.jpg"
374
- ],
375
- inputs=image_input,
376
- label="",
377
- elem_classes=["example-gallery"]
378
- )
379
-
380
- with gr.Column(scale=1, elem_classes=["content-card"]):
381
- gr.HTML('<div class="section-label">📝 Output</div>')
382
-
383
- with gr.Tabs(elem_classes=["tabs-container"]):
384
- with gr.Tab("Text"):
385
- output_text = gr.Textbox(
386
- label="",
387
- lines=18,
388
- elem_classes=["output-area"],
389
- show_copy_button=True
390
- )
391
-
392
- with gr.Tab("Markdown"):
393
- output_md = gr.Markdown(
394
- value="",
395
- elem_classes=["md-preview"]
396
- )
397
-
398
- gr.HTML("""
399
- <div class="main-wrapper">
400
- <div class="footer-note">
401
- 🚀 Powered by <strong>zai-org/GLM-OCR</strong> •
402
- Supports text, formula, and table recognition
403
- </div>
404
- </div>
405
- """)
406
-
407
- def run_ocr(image, task):
408
- result = process_image(image, task)
409
- return result, result
410
-
411
- btn.click(
412
- run_ocr,
413
- [image_input, task],
414
- [output_text, output_md]
415
- )
416
-
417
- image_input.change(
418
- lambda: ("", ""),
419
- None,
420
- [output_text, output_md]
421
- )
422
-
423
- if __name__ == "__main__":
424
- demo.queue(max_size=50).launch(
425
- show_error=True,
426
- ssr_mode=False
427
- )import gradio as gr
428
- import torch
429
- import spaces
430
- import os
431
- import tempfile
432
- from PIL import Image, ImageOps
433
- from transformers import AutoProcessor, AutoModelForImageTextToText
434
- from io import BytesIO
435
- import base64
436
-
437
- MODEL_PATH = "zai-org/GLM-OCR"
438
-
439
- # Load model and processor
440
- processor = AutoProcessor.from_pretrained(MODEL_PATH, trust_remote_code=True)
441
- model = AutoModelForImageTextToText.from_pretrained(
442
- pretrained_model_name_or_path=MODEL_PATH,
443
- torch_dtype=torch.bfloat16,
444
- device_map="auto",
445
- trust_remote_code=True
446
- )
447
-
448
- TASK_PROMPTS = {
449
- "Text": "Text Recognition:",
450
- "Formula": "Formula Recognition:",
451
- "Table": "Table Recognition:",
452
- }
453
-
454
- @spaces.GPU
455
- def process_image(image, task):
456
- if image is None:
457
- return "⚠️ Please upload an image first"
458
-
459
- if image.mode in ('RGBA', 'LA', 'P'):
460
- image = image.convert('RGB')
461
- image = ImageOps.exif_transpose(image)
462
-
463
- tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
464
- image.save(tmp.name, 'PNG')
465
- tmp.close()
466
-
467
- prompt = TASK_PROMPTS.get(task, "Text Recognition:")
468
-
469
- messages = [
470
- {
471
- "role": "user",
472
- "content": [
473
- {"type": "image", "url": tmp.name},
474
- {"type": "text", "text": prompt}
475
- ],
476
- }
477
- ]
478
-
479
- inputs = processor.apply_chat_template(
480
- messages,
481
- tokenize=True,
482
- add_generation_prompt=True,
483
- return_dict=True,
484
- return_tensors="pt"
485
- ).to(model.device)
486
-
487
- inputs.pop("token_type_ids", None)
488
-
489
- generated_ids = model.generate(**inputs, max_new_tokens=8192)
490
- output_text = processor.decode(
491
- generated_ids[0][inputs["input_ids"].shape[1]:],
492
- skip_special_tokens=True
493
- )
494
-
495
- os.unlink(tmp.name)
496
-
497
- return output_text.strip()
498
-
499
- css = """
500
- @import url('https://fonts.googleapis.com/css2?family=Outfit:wght@400;600;900&family=IBM+Plex+Mono:wght@400;500&display=swap');
501
-
502
- :root {
503
- --input-focus: #2d8cf0;
504
- --font-color: #323232;
505
- --font-color-sub: #666;
506
- --bg-color: beige;
507
- --main-color: black;
508
- --accent-bg: lightblue;
509
- }
510
-
511
- * {
512
- font-family: 'Outfit', Arial, sans-serif !important;
513
- }
514
-
515
- .gradio-container {
516
- background: linear-gradient(135deg, #e8f5e9, #e3f2fd, #fce4ec) !important;
517
- min-height: 100vh;
518
- }
519
-
520
- .main-wrapper {
521
- max-width: 1000px;
522
- margin: 0 auto;
523
- padding: 30px 20px;
524
- }
525
-
526
- .header-card {
527
- background: var(--accent-bg);
528
- padding: 25px 30px;
529
- border-radius: 8px;
530
- border: 3px solid var(--main-color);
531
- box-shadow: 6px 6px var(--main-color);
532
- margin-bottom: 25px;
533
- text-align: center;
534
- }
535
-
536
- .header-title {
537
- font-size: 2.5em;
538
- font-weight: 900;
539
- color: var(--font-color);
540
- margin: 0;
541
- letter-spacing: -1px;
542
- }
543
-
544
- .header-subtitle {
545
- font-size: 1.1em;
546
- font-weight: 600;
547
- color: var(--font-color-sub);
548
- margin-top: 8px;
549
- }
550
-
551
- .content-card {
552
- background: var(--accent-bg);
553
- padding: 25px;
554
- border-radius: 8px;
555
- border: 3px solid var(--main-color);
556
- box-shadow: 6px 6px var(--main-color);
557
- }
558
-
559
- .section-label {
560
- font-weight: 700;
561
- font-size: 0.95em;
562
- color: var(--font-color);
563
- margin-bottom: 10px;
564
- text-transform: uppercase;
565
- letter-spacing: 0.5px;
566
- }
567
-
568
- .input-field, .input-field textarea, .input-field input {
569
- border-radius: 6px !important;
570
- border: 2px solid var(--main-color) !important;
571
- background-color: var(--bg-color) !important;
572
- box-shadow: 4px 4px var(--main-color) !important;
573
- font-size: 15px !important;
574
- font-weight: 500 !important;
575
- color: var(--font-color) !important;
576
- }
577
-
578
- .input-field textarea:focus, .input-field input:focus {
579
- border-color: var(--input-focus) !important;
580
- outline: none !important;
581
- }
582
-
583
- .upload-box {
584
- border: 3px dashed var(--main-color) !important;
585
- border-radius: 8px !important;
586
- background-color: var(--bg-color) !important;
587
- box-shadow: 4px 4px var(--main-color) !important;
588
- min-height: 200px !important;
589
- transition: all 0.2s ease;
590
- }
591
-
592
- .upload-box:hover {
593
- background-color: #f5f5dc !important;
594
- }
595
-
596
- .dropdown-field {
597
- border-radius: 6px !important;
598
- border: 2px solid var(--main-color) !important;
599
- background-color: var(--bg-color) !important;
600
- box-shadow: 4px 4px var(--main-color) !important;
601
- }
602
-
603
- .task-buttons {
604
- display: flex;
605
- gap: 12px;
606
- flex-wrap: wrap;
607
- }
608
-
609
- .task-btn {
610
- flex: 1;
611
- min-width: 100px;
612
- height: 45px;
613
- border-radius: 6px !important;
614
- border: 2px solid var(--main-color) !important;
615
- background-color: var(--bg-color) !important;
616
- box-shadow: 4px 4px var(--main-color) !important;
617
- font-size: 15px !important;
618
- font-weight: 600 !important;
619
- color: var(--font-color) !important;
620
- cursor: pointer;
621
- transition: all 0.1s ease;
622
- }
623
-
624
- .task-btn:hover {
625
- background-color: #f5f5dc !important;
626
- }
627
-
628
- .task-btn.selected, .task-btn:active {
629
- box-shadow: 0px 0px var(--main-color) !important;
630
- transform: translate(3px, 3px);
631
- background-color: #ffd54f !important;
632
- }
633
-
634
- .go-button {
635
- width: 100%;
636
- height: 55px;
637
- border-radius: 8px !important;
638
- border: 3px solid var(--main-color) !important;
639
- background: linear-gradient(135deg, #ffd54f, #ffb300) !important;
640
- box-shadow: 5px 5px var(--main-color) !important;
641
- font-size: 1.2em !important;
642
- font-weight: 700 !important;
643
- color: var(--font-color) !important;
644
- cursor: pointer;
645
- transition: all 0.15s ease;
646
- margin-top: 15px;
647
- }
648
-
649
- .go-button:hover {
650
- background: linear-gradient(135deg, #ffca28, #ffa000) !important;
651
- }
652
-
653
- .go-button:active {
654
- box-shadow: 0px 0px var(--main-color) !important;
655
- transform: translate(4px, 4px);
656
- }
657
-
658
- .output-area {
659
- background-color: var(--bg-color) !important;
660
- border: 2px solid var(--main-color) !important;
661
- border-radius: 8px !important;
662
- box-shadow: 4px 4px var(--main-color) !important;
663
- font-family: 'IBM Plex Mono', monospace !important;
664
- min-height: 300px !important;
665
- padding: 15px !important;
666
- }
667
-
668
- .output-area textarea {
669
- font-family: 'IBM Plex Mono', monospace !important;
670
- background-color: transparent !important;
671
- border: none !important;
672
- box-shadow: none !important;
673
- }
674
-
675
- .example-gallery {
676
- margin-top: 15px;
677
- }
678
-
679
- .example-gallery img {
680
- border: 2px solid var(--main-color) !important;
681
- border-radius: 6px !important;
682
- box-shadow: 3px 3px var(--main-color) !important;
683
- transition: all 0.15s ease;
684
- }
685
-
686
- .example-gallery img:hover {
687
- transform: translate(-2px, -2px);
688
- box-shadow: 5px 5px var(--main-color) !important;
689
- }
690
-
691
- .md-preview {
692
- background-color: var(--bg-color) !important;
693
- border: 2px solid var(--main-color) !important;
694
- border-radius: 8px !important;
695
- box-shadow: 4px 4px var(--main-color) !important;
696
- padding: 20px !important;
697
- min-height: 300px;
698
- }
699
-
700
- .tabs-container button {
701
- border: 2px solid var(--main-color) !important;
702
- background-color: var(--bg-color) !important;
703
- font-weight: 600 !important;
704
- border-radius: 6px 6px 0 0 !important;
705
- margin-right: 5px;
706
- }
707
-
708
- .tabs-container button.selected {
709
- background-color: #ffd54f !important;
710
- box-shadow: none !important;
711
- }
712
-
713
- .footer-note {
714
- text-align: center;
715
- margin-top: 20px;
716
- padding: 15px;
717
- background: rgba(255,255,255,0.5);
718
- border-radius: 8px;
719
- border: 2px solid var(--main-color);
720
- font-size: 0.9em;
721
- color: var(--font-color-sub);
722
- }
723
-
724
- footer {
725
- display: none !important;
726
- }
727
-
728
- .gr-box, .gr-form, .gr-panel {
729
- border: none !important;
730
- background: transparent !important;
731
- box-shadow: none !important;
732
- }
733
-
734
- label {
735
- font-weight: 700 !important;
736
- font-size: 0.9em !important;
737
- color: var(--font-color) !important;
738
- text-transform: uppercase !important;
739
- letter-spacing: 0.5px !important;
740
- }
741
-
742
- .copy-btn {
743
- border: 2px solid var(--main-color) !important;
744
- background-color: var(--bg-color) !important;
745
- box-shadow: 2px 2px var(--main-color) !important;
746
- border-radius: 4px !important;
747
- }
748
-
749
- .copy-btn:active {
750
- box-shadow: 0px 0px var(--main-color) !important;
751
- transform: translate(2px, 2px);
752
- }
753
- """
754
-
755
  with gr.Blocks(title="GLM-OCR", theme=gr.themes.Soft()) as demo:
756
 
757
  gr.HTML("""
 
326
  }
327
  """
328
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
329
  with gr.Blocks(title="GLM-OCR", theme=gr.themes.Soft()) as demo:
330
 
331
  gr.HTML("""