Muleex12 commited on
Commit
1f0f36c
·
verified ·
1 Parent(s): daee0f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -13
app.py CHANGED
@@ -1,13 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  @app.route("/predict", methods=["POST"])
2
  def predict():
3
  try:
4
  data = request.get_json()
5
 
6
- import pandas as pd
7
-
8
  df = pd.DataFrame([data])
9
 
10
- # Rename to training names
 
 
11
  df.rename(columns={
12
  "Product_Weight": "Item_Weight",
13
  "Product_Sugar_Content": "Item_Fat_Content",
@@ -21,29 +38,35 @@ def predict():
21
  "Product_Id_char": "Item_Identifier"
22
  }, inplace=True)
23
 
24
- # 🔥 FORCE correct categories (VERY IMPORTANT)
 
 
 
 
 
 
25
  df["Outlet_Size"] = df["Outlet_Size"].map({
26
  "Small": "Small",
27
  "Medium": "Medium",
28
  "High": "High"
29
  })
30
 
31
- df["Outlet_Type"] = df["Outlet_Type"].map({
32
- "Supermarket": "Supermarket Type1",
33
- "Grocery Store": "Grocery Store"
34
- })
35
-
36
- # One-hot encoding
37
  df = pd.get_dummies(df)
38
 
39
- # Align with model EXACT columns
40
  df = df.reindex(columns=model.feature_names_in_, fill_value=0)
41
 
42
- print("Final input to model:", df)
43
 
 
44
  prediction = model.predict(df)[0]
45
 
46
  return jsonify({"Sales": float(prediction)})
47
 
48
  except Exception as e:
49
- return jsonify({"error": str(e)})
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import pandas as pd
3
+ import joblib
4
+
5
+ # Initialize Flask app
6
+ app = Flask(__name__)
7
+
8
+ # Load trained model
9
+ model = joblib.load("model.pkl")
10
+
11
+
12
+ @app.route("/")
13
+ def home():
14
+ return "Backend is running successfully"
15
+
16
+
17
  @app.route("/predict", methods=["POST"])
18
  def predict():
19
  try:
20
  data = request.get_json()
21
 
22
+ # Convert incoming data to DataFrame
 
23
  df = pd.DataFrame([data])
24
 
25
+ print("Incoming Data:", df)
26
+
27
+ # Rename columns to match training dataset
28
  df.rename(columns={
29
  "Product_Weight": "Item_Weight",
30
  "Product_Sugar_Content": "Item_Fat_Content",
 
38
  "Product_Id_char": "Item_Identifier"
39
  }, inplace=True)
40
 
41
+ # FIX STORE TYPE MISMATCH (CRITICAL)
42
+ df["Outlet_Type"] = df["Outlet_Type"].map({
43
+ "Supermarket": "Supermarket Type1",
44
+ "Grocery Store": "Food Mart"
45
+ })
46
+
47
+ # OPTIONAL: Fix Outlet Size if needed
48
  df["Outlet_Size"] = df["Outlet_Size"].map({
49
  "Small": "Small",
50
  "Medium": "Medium",
51
  "High": "High"
52
  })
53
 
54
+ # Convert categorical → numerical
 
 
 
 
 
55
  df = pd.get_dummies(df)
56
 
57
+ # Align with model features EXACTLY
58
  df = df.reindex(columns=model.feature_names_in_, fill_value=0)
59
 
60
+ print("Processed Data:", df)
61
 
62
+ # Predict
63
  prediction = model.predict(df)[0]
64
 
65
  return jsonify({"Sales": float(prediction)})
66
 
67
  except Exception as e:
68
+ return jsonify({"error": str(e)})
69
+
70
+
71
+ if __name__ == "__main__":
72
+ app.run(debug=True)