Spaces:
Runtime error
Runtime error
| # Import necessary libraries | |
| import numpy as np | |
| import joblib # For loading the serialized model | |
| import pandas as pd # For data manipulation | |
| from flask import Flask, request, jsonify # For creating the Flask API | |
| # Initialize the Flask application | |
| store_product_sales_predictor_api = Flask("SuperKart Store Product Sales Predictor") | |
| # Load the trained machine learning model | |
| model = joblib.load("/content/drive/My Drive/rf_tuned.pk1") | |
| # 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 Product Sale Prediction API!" | |
| # Define an endpoint for single sale prediction (POST request) | |
| def predict_product_sales(): | |
| """ | |
| This function handles POST requests to the '/v1/sales' endpoint. | |
| It expects a JSON product id and returns the predicted product sales | |
| as a JSON response. | |
| """ | |
| # Get the JSON data from the request body | |
| product_sale = request.get_json() | |
| # Extract relevant features from the JSON data | |
| sample = { | |
| 'Product_Weight': product_sale['Product_Weight'], | |
| 'Product_Sugar_Content': product_sale['Product_Sugar_Content'], | |
| 'Product_Allocated_Area': product_sale['Product_Allocated_Area'], | |
| 'Product_Type': product_sale['Product_Type'], | |
| 'Product_Allocated_Area': product_sale['Product_Allocated_Area'], | |
| 'Product_Type': product_sale['Product_Type'], | |
| 'Product_MRP': product_sale['Product_MRP'], | |
| 'Store_Id': product_sale['Store_Id'], | |
| 'Store_Size': product_sale['Store_Size'], | |
| 'Store_Location_City_Type': product_sale['Store_Location_City_Type'], | |
| 'Store_Type': product_sale['Store_Type'], | |
| 'Store_Establishment_Year': product_sale['Store_Establishment_Year'] | |
| } | |
| # Convert the extracted data into a Pandas DataFrame | |
| input_data = pd.DataFrame([sample]) | |
| # Make prediction | |
| predicted_sale = model.predict(input_data)[0] | |
| # The target variable was not log-transformed during training, so no need to apply np.exp here. | |
| # actual_sale = np.exp(predicted_sale) | |
| # Convert predicted_sale to Python float | |
| actual_sale = round(float(predicted_sale), 2) | |
| # The conversion above is needed as we convert the model prediction (log price) to actual price using np.exp, which returns predictions as NumPy float32 values. | |
| # When we send this value directly within a JSON response, Flask's jsonify function encounters a datatype error | |
| # Return the actual price | |
| return jsonify({'Actual Sale': actual_sale}) | |
| # Run the Flask application in debug mode if this script is executed directly | |
| if __name__ == '__main__': | |
| store_product_sales_predictor_api.run(debug=True) | |
| Overwriting backend_files/app.py | |
| Dependencies File | |
| %%writefile backend_files/requirements.txt | |
| pandas==2.2.2 | |
| numpy==2.0.2 | |
| scikit-learn==1.6.1 | |
| xgboost==2.1.4 | |
| joblib==1.4.2 | |
| Werkzeug==2.2.2 | |
| flask==2.2.2 | |
| gunicorn==20.1.0 | |
| requests==2.28.1 | |
| uvicorn[standard] | |
| streamlit==1.43.2 | |
| Writing backend_files/requirements.txt | |
| Dockerfile | |
| %%writefile backend_files/Dockerfile | |
| FROM python:3.9-slim | |
| # Set the working directory inside the container | |
| WORKDIR /app | |
| # Copy all files from the current directory to the container's working directory | |
| COPY . . | |
| # Install dependencies from the requirements file without using cache to reduce image size | |
| RUN pip install --no-cache-dir --upgrade -r requirements.txt | |
| # Define the command to start the application using Gunicorn with 4 worker processes | |
| # - `-w 4`: Uses 4 worker processes for handling requests | |
| # - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces | |
| # - `app:app`: Runs the Flask app (assuming `app.py` contains the Flask instance named `app`) | |
| CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:rental_price_predictor_api"] | |