File size: 948 Bytes
d28e1f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210ec44
 
 
 
d28e1f9
 
 
1e68cdc
 
d28e1f9
 
 
 
 
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
import pickle
import numpy as np
import pandas as pd
from fastapi import FastAPI
from pydantic import BaseModel

# Load the trained model
with open("expense_model.pkl", "rb") as f:
    model = pickle.load(f)

# Initialize FastAPI app
app = FastAPI()

class ForecastRequest(BaseModel):
    month: str  # Example: "2024-06-01"

class ForecastResponse(BaseModel):
    predicted_expense: float

@app.get("/")  # Check if the base URL works
def home():
    return {"message": "Expense Forecast API is running!"}
    
@app.post("/predict", response_model=ForecastResponse)
async def predict_expense(request: ForecastRequest):
    # Convert input month to numerical format
    start_date = df["ds"].min()  # Get the first date in dataset
    months_since_start = (date - start_date).days / 30

    # Predict using the model
    prediction = model.predict(np.array([[months_since_start]]))[0]
    
    return ForecastResponse(predicted_expense=prediction)