Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,7 @@ import joblib
|
|
| 3 |
import pandas as pd
|
| 4 |
from flask import Flask, request, jsonify
|
| 5 |
|
| 6 |
-
|
| 7 |
-
app = Flask("SuperKart")
|
| 8 |
|
| 9 |
MODEL_PATH = "superkart_model_v1_0.joblib"
|
| 10 |
model = None
|
|
@@ -14,53 +13,41 @@ def load_model():
|
|
| 14 |
global model
|
| 15 |
if model is None:
|
| 16 |
if not os.path.exists(MODEL_PATH):
|
| 17 |
-
raise FileNotFoundError(f"Model not found: {MODEL_PATH}")
|
| 18 |
model = joblib.load(MODEL_PATH)
|
| 19 |
|
| 20 |
|
| 21 |
-
# Health check (
|
| 22 |
@app.route("/", methods=["GET"])
|
| 23 |
def health():
|
| 24 |
return "SuperKart Backend is running"
|
| 25 |
|
| 26 |
|
| 27 |
-
|
| 28 |
-
@app.route("/predict", methods=["POST"])
|
| 29 |
def predict():
|
| 30 |
try:
|
| 31 |
load_model()
|
| 32 |
|
| 33 |
data = request.get_json(force=True)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
# FEATURE ENGINEERING (IMPORTANT)
|
| 40 |
-
# -------------------------------
|
| 41 |
|
| 42 |
-
|
| 43 |
-
if "Store_Establishment_Year" in df.columns:
|
| 44 |
-
df["Store_Age_Years"] = 2025 - df["Store_Establishment_Year"]
|
| 45 |
-
df.drop(columns=["Store_Establishment_Year"], inplace=True)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
if "Product_Id_char" not in df.columns:
|
| 49 |
-
df["Product_Id_char"] = "A"
|
| 50 |
-
|
| 51 |
-
# Product_Type_Category (same as Product_Type)
|
| 52 |
-
if "Product_Type" in df.columns:
|
| 53 |
-
df["Product_Type_Category"] = df["Product_Type"]
|
| 54 |
-
df.drop(columns=["Product_Type"], inplace=True)
|
| 55 |
-
|
| 56 |
-
# -------------------------------
|
| 57 |
-
# PREDICTION
|
| 58 |
-
# -------------------------------
|
| 59 |
-
prediction = model.predict(df)
|
| 60 |
-
|
| 61 |
-
return jsonify({
|
| 62 |
-
"predictions": prediction.tolist()
|
| 63 |
-
})
|
| 64 |
|
| 65 |
except KeyError as e:
|
| 66 |
return jsonify({"error": f"Missing field: {str(e)}"}), 400
|
|
@@ -68,6 +55,5 @@ def predict():
|
|
| 68 |
return jsonify({"error": str(e)}), 500
|
| 69 |
|
| 70 |
|
| 71 |
-
# Run on Hugging Face required port
|
| 72 |
if __name__ == "__main__":
|
| 73 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
from flask import Flask, request, jsonify
|
| 5 |
|
| 6 |
+
app = Flask(__name__)
|
|
|
|
| 7 |
|
| 8 |
MODEL_PATH = "superkart_model_v1_0.joblib"
|
| 9 |
model = None
|
|
|
|
| 13 |
global model
|
| 14 |
if model is None:
|
| 15 |
if not os.path.exists(MODEL_PATH):
|
| 16 |
+
raise FileNotFoundError(f"Model file not found: {MODEL_PATH}")
|
| 17 |
model = joblib.load(MODEL_PATH)
|
| 18 |
|
| 19 |
|
| 20 |
+
# Health check (important for deployment)
|
| 21 |
@app.route("/", methods=["GET"])
|
| 22 |
def health():
|
| 23 |
return "SuperKart Backend is running"
|
| 24 |
|
| 25 |
|
| 26 |
+
@app.route("/v1/predict", methods=["POST"])
|
|
|
|
| 27 |
def predict():
|
| 28 |
try:
|
| 29 |
load_model()
|
| 30 |
|
| 31 |
data = request.get_json(force=True)
|
| 32 |
|
| 33 |
+
sample = {
|
| 34 |
+
"Product_Weight": data["Product_Weight"],
|
| 35 |
+
"Product_Sugar_Content": data["Product_Sugar_Content"],
|
| 36 |
+
"Product_Allocated_Area": data["Product_Allocated_Area"],
|
| 37 |
+
"Product_MRP": data["Product_MRP"],
|
| 38 |
+
"Store_Size": data["Store_Size"],
|
| 39 |
+
"Store_Location_City_Type": data["Store_Location_City_Type"],
|
| 40 |
+
"Store_Type": data["Store_Type"],
|
| 41 |
+
"Product_Id_char": data["Product_Id_char"],
|
| 42 |
+
"Store_Age_Years": data["Store_Age_Years"],
|
| 43 |
+
"Product_Type_Category": data["Product_Type_Category"],
|
| 44 |
+
}
|
| 45 |
|
| 46 |
+
query_df = pd.DataFrame([sample])
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
prediction = model.predict(query_df)[0]
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
return jsonify({"predictions": float(prediction)})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
except KeyError as e:
|
| 53 |
return jsonify({"error": f"Missing field: {str(e)}"}), 400
|
|
|
|
| 55 |
return jsonify({"error": str(e)}), 500
|
| 56 |
|
| 57 |
|
|
|
|
| 58 |
if __name__ == "__main__":
|
| 59 |
app.run(host="0.0.0.0", port=7860)
|