Spaces:
Sleeping
Sleeping
| import os | |
| import joblib | |
| import pandas as pd | |
| from flask import Flask, request, jsonify | |
| app = Flask(__name__) | |
| MODEL_PATH = "superkart_model_v1_0.joblib" | |
| model = None | |
| def load_model(): | |
| global model | |
| if model is None: | |
| if not os.path.exists(MODEL_PATH): | |
| raise FileNotFoundError(f"Model file not found: {MODEL_PATH}") | |
| model = joblib.load(MODEL_PATH) | |
| # Health check (important for deployment) | |
| def health(): | |
| return "SuperKart Backend is running" | |
| # Changed from /v1/predict to match frontend | |
| def predict(): | |
| try: | |
| load_model() | |
| data = request.get_json(force=True) | |
| # Keys must be strings to match the JSON sent by Streamlit | |
| sample = { | |
| "Product_Weight": data["Product_Weight"][0], | |
| "Product_Sugar_Content": data["Product_Sugar_Content"][0], | |
| "Product_Allocated_Area": data["Product_Allocated_Area"][0], | |
| "Product_Type": data["Product_Type"][0], | |
| "Product_MRP": data["Product_MRP"][0], | |
| "Store_Establishment_Year": data["Store_Establishment_Year"][0], | |
| "Store_Size": data["Store_Size"][0], | |
| "Store_Location_City_Type": data["Store_Location_City_Type"][0], | |
| "Store_Type": data["Store_Type"][0] | |
| } | |
| query_df = pd.DataFrame([sample]) | |
| prediction = model.predict(query_df).tolist() | |
| return jsonify({"predictions": prediction}) | |
| except Exception as e: | |
| return jsonify({"error": str(e)}), 500 | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) |