Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,39 +21,38 @@ def home():
|
|
| 21 |
# Prediction route
|
| 22 |
@sales_prediction_api.route("/predict", methods=["POST"])
|
| 23 |
def predict():
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
})
|
| 57 |
|
| 58 |
if __name__ == '__main__':
|
| 59 |
sales_prediction_api.run(debug=True)
|
|
|
|
| 21 |
# Prediction route
|
| 22 |
@sales_prediction_api.route("/predict", methods=["POST"])
|
| 23 |
def predict():
|
| 24 |
+
data = request.get_json()
|
| 25 |
+
|
| 26 |
+
# Model choice
|
| 27 |
+
model_choice = data.get("model", "dt")
|
| 28 |
+
|
| 29 |
+
# Extract features (MATCHES STREAMLIT KEYS)
|
| 30 |
+
sample = {
|
| 31 |
+
"Product_Weight": data["Product_Weight"],
|
| 32 |
+
"Product_Sugar_Content": data["Product_Sugar_Content"],
|
| 33 |
+
"Product_Allocated_Area": data["Product_Allocated_Area"],
|
| 34 |
+
"Product_Type": data["Product_Type"],
|
| 35 |
+
"Product_MRP": data["Product_MRP"],
|
| 36 |
+
"Store_Size": data["Store_Size"],
|
| 37 |
+
"Store_Location_City_Type": data["Store_Location_City_Type"],
|
| 38 |
+
"Store_Type": data["Store_Type"],
|
| 39 |
+
"Store_Age": data["Store_Age"]
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
sample_df = pd.DataFrame([sample])
|
| 43 |
+
|
| 44 |
+
# Select model
|
| 45 |
+
if model_choice == "dt":
|
| 46 |
+
prediction = dt_model.predict(sample_df)[0]
|
| 47 |
+
elif model_choice == "xgb":
|
| 48 |
+
prediction = xgb_model.predict(sample_df)[0]
|
| 49 |
+
else:
|
| 50 |
+
return jsonify({"error": "Invalid model choice"}), 400
|
| 51 |
+
|
| 52 |
+
return jsonify({
|
| 53 |
+
"Prediction": float(prediction),
|
| 54 |
+
"ModelUsed": model_choice
|
| 55 |
+
})
|
|
|
|
| 56 |
|
| 57 |
if __name__ == '__main__':
|
| 58 |
sales_prediction_api.run(debug=True)
|