Update app.py
Browse files
app.py
CHANGED
|
@@ -585,67 +585,156 @@ def export_json(image_name, result_text):
|
|
| 585 |
# export_btn.click(fn=lambda: gr.update(visible=True), outputs=[json_file])
|
| 586 |
# --- 10. Gradio UI ---
|
| 587 |
css = """
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
|
|
|
| 591 |
}
|
| 592 |
-
.gradio-
|
| 593 |
-
|
| 594 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 595 |
}
|
| 596 |
"""
|
| 597 |
-
with gr.Blocks(title="Camel-Doc-OCR") as demo:
|
| 598 |
-
gr.Markdown("## 🧾 Camel-Doc-OCR (Qwen2.5-VL, 4-bit)")
|
| 599 |
-
gr.Markdown(" ") # Tạo khoảng cách đầu trang để tránh lỗi scroll
|
| 600 |
-
|
| 601 |
-
with gr.Column(): # 👈 Bao toàn bộ layout bên trong để đảm bảo bố cục không bị vỡ
|
| 602 |
-
|
| 603 |
-
# --- Status Bar ---
|
| 604 |
-
with gr.Row():
|
| 605 |
-
status_txt = gr.Textbox(label="Status & Memory", interactive=False)
|
| 606 |
-
cache_txt = gr.Textbox(label="Cache Stats", interactive=False)
|
| 607 |
-
clear_btn = gr.Button("Clear Cache")
|
| 608 |
-
clear_btn.click(fn=lambda: (cache_clear(), f"Cache: {len(_mru_cache)}/{CACHE_MAX_SIZE}"), outputs=[cache_txt])
|
| 609 |
-
|
| 610 |
-
# --- Input Fields ---
|
| 611 |
-
file_input = gr.File(label="📤 Tải ảnh hoặc PDF", file_types=[".jpg", ".jpeg", ".png", ".pdf"])
|
| 612 |
-
prompt_input = gr.Textbox(label="Prompt thuần", lines=2)
|
| 613 |
-
config_input = gr.Textbox(label="JSON Prompt", lines=12)
|
| 614 |
-
|
| 615 |
-
# --- Prompt Templates ---
|
| 616 |
-
gr.Markdown("### 📑 Chọn mẫu prompt:")
|
| 617 |
-
with gr.Row():
|
| 618 |
-
for key in prompt_templates:
|
| 619 |
-
gr.Button(f"Mẫu {key}").click(
|
| 620 |
-
fn=lambda _, k=key: insert_template(k),
|
| 621 |
-
inputs=[],
|
| 622 |
-
outputs=config_input
|
| 623 |
-
)
|
| 624 |
-
|
| 625 |
-
# --- Run & Export Buttons ---
|
| 626 |
-
run_btn = gr.Button("🚀 Chạy OCR")
|
| 627 |
-
export_btn = gr.Button("📦 Xuất JSON", visible=False)
|
| 628 |
-
|
| 629 |
-
# --- Hidden & Output ---
|
| 630 |
-
hidden_name = gr.Textbox(visible=False)
|
| 631 |
-
result_output = gr.Textbox(label="Kết quả trích xuất", lines=20)
|
| 632 |
-
json_file = gr.File(label="File JSON", visible=False, file_types=[".json"])
|
| 633 |
-
json_text = gr.Code(label="JSON Output", language="json", lines=20)
|
| 634 |
-
|
| 635 |
-
# --- Run Inference ---
|
| 636 |
-
run_btn.click(
|
| 637 |
-
fn=handle_file,
|
| 638 |
-
inputs=[file_input, prompt_input, config_input],
|
| 639 |
-
outputs=[hidden_name, result_output]
|
| 640 |
-
)
|
| 641 |
-
|
| 642 |
-
run_btn.click(fn=get_memory_info, outputs=[status_txt])
|
| 643 |
-
run_btn.click(fn=lambda: f"Cache: {len(_mru_cache)}/{CACHE_MAX_SIZE}", outputs=[cache_txt])
|
| 644 |
-
run_btn.click(fn=lambda: gr.update(visible=True), outputs=[export_btn])
|
| 645 |
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
|
| 650 |
if __name__ == "__main__":
|
| 651 |
demo.launch()
|
|
|
|
| 585 |
# export_btn.click(fn=lambda: gr.update(visible=True), outputs=[json_file])
|
| 586 |
# --- 10. Gradio UI ---
|
| 587 |
css = """
|
| 588 |
+
.gradio-textbox textarea {
|
| 589 |
+
font-size: 13px !important;
|
| 590 |
+
line-height: 1.3 !important;
|
| 591 |
+
padding: 6px 8px !important;
|
| 592 |
}
|
| 593 |
+
.gradio-textbox label {
|
| 594 |
+
font-size: 13px !important;
|
| 595 |
+
font-weight: 600 !important;
|
| 596 |
+
margin-bottom: 4px !important;
|
| 597 |
+
}
|
| 598 |
+
.gradio-button {
|
| 599 |
+
font-size: 12px !important;
|
| 600 |
+
padding: 4px 8px !important;
|
| 601 |
+
height: 28px !important;
|
| 602 |
+
min-height: 28px !important;
|
| 603 |
+
margin: 2px !important;
|
| 604 |
+
}
|
| 605 |
+
.gradio-button[data-variant="primary"] {
|
| 606 |
+
height: 36px !important;
|
| 607 |
+
font-size: 13px !important;
|
| 608 |
+
padding: 8px 16px !important;
|
| 609 |
+
}
|
| 610 |
+
.gradio-file {
|
| 611 |
+
font-size: 13px !important;
|
| 612 |
+
}
|
| 613 |
+
.gradio-file .file-upload {
|
| 614 |
+
padding: 8px !important;
|
| 615 |
+
min-height: 80px !important;
|
| 616 |
+
}
|
| 617 |
+
.gradio-markdown h3 {
|
| 618 |
+
font-size: 14px !important;
|
| 619 |
+
margin: 8px 0 4px 0 !important;
|
| 620 |
+
}
|
| 621 |
+
.gradio-markdown h2 {
|
| 622 |
+
font-size: 18px !important;
|
| 623 |
+
margin: 8px 0 !important;
|
| 624 |
+
}
|
| 625 |
+
.gradio-code {
|
| 626 |
+
font-size: 12px !important;
|
| 627 |
}
|
| 628 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 629 |
|
| 630 |
+
with gr.Blocks(title="Camel-Doc-OCR", css=css) as demo:
|
| 631 |
+
gr.Markdown("## 🧾 Camel-Doc-OCR (Qwen2.5-VL, 4-bit)")
|
| 632 |
+
|
| 633 |
+
# --- Status Bar (Full width) ---
|
| 634 |
+
with gr.Row():
|
| 635 |
+
status_txt = gr.Textbox(label="Status & Memory", interactive=False, scale=2)
|
| 636 |
+
cache_txt = gr.Textbox(label="Cache Stats", interactive=False, scale=1)
|
| 637 |
+
clear_btn = gr.Button("Clear Cache", scale=1)
|
| 638 |
+
clear_btn.click(fn=lambda: (cache_clear(), f"Cache: {len(_mru_cache)}/{CACHE_MAX_SIZE}"), outputs=[cache_txt])
|
| 639 |
+
|
| 640 |
+
# --- Main Layout: 2 Columns ---
|
| 641 |
+
with gr.Row():
|
| 642 |
+
# === LEFT COLUMN: Input ===
|
| 643 |
+
with gr.Column(scale=1):
|
| 644 |
+
gr.Markdown("### 📥 INPUT")
|
| 645 |
+
|
| 646 |
+
# File Input
|
| 647 |
+
file_input = gr.File(
|
| 648 |
+
label="📤 Tải ảnh hoặc PDF",
|
| 649 |
+
file_types=[".jpg", ".jpeg", ".png", ".pdf"],
|
| 650 |
+
height=100
|
| 651 |
+
)
|
| 652 |
+
|
| 653 |
+
# Prompt Input
|
| 654 |
+
prompt_input = gr.Textbox(
|
| 655 |
+
label="Prompt thuần",
|
| 656 |
+
lines=2,
|
| 657 |
+
placeholder="Nhập prompt tùy chỉnh...",
|
| 658 |
+
max_lines=3
|
| 659 |
+
)
|
| 660 |
+
|
| 661 |
+
# JSON Config
|
| 662 |
+
config_input = gr.Textbox(
|
| 663 |
+
label="JSON Prompt",
|
| 664 |
+
lines=6,
|
| 665 |
+
placeholder="Cấu hình JSON sẽ xuất hiện ở đây...",
|
| 666 |
+
max_lines=8
|
| 667 |
+
)
|
| 668 |
+
|
| 669 |
+
# Prompt Templates
|
| 670 |
+
gr.Markdown("### 📑 Mẫu:")
|
| 671 |
+
with gr.Row():
|
| 672 |
+
for key in list(prompt_templates.keys()): # All buttons in one row
|
| 673 |
+
gr.Button(f"{key}", size="sm", scale=1).click(
|
| 674 |
+
fn=lambda *, k=key: insert_template(k),
|
| 675 |
+
inputs=[],
|
| 676 |
+
outputs=config_input
|
| 677 |
+
)
|
| 678 |
+
|
| 679 |
+
# Run Button
|
| 680 |
+
run_btn = gr.Button("🚀 Chạy OCR", variant="primary")
|
| 681 |
+
|
| 682 |
+
# === RIGHT COLUMN: Output ===
|
| 683 |
+
with gr.Column(scale=1):
|
| 684 |
+
gr.Markdown("### 📤 OUTPUT")
|
| 685 |
+
|
| 686 |
+
# Result Output
|
| 687 |
+
result_output = gr.Textbox(
|
| 688 |
+
label="Kết quả trích xuất",
|
| 689 |
+
lines=10,
|
| 690 |
+
placeholder="Kết quả sẽ hiển thị ở đây sau khi chạy OCR...",
|
| 691 |
+
max_lines=12
|
| 692 |
+
)
|
| 693 |
+
|
| 694 |
+
# Export Section
|
| 695 |
+
with gr.Row():
|
| 696 |
+
export_btn = gr.Button("📦 Xuất JSON", visible=False, variant="secondary", size="sm")
|
| 697 |
+
|
| 698 |
+
# JSON Output
|
| 699 |
+
json_text = gr.Code(
|
| 700 |
+
label="JSON Output",
|
| 701 |
+
language="json",
|
| 702 |
+
lines=6,
|
| 703 |
+
visible=False
|
| 704 |
+
)
|
| 705 |
+
|
| 706 |
+
# Download File
|
| 707 |
+
json_file = gr.File(
|
| 708 |
+
label="File JSON để tải",
|
| 709 |
+
visible=False,
|
| 710 |
+
file_types=[".json"]
|
| 711 |
+
)
|
| 712 |
+
|
| 713 |
+
# --- Hidden Fields ---
|
| 714 |
+
hidden_name = gr.Textbox(visible=False)
|
| 715 |
+
|
| 716 |
+
# --- Event Handlers ---
|
| 717 |
+
|
| 718 |
+
# Run Inference
|
| 719 |
+
run_btn.click(
|
| 720 |
+
fn=handle_file,
|
| 721 |
+
inputs=[file_input, prompt_input, config_input],
|
| 722 |
+
outputs=[hidden_name, result_output]
|
| 723 |
+
)
|
| 724 |
+
|
| 725 |
+
run_btn.click(fn=get_memory_info, outputs=[status_txt])
|
| 726 |
+
run_btn.click(fn=lambda: f"Cache: {len(_mru_cache)}/{CACHE_MAX_SIZE}", outputs=[cache_txt])
|
| 727 |
+
run_btn.click(fn=lambda: gr.update(visible=True), outputs=[export_btn])
|
| 728 |
+
|
| 729 |
+
# Export JSON
|
| 730 |
+
export_btn.click(
|
| 731 |
+
fn=export_json,
|
| 732 |
+
inputs=[hidden_name, result_output],
|
| 733 |
+
outputs=[json_file, json_text]
|
| 734 |
+
)
|
| 735 |
+
|
| 736 |
+
export_btn.click(fn=lambda: gr.update(visible=True), outputs=[json_file])
|
| 737 |
+
export_btn.click(fn=lambda: gr.update(visible=True), outputs=[json_text])
|
| 738 |
|
| 739 |
if __name__ == "__main__":
|
| 740 |
demo.launch()
|