shailgsits's picture
Upload folder using huggingface_hub
f0e10d7 verified
Raw
History Blame Contribute Delete
1.09 kB
import time
from fastapi import FastAPI
from fastapi import UploadFile
from fastapi import File
from fastapi import HTTPException
from PIL import Image
from api.predictor import predict
app = FastAPI(
title="PAN Card Classification API",
version="1.0"
)
@app.get("/")
def root():
return {
"message": "PAN Card Classifier Running"
}
@app.get("/health")
def health():
return {
"status": "OK"
}
@app.post("/predict")
async def predict_pan(
file: UploadFile = File(...)
):
if file.content_type not in [
"image/jpeg",
"image/png",
"image/jpg"
]:
raise HTTPException(
status_code=400,
detail="Only JPG and PNG images are allowed."
)
start = time.perf_counter()
image = Image.open(file.file)
result = predict(image)
end = time.perf_counter()
return {
"success": True,
"prediction": result,
"model": "MobileNetV2",
"processing_time_ms": round(
(end - start) * 1000,
2,
),
}