Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -2,7 +2,7 @@ import os
|
|
2 |
import io
|
3 |
import time
|
4 |
import PIL.Image
|
5 |
-
from fastapi import FastAPI, File, UploadFile, HTTPException
|
6 |
from fastapi.responses import JSONResponse
|
7 |
from pdf2image import convert_from_bytes
|
8 |
from google import genai
|
@@ -10,6 +10,14 @@ from google.genai.errors import ClientError
|
|
10 |
|
11 |
app = FastAPI(title="PDF/Image Text Extraction API")
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# Retrieve the API key from an environment variable.
|
14 |
API_KEY = os.getenv("API_KEY")
|
15 |
if not API_KEY:
|
|
|
2 |
import io
|
3 |
import time
|
4 |
import PIL.Image
|
5 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException, Request
|
6 |
from fastapi.responses import JSONResponse
|
7 |
from pdf2image import convert_from_bytes
|
8 |
from google import genai
|
|
|
10 |
|
11 |
app = FastAPI(title="PDF/Image Text Extraction API")
|
12 |
|
13 |
+
# Global exception handler to always return JSON responses
|
14 |
+
@app.exception_handler(Exception)
|
15 |
+
async def global_exception_handler(request: Request, exc: Exception):
|
16 |
+
return JSONResponse(
|
17 |
+
status_code=500,
|
18 |
+
content={"detail": str(exc)}
|
19 |
+
)
|
20 |
+
|
21 |
# Retrieve the API key from an environment variable.
|
22 |
API_KEY = os.getenv("API_KEY")
|
23 |
if not API_KEY:
|