Download app.py from SRGL/SuperKartSalesRevenuePredictorBackend: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/spaces/SRGL/SuperKartSalesRevenuePredictorBackend/resolve/main/app.py
- Command line
-
hf download hf://spaces/SRGL/SuperKartSalesRevenuePredictorBackend/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SRGL/SuperKartSalesRevenuePredictorBackend/resolve/main/app.py
2.78 kB
| # Import necessary libraries | |
| import numpy as np | |
| import joblib # For loading the serialized model | |
| import pandas as pd # For data manipulation | |
| import logging | |
| import sys | |
| from flask import Flask, request, jsonify # For creating the Flask API | |
| # Initialize the Flask application | |
| sales_revenue_predictor_api = Flask("SuperKart Sales Revenue Predictor") | |
| # Configure logging to output to stdout (Hugging Face captures this) | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
| handlers=[logging.StreamHandler(sys.stdout)] | |
| ) | |
| logger = logging.getLogger("SuperKart Sales Revenue Predictor") | |
| # Load the trained machine learning model | |
| model = joblib.load("superkart_sales_prediction_model_v2_0.joblib") | |
| # Define a route for the home page (GET request) | |
| def home(): | |
| """ | |
| This function handles GET requests to the root URL ('/') of the API. | |
| It returns a simple welcome message. | |
| """ | |
| return "Welcome to the SuperKart Sales Revenue Prediction API!" | |
| # Define an endpoint for single property prediction (POST request) | |
| def predict_sales_revenue(): | |
| """ | |
| This function handles POST requests to the '/v1/sales' endpoint. | |
| It expects a JSON payload containing property details and returns | |
| the predicted Sales Revenue as a JSON response. | |
| """ | |
| logger.info(f"Request received: {request.method} {request.path}") | |
| logger.debug(f"Headers: {dict(request.headers)}") | |
| logger.debug(f"Body: {request.get_json()}") | |
| # Get the JSON data from the request body | |
| property_data = request.get_json() | |
| # Extract relevant features from the JSON data | |
| sample = { | |
| 'Store_Id': property_data['store_id'], | |
| 'Product_Type': property_data['product_type'], | |
| 'Product_Sugar_Content': property_data['product_sugar_content'], | |
| 'Product_MRP': property_data['product_mrp'], | |
| 'Product_Weight': property_data['product_weight'], | |
| 'Product_Allocated_Area': property_data['product_allocated_area'] | |
| } | |
| # Convert the extracted data into a Pandas DataFrame | |
| input_data = pd.DataFrame([sample]) | |
| logger.debug(f"input Data : {input_data}") | |
| # Make prediction (get sales revenue) | |
| predicted_sales_revenue = model.predict(input_data)[0] | |
| # Convert predicted_sales_revenue to Python float | |
| predicted_sales_revenue = round(float(predicted_sales_revenue), 2) | |
| logger.debug(f"Predicted Sales Revenue : {predicted_sales_revenue}") | |
| # Return the predicted_sales_revenue | |
| return jsonify({'Predicted Sales Revenue': predicted_sales_revenue}) | |
| # Run the Flask application in debug mode if this script is executed directly | |
| if __name__ == '__main__': | |
| sales_revenue_predictor_api.run(debug=True) | |