Spaces:
Runtime error
Runtime error
AdityaAjithKumar commited on
Commit ·
22d960b
1
Parent(s): f60674b
fix: remove duplicate imports, fix NameError on all-models-fail, add error handling in endpoint
Browse files- app.py +13 -12
- ocr/gemini_face_extractor.py +6 -9
- ocr/gemini_service.py +10 -38
- requirements.txt +0 -0
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import asyncio
|
| 2 |
-
from fastapi import FastAPI, UploadFile, File
|
| 3 |
|
| 4 |
from ocr.gemini_service import parse_pan_with_fallback
|
| 5 |
from ocr.gemini_face_extractor import detect_face
|
|
@@ -10,18 +10,19 @@ app = FastAPI()
|
|
| 10 |
|
| 11 |
@app.post("/kyc/pan")
|
| 12 |
async def process_pan(file: UploadFile = File(...)):
|
| 13 |
-
|
| 14 |
image_bytes = await file.read()
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
face_bbox = face_result
|
|
|
|
|
|
|
| 25 |
|
| 26 |
face_image = crop_face(image_bytes, face_bbox)
|
| 27 |
|
|
@@ -29,5 +30,5 @@ async def process_pan(file: UploadFile = File(...)):
|
|
| 29 |
"success": True,
|
| 30 |
"parsed_data": pan_result.get("data"),
|
| 31 |
"face_image": face_image,
|
| 32 |
-
"engine_used": pan_result.get("engine_used")
|
| 33 |
-
}
|
|
|
|
| 1 |
import asyncio
|
| 2 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 3 |
|
| 4 |
from ocr.gemini_service import parse_pan_with_fallback
|
| 5 |
from ocr.gemini_face_extractor import detect_face
|
|
|
|
| 10 |
|
| 11 |
@app.post("/kyc/pan")
|
| 12 |
async def process_pan(file: UploadFile = File(...)):
|
|
|
|
| 13 |
image_bytes = await file.read()
|
| 14 |
|
| 15 |
+
try:
|
| 16 |
+
pan_result, face_result = await asyncio.gather(
|
| 17 |
+
asyncio.to_thread(parse_pan_with_fallback, image_bytes),
|
| 18 |
+
asyncio.to_thread(detect_face, image_bytes),
|
| 19 |
+
)
|
| 20 |
+
except Exception as e:
|
| 21 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 22 |
|
| 23 |
+
face_bbox = face_result.get("box_2d")
|
| 24 |
+
if face_bbox is None:
|
| 25 |
+
raise HTTPException(status_code=422, detail="Could not detect face on the PAN card.")
|
| 26 |
|
| 27 |
face_image = crop_face(image_bytes, face_bbox)
|
| 28 |
|
|
|
|
| 30 |
"success": True,
|
| 31 |
"parsed_data": pan_result.get("data"),
|
| 32 |
"face_image": face_image,
|
| 33 |
+
"engine_used": pan_result.get("engine_used"),
|
| 34 |
+
}
|
ocr/gemini_face_extractor.py
CHANGED
|
@@ -9,7 +9,7 @@ load_dotenv()
|
|
| 9 |
client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
|
| 10 |
|
| 11 |
PROMPT = """
|
| 12 |
-
Find the photograph of the person on this PAN card.Make sure to
|
| 13 |
|
| 14 |
Return JSON:
|
| 15 |
|
|
@@ -22,22 +22,19 @@ Coordinates must be normalized between 0 and 1000.
|
|
| 22 |
Return ONLY JSON.
|
| 23 |
"""
|
| 24 |
|
| 25 |
-
def detect_face(image_bytes):
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
mime_type="image/jpeg"
|
| 30 |
-
)
|
| 31 |
|
| 32 |
config = types.GenerateContentConfig(
|
| 33 |
response_mime_type="application/json",
|
| 34 |
-
thinking_config=types.ThinkingConfig(thinking_budget=0)
|
| 35 |
)
|
| 36 |
|
| 37 |
response = client.models.generate_content(
|
| 38 |
model="gemini-2.5-flash",
|
| 39 |
contents=[image, PROMPT],
|
| 40 |
-
config=config
|
| 41 |
)
|
| 42 |
|
| 43 |
-
return json.loads(response.text)
|
|
|
|
| 9 |
client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
|
| 10 |
|
| 11 |
PROMPT = """
|
| 12 |
+
Find the photograph of the person on this PAN card. Make sure to get the exact position of the entire photo.
|
| 13 |
|
| 14 |
Return JSON:
|
| 15 |
|
|
|
|
| 22 |
Return ONLY JSON.
|
| 23 |
"""
|
| 24 |
|
|
|
|
| 25 |
|
| 26 |
+
def detect_face(image_bytes):
|
| 27 |
+
image = types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg")
|
|
|
|
|
|
|
| 28 |
|
| 29 |
config = types.GenerateContentConfig(
|
| 30 |
response_mime_type="application/json",
|
| 31 |
+
thinking_config=types.ThinkingConfig(thinking_budget=0),
|
| 32 |
)
|
| 33 |
|
| 34 |
response = client.models.generate_content(
|
| 35 |
model="gemini-2.5-flash",
|
| 36 |
contents=[image, PROMPT],
|
| 37 |
+
config=config,
|
| 38 |
)
|
| 39 |
|
| 40 |
+
return json.loads(response.text)
|
ocr/gemini_service.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
from google import genai
|
| 2 |
from google.genai import types
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
-
import os
|
| 5 |
-
import json
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
import io
|
|
|
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
|
@@ -13,7 +13,7 @@ client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
|
|
| 13 |
MODELS = [
|
| 14 |
"gemini-3-flash-preview",
|
| 15 |
"gemini-2.5-flash",
|
| 16 |
-
"gemini-2.0-flash"
|
| 17 |
]
|
| 18 |
|
| 19 |
PROMPT = """
|
|
@@ -33,70 +33,42 @@ Return ONLY valid JSON.
|
|
| 33 |
"""
|
| 34 |
|
| 35 |
|
| 36 |
-
from PIL import Image
|
| 37 |
-
import io
|
| 38 |
-
|
| 39 |
-
|
| 40 |
def smart_resize(image_bytes, threshold=1400):
|
| 41 |
-
|
| 42 |
img = Image.open(io.BytesIO(image_bytes))
|
| 43 |
img = img.convert("RGB")
|
| 44 |
width, height = img.size
|
| 45 |
|
| 46 |
-
# Only resize if large
|
| 47 |
if width > threshold or height > threshold:
|
| 48 |
-
|
| 49 |
img.thumbnail((1024, 1024))
|
| 50 |
-
|
| 51 |
buffer = io.BytesIO()
|
| 52 |
img.save(buffer, format="JPEG", quality=85)
|
| 53 |
-
|
| 54 |
return buffer.getvalue(), True
|
| 55 |
|
| 56 |
return image_bytes, False
|
| 57 |
|
|
|
|
| 58 |
def parse_pan_with_fallback(image_bytes):
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
image = types.Part.from_bytes(
|
| 63 |
-
data=image_bytes,
|
| 64 |
-
mime_type="image/jpeg"
|
| 65 |
-
)
|
| 66 |
|
| 67 |
config = types.GenerateContentConfig(
|
| 68 |
response_mime_type="application/json",
|
| 69 |
-
thinking_config=types.ThinkingConfig(
|
| 70 |
-
thinking_budget=0
|
| 71 |
-
)
|
| 72 |
)
|
| 73 |
|
| 74 |
last_error = None
|
| 75 |
|
| 76 |
for model in MODELS:
|
| 77 |
-
|
| 78 |
try:
|
| 79 |
-
|
| 80 |
response = client.models.generate_content(
|
| 81 |
model=model,
|
| 82 |
contents=[image, PROMPT],
|
| 83 |
-
config=config
|
| 84 |
)
|
| 85 |
-
|
| 86 |
parsed = json.loads(response.text)
|
| 87 |
-
|
| 88 |
-
return {
|
| 89 |
-
"engine_used": model,
|
| 90 |
-
"data": parsed
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
except Exception as e:
|
| 94 |
-
|
| 95 |
last_error = str(e)
|
| 96 |
-
continue
|
| 97 |
|
| 98 |
-
|
| 99 |
-
"engine_used": model,
|
| 100 |
-
"resized": resized,
|
| 101 |
-
"data": parsed
|
| 102 |
-
}
|
|
|
|
| 1 |
from google import genai
|
| 2 |
from google.genai import types
|
| 3 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
import io
|
| 7 |
+
import json
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
|
|
|
| 13 |
MODELS = [
|
| 14 |
"gemini-3-flash-preview",
|
| 15 |
"gemini-2.5-flash",
|
| 16 |
+
"gemini-2.0-flash",
|
| 17 |
]
|
| 18 |
|
| 19 |
PROMPT = """
|
|
|
|
| 33 |
"""
|
| 34 |
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def smart_resize(image_bytes, threshold=1400):
|
|
|
|
| 37 |
img = Image.open(io.BytesIO(image_bytes))
|
| 38 |
img = img.convert("RGB")
|
| 39 |
width, height = img.size
|
| 40 |
|
|
|
|
| 41 |
if width > threshold or height > threshold:
|
|
|
|
| 42 |
img.thumbnail((1024, 1024))
|
|
|
|
| 43 |
buffer = io.BytesIO()
|
| 44 |
img.save(buffer, format="JPEG", quality=85)
|
|
|
|
| 45 |
return buffer.getvalue(), True
|
| 46 |
|
| 47 |
return image_bytes, False
|
| 48 |
|
| 49 |
+
|
| 50 |
def parse_pan_with_fallback(image_bytes):
|
| 51 |
+
image_bytes, _ = smart_resize(image_bytes)
|
| 52 |
|
| 53 |
+
image = types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
config = types.GenerateContentConfig(
|
| 56 |
response_mime_type="application/json",
|
| 57 |
+
thinking_config=types.ThinkingConfig(thinking_budget=0),
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
last_error = None
|
| 61 |
|
| 62 |
for model in MODELS:
|
|
|
|
| 63 |
try:
|
|
|
|
| 64 |
response = client.models.generate_content(
|
| 65 |
model=model,
|
| 66 |
contents=[image, PROMPT],
|
| 67 |
+
config=config,
|
| 68 |
)
|
|
|
|
| 69 |
parsed = json.loads(response.text)
|
| 70 |
+
return {"engine_used": model, "data": parsed}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
except Exception as e:
|
|
|
|
| 72 |
last_error = str(e)
|
|
|
|
| 73 |
|
| 74 |
+
raise RuntimeError(f"All models failed. Last error: {last_error}")
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|