from fastapi import FastAPI, Depends, HTTPException, status, UploadFile, File from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import FileResponse from fastapi.security import APIKeyHeader import io import os import base64 import requests from dotenv import load_dotenv # Load .env file for local development load_dotenv() app = FastAPI(title="Image OCR & Vision API") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # ── Lazy OCR model ───────────────────────────────────────────────────────────── _ocr_reader = None def get_ocr_reader(): global _ocr_reader if _ocr_reader is None: import easyocr print("Loading EasyOCR model...") _ocr_reader = easyocr.Reader(['en']) print("EasyOCR ready!") return _ocr_reader # ── Config ───────────────────────────────────────────────────────────────────── API_KEY = os.environ.get("API_KEY", "xkLm9Qz4BvWtUa2Nf8PjX1") API_KEY_NAME = "x-api-key" api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False) GOOGLE_VISION_API_KEY = os.environ.get("GOOGLE_VISION_API_KEY", "") def get_api_key(api_key: str = Depends(api_key_header)): if api_key == API_KEY: return api_key raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid or missing API Key") # ── OCR helpers ───────────────────────────────────────────────────────────────── def extract_text_from_image_bytes(image_bytes: bytes) -> str: reader = get_ocr_reader() result = reader.readtext(image_bytes, detail=0) return "\n".join(result) def process_pdf(pdf_bytes: bytes) -> str: import fitz doc = fitz.open(stream=pdf_bytes, filetype="pdf") pages = [] for i in range(len(doc)): page = doc.load_page(i) pix = page.get_pixmap(matrix=fitz.Matrix(2, 2)) text = extract_text_from_image_bytes(pix.tobytes("png")) if text.strip(): pages.append(f"--- Page {i+1} ---\n{text}") doc.close() return "\n\n".join(pages) # ── Google Vision API ─────────────────────────────────────────────────────────── def classify_with_google(image_bytes: bytes) -> dict: """ Calls Google Vision API to detect labels, objects and web entities. Returns a structured result with category and labels. """ if not GOOGLE_VISION_API_KEY: raise HTTPException( status_code=503, detail="Google Vision API key is not configured. Add GOOGLE_VISION_API_KEY to your environment secrets." ) encoded = base64.b64encode(image_bytes).decode("utf-8") payload = { "requests": [{ "image": {"content": encoded}, "features": [ {"type": "LABEL_DETECTION", "maxResults": 10}, {"type": "OBJECT_LOCALIZATION","maxResults": 5}, {"type": "WEB_DETECTION", "maxResults": 5}, ] }] } url = f"https://vision.googleapis.com/v1/images:annotate?key={GOOGLE_VISION_API_KEY}" resp = requests.post(url, json=payload, timeout=20) if resp.status_code == 400: raise HTTPException(status_code=400, detail="Invalid Google Vision API key or request.") if resp.status_code in (403, 429): raise HTTPException(status_code=429, detail="Google Vision quota exceeded or API not enabled.") resp.raise_for_status() data = resp.json()["responses"][0] labels = [l["description"] for l in data.get("labelAnnotations", [])] objects = [o["name"] for o in data.get("localizedObjectAnnotations", [])] # Web detection gives best product names web_data = data.get("webDetection", {}) web_entities = [e["description"] for e in web_data.get("webEntities", []) if e.get("description")] best_guess = [g["label"] for g in web_data.get("bestGuessLabels", [])] # Combine: best guess > web entities > objects > labels all_labels = list(dict.fromkeys(best_guess + web_entities + objects + labels)) category = all_labels[0] if all_labels else "Unknown" return { "labels": all_labels[:10], # top 10 "category": category, "source": "google_vision" } # ── Routes ────────────────────────────────────────────────────────────────────── @app.get("/", tags=["UI"]) async def serve_ui(): return FileResponse("index.html") @app.post("/ocr", tags=["OCR"]) async def perform_ocr( api_key: str = Depends(get_api_key), file: UploadFile = File(...) ): """Extract text from an image or PDF.""" contents = await file.read() filename = file.filename or "" content_type = file.content_type or "" is_pdf = content_type == "application/pdf" or filename.lower().endswith(".pdf") is_image = content_type.startswith("image/") if not is_pdf and not is_image: raise HTTPException(status_code=400, detail="Upload an image or PDF.") try: text = process_pdf(contents) if is_pdf else extract_text_from_image_bytes(contents) file_type = "pdf" if is_pdf else "image" return {"success": True, "filename": filename, "file_type": file_type, "extracted_text": text.strip()} except Exception as e: raise HTTPException(status_code=500, detail=f"OCR failed: {str(e)}") @app.post("/classify", tags=["Image Classification"]) async def classify_image( api_key: str = Depends(get_api_key), file: UploadFile = File(...) ): """ Identify a product or object in an image using Google Vision AI. Requires GOOGLE_VISION_API_KEY to be set in environment secrets. """ if not file.content_type.startswith("image/"): raise HTTPException(status_code=400, detail="Please upload an image file.") contents = await file.read() result = classify_with_google(contents) return {"success": True, "filename": file.filename, **result} if __name__ == "__main__": import uvicorn port = int(os.environ.get("PORT", 4002)) uvicorn.run("main:app", host="0.0.0.0", port=port, reload=True)