Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -114,12 +114,14 @@ def extract_invoice_data(file_data, content_type, json_schema):
|
|
| 114 |
img.save(img_byte_arr, format="PNG", dpi=(300, 300))
|
| 115 |
base64_encoded = base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')
|
| 116 |
base64_images.append(f"data:{content_type};base64,{base64_encoded}")
|
|
|
|
|
|
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
logger.error(f"Error converting PDF to image: {e}")
|
| 120 |
return {"error": "Failed to process PDF"}, None
|
| 121 |
|
| 122 |
-
|
| 123 |
# Handle direct image files
|
| 124 |
base64_encoded = base64.b64encode(file_data).decode('utf-8')
|
| 125 |
base64_images.append(f"data:{content_type.lower()};base64,{base64_encoded}")
|
|
@@ -145,6 +147,9 @@ def extract_invoice_data(file_data, content_type, json_schema):
|
|
| 145 |
except Exception as e:
|
| 146 |
logger.error(f"Error in OpenAI processing: {e}")
|
| 147 |
return {"error": str(e)}, base64_images
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
def get_content_type_from_s3(file_key):
|
| 150 |
"""Fetch the content type (MIME type) of a file stored in S3."""
|
|
|
|
| 114 |
img.save(img_byte_arr, format="PNG", dpi=(300, 300))
|
| 115 |
base64_encoded = base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')
|
| 116 |
base64_images.append(f"data:{content_type};base64,{base64_encoded}")
|
| 117 |
+
|
| 118 |
+
content_type = "image/png"
|
| 119 |
|
| 120 |
except Exception as e:
|
| 121 |
logger.error(f"Error converting PDF to image: {e}")
|
| 122 |
return {"error": "Failed to process PDF"}, None
|
| 123 |
|
| 124 |
+
elif content_type.startswith("image/"):
|
| 125 |
# Handle direct image files
|
| 126 |
base64_encoded = base64.b64encode(file_data).decode('utf-8')
|
| 127 |
base64_images.append(f"data:{content_type.lower()};base64,{base64_encoded}")
|
|
|
|
| 147 |
except Exception as e:
|
| 148 |
logger.error(f"Error in OpenAI processing: {e}")
|
| 149 |
return {"error": str(e)}, base64_images
|
| 150 |
+
|
| 151 |
+
else:
|
| 152 |
+
return {"error": f"Unsupported file type: {content_type}"}
|
| 153 |
|
| 154 |
def get_content_type_from_s3(file_key):
|
| 155 |
"""Fetch the content type (MIME type) of a file stored in S3."""
|