Spaces:
Runtime error
Runtime error
Update api.py
Browse files
api.py
CHANGED
|
@@ -78,39 +78,40 @@ def extractData(input_img):
|
|
| 78 |
return df, company_name, invoice_number, invoice_date
|
| 79 |
|
| 80 |
|
| 81 |
-
@app.post("/process-images/", response_model=
|
| 82 |
-
async def
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
image_bytes = base64.b64decode(image_data.image)
|
| 89 |
nparr = np.frombuffer(image_bytes, np.uint8)
|
| 90 |
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 91 |
|
|
|
|
| 92 |
df, company_name, invoice_number, invoice_date = extractData(img)
|
| 93 |
|
|
|
|
| 94 |
if df.empty:
|
| 95 |
-
table_data = []
|
| 96 |
else:
|
| 97 |
table_data = df.to_dict(orient='records')
|
| 98 |
|
|
|
|
| 99 |
response_data = ProcessedData(
|
| 100 |
company_name=company_name,
|
| 101 |
invoice_number=invoice_number,
|
| 102 |
invoice_date=invoice_date,
|
| 103 |
table_data=table_data
|
| 104 |
)
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
results.append(response_data.model_dump()) # Use model_dump instead of dict
|
| 108 |
-
except ValidationError as e:
|
| 109 |
-
# Log the validation error
|
| 110 |
-
print(f"Validation Error: {e}")
|
| 111 |
-
# Continue with the loop or add default/error information if needed
|
| 112 |
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
|
| 115 |
@app.post("/save-invoice")
|
| 116 |
async def save_invoice(invoice: Invoice, request: Request):
|
|
|
|
| 78 |
return df, company_name, invoice_number, invoice_date
|
| 79 |
|
| 80 |
|
| 81 |
+
@app.post("/process-images/", response_model=ProcessedData)
|
| 82 |
+
async def process_image(image_data: ImageData):
|
| 83 |
+
# Ensure the image data is provided
|
| 84 |
+
if not image_data:
|
| 85 |
+
raise HTTPException(status_code=400, detail="Image data cannot be empty.")
|
| 86 |
|
| 87 |
+
try:
|
| 88 |
+
# Decode the base64 image data
|
| 89 |
image_bytes = base64.b64decode(image_data.image)
|
| 90 |
nparr = np.frombuffer(image_bytes, np.uint8)
|
| 91 |
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 92 |
|
| 93 |
+
# Extract data from the image using your custom method
|
| 94 |
df, company_name, invoice_number, invoice_date = extractData(img)
|
| 95 |
|
| 96 |
+
# If the table data is empty, use an empty list
|
| 97 |
if df.empty:
|
| 98 |
+
table_data = []
|
| 99 |
else:
|
| 100 |
table_data = df.to_dict(orient='records')
|
| 101 |
|
| 102 |
+
# Prepare the response data
|
| 103 |
response_data = ProcessedData(
|
| 104 |
company_name=company_name,
|
| 105 |
invoice_number=invoice_number,
|
| 106 |
invoice_date=invoice_date,
|
| 107 |
table_data=table_data
|
| 108 |
)
|
| 109 |
+
print(response_data)
|
| 110 |
+
return response_data # Return the processed data as a response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
except Exception as e:
|
| 113 |
+
# Handle any errors that occur during processing
|
| 114 |
+
raise HTTPException(status_code=500, detail=f"Error processing the image: {str(e)}")
|
| 115 |
|
| 116 |
@app.post("/save-invoice")
|
| 117 |
async def save_invoice(invoice: Invoice, request: Request):
|