Upload inference.py with huggingface_hub
Browse files- inference.py +24 -0
inference.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import numpy as np
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
|
| 6 |
+
# Load the model (ensure this path matches the location of your model)
|
| 7 |
+
with open("expense_model.pkl", "rb") as model_file:
|
| 8 |
+
model = pickle.load(model_file)
|
| 9 |
+
|
| 10 |
+
# Initialize the FastAPI app
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
class ForecastRequest(BaseModel):
|
| 14 |
+
month: float # The input feature (number of months)
|
| 15 |
+
|
| 16 |
+
class ForecastResponse(BaseModel):
|
| 17 |
+
predicted_expense: float
|
| 18 |
+
|
| 19 |
+
@app.post("/predict", response_model=ForecastResponse)
|
| 20 |
+
async def predict_expense(request: ForecastRequest):
|
| 21 |
+
# Predict the expense for the given month
|
| 22 |
+
predicted_expense = model.predict(np.array([[request.month]]))[0]
|
| 23 |
+
return ForecastResponse(predicted_expense=predicted_expense)
|
| 24 |
+
|