Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -116,10 +116,21 @@ PIPELINE_CONFIGS = {
|
|
| 116 |
}
|
| 117 |
|
| 118 |
@spaces.GPU
|
| 119 |
-
def process_htr(image_path: str, document_type: Literal["letter_english", "letter_swedish", "spread_english", "spread_swedish"] = "
|
| 120 |
-
"""
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
try:
|
| 125 |
original_filename = Path(image_path).stem or "output"
|
|
@@ -128,7 +139,7 @@ def process_htr(image_path: str, document_type: Literal["letter_english", "lette
|
|
| 128 |
try:
|
| 129 |
config = json.loads(custom_settings)
|
| 130 |
except json.JSONDecodeError:
|
| 131 |
-
return "Error: Invalid JSON in custom_settings parameter"
|
| 132 |
else:
|
| 133 |
config = PIPELINE_CONFIGS[document_type]
|
| 134 |
|
|
@@ -138,7 +149,7 @@ def process_htr(image_path: str, document_type: Literal["letter_english", "lette
|
|
| 138 |
try:
|
| 139 |
processed_collection = pipeline.run(collection)
|
| 140 |
except Exception as pipeline_error:
|
| 141 |
-
return f"Error: Pipeline execution failed: {str(pipeline_error)}"
|
| 142 |
|
| 143 |
temp_dir = Path(tempfile.mkdtemp())
|
| 144 |
export_dir = temp_dir / output_format
|
|
@@ -155,12 +166,13 @@ def process_htr(image_path: str, document_type: Literal["letter_english", "lette
|
|
| 155 |
output_file_path = new_path
|
| 156 |
break
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
| 161 |
|
| 162 |
except Exception as e:
|
| 163 |
-
return f"Error: HTR processing failed: {str(e)}"
|
| 164 |
|
| 165 |
def extract_text_from_collection(collection: Collection) -> str:
|
| 166 |
text_lines = []
|
|
@@ -175,20 +187,17 @@ def create_htrflow_mcp_server():
|
|
| 175 |
fn=process_htr,
|
| 176 |
inputs=[
|
| 177 |
gr.Image(type="filepath", label="Upload Image or Enter URL"),
|
| 178 |
-
gr.Dropdown(choices=["letter_english", "letter_swedish", "spread_english", "spread_swedish"], value="
|
| 179 |
gr.Dropdown(choices=CHOICES, value=DEFAULT_OUTPUT, label="Output Format"),
|
| 180 |
-
gr.Textbox(label="Custom Settings (JSON)", placeholder="Optional custom pipeline settings"),
|
| 181 |
-
],
|
| 182 |
-
outputs=[
|
| 183 |
-
gr.Textbox(label="Extracted Text", lines=10),
|
| 184 |
-
gr.File(label="Download Output File")
|
| 185 |
],
|
|
|
|
| 186 |
title="HTRflow MCP Server",
|
| 187 |
-
description="Process handwritten text from uploaded file or URL and get
|
| 188 |
api_name="process_htr",
|
| 189 |
)
|
| 190 |
return demo
|
| 191 |
|
| 192 |
if __name__ == "__main__":
|
| 193 |
demo = create_htrflow_mcp_server()
|
| 194 |
-
demo.launch(mcp_server=True)
|
|
|
|
| 116 |
}
|
| 117 |
|
| 118 |
@spaces.GPU
|
| 119 |
+
def process_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 and return extracted text with specified format file.
|
| 122 |
+
|
| 123 |
+
Args:
|
| 124 |
+
image_path (str): Path to the image file to process
|
| 125 |
+
document_type (str): Type of document processing template to use
|
| 126 |
+
output_format (str): Output format for the processed file
|
| 127 |
+
custom_settings (str): Optional custom pipeline settings as JSON
|
| 128 |
+
|
| 129 |
+
Returns:
|
| 130 |
+
str: The path to the output file or error message
|
| 131 |
+
"""
|
| 132 |
+
if not image_path:
|
| 133 |
+
return "Error: No image provided"
|
| 134 |
|
| 135 |
try:
|
| 136 |
original_filename = Path(image_path).stem or "output"
|
|
|
|
| 139 |
try:
|
| 140 |
config = json.loads(custom_settings)
|
| 141 |
except json.JSONDecodeError:
|
| 142 |
+
return "Error: Invalid JSON in custom_settings parameter"
|
| 143 |
else:
|
| 144 |
config = PIPELINE_CONFIGS[document_type]
|
| 145 |
|
|
|
|
| 149 |
try:
|
| 150 |
processed_collection = pipeline.run(collection)
|
| 151 |
except Exception as pipeline_error:
|
| 152 |
+
return f"Error: Pipeline execution failed: {str(pipeline_error)}"
|
| 153 |
|
| 154 |
temp_dir = Path(tempfile.mkdtemp())
|
| 155 |
export_dir = temp_dir / output_format
|
|
|
|
| 166 |
output_file_path = new_path
|
| 167 |
break
|
| 168 |
|
| 169 |
+
if output_file_path and os.path.exists(output_file_path):
|
| 170 |
+
return output_file_path
|
| 171 |
+
else:
|
| 172 |
+
return "Error: Failed to generate output file"
|
| 173 |
|
| 174 |
except Exception as e:
|
| 175 |
+
return f"Error: HTR processing failed: {str(e)}"
|
| 176 |
|
| 177 |
def extract_text_from_collection(collection: Collection) -> str:
|
| 178 |
text_lines = []
|
|
|
|
| 187 |
fn=process_htr,
|
| 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"),
|
| 191 |
gr.Dropdown(choices=CHOICES, value=DEFAULT_OUTPUT, label="Output Format"),
|
| 192 |
+
gr.Textbox(label="Custom Settings (JSON)", placeholder="Optional custom pipeline settings", value=""),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
],
|
| 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="process_htr",
|
| 198 |
)
|
| 199 |
return demo
|
| 200 |
|
| 201 |
if __name__ == "__main__":
|
| 202 |
demo = create_htrflow_mcp_server()
|
| 203 |
+
demo.launch(mcp_server=True, share=False, debug=True)
|