Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -51,24 +51,24 @@ async def process_single_file(file: UploadFile) -> dict:
|
|
| 51 |
image_path = None
|
| 52 |
|
| 53 |
try:
|
| 54 |
-
# Save uploaded file temporarily
|
| 55 |
with open(file_path, "wb") as f:
|
| 56 |
shutil.copyfileobj(file.file, f)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
except Exception as ex:
|
| 74 |
return {"error": f"Processing failed for {file.filename}: {str(ex)}"}
|
|
|
|
| 51 |
image_path = None
|
| 52 |
|
| 53 |
try:
|
|
|
|
| 54 |
with open(file_path, "wb") as f:
|
| 55 |
shutil.copyfileobj(file.file, f)
|
| 56 |
+
loop = asyncio.get_event_loop()
|
| 57 |
+
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
|
| 58 |
+
if file_ext == ".pdf":
|
| 59 |
+
# Convert PDF to image in a thread
|
| 60 |
+
images = await loop.run_in_executor(executor, convert_from_path, file_path, 300)
|
| 61 |
+
if not images:
|
| 62 |
+
return {"error": f"No pages found in PDF: {file.filename}"}
|
| 63 |
+
img = resize_to_640(images[0])
|
| 64 |
+
image_path = os.path.join(UPLOAD_DIR, f"{uuid.uuid4().hex}.png")
|
| 65 |
+
await loop.run_in_executor(executor, img.save, image_path)
|
| 66 |
+
else:
|
| 67 |
+
image_path = file_path
|
| 68 |
+
|
| 69 |
+
# Run inference in a thread
|
| 70 |
+
extracted_data = await loop.run_in_executor(executor, extract_invoice_data_from_image, image_path)
|
| 71 |
+
return {"filename": file.filename, "data": extracted_data}
|
| 72 |
|
| 73 |
except Exception as ex:
|
| 74 |
return {"error": f"Processing failed for {file.filename}: {str(ex)}"}
|