Spaces:
Sleeping
Sleeping
| 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 | |
| # Check if the base URL works | |
| def home(): | |
| return {"message": "Expense Forecast API is running!"} | |
| 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) | |