Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
import uvicorn
|
| 5 |
from fastapi import FastAPI
|
|
|
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from paddleocr import PaddleOCR, PPStructureV3
|
| 8 |
|
|
@@ -112,35 +113,42 @@ def run_ocr(ocr_input: OcrInput):
|
|
| 112 |
except Exception as e:
|
| 113 |
return {"error": f"An error occurred in /ocr: {str(e)}"}
|
| 114 |
|
| 115 |
-
@app.post("/structure")
|
| 116 |
def run_structure_analysis(ocr_input: OcrInput):
|
| 117 |
"""Endpoint for layout analysis, table recognition, and text extraction (PPStructure)."""
|
| 118 |
try:
|
| 119 |
-
print
|
| 120 |
-
|
| 121 |
img = decode_image(ocr_input.image_base64)
|
| 122 |
if img is None:
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
print ("** Request for structure analysis received. Running structure_engine.**")
|
| 126 |
# Use the structure_engine here
|
| 127 |
raw_result = structure_engine.predict(img)
|
| 128 |
-
print
|
| 129 |
-
|
| 130 |
-
print ("** We have got the result back. Now converting to JSON **")
|
| 131 |
|
| 132 |
# --- CRITICAL STEP: Clean the result before returning ---
|
| 133 |
json_safe_result = clean_ppstructure_result(raw_result)
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
# --------------------------------------------------------
|
| 136 |
-
|
| 137 |
-
print ("** Structure Analysis Complete. Returning the JSON result.**")
|
| 138 |
-
# print(json_safe_result) # Helpful for debugging, but can be verbose in production logs
|
| 139 |
-
|
| 140 |
-
return {"result": json_safe_result}
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
except Exception as e:
|
| 143 |
-
|
|
|
|
|
|
|
| 144 |
# --- Run the App ---
|
| 145 |
|
| 146 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import uvicorn
|
| 5 |
from fastapi import FastAPI
|
| 6 |
+
from fastapi.responses import PlainTextResponse
|
| 7 |
from pydantic import BaseModel
|
| 8 |
from paddleocr import PaddleOCR, PPStructureV3
|
| 9 |
|
|
|
|
| 113 |
except Exception as e:
|
| 114 |
return {"error": f"An error occurred in /ocr: {str(e)}"}
|
| 115 |
|
| 116 |
+
@app.post("/structure", response_class=PlainTextResponse)
|
| 117 |
def run_structure_analysis(ocr_input: OcrInput):
|
| 118 |
"""Endpoint for layout analysis, table recognition, and text extraction (PPStructure)."""
|
| 119 |
try:
|
| 120 |
+
print("** /structure called. Decoding image **")
|
| 121 |
+
|
| 122 |
img = decode_image(ocr_input.image_base64)
|
| 123 |
if img is None:
|
| 124 |
+
# For errors, you can still return a PlainTextResponse
|
| 125 |
+
return PlainTextResponse("Error: Invalid image data. Could not decode.", status_code=400)
|
| 126 |
+
|
| 127 |
+
print("** Request for structure analysis received. Running structure_engine.**")
|
| 128 |
|
|
|
|
| 129 |
# Use the structure_engine here
|
| 130 |
raw_result = structure_engine.predict(img)
|
| 131 |
+
print(raw_result)
|
| 132 |
+
print("** We have got the result back. Now converting to text **")
|
|
|
|
| 133 |
|
| 134 |
# --- CRITICAL STEP: Clean the result before returning ---
|
| 135 |
json_safe_result = clean_ppstructure_result(raw_result)
|
| 136 |
+
# Convert the processed result (which is a Python object/dict) into a JSON string
|
| 137 |
+
# to ensure it's properly formatted text.
|
| 138 |
+
import json
|
| 139 |
+
text_result = json.dumps({"result": json_safe_result}, indent=2)
|
| 140 |
# --------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
+
print("** Structure Analysis Complete. Returning the text result.**")
|
| 143 |
+
|
| 144 |
+
# Return the string directly. PlainTextResponse will handle setting
|
| 145 |
+
# the Content-Type header to text/plain.
|
| 146 |
+
return text_result
|
| 147 |
+
|
| 148 |
except Exception as e:
|
| 149 |
+
# Return error as plain text with an appropriate status code
|
| 150 |
+
error_message = f"An error occurred in /structure: {str(e)}"
|
| 151 |
+
return PlainTextResponse(f"Error: {error_message}", status_code=500)
|
| 152 |
# --- Run the App ---
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|