from fastapi import FastAPI, HTTPException from pydantic import BaseModel import time import os from dotenv import load_dotenv load_dotenv() from src.helpers.logger import logger from src.main import ImageMetadataDetector app = FastAPI() # Global detector instance logger.info("Initializing ImageMetadataDetector...") detector = ImageMetadataDetector() # Request schema class ImageRequest(BaseModel): path: str type: str # "url" or "local_path" # Health check @app.get("/") def root(): return {"status": "running", "provider":"exif_detector"} ENV = os.getenv("ENV", "development") # Main processing endpoint @app.post("/process") def process_image(request: ImageRequest): start_time = time.time() logger.info(f"Processing image request: path='{request.path}', type='{request.type}'") try: result = detector.predict(request.path, request.type) processing_time = round(time.time() - start_time, 4) logger.info(f"Successfully processed image in {processing_time}s") return result except Exception as e: logger.error(f"Error processing image: {str(e)}", exc_info=True) # Hide detailed error message in production error_detail = "An internal error occurred while processing the image metadata" if ENV == "production" else str(e) raise HTTPException(status_code=400, detail=error_detail) # uvicorn app:app --reload --host 0.0.0.0 --port 8000