Update app.py
Browse files
app.py
CHANGED
|
@@ -116,16 +116,14 @@ PIPELINE_CONFIGS = {
|
|
| 116 |
}
|
| 117 |
|
| 118 |
@spaces.GPU
|
| 119 |
-
def process_htr(
|
| 120 |
"""Process handwritten text recognition and return extracted text with specified format file."""
|
| 121 |
-
if
|
| 122 |
return "Error: No image provided", None
|
| 123 |
|
| 124 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
|
| 125 |
-
image.save(temp_file.name, "PNG")
|
| 126 |
-
temp_image_path = temp_file.name
|
| 127 |
-
|
| 128 |
try:
|
|
|
|
|
|
|
| 129 |
if custom_settings:
|
| 130 |
try:
|
| 131 |
config = json.loads(custom_settings)
|
|
@@ -134,7 +132,7 @@ def process_htr(image: Image.Image, document_type: Literal["letter_english", "le
|
|
| 134 |
else:
|
| 135 |
config = PIPELINE_CONFIGS[document_type]
|
| 136 |
|
| 137 |
-
collection = Collection([
|
| 138 |
pipeline = Pipeline.from_config(config)
|
| 139 |
|
| 140 |
try:
|
|
@@ -149,7 +147,12 @@ def process_htr(image: Image.Image, document_type: Literal["letter_english", "le
|
|
| 149 |
output_file_path = None
|
| 150 |
for root, _, files in os.walk(export_dir):
|
| 151 |
for file in files:
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
break
|
| 154 |
|
| 155 |
extracted_text = extract_text_from_collection(processed_collection)
|
|
@@ -158,12 +161,8 @@ def process_htr(image: Image.Image, document_type: Literal["letter_english", "le
|
|
| 158 |
|
| 159 |
except Exception as e:
|
| 160 |
return f"Error: HTR processing failed: {str(e)}", None
|
| 161 |
-
finally:
|
| 162 |
-
if os.path.exists(temp_image_path):
|
| 163 |
-
os.unlink(temp_image_path)
|
| 164 |
|
| 165 |
def extract_text_from_collection(collection: Collection) -> str:
|
| 166 |
-
"""Extract plain text from processed collection."""
|
| 167 |
text_lines = []
|
| 168 |
for page in collection.pages:
|
| 169 |
for node in page.traverse():
|
|
@@ -175,7 +174,7 @@ def create_htrflow_mcp_server():
|
|
| 175 |
demo = gr.Interface(
|
| 176 |
fn=process_htr,
|
| 177 |
inputs=[
|
| 178 |
-
gr.Image(type="
|
| 179 |
gr.Dropdown(choices=["letter_english", "letter_swedish", "spread_english", "spread_swedish"], value="letter_english", label="Document Type"),
|
| 180 |
gr.Dropdown(choices=CHOICES, value=DEFAULT_OUTPUT, label="Output Format"),
|
| 181 |
gr.Textbox(label="Custom Settings (JSON)", placeholder="Optional custom pipeline settings"),
|
|
@@ -185,7 +184,7 @@ def create_htrflow_mcp_server():
|
|
| 185 |
gr.File(label="Download Output File")
|
| 186 |
],
|
| 187 |
title="HTRflow MCP Server",
|
| 188 |
-
description="Process handwritten text and get extracted text with output file in specified format",
|
| 189 |
api_name="process_htr",
|
| 190 |
)
|
| 191 |
return demo
|
|
|
|
| 116 |
}
|
| 117 |
|
| 118 |
@spaces.GPU
|
| 119 |
+
def process_htr(image_path: str, document_type: Literal["letter_english", "letter_swedish", "spread_english", "spread_swedish"] = "letter_english", output_format: Literal["txt", "alto", "page", "json"] = DEFAULT_OUTPUT, custom_settings: Optional[str] = None):
|
| 120 |
"""Process handwritten text recognition and return extracted text with specified format file."""
|
| 121 |
+
if image_path is None:
|
| 122 |
return "Error: No image provided", None
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
try:
|
| 125 |
+
original_filename = Path(image_path).stem or "output"
|
| 126 |
+
|
| 127 |
if custom_settings:
|
| 128 |
try:
|
| 129 |
config = json.loads(custom_settings)
|
|
|
|
| 132 |
else:
|
| 133 |
config = PIPELINE_CONFIGS[document_type]
|
| 134 |
|
| 135 |
+
collection = Collection([image_path])
|
| 136 |
pipeline = Pipeline.from_config(config)
|
| 137 |
|
| 138 |
try:
|
|
|
|
| 147 |
output_file_path = None
|
| 148 |
for root, _, files in os.walk(export_dir):
|
| 149 |
for file in files:
|
| 150 |
+
old_path = os.path.join(root, file)
|
| 151 |
+
file_ext = Path(file).suffix
|
| 152 |
+
new_filename = f"{original_filename}.{output_format}" if not file_ext else f"{original_filename}{file_ext}"
|
| 153 |
+
new_path = os.path.join(root, new_filename)
|
| 154 |
+
os.rename(old_path, new_path)
|
| 155 |
+
output_file_path = new_path
|
| 156 |
break
|
| 157 |
|
| 158 |
extracted_text = extract_text_from_collection(processed_collection)
|
|
|
|
| 161 |
|
| 162 |
except Exception as e:
|
| 163 |
return f"Error: HTR processing failed: {str(e)}", None
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
def extract_text_from_collection(collection: Collection) -> str:
|
|
|
|
| 166 |
text_lines = []
|
| 167 |
for page in collection.pages:
|
| 168 |
for node in page.traverse():
|
|
|
|
| 174 |
demo = gr.Interface(
|
| 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="letter_english", label="Document Type"),
|
| 179 |
gr.Dropdown(choices=CHOICES, value=DEFAULT_OUTPUT, label="Output Format"),
|
| 180 |
gr.Textbox(label="Custom Settings (JSON)", placeholder="Optional custom pipeline settings"),
|
|
|
|
| 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 extracted text with output file in specified format",
|
| 188 |
api_name="process_htr",
|
| 189 |
)
|
| 190 |
return demo
|