File size: 770 Bytes
1c5a522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pickle
import numpy as np
from fastapi import FastAPI
from pydantic import BaseModel

# Load the model (ensure this path matches the location of your model)
with open("expense_model.pkl", "rb") as model_file:
    model = pickle.load(model_file)

# Initialize the FastAPI app
app = FastAPI()

class ForecastRequest(BaseModel):
    month: float  # The input feature (number of months)

class ForecastResponse(BaseModel):
    predicted_expense: float

@app.post("/predict", response_model=ForecastResponse)
async def predict_expense(request: ForecastRequest):
    # Predict the expense for the given month
    predicted_expense = model.predict(np.array([[request.month]]))[0]
    return ForecastResponse(predicted_expense=predicted_expense)