Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,6 +35,33 @@ def decode_image(image_base64: str):
|
|
| 35 |
img = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
|
| 36 |
return img
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# --- API Endpoints ---
|
| 39 |
|
| 40 |
@app.get("/")
|
|
@@ -74,12 +101,17 @@ def run_structure_analysis(ocr_input: OcrInput):
|
|
| 74 |
|
| 75 |
print ("** Request for structure analysis received. Running structure_engine.**")
|
| 76 |
# Use the structure_engine here
|
| 77 |
-
|
|
|
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
print ("** Structure Analysis Complete. Returning the result.**")
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
return {"result":
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
return {"error": f"An error occurred in /structure: {str(e)}"}
|
|
|
|
| 35 |
img = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
|
| 36 |
return img
|
| 37 |
|
| 38 |
+
def clean_ppstructure_result(result):
|
| 39 |
+
"""
|
| 40 |
+
Recursively converts numpy arrays in the PPStructure result to standard lists.
|
| 41 |
+
Also optionally removes the 'img' field (raw numpy image crop) to keep standard JSON payloads light.
|
| 42 |
+
"""
|
| 43 |
+
if isinstance(result, list):
|
| 44 |
+
return [clean_ppstructure_result(item) for item in result]
|
| 45 |
+
elif isinstance(result, dict):
|
| 46 |
+
cleaned_dict = {}
|
| 47 |
+
for key, value in result.items():
|
| 48 |
+
# PPStructure often includes the raw image crop as a numpy array in the 'img' key.
|
| 49 |
+
# Returning this in standard JSON is usually a bad idea (too heavy).
|
| 50 |
+
# Comment out the next two lines if you actually NEED raw pixel data in your JSON.
|
| 51 |
+
if key == 'img':
|
| 52 |
+
continue
|
| 53 |
+
|
| 54 |
+
cleaned_dict[key] = clean_ppstructure_result(value)
|
| 55 |
+
return cleaned_dict
|
| 56 |
+
elif isinstance(result, np.ndarray):
|
| 57 |
+
return result.tolist() # crucial: converts numpy array to standard list
|
| 58 |
+
elif isinstance(result, (np.float32, np.float64)):
|
| 59 |
+
return float(result) # crucial: converts numpy floats to standard python floats
|
| 60 |
+
elif isinstance(result, (np.int32, np.int64)):
|
| 61 |
+
return int(result)
|
| 62 |
+
else:
|
| 63 |
+
return result
|
| 64 |
+
|
| 65 |
# --- API Endpoints ---
|
| 66 |
|
| 67 |
@app.get("/")
|
|
|
|
| 101 |
|
| 102 |
print ("** Request for structure analysis received. Running structure_engine.**")
|
| 103 |
# Use the structure_engine here
|
| 104 |
+
# Use the structure_engine here
|
| 105 |
+
raw_result = structure_engine.predict(img)
|
| 106 |
|
| 107 |
+
# --- CRITICAL STEP: Clean the result before returning ---
|
| 108 |
+
json_safe_result = clean_ppstructure_result(raw_result)
|
| 109 |
+
# --------------------------------------------------------
|
| 110 |
+
|
| 111 |
print ("** Structure Analysis Complete. Returning the result.**")
|
| 112 |
+
# print(json_safe_result) # Helpful for debugging, but can be verbose in production logs
|
| 113 |
+
|
| 114 |
+
return {"result": json_safe_result}
|
| 115 |
|
| 116 |
except Exception as e:
|
| 117 |
return {"error": f"An error occurred in /structure: {str(e)}"}
|