Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -116,18 +116,44 @@ PIPELINE_CONFIGS = {
|
|
| 116 |
}
|
| 117 |
|
| 118 |
@spaces.GPU
|
| 119 |
-
def
|
| 120 |
"""
|
| 121 |
-
Process handwritten text recognition and return extracted text
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
Args:
|
| 124 |
-
image_path (str):
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
Returns:
|
| 130 |
-
str: The path to the output file
|
|
|
|
|
|
|
| 131 |
"""
|
| 132 |
if not image_path:
|
| 133 |
return "Error: No image provided"
|
|
@@ -184,7 +210,7 @@ def extract_text_from_collection(collection: Collection) -> str:
|
|
| 184 |
|
| 185 |
def create_htrflow_mcp_server():
|
| 186 |
demo = gr.Interface(
|
| 187 |
-
fn=
|
| 188 |
inputs=[
|
| 189 |
gr.Image(type="filepath", label="Upload Image or Enter URL"),
|
| 190 |
gr.Dropdown(choices=["letter_english", "letter_swedish", "spread_english", "spread_swedish"], value="letter_swedish", label="Document Type"),
|
|
@@ -194,10 +220,10 @@ def create_htrflow_mcp_server():
|
|
| 194 |
outputs=gr.File(label="Download Output File"),
|
| 195 |
title="HTRflow MCP Server",
|
| 196 |
description="Process handwritten text from uploaded file or URL and get output file in specified format",
|
| 197 |
-
api_name="
|
| 198 |
)
|
| 199 |
return demo
|
| 200 |
|
| 201 |
if __name__ == "__main__":
|
| 202 |
demo = create_htrflow_mcp_server()
|
| 203 |
-
demo.launch(mcp_server=True, share=False, debug=
|
|
|
|
| 116 |
}
|
| 117 |
|
| 118 |
@spaces.GPU
|
| 119 |
+
def htrflow_htr(image_path: str, document_type: Literal["letter_english", "letter_swedish", "spread_english", "spread_swedish"] = "letter_swedish", output_format: Literal["txt", "alto", "page", "json"] = DEFAULT_OUTPUT, custom_settings: Optional[str] = None) -> str:
|
| 120 |
"""
|
| 121 |
+
Process handwritten text recognition (HTR) on uploaded images and return extracted text in the specified format.
|
| 122 |
+
|
| 123 |
+
This function uses machine learning models to automatically detect, segment, and transcribe handwritten text
|
| 124 |
+
from historical documents. It supports different document types and languages, with specialized models
|
| 125 |
+
trained on historical handwriting from the Swedish National Archives (Riksarkivet).
|
| 126 |
|
| 127 |
Args:
|
| 128 |
+
image_path (str): The file path or URL to the image containing handwritten text to be processed.
|
| 129 |
+
Supports common image formats like JPG, PNG, TIFF.
|
| 130 |
+
|
| 131 |
+
document_type (Literal): The type of document and language processing template to use.
|
| 132 |
+
Available options:
|
| 133 |
+
- "letter_english": Single-page English handwritten letters (default: "letter_swedish")
|
| 134 |
+
- "letter_swedish": Single-page Swedish handwritten letters
|
| 135 |
+
- "spread_english": Two-page spread English documents with marginalia
|
| 136 |
+
- "spread_swedish": Two-page spread Swedish documents with marginalia
|
| 137 |
+
Default: "letter_swedish"
|
| 138 |
+
|
| 139 |
+
output_format (Literal): The format for the output file containing the transcribed text.
|
| 140 |
+
Available options:
|
| 141 |
+
- "txt": Plain text format with line breaks
|
| 142 |
+
- "alto": ALTO XML format with detailed layout and coordinate information
|
| 143 |
+
- "page": PAGE XML format with structural markup and positioning data
|
| 144 |
+
- "json": JSON format with structured text, layout information and metadata
|
| 145 |
+
Default: "alto"
|
| 146 |
+
Note: Both "alto" and "page" formats are XML-based with layout information.
|
| 147 |
+
|
| 148 |
+
custom_settings (Optional[str]): Advanced users can provide custom pipeline configuration as a
|
| 149 |
+
JSON string to override the default processing steps. This allows
|
| 150 |
+
fine-tuning of model parameters, batch sizes, and processing workflow.
|
| 151 |
+
Default: None (uses predefined configuration for document_type)
|
| 152 |
|
| 153 |
Returns:
|
| 154 |
+
str: The file path to the generated output file containing the transcribed text in the requested format,
|
| 155 |
+
or an error message if processing fails. The output file will be named based on the original
|
| 156 |
+
image filename with the appropriate extension (.txt, .xml, or .json).
|
| 157 |
"""
|
| 158 |
if not image_path:
|
| 159 |
return "Error: No image provided"
|
|
|
|
| 210 |
|
| 211 |
def create_htrflow_mcp_server():
|
| 212 |
demo = gr.Interface(
|
| 213 |
+
fn=htrflow_htr,
|
| 214 |
inputs=[
|
| 215 |
gr.Image(type="filepath", label="Upload Image or Enter URL"),
|
| 216 |
gr.Dropdown(choices=["letter_english", "letter_swedish", "spread_english", "spread_swedish"], value="letter_swedish", label="Document Type"),
|
|
|
|
| 220 |
outputs=gr.File(label="Download Output File"),
|
| 221 |
title="HTRflow MCP Server",
|
| 222 |
description="Process handwritten text from uploaded file or URL and get output file in specified format",
|
| 223 |
+
api_name="htrflow_htr",
|
| 224 |
)
|
| 225 |
return demo
|
| 226 |
|
| 227 |
if __name__ == "__main__":
|
| 228 |
demo = create_htrflow_mcp_server()
|
| 229 |
+
demo.launch(mcp_server=True, share=False, debug=False)
|