Spaces:
Sleeping
Sleeping
| from flask import Flask, request, jsonify | |
| import joblib | |
| import numpy as np | |
| import pandas as pd | |
| # Initialize Flask app | |
| store_sales_predictor_api = Flask("Super Kart Store Sales Predictor Application") | |
| # Load the trained model | |
| try: | |
| superkart_model = joblib.load("superkart_storesales_prediction_model_v1_0.joblib") | |
| print("Model loaded successfully.") | |
| except FileNotFoundError: | |
| print("Error: 'superkart_storesales_prediction_model_v1_0.joblib' not found. Please train and save the model first.") | |
| superkart_model = None | |
| # Define home page for app | |
| def home(): | |
| return "Welcome to Super Kart Store Sales Predictor Application",200 | |
| # Health check endpoint | |
| def health_check(): | |
| """ | |
| Returns a 200 status code and a JSON response to indicate the service is healthy. | |
| """ | |
| return jsonify({"status": "healthy"}), 200 | |
| # Define prediction form page for app | |
| def predict_sales_price(): | |
| """ | |
| Handles prediction requests. | |
| Expects a JSON payload with 'features'. | |
| """ | |
| try: | |
| # Get data from the POST request | |
| payload = request.get_json() | |
| # Extract Relevant Features from Payload | |
| app_features = { | |
| "Product_Weight": payload["Product_Weight"], | |
| "Product_Sugar_Content": payload["Product_Sugar_Content"], | |
| "Product_Allocated_Area": payload["Product_Allocated_Area"], | |
| "Product_Type": payload["Product_Type"], | |
| "Product_MRP": payload["Product_MRP"], | |
| "Store_Establishment_Year": payload["Store_Establishment_Year"], | |
| "Store_Location_City_Type": payload["Store_Location_City_Type"], | |
| "Store_Id": payload["Store_Id"], | |
| "Store_Type": payload["Store_Type"], | |
| "Store_Size": payload["Store_Size"]} | |
| # store app_features in dataframe | |
| input_data = pd.DataFrame([app_features]) | |
| # Make prediction and get store sales | |
| predicted_sales = superkart_model.predict(input_data)[0] | |
| # calculate actual value | |
| #predicted_sales_value = np.exp(predicted_sales) | |
| # convert value to python float | |
| predicted_sales_value = round(float(predicted_sales),2) | |
| return jsonify({"predicted store sales total": predicted_sales_value}), 200 | |
| except Exception as e: | |
| return jsonify({"error": str(e)}), 500 | |
| # Run the Flask app in debug mode | |
| if __name__ == '__main__': | |
| app.run(debug=True) | |