Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
import os
|
2 |
import io
|
3 |
import time
|
4 |
-
import tempfile
|
5 |
import PIL.Image
|
6 |
from fastapi import FastAPI, File, UploadFile, HTTPException
|
|
|
7 |
from pdf2image import convert_from_bytes
|
8 |
from google import genai
|
9 |
-
from google.genai import types
|
10 |
from google.genai.errors import ClientError
|
11 |
|
12 |
app = FastAPI(title="PDF/Image Text Extraction API")
|
@@ -36,11 +35,11 @@ def extract_text_from_image(img):
|
|
36 |
)
|
37 |
return response.text
|
38 |
except ClientError as e:
|
39 |
-
#
|
40 |
-
if e.
|
|
|
41 |
if attempt < max_retries - 1:
|
42 |
-
|
43 |
-
time.sleep(2 ** attempt)
|
44 |
continue
|
45 |
else:
|
46 |
raise HTTPException(
|
@@ -53,7 +52,7 @@ def extract_text_from_image(img):
|
|
53 |
detail=f"Error processing image: {str(e)}"
|
54 |
)
|
55 |
|
56 |
-
@app.post("/upload", summary="Upload a PDF or image file", response_description="Returns
|
57 |
async def upload_file(file: UploadFile = File(...)):
|
58 |
if not file.filename:
|
59 |
raise HTTPException(status_code=400, detail="No file provided")
|
@@ -82,9 +81,9 @@ async def upload_file(file: UploadFile = File(...)):
|
|
82 |
|
83 |
output_text += extract_text_from_image(img) + "\n\n"
|
84 |
|
85 |
-
# Return the extracted text
|
86 |
-
return {"extracted_text": output_text}
|
87 |
|
88 |
@app.get("/", summary="Health Check")
|
89 |
async def root():
|
90 |
-
return {"message": "API is up and running."}
|
|
|
1 |
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
|
|
|
9 |
from google.genai.errors import ClientError
|
10 |
|
11 |
app = FastAPI(title="PDF/Image Text Extraction API")
|
|
|
35 |
)
|
36 |
return response.text
|
37 |
except ClientError as e:
|
38 |
+
# Extract error code from the exception arguments
|
39 |
+
error_code = e.args[0] if e.args and isinstance(e.args[0], int) else None
|
40 |
+
if error_code == 429:
|
41 |
if attempt < max_retries - 1:
|
42 |
+
time.sleep(2 ** attempt) # Exponential backoff before retrying
|
|
|
43 |
continue
|
44 |
else:
|
45 |
raise HTTPException(
|
|
|
52 |
detail=f"Error processing image: {str(e)}"
|
53 |
)
|
54 |
|
55 |
+
@app.post("/upload", summary="Upload a PDF or image file", response_description="Returns extracted text as JSON")
|
56 |
async def upload_file(file: UploadFile = File(...)):
|
57 |
if not file.filename:
|
58 |
raise HTTPException(status_code=400, detail="No file provided")
|
|
|
81 |
|
82 |
output_text += extract_text_from_image(img) + "\n\n"
|
83 |
|
84 |
+
# Return the extracted text in a JSON response.
|
85 |
+
return JSONResponse(content={"extracted_text": output_text})
|
86 |
|
87 |
@app.get("/", summary="Health Check")
|
88 |
async def root():
|
89 |
+
return JSONResponse(content={"message": "API is up and running."})
|