File size: 926 Bytes
b8d885c a00aae6 d27b7d8 b8d885c a00aae6 d27b7d8 b8d885c a00aae6 d27b7d8 b8d885c a00aae6 b8d885c |
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 pandas as pd
import json
def load_model():
try:
with open("model/expense_forecaster_model.pkl", "rb") as f:
model = pickle.load(f)
return model
except Exception as e:
print(f"Error loading model: {e}")
return None
def predict(data):
model = load_model()
if model is None:
return {"error": "Model loading failed"}
try:
# Ensure data is a dictionary
if not isinstance(data, dict):
return {"error": "Input data must be a dictionary"}
df = pd.DataFrame([data])
prediction = model.predict(df)
return prediction.tolist()
except Exception as e:
return {"error": f"Prediction error: {e}"}
if __name__ == "__main__":
example_input = {"income": 5000, "previous_expenses": 3000, "month": 12}
prediction = predict(example_input)
print(f"Prediction: {prediction}") |