Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,7 +1,90 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
import numpy as np
|
| 3 |
+
import joblib # For loading the serialized model
|
| 4 |
+
import pandas as pd # For data manipulation
|
| 5 |
+
from flask import Flask, request, jsonify # For creating the Flask API
|
| 6 |
|
| 7 |
+
# Initialize the Flask application
|
| 8 |
+
sales_predictor_api = Flask("SuperKart Sales Predictor")
|
| 9 |
|
| 10 |
+
# Load the trained machine learning model
|
| 11 |
+
model = joblib.load("sales_predictor.joblib")
|
| 12 |
+
|
| 13 |
+
# Define a route for the home page (GET request)
|
| 14 |
+
@sales_predictor_api.get('/')
|
| 15 |
+
def home():
|
| 16 |
+
"""
|
| 17 |
+
This function handles GET requests to the root URL ('/') of the API.
|
| 18 |
+
It returns a simple welcome message.
|
| 19 |
+
"""
|
| 20 |
+
return "Welcome to the SuperKart Sales Prediction API!"
|
| 21 |
+
|
| 22 |
+
# Define an endpoint for single product prediction (POST request)
|
| 23 |
+
@sales_predictor_api.post('/v1/predict')
|
| 24 |
+
def predict_sales():
|
| 25 |
+
"""
|
| 26 |
+
This function handles POST requests to the '/v1/predict' endpoint.
|
| 27 |
+
It expects a JSON payload containing product and store details and returns
|
| 28 |
+
the predicted sales as a JSON response.
|
| 29 |
+
"""
|
| 30 |
+
# Get the JSON data from the request body
|
| 31 |
+
product_store_data = request.get_json()
|
| 32 |
+
|
| 33 |
+
# Extract relevant features from the JSON data
|
| 34 |
+
sample = {
|
| 35 |
+
'Product_Weight': product_store_data['Product_Weight'],
|
| 36 |
+
'Product_Sugar_Content': product_store_data['Product_Sugar_Content'],
|
| 37 |
+
'Product_Allocated_Area': product_store_data['Product_Allocated_Area'],
|
| 38 |
+
'Product_Type': product_store_data['Product_Type'],
|
| 39 |
+
'Product_MRP': product_store_data['Product_MRP'],
|
| 40 |
+
'Store_Establishment_Year_Before_2005': product_store_data['Store_Establishment_Year_Before_2005'],
|
| 41 |
+
'Store_Size': product_store_data['Store_Size'],
|
| 42 |
+
'Store_Location_City_Type': product_store_data['Store_Location_City_Type'],
|
| 43 |
+
'Store_Type': product_store_data['Store_Type']
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
# Convert the extracted data into a Pandas DataFrame
|
| 47 |
+
input_data = pd.DataFrame([sample])
|
| 48 |
+
|
| 49 |
+
# Make prediction
|
| 50 |
+
predicted_sales = model.predict(input_data)[0]
|
| 51 |
+
|
| 52 |
+
# Convert predicted_sales to Python float
|
| 53 |
+
predicted_sales = round(float(predicted_sales), 2)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Return the predicted sales
|
| 57 |
+
return jsonify({'Predicted Sales Total (in dollars)': predicted_sales})
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# Define an endpoint for batch prediction (POST request) - This endpoint is not functional with the current model
|
| 61 |
+
# as the model is trained on individual product-store combinations, not batch data with IDs.
|
| 62 |
+
# @sales_predictor_api.post('/v1/predictbatch')
|
| 63 |
+
# def predict_sales_batch():
|
| 64 |
+
# """
|
| 65 |
+
# This function handles POST requests to the '/v1/predictbatch' endpoint.
|
| 66 |
+
# It expects a CSV file containing product and store details for multiple entries
|
| 67 |
+
# and returns the predicted sales as a dictionary in the JSON response.
|
| 68 |
+
# """
|
| 69 |
+
# # Get the uploaded CSV file from the request
|
| 70 |
+
# file = request.files['file']
|
| 71 |
+
|
| 72 |
+
# # Read the CSV file into a Pandas DataFrame
|
| 73 |
+
# input_data = pd.read_csv(file)
|
| 74 |
+
|
| 75 |
+
# # Make predictions for all entries in the DataFrame
|
| 76 |
+
# predicted_sales = model.predict(input_data).tolist()
|
| 77 |
+
|
| 78 |
+
# # For batch prediction with IDs, you would need an ID column in the input data
|
| 79 |
+
# # and a way to associate predictions with IDs.
|
| 80 |
+
# # Since the current model predicts for individual entries based on features,
|
| 81 |
+
# # this batch endpoint is not directly applicable without further modifications
|
| 82 |
+
# # to handle input data with IDs and return predictions associated with those IDs.
|
| 83 |
+
|
| 84 |
+
# # Returning a placeholder response
|
| 85 |
+
# return jsonify({"message": "Batch prediction endpoint is not yet implemented for this model."})
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# Run the Flask application in debug mode if this script is executed directly
|
| 89 |
+
if __name__ == '__main__':
|
| 90 |
+
sales_predictor_api.run(debug=True)
|