Spaces:
Sleeping
Sleeping
| # Import necessary libraries | |
| import joblib | |
| import pandas as pd | |
| from flask import Flask, request, jsonify | |
| print("Starting SuperKart Flask API...") | |
| # Initialize Flask app | |
| app = Flask(__name__) | |
| # Load trained model | |
| try: | |
| model = joblib.load("superkart_sales_model.pkl") | |
| print("Model loaded successfully") | |
| except Exception as e: | |
| print("Error loading model:", e) | |
| raise | |
| # Home route (health check) | |
| def home(): | |
| return "✅ SuperKart Sales Prediction API is running!" | |
| # ---------- SINGLE PREDICTION ---------- | |
| def predict_sales(): | |
| data = request.get_json() | |
| sample = { | |
| "Product_Id": data["Product_Id"], | |
| "Product_Weight": data["Product_Weight"], | |
| "Product_Sugar_Content": data["Product_Sugar_Content"], | |
| "Product_Allocated_Area": data["Product_Allocated_Area"], | |
| "Product_Type": data["Product_Type"], | |
| "Product_MRP": data["Product_MRP"], | |
| "Store_Id": data["Store_Id"], | |
| "Store_Type": data["Store_Type"], | |
| "Store_Size": data["Store_Size"], | |
| "Store_Location_City_Type": data["Store_Location_City_Type"], | |
| "Store_Current_Age": data["Store_Current_Age"], | |
| } | |
| input_df = pd.DataFrame([sample]) | |
| prediction = model.predict(input_df)[0] | |
| return jsonify({ | |
| "Predicted_Sales": round(float(prediction), 2) | |
| }) | |
| # ---------- BATCH PREDICTION ---------- | |
| def predict_sales_batch(): | |
| file = request.files["file"] | |
| df = pd.read_csv(file) | |
| predictions = model.predict(df) | |
| predictions = [round(float(p), 2) for p in predictions] | |
| return jsonify({ | |
| "Predicted_Sales": predictions | |
| }) | |
| # Run locally (Hugging Face ignores this but keeps it safe) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) | |