Update inference.py
Browse files- inference.py +15 -18
inference.py
CHANGED
|
@@ -1,21 +1,18 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
input_date = pd.to_datetime(input_date_str)
|
| 14 |
-
# Use the minimum date from your training data (2024-01-01) as a reference point
|
| 15 |
-
min_date = pd.to_datetime("2024-01-01")
|
| 16 |
-
numerical_date = (input_date - min_date) / pd.Timedelta(days=30)
|
| 17 |
-
|
| 18 |
-
# Make prediction
|
| 19 |
-
prediction = model.predict(pd.DataFrame({"ds": [numerical_date]}))
|
| 20 |
-
|
| 21 |
-
return prediction[0]
|
|
|
|
| 1 |
+
import sklearn # Explicit import
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import pickle
|
| 4 |
+
from datetime import datetime
|
| 5 |
|
| 6 |
+
class ExpenseForecaster:
|
| 7 |
+
def __init__(self, model_path="model/expense_forecaster_model.pkl"):
|
| 8 |
+
with open(model_path, "rb") as model_file:
|
| 9 |
+
self.model = pickle.load(model_file)
|
| 10 |
+
self.min_date = pd.to_datetime("2024-01-01") # Reference date
|
| 11 |
|
| 12 |
+
def __call__(self, input_date_str):
|
| 13 |
+
input_date = pd.to_datetime(input_date_str)
|
| 14 |
+
numerical_date = (input_date - self.min_date) / pd.Timedelta(days=30)
|
| 15 |
+
prediction = self.model.predict(pd.DataFrame({"ds": [numerical_date]}))
|
| 16 |
+
return prediction[0].tolist() # Return as a list
|
| 17 |
|
| 18 |
+
model = ExpenseForecaster() # Instantiate the model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|