Create api.py
Browse files
api.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from inference import predict
|
| 4 |
+
|
| 5 |
+
app = FastAPI(title="Your Model API", version="1.0.0")
|
| 6 |
+
|
| 7 |
+
class PredictionRequest(BaseModel):
|
| 8 |
+
text: str
|
| 9 |
+
|
| 10 |
+
class PredictionResponse(BaseModel):
|
| 11 |
+
prediction: dict
|
| 12 |
+
status: str
|
| 13 |
+
|
| 14 |
+
@app.get("/")
|
| 15 |
+
def read_root():
|
| 16 |
+
return {"message": "Your Model API is running!"}
|
| 17 |
+
|
| 18 |
+
@app.post("/predict", response_model=PredictionResponse)
|
| 19 |
+
async def predict_endpoint(request: PredictionRequest):
|
| 20 |
+
try:
|
| 21 |
+
result = predict(request.text)
|
| 22 |
+
return PredictionResponse(
|
| 23 |
+
prediction=result,
|
| 24 |
+
status="success"
|
| 25 |
+
)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return PredictionResponse(
|
| 28 |
+
prediction={},
|
| 29 |
+
status=f"error: {str(e)}"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Run with: uvicorn api:app --host 0.0.0.0 --port 8000
|