Spaces:
Sleeping
Sleeping
π Fix: Ensure backend starts and runs Flask app
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ import traceback
|
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
-
# === Load Trained Model ===
|
| 11 |
MODEL_PATH = "best_model.pkl"
|
| 12 |
|
| 13 |
try:
|
|
@@ -26,32 +25,14 @@ def predict_single():
|
|
| 26 |
try:
|
| 27 |
data = request.get_json()
|
| 28 |
df = pd.DataFrame([data])
|
|
|
|
| 29 |
df["Store_Age"] = 2025 - df["Store_Establishment_Year"]
|
| 30 |
df["Price_per_kg"] = df["Product_MRP"] / df["Product_Weight"]
|
| 31 |
df["MRP_Band"] = pd.cut(df["Product_MRP"], bins=[0, 100, 200, float("inf")], labels=["Low", "Mid", "High"])
|
|
|
|
| 32 |
pred_log = model.predict(df)[0]
|
| 33 |
pred = np.expm1(pred_log)
|
| 34 |
return jsonify({"Predicted_Sales": round(float(pred), 2)}), 200
|
| 35 |
-
except Exception as e:
|
| 36 |
-
return jsonify({"error": str(e)}), 500
|
| 37 |
-
|
| 38 |
-
@app.route("/v1/forecastbatch", methods=["POST"])
|
| 39 |
-
def predict_batch():
|
| 40 |
-
try:
|
| 41 |
-
file = request.files.get("file")
|
| 42 |
-
if file is None:
|
| 43 |
-
return jsonify({"error": "No file uploaded"}), 400
|
| 44 |
|
| 45 |
-
df = pd.read_csv(file)
|
| 46 |
-
df["Store_Age"] = 2025 - df["Store_Establishment_Year"]
|
| 47 |
-
df["Price_per_kg"] = df["Product_MRP"] / df["Product_Weight"]
|
| 48 |
-
df["MRP_Band"] = pd.cut(df["Product_MRP"], bins=[0, 100, 200, float("inf")], labels=["Low", "Mid", "High"])
|
| 49 |
-
preds = model.predict(df)
|
| 50 |
-
results = [round(float(np.expm1(p)), 2) for p in preds]
|
| 51 |
-
return jsonify({"Predictions": results}), 200
|
| 52 |
except Exception as e:
|
| 53 |
return jsonify({"error": str(e)}), 500
|
| 54 |
-
|
| 55 |
-
if __name__ == "__main__":
|
| 56 |
-
port = int(os.environ.get("PORT", 7860))
|
| 57 |
-
app.run(host="0.0.0.0", port=port)
|
|
|
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
|
|
|
| 10 |
MODEL_PATH = "best_model.pkl"
|
| 11 |
|
| 12 |
try:
|
|
|
|
| 25 |
try:
|
| 26 |
data = request.get_json()
|
| 27 |
df = pd.DataFrame([data])
|
| 28 |
+
|
| 29 |
df["Store_Age"] = 2025 - df["Store_Establishment_Year"]
|
| 30 |
df["Price_per_kg"] = df["Product_MRP"] / df["Product_Weight"]
|
| 31 |
df["MRP_Band"] = pd.cut(df["Product_MRP"], bins=[0, 100, 200, float("inf")], labels=["Low", "Mid", "High"])
|
| 32 |
+
|
| 33 |
pred_log = model.predict(df)[0]
|
| 34 |
pred = np.expm1(pred_log)
|
| 35 |
return jsonify({"Predicted_Sales": round(float(pred), 2)}), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|