File size: 1,838 Bytes
9eae827
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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)