Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import json
|
| 3 |
import tempfile
|
| 4 |
import os
|
| 5 |
-
from typing import List, Optional, Literal
|
| 6 |
from PIL import Image
|
| 7 |
import spaces
|
| 8 |
from pathlib import Path
|
|
@@ -120,7 +120,7 @@ PIPELINE_CONFIGS = {
|
|
| 120 |
}
|
| 121 |
|
| 122 |
@spaces.GPU
|
| 123 |
-
def htrflow_htr_url(image_path: str, document_type: FormatChoices = "letter_swedish", output_format: FileChoices = DEFAULT_OUTPUT, custom_settings: Optional[str] = None, server_name= "https://gabriel-htrflow-mcp.hf.space") -> str:
|
| 124 |
"""
|
| 125 |
Process handwritten text recognition (HTR) on uploaded images and return both file content and download link.
|
| 126 |
|
|
@@ -132,7 +132,7 @@ def htrflow_htr_url(image_path: str, document_type: FormatChoices = "letter_swed
|
|
| 132 |
image_path (str): The file path or URL to the image containing handwritten text to be processed.
|
| 133 |
Supports common image formats like JPG, PNG, TIFF.
|
| 134 |
|
| 135 |
-
document_type (
|
| 136 |
Available options:
|
| 137 |
- "letter_english": Single-page English handwritten letters
|
| 138 |
- "letter_swedish": Single-page Swedish handwritten letters (default)
|
|
@@ -140,7 +140,7 @@ def htrflow_htr_url(image_path: str, document_type: FormatChoices = "letter_swed
|
|
| 140 |
- "spread_swedish": Two-page spread Swedish documents with marginalia
|
| 141 |
Default: "letter_swedish"
|
| 142 |
|
| 143 |
-
output_format (
|
| 144 |
Available options:
|
| 145 |
- "txt": Plain text format with line breaks
|
| 146 |
- "alto": ALTO XML format with detailed layout and coordinate information
|
|
@@ -152,15 +152,24 @@ def htrflow_htr_url(image_path: str, document_type: FormatChoices = "letter_swed
|
|
| 152 |
JSON string to override the default processing steps.
|
| 153 |
Default: None (uses predefined configuration for document_type)
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
"""
|
| 162 |
if not image_path:
|
| 163 |
-
|
|
|
|
| 164 |
|
| 165 |
try:
|
| 166 |
original_filename = Path(image_path).stem or "output"
|
|
@@ -169,7 +178,8 @@ def htrflow_htr_url(image_path: str, document_type: FormatChoices = "letter_swed
|
|
| 169 |
try:
|
| 170 |
config = json.loads(custom_settings)
|
| 171 |
except json.JSONDecodeError:
|
| 172 |
-
|
|
|
|
| 173 |
else:
|
| 174 |
config = PIPELINE_CONFIGS[document_type]
|
| 175 |
|
|
@@ -179,7 +189,10 @@ def htrflow_htr_url(image_path: str, document_type: FormatChoices = "letter_swed
|
|
| 179 |
try:
|
| 180 |
processed_collection = pipeline.run(collection)
|
| 181 |
except Exception as pipeline_error:
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
temp_dir = Path(tempfile.mkdtemp())
|
| 185 |
export_dir = temp_dir / output_format
|
|
@@ -197,29 +210,31 @@ def htrflow_htr_url(image_path: str, document_type: FormatChoices = "letter_swed
|
|
| 197 |
break
|
| 198 |
|
| 199 |
if output_file_path and os.path.exists(output_file_path):
|
| 200 |
-
|
| 201 |
with open(output_file_path, 'r', encoding='utf-8') as f:
|
| 202 |
file_content = f.read()
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
file_name = Path(output_file_path).name
|
| 207 |
-
|
|
|
|
|
|
|
| 208 |
markdown_link = f"[{file_name}]({download_url})"
|
| 209 |
|
| 210 |
result = {
|
|
|
|
| 211 |
"content": file_content,
|
| 212 |
"file_path": markdown_link
|
| 213 |
}
|
| 214 |
|
| 215 |
-
|
|
|
|
| 216 |
else:
|
| 217 |
-
|
|
|
|
| 218 |
|
| 219 |
except Exception as e:
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
def htrflow_visualizer(image: str, htr_document: str) -> str:
|
| 224 |
pass
|
| 225 |
|
|
@@ -240,7 +255,10 @@ def create_htrflow_mcp_server():
|
|
| 240 |
gr.Dropdown(choices=FILE_CHOICES, value=DEFAULT_OUTPUT, label="Output Format"),
|
| 241 |
gr.Textbox(label="Custom Settings (JSON)", placeholder="Optional custom pipeline settings", value=""),
|
| 242 |
],
|
| 243 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 244 |
description="Process handwritten text from uploaded file or URL and get both content and download link in JSON format",
|
| 245 |
api_name="htrflow_htr_url",
|
| 246 |
)
|
|
|
|
| 2 |
import json
|
| 3 |
import tempfile
|
| 4 |
import os
|
| 5 |
+
from typing import List, Optional, Literal, Tuple
|
| 6 |
from PIL import Image
|
| 7 |
import spaces
|
| 8 |
from pathlib import Path
|
|
|
|
| 120 |
}
|
| 121 |
|
| 122 |
@spaces.GPU
|
| 123 |
+
def htrflow_htr_url(image_path: str, document_type: FormatChoices = "letter_swedish", output_format: FileChoices = DEFAULT_OUTPUT, custom_settings: Optional[str] = None, server_name: str = "https://gabriel-htrflow-mcp.hf.space") -> Tuple[str, str]:
|
| 124 |
"""
|
| 125 |
Process handwritten text recognition (HTR) on uploaded images and return both file content and download link.
|
| 126 |
|
|
|
|
| 132 |
image_path (str): The file path or URL to the image containing handwritten text to be processed.
|
| 133 |
Supports common image formats like JPG, PNG, TIFF.
|
| 134 |
|
| 135 |
+
document_type (FormatChoices): The type of document and language processing template to use.
|
| 136 |
Available options:
|
| 137 |
- "letter_english": Single-page English handwritten letters
|
| 138 |
- "letter_swedish": Single-page Swedish handwritten letters (default)
|
|
|
|
| 140 |
- "spread_swedish": Two-page spread Swedish documents with marginalia
|
| 141 |
Default: "letter_swedish"
|
| 142 |
|
| 143 |
+
output_format (FileChoices): The format for the output file containing the transcribed text.
|
| 144 |
Available options:
|
| 145 |
- "txt": Plain text format with line breaks
|
| 146 |
- "alto": ALTO XML format with detailed layout and coordinate information
|
|
|
|
| 152 |
JSON string to override the default processing steps.
|
| 153 |
Default: None (uses predefined configuration for document_type)
|
| 154 |
|
| 155 |
+
server_name (str): The base URL of the server for constructing download links.
|
| 156 |
+
Default: "https://gabriel-htrflow-mcp.hf.space"
|
| 157 |
+
|
| 158 |
+
Returns:
|
| 159 |
+
Tuple[str, str]: A tuple containing:
|
| 160 |
+
- JSON string with extracted text, file content and download link
|
| 161 |
+
- File path for direct download via gr.File
|
| 162 |
+
|
| 163 |
+
JSON structure:
|
| 164 |
+
{
|
| 165 |
+
"text": "extracted_plain_text_here",
|
| 166 |
+
"content": "full_file_content_here",
|
| 167 |
+
"file_path": "[file_name](download_url)"
|
| 168 |
+
}
|
| 169 |
"""
|
| 170 |
if not image_path:
|
| 171 |
+
error_json = json.dumps({"error": "No image provided"})
|
| 172 |
+
return error_json, None
|
| 173 |
|
| 174 |
try:
|
| 175 |
original_filename = Path(image_path).stem or "output"
|
|
|
|
| 178 |
try:
|
| 179 |
config = json.loads(custom_settings)
|
| 180 |
except json.JSONDecodeError:
|
| 181 |
+
error_json = json.dumps({"error": "Invalid JSON in custom_settings parameter"})
|
| 182 |
+
return error_json, None
|
| 183 |
else:
|
| 184 |
config = PIPELINE_CONFIGS[document_type]
|
| 185 |
|
|
|
|
| 189 |
try:
|
| 190 |
processed_collection = pipeline.run(collection)
|
| 191 |
except Exception as pipeline_error:
|
| 192 |
+
error_json = json.dumps({"error": f"Pipeline execution failed: {str(pipeline_error)}"})
|
| 193 |
+
return error_json, None
|
| 194 |
+
|
| 195 |
+
extracted_text = extract_text_from_collection(processed_collection)
|
| 196 |
|
| 197 |
temp_dir = Path(tempfile.mkdtemp())
|
| 198 |
export_dir = temp_dir / output_format
|
|
|
|
| 210 |
break
|
| 211 |
|
| 212 |
if output_file_path and os.path.exists(output_file_path):
|
|
|
|
| 213 |
with open(output_file_path, 'r', encoding='utf-8') as f:
|
| 214 |
file_content = f.read()
|
| 215 |
|
|
|
|
|
|
|
| 216 |
file_name = Path(output_file_path).name
|
| 217 |
+
|
| 218 |
+
server_base = server_name.rstrip('/')
|
| 219 |
+
download_url = f"{server_base}/gradio_api/file={os.path.abspath(output_file_path)}"
|
| 220 |
markdown_link = f"[{file_name}]({download_url})"
|
| 221 |
|
| 222 |
result = {
|
| 223 |
+
"text": extracted_text,
|
| 224 |
"content": file_content,
|
| 225 |
"file_path": markdown_link
|
| 226 |
}
|
| 227 |
|
| 228 |
+
json_result = json.dumps(result, ensure_ascii=False, indent=2)
|
| 229 |
+
return json_result, output_file_path
|
| 230 |
else:
|
| 231 |
+
error_json = json.dumps({"error": "Failed to generate output file"})
|
| 232 |
+
return error_json, None
|
| 233 |
|
| 234 |
except Exception as e:
|
| 235 |
+
error_json = json.dumps({"error": f"HTR processing failed: {str(e)}"})
|
| 236 |
+
return error_json, None
|
| 237 |
+
|
| 238 |
def htrflow_visualizer(image: str, htr_document: str) -> str:
|
| 239 |
pass
|
| 240 |
|
|
|
|
| 255 |
gr.Dropdown(choices=FILE_CHOICES, value=DEFAULT_OUTPUT, label="Output Format"),
|
| 256 |
gr.Textbox(label="Custom Settings (JSON)", placeholder="Optional custom pipeline settings", value=""),
|
| 257 |
],
|
| 258 |
+
outputs=[
|
| 259 |
+
gr.Textbox(label="HTR Result (JSON)", lines=10),
|
| 260 |
+
gr.File(label="Download HTR Output File")
|
| 261 |
+
],
|
| 262 |
description="Process handwritten text from uploaded file or URL and get both content and download link in JSON format",
|
| 263 |
api_name="htrflow_htr_url",
|
| 264 |
)
|