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)