Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -481,7 +481,7 @@ def htr_generate_files(
|
|
| 481 |
|
| 482 |
def htr_visualize(
|
| 483 |
image_input: Union[str, List[str]],
|
| 484 |
-
htr_documents: Union[
|
| 485 |
progress: gr.Progress = gr.Progress()
|
| 486 |
) -> str:
|
| 487 |
"""
|
|
@@ -502,14 +502,33 @@ def htr_visualize(
|
|
| 502 |
|
| 503 |
# Parse inputs
|
| 504 |
image_paths = parse_image_input(image_input)
|
| 505 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
|
| 507 |
if not image_paths:
|
| 508 |
raise ValueError("No images provided")
|
| 509 |
|
| 510 |
-
if not htr_paths:
|
| 511 |
-
raise ValueError("No HTR documents provided")
|
| 512 |
-
|
| 513 |
if len(image_paths) != len(htr_paths):
|
| 514 |
raise ValueError(f"Number of images ({len(image_paths)}) doesn't match number of HTR documents ({len(htr_paths)})")
|
| 515 |
|
|
@@ -536,14 +555,20 @@ def htr_visualize(
|
|
| 536 |
if processed_image.startswith(tempfile.gettempdir()):
|
| 537 |
temp_files.append(processed_image)
|
| 538 |
|
| 539 |
-
# Generate visualization
|
| 540 |
-
|
|
|
|
| 541 |
|
| 542 |
-
if
|
| 543 |
-
|
|
|
|
|
|
|
|
|
|
| 544 |
viz_path = temp_dir / f"{image_name}_visualization.png"
|
| 545 |
shutil.move(viz_result, str(viz_path))
|
| 546 |
output_files.append(viz_path)
|
|
|
|
|
|
|
| 547 |
|
| 548 |
except Exception as e:
|
| 549 |
# Create error file for this visualization
|
|
@@ -551,6 +576,7 @@ def htr_visualize(
|
|
| 551 |
with open(error_path, 'w') as f:
|
| 552 |
f.write(f"Visualization failed: {str(e)}")
|
| 553 |
output_files.append(error_path)
|
|
|
|
| 554 |
|
| 555 |
# Cleanup temp files
|
| 556 |
for temp_file in temp_files:
|
|
@@ -619,7 +645,7 @@ def create_htrflow_mcp_server():
|
|
| 619 |
outputs=[gr.Textbox(label="Extracted Text", lines=20)],
|
| 620 |
title="Extract Text from Handwritten Documents",
|
| 621 |
description="Process one or more handwritten document images. Works with letters and book spreads in English and Swedish.",
|
| 622 |
-
api_name="
|
| 623 |
)
|
| 624 |
|
| 625 |
# HTR File generation interface
|
|
@@ -653,7 +679,7 @@ def create_htrflow_mcp_server():
|
|
| 653 |
outputs=[gr.File(label="Download HTR Output")],
|
| 654 |
title="Generate HTR Output Files",
|
| 655 |
description="Process handwritten documents and export in various formats. Returns ZIP for multiple files.",
|
| 656 |
-
api_name="
|
| 657 |
)
|
| 658 |
|
| 659 |
# HTR Visualization interface
|
|
@@ -674,32 +700,7 @@ def create_htrflow_mcp_server():
|
|
| 674 |
outputs=gr.File(label="Download Visualization"),
|
| 675 |
title="Visualize HTR Results",
|
| 676 |
description="Create annotated images showing detected regions and text. Files must be in matching order.",
|
| 677 |
-
api_name="
|
| 678 |
-
)
|
| 679 |
-
|
| 680 |
-
# Simplified interface for lambda compatibility (keeping for backward compatibility)
|
| 681 |
-
simple_text_interface = gr.Interface(
|
| 682 |
-
fn=lambda img, doc_type, settings: htr_text(img, doc_type, settings, "separate"),
|
| 683 |
-
inputs=[
|
| 684 |
-
gr.Image(type="filepath", label="Upload Image"),
|
| 685 |
-
gr.Dropdown(choices=FORMAT_CHOICES, value="letter_swedish", label="Document Type"),
|
| 686 |
-
gr.Textbox(label="Custom Settings (JSON)", value="", lines=3),
|
| 687 |
-
],
|
| 688 |
-
outputs=[gr.Textbox(label="Extracted Text", lines=15)],
|
| 689 |
-
api_name="_lambda_",
|
| 690 |
-
)
|
| 691 |
-
|
| 692 |
-
simple_file_interface = gr.Interface(
|
| 693 |
-
fn=lambda img, doc_type, fmt, settings, srv: htr_generate_files(img, doc_type, fmt, settings),
|
| 694 |
-
inputs=[
|
| 695 |
-
gr.Image(type="filepath"),
|
| 696 |
-
gr.Dropdown(choices=FORMAT_CHOICES, value="letter_swedish"),
|
| 697 |
-
gr.Dropdown(choices=FILE_CHOICES, value=DEFAULT_OUTPUT),
|
| 698 |
-
gr.Textbox(value="", lines=3),
|
| 699 |
-
gr.Textbox(value="https://gabriel-htrflow-mcp.hf.space", visible=False),
|
| 700 |
-
],
|
| 701 |
-
outputs=[gr.File()],
|
| 702 |
-
api_name="_lambda__1",
|
| 703 |
)
|
| 704 |
|
| 705 |
# Create tabbed interface
|
|
@@ -708,27 +709,16 @@ def create_htrflow_mcp_server():
|
|
| 708 |
htr_text_interface,
|
| 709 |
htr_files_interface,
|
| 710 |
htr_viz_interface,
|
| 711 |
-
simple_text_interface,
|
| 712 |
-
simple_file_interface,
|
| 713 |
],
|
| 714 |
[
|
| 715 |
"๐ Extract Text",
|
| 716 |
"๐ Generate Files",
|
| 717 |
"๐ผ๏ธ Visualize Results",
|
| 718 |
-
"", # Hidden tabs for backward compatibility
|
| 719 |
-
"",
|
| 720 |
],
|
| 721 |
title="๐๏ธ HTRflow - Handwritten Text Recognition",
|
| 722 |
analytics_enabled=False,
|
| 723 |
)
|
| 724 |
|
| 725 |
-
# Hide the last two tabs (for backward compatibility only)
|
| 726 |
-
demo.css = """
|
| 727 |
-
.tabitem:nth-child(4), .tabitem:nth-child(5) {
|
| 728 |
-
display: none !important;
|
| 729 |
-
}
|
| 730 |
-
"""
|
| 731 |
-
|
| 732 |
return demo
|
| 733 |
|
| 734 |
|
|
|
|
| 481 |
|
| 482 |
def htr_visualize(
|
| 483 |
image_input: Union[str, List[str]],
|
| 484 |
+
htr_documents: Union[List[str], None],
|
| 485 |
progress: gr.Progress = gr.Progress()
|
| 486 |
) -> str:
|
| 487 |
"""
|
|
|
|
| 502 |
|
| 503 |
# Parse inputs
|
| 504 |
image_paths = parse_image_input(image_input)
|
| 505 |
+
|
| 506 |
+
# Handle htr_documents - it should be a list of file paths
|
| 507 |
+
if not htr_documents:
|
| 508 |
+
raise ValueError("No HTR documents provided")
|
| 509 |
+
|
| 510 |
+
# If htr_documents is a list of file objects from Gradio, extract paths
|
| 511 |
+
htr_paths = []
|
| 512 |
+
if isinstance(htr_documents, list):
|
| 513 |
+
for doc in htr_documents:
|
| 514 |
+
if isinstance(doc, str):
|
| 515 |
+
htr_paths.append(doc)
|
| 516 |
+
elif hasattr(doc, 'name'):
|
| 517 |
+
htr_paths.append(doc.name)
|
| 518 |
+
else:
|
| 519 |
+
htr_paths.append(str(doc))
|
| 520 |
+
else:
|
| 521 |
+
# Single file case
|
| 522 |
+
if isinstance(htr_documents, str):
|
| 523 |
+
htr_paths = [htr_documents]
|
| 524 |
+
elif hasattr(htr_documents, 'name'):
|
| 525 |
+
htr_paths = [htr_documents.name]
|
| 526 |
+
else:
|
| 527 |
+
htr_paths = [str(htr_documents)]
|
| 528 |
|
| 529 |
if not image_paths:
|
| 530 |
raise ValueError("No images provided")
|
| 531 |
|
|
|
|
|
|
|
|
|
|
| 532 |
if len(image_paths) != len(htr_paths):
|
| 533 |
raise ValueError(f"Number of images ({len(image_paths)}) doesn't match number of HTR documents ({len(htr_paths)})")
|
| 534 |
|
|
|
|
| 555 |
if processed_image.startswith(tempfile.gettempdir()):
|
| 556 |
temp_files.append(processed_image)
|
| 557 |
|
| 558 |
+
# Generate visualization - use the last parameter for output path
|
| 559 |
+
output_viz_path = str(temp_dir / f"{image_name}_visualization.png")
|
| 560 |
+
viz_result = htrflow_visualizer(processed_image, htr_path, output_viz_path)
|
| 561 |
|
| 562 |
+
# Check if visualization was created
|
| 563 |
+
if os.path.exists(output_viz_path):
|
| 564 |
+
output_files.append(Path(output_viz_path))
|
| 565 |
+
elif viz_result and os.path.exists(viz_result):
|
| 566 |
+
# Fallback: if viz_result points to a different file
|
| 567 |
viz_path = temp_dir / f"{image_name}_visualization.png"
|
| 568 |
shutil.move(viz_result, str(viz_path))
|
| 569 |
output_files.append(viz_path)
|
| 570 |
+
else:
|
| 571 |
+
raise ValueError("Visualization generation failed - no output file created")
|
| 572 |
|
| 573 |
except Exception as e:
|
| 574 |
# Create error file for this visualization
|
|
|
|
| 576 |
with open(error_path, 'w') as f:
|
| 577 |
f.write(f"Visualization failed: {str(e)}")
|
| 578 |
output_files.append(error_path)
|
| 579 |
+
print(f"Error visualizing {image_name}: {str(e)}")
|
| 580 |
|
| 581 |
# Cleanup temp files
|
| 582 |
for temp_file in temp_files:
|
|
|
|
| 645 |
outputs=[gr.Textbox(label="Extracted Text", lines=20)],
|
| 646 |
title="Extract Text from Handwritten Documents",
|
| 647 |
description="Process one or more handwritten document images. Works with letters and book spreads in English and Swedish.",
|
| 648 |
+
api_name="htr_text",
|
| 649 |
)
|
| 650 |
|
| 651 |
# HTR File generation interface
|
|
|
|
| 679 |
outputs=[gr.File(label="Download HTR Output")],
|
| 680 |
title="Generate HTR Output Files",
|
| 681 |
description="Process handwritten documents and export in various formats. Returns ZIP for multiple files.",
|
| 682 |
+
api_name="htr_generate_files",
|
| 683 |
)
|
| 684 |
|
| 685 |
# HTR Visualization interface
|
|
|
|
| 700 |
outputs=gr.File(label="Download Visualization"),
|
| 701 |
title="Visualize HTR Results",
|
| 702 |
description="Create annotated images showing detected regions and text. Files must be in matching order.",
|
| 703 |
+
api_name="htr_visualize",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 704 |
)
|
| 705 |
|
| 706 |
# Create tabbed interface
|
|
|
|
| 709 |
htr_text_interface,
|
| 710 |
htr_files_interface,
|
| 711 |
htr_viz_interface,
|
|
|
|
|
|
|
| 712 |
],
|
| 713 |
[
|
| 714 |
"๐ Extract Text",
|
| 715 |
"๐ Generate Files",
|
| 716 |
"๐ผ๏ธ Visualize Results",
|
|
|
|
|
|
|
| 717 |
],
|
| 718 |
title="๐๏ธ HTRflow - Handwritten Text Recognition",
|
| 719 |
analytics_enabled=False,
|
| 720 |
)
|
| 721 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 722 |
return demo
|
| 723 |
|
| 724 |
|