-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathmain.py
More file actions
212 lines (178 loc) · 6.25 KB
/
main.py
File metadata and controls
212 lines (178 loc) · 6.25 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from fastapi import FastAPI, HTTPException, Depends, Header, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.openapi.utils import get_openapi
from typing import Dict, List, Optional
from pydantic import BaseModel, Field
import json
import time
import asyncio
from datetime import datetime
import logging
from functools import lru_cache
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# API versioning
API_VERSION = "v1"
API_PREFIX = f"/api/{API_VERSION}"
# Initialize FastAPI app
app = FastAPI(
title="ML Model API",
description="A production-ready FastAPI application serving ML model predictions",
version="1.0.0"
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Rate limiting
class RateLimiter:
def __init__(self, requests_per_minute: int = 60):
self.requests_per_minute = requests_per_minute
self.requests = []
def is_allowed(self) -> bool:
now = time.time()
minute_ago = now - 60
# Remove requests older than 1 minute
self.requests = [req for req in self.requests if req > minute_ago]
if len(self.requests) >= self.requests_per_minute:
return False
self.requests.append(now)
return True
rate_limiter = RateLimiter()
# Pydantic models for request/response
class PredictionRequest(BaseModel):
features: List[float] = Field(..., description="List of input features for prediction")
model_version: Optional[str] = Field(None, description="Specific model version to use")
class Config:
schema_extra = {
"example": {
"features": [1.0, 2.0, 3.0, 4.0],
"model_version": "1.0.0"
}
}
class PredictionResponse(BaseModel):
prediction: float = Field(..., description="Model prediction result")
confidence: float = Field(..., description="Prediction confidence score")
model_version: str = Field(..., description="Version of the model used")
prediction_id: str = Field(..., description="Unique identifier for the prediction")
class HealthResponse(BaseModel):
status: str = Field(..., description="Current health status of the API")
timestamp: str = Field(..., description="Current server timestamp")
version: str = Field(..., description="API version")
class MetadataResponse(BaseModel):
model_name: str = Field(..., description="Name of the ML model")
model_version: str = Field(..., description="Current model version")
input_shape: List[int] = Field(..., description="Expected input shape")
output_shape: List[int] = Field(..., description="Output shape")
last_updated: str = Field(..., description="Last model update timestamp")
# Mock ML model class (replace with your actual model)
class MLModel:
@lru_cache(maxsize=1)
def load_model():
# Simulate model loading
time.sleep(1)
return "model_loaded"
def predict(self, features: List[float]) -> tuple[float, float]:
# Simulate prediction
prediction = sum(features) / len(features)
confidence = 0.95
return prediction, confidence
# Initialize model
model = MLModel()
# Authentication dependency
async def verify_api_key(api_key: str = Header(..., description="API key for authentication")):
if api_key != "your-secret-key": # Replace with secure key management
raise HTTPException(status_code=401, detail="Invalid API key")
return api_key
# Middleware for rate limiting
@app.middleware("http")
async def rate_limit_middleware(request: Request, call_next):
if not rate_limiter.is_allowed():
return JSONResponse(
status_code=429,
content={"detail": "Too many requests"}
)
response = await call_next(request)
return response
# API endpoints
@app.post(
f"{API_PREFIX}/predict",
response_model=PredictionResponse,
dependencies=[Depends(verify_api_key)]
)
async def predict(request: PredictionRequest):
try:
logger.info(f"Received prediction request with {len(request.features)} features")
# Input validation
if not request.features:
raise HTTPException(status_code=400, detail="No features provided")
# Make prediction
prediction, confidence = model.predict(request.features)
# Generate response
response = PredictionResponse(
prediction=prediction,
confidence=confidence,
model_version="1.0.0",
prediction_id=f"pred_{int(time.time())}"
)
logger.info(f"Prediction completed: {response.prediction_id}")
return response
except Exception as e:
logger.error(f"Prediction error: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get(
f"{API_PREFIX}/health",
response_model=HealthResponse
)
async def health_check():
return HealthResponse(
status="healthy",
timestamp=datetime.now().isoformat(),
version=API_VERSION
)
@app.get(
f"{API_PREFIX}/metadata",
response_model=MetadataResponse,
dependencies=[Depends(verify_api_key)]
)
async def get_metadata():
return MetadataResponse(
model_name="Example ML Model",
model_version="1.0.0",
input_shape=[4],
output_shape=[1],
last_updated=datetime.now().isoformat()
)
# Custom OpenAPI schema
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title=app.title,
version=app.version,
description=app.description,
routes=app.routes,
)
# Add security schemes
openapi_schema["components"]["securitySchemes"] = {
"ApiKeyHeader": {
"type": "apiKey",
"in": "header",
"name": "api_key"
}
}
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)