Lokiiparihar commited on
Commit
238f5f2
·
verified ·
1 Parent(s): 22d1001

Update app.py

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