Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import joblib | |
| from flask import Flask, request, jsonify | |
| import os # Import os for debugging | |
| superkart_api = Flask(__name__) | |
| # Define MODEL_FILENAME inside the app.py string | |
| MODEL_FILENAME = "tuned_gradient_boosting_regressor_model.joblib" # <-- ADD THIS LINE | |
| print("--- APP STARTUP: Flask app instance created ---") | |
| # Load the trained model | |
| try: | |
| print(f"--- APP STARTUP: Attempting to load model from {os.path.join(os.getcwd(), MODEL_FILENAME)} ---") | |
| model = joblib.load("tuned_gradient_boosting_regressor_model.joblib") | |
| print("--- APP STARTUP: Model loaded successfully ---") | |
| except Exception as e: | |
| print(f"--- APP STARTUP: ERROR loading model: {e} ---") | |
| raise # Re-raise to crash early if model loading fails | |
| print("--- APP STARTUP: Model variable assigned ---") | |
| def home(): | |
| print("--- REQUEST: Home endpoint accessed ---") | |
| return "Welcome to the Superkart Sales Prediction API!" | |
| def predict_sales(): | |
| print("--- REQUEST: Predict endpoint accessed ---") | |
| data = request.get_json() | |
| # ... (rest of your predict_sales logic) ... | |
| sample = { | |
| 'Product_Weight': data['Product_Weight'], | |
| 'Product_Sugar_Content': data['Product_Sugar_Content'], | |
| 'Product_Allocated_Area': data['Product_Allocated_Area'], | |
| 'Product_MRP': data['Product_MRP'], | |
| 'Store_Size': data['Store_Size'], | |
| 'Store_Location_City_Type': data['Store_Location_City_Type'], | |
| 'Store_Type': data['Store_Type'], | |
| 'Product_Id_char': data['Product_Id_char'], | |
| 'Store_Age_Years': data['Store_Age_Years'], | |
| 'Product_Type_Category': data['Product_Type_Category'] | |
| } | |
| input_df = pd.DataFrame([sample]) | |
| prediction = model.predict(input_df).tolist()[0] | |
| print(f"--- REQUEST: Prediction made: {prediction} ---") | |
| return jsonify({'Sales': prediction}) | |
| if __name__ == '__main__': | |
| print("--- APP STARTUP: Running in __main__ block ---") | |
| # import os # Redundant import removed | |
| port = int(os.environ.get("PORT", 7860)) | |
| superkart_api.run(host="0.0.0.0", port=port) | |