Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,42 +1,53 @@
|
|
| 1 |
-
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
import joblib
|
| 4 |
import pandas as pd
|
| 5 |
-
import
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
bundle
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
return "Sales Forecasting Backend is running!"
|
| 19 |
|
| 20 |
-
@app.route(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def predict():
|
| 22 |
try:
|
| 23 |
data = request.get_json(force=True)
|
| 24 |
-
|
| 25 |
-
# Assuming the incoming data is a list of dictionaries, where each dictionary is a data point
|
| 26 |
-
input_data = pd.DataFrame(data)
|
| 27 |
-
|
| 28 |
-
# Align columns with the training data, adding missing columns with a default value (e.g., 0 or NaN)
|
| 29 |
-
input_data_processed = input_data.reindex(columns=feature_cols, fill_value=0)
|
| 30 |
-
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
return jsonify(predictions
|
| 37 |
except Exception as e:
|
| 38 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
if __name__ ==
|
| 41 |
-
|
| 42 |
-
app.run(host='0.0.0.0', port=5000)
|
|
|
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import joblib
|
| 3 |
import pandas as pd
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
MODEL_PATH = os.getenv("MODEL_PATH", "best_model_random_forest.joblib")
|
| 9 |
+
|
| 10 |
+
# Load the model bundle safely
|
| 11 |
+
if not os.path.exists(MODEL_PATH):
|
| 12 |
+
raise FileNotFoundError(f"Model file not found: {MODEL_PATH}. Please ensure it's uploaded to the Space.")
|
| 13 |
+
|
| 14 |
+
bundle = joblib.load(MODEL_PATH)
|
| 15 |
|
| 16 |
+
# Extract actual model from dict or use as-is
|
| 17 |
+
if isinstance(bundle, dict):
|
| 18 |
+
model = bundle.get("model", None)
|
| 19 |
+
feature_cols = bundle.get("feature_cols", None)
|
| 20 |
+
else:
|
| 21 |
+
model = bundle
|
| 22 |
+
feature_cols = None
|
| 23 |
|
| 24 |
+
if model is None:
|
| 25 |
+
raise ValueError("Model object not found inside the loaded bundle. Please verify the saved file structure.")
|
|
|
|
| 26 |
|
| 27 |
+
@app.route("/health", methods=["GET"])
|
| 28 |
+
def health():
|
| 29 |
+
return jsonify({
|
| 30 |
+
"status": "ok",
|
| 31 |
+
"model_path": MODEL_PATH
|
| 32 |
+
})
|
| 33 |
+
|
| 34 |
+
@app.route("/predict", methods=["POST"])
|
| 35 |
def predict():
|
| 36 |
try:
|
| 37 |
data = request.get_json(force=True)
|
| 38 |
+
df = pd.DataFrame([data]) if isinstance(data, dict) else pd.DataFrame(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Align columns with training data if available
|
| 41 |
+
if feature_cols is not None:
|
| 42 |
+
df = df.reindex(columns=feature_cols, fill_value=0)
|
| 43 |
|
| 44 |
+
preds = model.predict(df)
|
| 45 |
+
return jsonify({"predictions": [float(p) for p in preds]})
|
| 46 |
except Exception as e:
|
| 47 |
+
return jsonify({
|
| 48 |
+
"error": "Prediction failed",
|
| 49 |
+
"details": str(e)
|
| 50 |
+
})
|
| 51 |
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
app.run(host="0.0.0.0", port=5000)
|
|
|