ProfessorCEO's picture
Cool Shot Systems
9eae827 verified
import os
from fastapi import FastAPI, Depends, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from auth import get_current_user
app = FastAPI(
title="Image Prediction API",
description="AI-powered image prediction service",
version="0.1.0"
)
# Configure CORS - use environment variable for allowed origins in production
allowed_origins = os.getenv("ALLOWED_ORIGINS", "").split(",") if os.getenv("ALLOWED_ORIGINS") else ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=allowed_origins,
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
class ImagePredictRequest(BaseModel):
image_url: str
class ImagePredictResponse(BaseModel):
prediction: str
confidence: float
labels: list[str]
image_url: str
@app.get("/")
async def root():
"""Health check endpoint."""
return {"status": "healthy", "service": "image-api"}
@app.get("/health")
async def health():
"""Health check endpoint."""
return {"status": "healthy"}
@app.post("/predict", response_model=ImagePredictResponse)
async def predict(
request: ImagePredictRequest,
current_user: dict = Depends(get_current_user)
):
"""
Protected endpoint for image prediction.
Requires valid Bearer token.
"""
# Placeholder prediction logic
# In a real application, this would call an ML model
image_url = request.image_url
# Simple mock prediction
prediction = "Object detected"
confidence = 0.92
labels = ["object", "scene", "outdoor"]
return ImagePredictResponse(
prediction=prediction,
confidence=confidence,
labels=labels,
image_url=image_url
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8002)