Update app.py
Browse files
app.py
CHANGED
|
@@ -535,54 +535,107 @@ def export_json(image_name, result_text):
|
|
| 535 |
except Exception as e:
|
| 536 |
return "", f"[Export JSON Failed]: {e}"
|
| 537 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 538 |
# --- 10. Gradio UI ---
|
| 539 |
with gr.Blocks(title="Camel-Doc-OCR") as demo:
|
| 540 |
-
gr.Markdown("Camel-Doc-OCR (Qwen2.5-VL, 4-bit)")
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
run_btn.click(fn=lambda: gr.update(visible=True), outputs=[export_btn])
|
| 582 |
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
|
| 587 |
if __name__ == "__main__":
|
| 588 |
demo.launch()
|
|
|
|
| 535 |
except Exception as e:
|
| 536 |
return "", f"[Export JSON Failed]: {e}"
|
| 537 |
|
| 538 |
+
# --- 10. Gradio UI ---
|
| 539 |
+
# with gr.Blocks(title="Camel-Doc-OCR") as demo:
|
| 540 |
+
# gr.Markdown("Camel-Doc-OCR (Qwen2.5-VL, 4-bit)")
|
| 541 |
+
|
| 542 |
+
# status_txt = gr.Textbox(label="Status & Memory", interactive=False)
|
| 543 |
+
# cache_txt = gr.Textbox(label="Cache Stats", interactive=False)
|
| 544 |
+
# clear_btn = gr.Button("Clear Cache")
|
| 545 |
+
# clear_btn.click(fn=lambda: (cache_clear(), f"Cache: {len(_mru_cache)}/{CACHE_MAX_SIZE}"), outputs=[cache_txt])
|
| 546 |
+
|
| 547 |
+
# file_input = gr.File(label="Tải ảnh hoặc PDF", file_types=[".jpg", ".jpeg", ".png", ".pdf"])
|
| 548 |
+
# prompt_input = gr.Textbox(label="Prompt thuần", lines=2)
|
| 549 |
+
# config_input = gr.Textbox(label="JSON Prompt", lines=12)
|
| 550 |
+
|
| 551 |
+
# gr.Markdown("Chọn mẫu prompt:")
|
| 552 |
+
# with gr.Row():
|
| 553 |
+
# # for key in prompt_templates:
|
| 554 |
+
# # btn = gr.Button(f"Mẫu {key}")
|
| 555 |
+
# # btn.click(fn=insert_template, inputs=[gr.State(key)], outputs=config_input)
|
| 556 |
+
# for key in prompt_templates:
|
| 557 |
+
# gr.Button(f"Mẫu {key}").click(
|
| 558 |
+
# fn=lambda k=key: insert_template(k),
|
| 559 |
+
# outputs=config_input
|
| 560 |
+
# )
|
| 561 |
+
|
| 562 |
+
|
| 563 |
+
# run_btn = gr.Button("Chạy OCR")
|
| 564 |
+
# export_btn = gr.Button("Xuất JSON", visible=False)
|
| 565 |
+
|
| 566 |
+
# hidden_name = gr.Textbox(visible=False)
|
| 567 |
+
# result_output = gr.Textbox(label="Kết quả trích xuất", lines=20)
|
| 568 |
+
# json_file = gr.File(label="File JSON", visible=False, file_types=[".json"])
|
| 569 |
+
# json_text = gr.Code(label="JSON Output", language="json", lines=20)
|
| 570 |
+
|
| 571 |
+
# # Run inference
|
| 572 |
+
# run_btn.click(
|
| 573 |
+
# fn=handle_file,
|
| 574 |
+
# inputs=[file_input, prompt_input, config_input],
|
| 575 |
+
# outputs=[hidden_name, result_output]
|
| 576 |
+
# )
|
| 577 |
+
|
| 578 |
+
# # Update memory status
|
| 579 |
+
# run_btn.click(fn=lambda: get_memory_info(), outputs=[status_txt])
|
| 580 |
+
# run_btn.click(fn=lambda: f"Cache: {len(_mru_cache)}/{CACHE_MAX_SIZE}", outputs=[cache_txt])
|
| 581 |
+
# run_btn.click(fn=lambda: gr.update(visible=True), outputs=[export_btn])
|
| 582 |
+
|
| 583 |
+
# # Export
|
| 584 |
+
# export_btn.click(fn=export_json, inputs=[hidden_name, result_output], outputs=[json_file, json_text])
|
| 585 |
+
# export_btn.click(fn=lambda: gr.update(visible=True), outputs=[json_file])
|
| 586 |
# --- 10. Gradio UI ---
|
| 587 |
with gr.Blocks(title="Camel-Doc-OCR") as demo:
|
| 588 |
+
gr.Markdown("## 🧾 Camel-Doc-OCR (Qwen2.5-VL, 4-bit)")
|
| 589 |
+
gr.Markdown(" ") # Tạo khoảng cách đầu trang để tránh lỗi scroll
|
| 590 |
+
|
| 591 |
+
with gr.Column(): # 👈 Bao toàn bộ layout bên trong để đảm bảo bố cục không bị vỡ
|
| 592 |
+
|
| 593 |
+
# --- Status Bar ---
|
| 594 |
+
with gr.Row():
|
| 595 |
+
status_txt = gr.Textbox(label="Status & Memory", interactive=False)
|
| 596 |
+
cache_txt = gr.Textbox(label="Cache Stats", interactive=False)
|
| 597 |
+
clear_btn = gr.Button("Clear Cache")
|
| 598 |
+
clear_btn.click(fn=lambda: (cache_clear(), f"Cache: {len(_mru_cache)}/{CACHE_MAX_SIZE}"), outputs=[cache_txt])
|
| 599 |
+
|
| 600 |
+
# --- Input Fields ---
|
| 601 |
+
file_input = gr.File(label="📤 Tải ảnh hoặc PDF", file_types=[".jpg", ".jpeg", ".png", ".pdf"])
|
| 602 |
+
prompt_input = gr.Textbox(label="Prompt thuần", lines=2)
|
| 603 |
+
config_input = gr.Textbox(label="JSON Prompt", lines=12)
|
| 604 |
+
|
| 605 |
+
# --- Prompt Templates ---
|
| 606 |
+
gr.Markdown("### 📑 Chọn mẫu prompt:")
|
| 607 |
+
with gr.Row():
|
| 608 |
+
for key in prompt_templates:
|
| 609 |
+
gr.Button(f"M��u {key}").click(
|
| 610 |
+
fn=lambda _, k=key: insert_template(k),
|
| 611 |
+
inputs=[],
|
| 612 |
+
outputs=config_input
|
| 613 |
+
)
|
| 614 |
+
|
| 615 |
+
# --- Run & Export Buttons ---
|
| 616 |
+
run_btn = gr.Button("🚀 Chạy OCR")
|
| 617 |
+
export_btn = gr.Button("📦 Xuất JSON", visible=False)
|
| 618 |
+
|
| 619 |
+
# --- Hidden & Output ---
|
| 620 |
+
hidden_name = gr.Textbox(visible=False)
|
| 621 |
+
result_output = gr.Textbox(label="Kết quả trích xuất", lines=20)
|
| 622 |
+
json_file = gr.File(label="File JSON", visible=False, file_types=[".json"])
|
| 623 |
+
json_text = gr.Code(label="JSON Output", language="json", lines=20)
|
| 624 |
+
|
| 625 |
+
# --- Run Inference ---
|
| 626 |
+
run_btn.click(
|
| 627 |
+
fn=handle_file,
|
| 628 |
+
inputs=[file_input, prompt_input, config_input],
|
| 629 |
+
outputs=[hidden_name, result_output]
|
| 630 |
+
)
|
| 631 |
|
| 632 |
+
run_btn.click(fn=get_memory_info, outputs=[status_txt])
|
| 633 |
+
run_btn.click(fn=lambda: f"Cache: {len(_mru_cache)}/{CACHE_MAX_SIZE}", outputs=[cache_txt])
|
| 634 |
+
run_btn.click(fn=lambda: gr.update(visible=True), outputs=[export_btn])
|
|
|
|
| 635 |
|
| 636 |
+
# --- Export JSON ---
|
| 637 |
+
export_btn.click(fn=export_json, inputs=[hidden_name, result_output], outputs=[json_file, json_text])
|
| 638 |
+
export_btn.click(fn=lambda: gr.update(visible=True), outputs=[json_file])
|
| 639 |
|
| 640 |
if __name__ == "__main__":
|
| 641 |
demo.launch()
|