| 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() |
|
|
| |
| logger.info("Initializing ImageMetadataDetector...") |
| detector = ImageMetadataDetector() |
|
|
|
|
| |
| class ImageRequest(BaseModel): |
| path: str |
| type: str |
|
|
|
|
| |
| @app.get("/") |
| def root(): |
| return {"status": "running", "provider":"exif_detector"} |
|
|
|
|
| ENV = os.getenv("ENV", "development") |
|
|
| |
| @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) |
| |
| 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) |
|
|
|
|
| |