Update app.py
Browse files
app.py
CHANGED
@@ -120,72 +120,49 @@ PIPELINE_CONFIGS = {
|
|
120 |
}
|
121 |
|
122 |
@spaces.GPU
|
123 |
-
def
|
124 |
-
"""
|
125 |
-
Process handwritten text recognition (HTR) on uploaded images and return both file content and download link.
|
126 |
-
|
127 |
-
This function uses machine learning models to automatically detect, segment, and transcribe handwritten text
|
128 |
-
from historical documents. It supports different document types and languages, with specialized models
|
129 |
-
trained on historical handwriting from the Swedish National Archives (Riksarkivet).
|
130 |
-
|
131 |
-
Args:
|
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)
|
139 |
-
- "spread_english": Two-page spread English documents with marginalia
|
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
|
147 |
-
- "page": PAGE XML format with structural markup and positioning data
|
148 |
-
- "json": JSON format with structured text, layout information and metadata
|
149 |
-
Default: "alto"
|
150 |
-
|
151 |
-
custom_settings (Optional[str]): Advanced users can provide custom pipeline configuration as a
|
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
|
161 |
-
- File path for direct download via gr.File (server_name/gradio_api/file=/tmp/gradio/{temp_folder}/{file_name})
|
162 |
-
"""
|
163 |
if not image_path:
|
164 |
-
|
165 |
-
return error_json, None
|
166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
try:
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
config = json.loads(custom_settings)
|
173 |
-
except json.JSONDecodeError:
|
174 |
-
error_json = json.dumps({"error": "Invalid JSON in custom_settings parameter"})
|
175 |
-
return error_json, None
|
176 |
-
else:
|
177 |
-
config = PIPELINE_CONFIGS[document_type]
|
178 |
|
179 |
-
|
180 |
-
|
|
|
|
|
|
|
|
|
181 |
|
182 |
-
|
183 |
-
|
184 |
-
except Exception as pipeline_error:
|
185 |
-
error_json = json.dumps({"error": f"Pipeline execution failed: {str(pipeline_error)}"})
|
186 |
-
return error_json, None
|
187 |
|
188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
|
190 |
temp_dir = Path(tempfile.mkdtemp())
|
191 |
export_dir = temp_dir / output_format
|
@@ -203,23 +180,12 @@ def htrflow_htr_url(image_path: str, document_type: FormatChoices = "letter_swed
|
|
203 |
break
|
204 |
|
205 |
if output_file_path and os.path.exists(output_file_path):
|
206 |
-
|
207 |
-
file_content = f.read()
|
208 |
-
|
209 |
-
result = {
|
210 |
-
"text": extracted_text,
|
211 |
-
"content": file_content,
|
212 |
-
}
|
213 |
-
|
214 |
-
json_result = json.dumps(result, ensure_ascii=False, indent=2)
|
215 |
-
return json_result, output_file_path
|
216 |
else:
|
217 |
-
|
218 |
-
return error_json, None
|
219 |
|
220 |
except Exception as e:
|
221 |
-
|
222 |
-
return error_json, None
|
223 |
|
224 |
def htrflow_visualizer(image: str, htr_document: str) -> str:
|
225 |
pass
|
@@ -233,20 +199,34 @@ def extract_text_from_collection(collection: Collection) -> str:
|
|
233 |
return "\n".join(text_lines)
|
234 |
|
235 |
def create_htrflow_mcp_server():
|
236 |
-
|
237 |
-
fn=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
inputs=[
|
239 |
gr.Image(type="filepath", label="Upload Image or Enter URL"),
|
240 |
gr.Dropdown(choices=FORMAT_CHOICES, value="letter_swedish", label="Document Type"),
|
241 |
gr.Dropdown(choices=FILE_CHOICES, value=DEFAULT_OUTPUT, label="Output Format"),
|
242 |
gr.Textbox(label="Custom Settings (JSON)", placeholder="Optional custom pipeline settings", value=""),
|
|
|
243 |
],
|
244 |
outputs=[
|
245 |
-
gr.Textbox(label="HTR Result (JSON)", lines=10),
|
246 |
gr.File(label="Download HTR Output File")
|
247 |
],
|
248 |
-
description="Process handwritten text
|
249 |
-
api_name="
|
250 |
)
|
251 |
|
252 |
htrflow_viz = gr.Interface(
|
@@ -261,8 +241,8 @@ def create_htrflow_mcp_server():
|
|
261 |
)
|
262 |
|
263 |
demo = gr.TabbedInterface(
|
264 |
-
[
|
265 |
-
["HTR
|
266 |
title="HTRflow Handwritten Text Recognition",
|
267 |
)
|
268 |
|
@@ -270,4 +250,4 @@ def create_htrflow_mcp_server():
|
|
270 |
|
271 |
if __name__ == "__main__":
|
272 |
demo = create_htrflow_mcp_server()
|
273 |
-
demo.launch(mcp_server=True, share=False, debug=False)
|
|
|
120 |
}
|
121 |
|
122 |
@spaces.GPU
|
123 |
+
def _process_htr_pipeline(image_path: str, document_type: FormatChoices, custom_settings: Optional[str] = None) -> Collection:
|
124 |
+
"""Process HTR pipeline and return the processed collection."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
if not image_path:
|
126 |
+
raise ValueError("No image provided")
|
|
|
127 |
|
128 |
+
if custom_settings:
|
129 |
+
try:
|
130 |
+
config = json.loads(custom_settings)
|
131 |
+
except json.JSONDecodeError:
|
132 |
+
raise ValueError("Invalid JSON in custom_settings parameter")
|
133 |
+
else:
|
134 |
+
config = PIPELINE_CONFIGS[document_type]
|
135 |
+
|
136 |
+
collection = Collection([image_path])
|
137 |
+
pipeline = Pipeline.from_config(config)
|
138 |
+
|
139 |
try:
|
140 |
+
processed_collection = pipeline.run(collection)
|
141 |
+
return processed_collection
|
142 |
+
except Exception as pipeline_error:
|
143 |
+
raise RuntimeError(f"Pipeline execution failed: {str(pipeline_error)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
+
def htr_text(image_path: str, document_type: FormatChoices = "letter_swedish", custom_settings: Optional[str] = None) -> str:
|
146 |
+
"""Extract text from handwritten documents using HTR."""
|
147 |
+
try:
|
148 |
+
processed_collection = _process_htr_pipeline(image_path, document_type, custom_settings)
|
149 |
+
extracted_text = extract_text_from_collection(processed_collection)
|
150 |
+
return extracted_text
|
151 |
|
152 |
+
except Exception as e:
|
153 |
+
return f"HTR text extraction failed: {str(e)}"
|
|
|
|
|
|
|
154 |
|
155 |
+
def htrflow_file(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") -> str:
|
156 |
+
"""
|
157 |
+
Process HTR and return a formatted file for download.
|
158 |
+
|
159 |
+
Returns:
|
160 |
+
str: File path for direct download via gr.File (server_name/gradio_api/file=/tmp/gradio/{temp_folder}/{file_name})
|
161 |
+
"""
|
162 |
+
try:
|
163 |
+
original_filename = Path(image_path).stem or "output"
|
164 |
+
|
165 |
+
processed_collection = _process_htr_pipeline(image_path, document_type, custom_settings)
|
166 |
|
167 |
temp_dir = Path(tempfile.mkdtemp())
|
168 |
export_dir = temp_dir / output_format
|
|
|
180 |
break
|
181 |
|
182 |
if output_file_path and os.path.exists(output_file_path):
|
183 |
+
return output_file_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
else:
|
185 |
+
return None
|
|
|
186 |
|
187 |
except Exception as e:
|
188 |
+
return None
|
|
|
189 |
|
190 |
def htrflow_visualizer(image: str, htr_document: str) -> str:
|
191 |
pass
|
|
|
199 |
return "\n".join(text_lines)
|
200 |
|
201 |
def create_htrflow_mcp_server():
|
202 |
+
htr_text_interface = gr.Interface(
|
203 |
+
fn=htr_text,
|
204 |
+
inputs=[
|
205 |
+
gr.Image(type="filepath", label="Upload Image or Enter URL"),
|
206 |
+
gr.Dropdown(choices=FORMAT_CHOICES, value="letter_swedish", label="Document Type"),
|
207 |
+
gr.Textbox(label="Custom Settings (JSON)", placeholder="Optional custom pipeline settings", value=""),
|
208 |
+
],
|
209 |
+
outputs=[
|
210 |
+
gr.Textbox(label="Extracted Text", lines=10)
|
211 |
+
],
|
212 |
+
description="Extract plain text from handwritten documents using HTR",
|
213 |
+
api_name="htr_text",
|
214 |
+
)
|
215 |
+
|
216 |
+
htrflow_file_interface = gr.Interface(
|
217 |
+
fn=htrflow_file,
|
218 |
inputs=[
|
219 |
gr.Image(type="filepath", label="Upload Image or Enter URL"),
|
220 |
gr.Dropdown(choices=FORMAT_CHOICES, value="letter_swedish", label="Document Type"),
|
221 |
gr.Dropdown(choices=FILE_CHOICES, value=DEFAULT_OUTPUT, label="Output Format"),
|
222 |
gr.Textbox(label="Custom Settings (JSON)", placeholder="Optional custom pipeline settings", value=""),
|
223 |
+
gr.Textbox(label="Server Name", value="https://gabriel-htrflow-mcp.hf.space", placeholder="Server URL for download links"),
|
224 |
],
|
225 |
outputs=[
|
|
|
226 |
gr.File(label="Download HTR Output File")
|
227 |
],
|
228 |
+
description="Process handwritten text and get formatted file (ALTO XML, PAGE XML, JSON, or TXT)",
|
229 |
+
api_name="htrflow_file",
|
230 |
)
|
231 |
|
232 |
htrflow_viz = gr.Interface(
|
|
|
241 |
)
|
242 |
|
243 |
demo = gr.TabbedInterface(
|
244 |
+
[htr_text_interface, htrflow_file_interface, htrflow_viz],
|
245 |
+
["HTR Text", "HTR File", "HTR Visualizer"],
|
246 |
title="HTRflow Handwritten Text Recognition",
|
247 |
)
|
248 |
|
|
|
250 |
|
251 |
if __name__ == "__main__":
|
252 |
demo = create_htrflow_mcp_server()
|
253 |
+
demo.launch(mcp_server=True, share=False, debug=False)#
|