nsriram78 commited on
Commit
2f7294a
·
verified ·
1 Parent(s): a36186c

Upload folder using huggingface_hub

Browse files
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.12-slim
2
+
3
+ # Set the working directory inside the container
4
+ WORKDIR /app
5
+
6
+ # Copy all files from the current directory to the container's working directory
7
+ COPY . .
8
+
9
+ # Install dependencies from the requirements file without using cache to reduce image size
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ # Define the command to start the application using Gunicorn with 4 worker processes
13
+ # - `-w 4`: Uses 4 worker processes for handling requests
14
+ # - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
15
+ # - `app:app`: Runs the Flask app (assuming `app.py` contains the Flask instance named `app`)
16
+ CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:sales_revenue_predictor_api"]
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import joblib # For loading the serialized model
3
+ import pandas as pd # For data manipulation
4
+ from flask import Flask, request, jsonify # For creating the Flask API
5
+ from datetime import datetime # added for dynamic current year
6
+
7
+ # Initialize the Flask application
8
+ sales_revenue_predictor_api = Flask("SuperKart Sales Revenue Predictor")
9
+
10
+ # Load the trained machine learning model
11
+ model = joblib.load("superkart_sales_revenue_prediction_model_v1_0.joblib")
12
+
13
+ # Define a route for the home page (GET request)
14
+ @sales_revenue_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 Product Sales Revenue Prediction API !"
21
+
22
+ # Define an endpoint for single product sales revenue prediction (POST request)
23
+ @sales_revenue_predictor_api.post('/v1/revenue')
24
+ def predict_sales_revenue():
25
+ """
26
+ This function handles POST requests to the '/v1/revenue' endpoint.
27
+ It expects a JSON payload containing property details and returns
28
+ the predicted product sales revenue as a JSON response.
29
+ """
30
+ # Get the JSON data from the request body
31
+ property_data = request.get_json()
32
+
33
+ # Extract relevant features from the JSON data
34
+ sample = {
35
+ 'Product_Weight': property_data['Product_Weight'],
36
+ 'Product_Sugar_Content': property_data['Product_Sugar_Content'],
37
+ 'Product_Allocated_Area': property_data['Product_Allocated_Area'],
38
+ 'Product_Type': property_data['Product_Type'],
39
+ 'Product_MRP': property_data['Product_MRP'],
40
+ 'Store_Size': property_data['Store_Size'],
41
+ 'Store_Location_City_Type': property_data['Store_Location_City_Type'],
42
+ 'Store_Type': property_data['Store_Type'],
43
+ 'Store_Establishment_Year': property_data['Store_Establishment_Year']
44
+ }
45
+
46
+ # Convert the extracted data into a Pandas DataFrame
47
+ record_input_data = pd.DataFrame([sample])
48
+
49
+ # Compute Store_Age
50
+ current_year_value = datetime.now().year
51
+ record_input_data['Store_Age'] = current_year_value - record_input_data['Store_Establishment_Year']
52
+
53
+ # Define bins and labels (open-ended last bin for 50+ years)
54
+ age_bins = [0, 10, 20, 30, float("inf")]
55
+ age_labels = ["<10 Years", "10–20 Years", "20–30 Years", "30+ Years"]
56
+
57
+ # Create binned column
58
+ record_input_data["Store_Age_Binned"] = pd.cut(
59
+ record_input_data["Store_Age"],
60
+ bins=age_bins,
61
+ labels=age_labels,
62
+ right=False
63
+ )
64
+
65
+ # Make prediction (get revenue)
66
+ predicted_store_revenue = model.predict(record_input_data)[0]
67
+
68
+ # Convert predicted_price to Python float
69
+ predicted_store_revenue = round(float(predicted_store_revenue), 2)
70
+ # When we send this value directly within a JSON response, Flask's jsonify function encounters a datatype error
71
+
72
+ # Return the actual price
73
+ return jsonify({'Predicted Product_Store_Sales_Total': predicted_store_revenue})
74
+
75
+
76
+ # Define an endpoint for batch prediction (POST request)
77
+ @sales_revenue_predictor_api.post('/v1/revenuebatch')
78
+ def predict_sales_revenue_batch():
79
+ """
80
+ This function handles POST requests to the '/v1/revenuebatch' endpoint.
81
+ It expects a CSV file containing property details for multiple properties
82
+ and returns the predicted product sales revenue as a dictionary in the JSON response.
83
+ """
84
+ # Get the uploaded CSV file from the request
85
+ file = request.files['file']
86
+
87
+ # Read the CSV file into a Pandas DataFrame
88
+ csv_input_data = pd.read_csv(file)
89
+
90
+ # Compute Store_Age
91
+ current_year_value = datetime.now().year
92
+ csv_input_data['Store_Age'] = current_year_value - csv_input_data['Store_Establishment_Year']
93
+ # Define bins and labels (open-ended last bin for 50+ years)
94
+ age_bins = [0, 10, 20, 30, float("inf")]
95
+ age_labels = ["<10 Years", "10–20 Years", "20–30 Years", "30+ Years"]
96
+
97
+ # Create binned column
98
+ csv_input_data["Store_Age_Binned"] = pd.cut(
99
+ csv_input_data["Store_Age"],
100
+ bins=age_bins,
101
+ labels=age_labels,
102
+ right=False
103
+ )
104
+
105
+ # Make predictions for all properties in the DataFrame (get log_prices)
106
+ predicted_store_revenues = model.predict(csv_input_data).tolist()
107
+
108
+ # Create a dictionary of predictions with product IDs as keys
109
+ if 'Product_Id' not in csv_input_data.columns:
110
+ return jsonify({"error": "Input file must contain a 'Product_Id' column"}), 400
111
+
112
+ # Create a dictionary of predictions with Product IDs as keys
113
+ product_ids = csv_input_data['Product_Id'].tolist() # Assuming 'Product_Id' is the property ID column
114
+ output_dict = dict(zip(product_ids, predicted_store_revenues)) # Use actual prices
115
+
116
+ # Return the predictions dictionary as a JSON response
117
+ return jsonify(output_dict)
118
+
119
+ # Run the Flask application in debug mode if this script is executed directly
120
+ if __name__ == '__main__':
121
+ sales_revenue_predictor_api.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pandas==2.3.0
2
+ numpy==2.0.2
3
+ scikit-learn==1.6.1
4
+ xgboost==3.0.2
5
+ joblib==1.5.0
6
+ Werkzeug==3.1.3
7
+ flask==3.1.2
8
+ gunicorn==23.0.0
9
+ requests==2.32.3
10
+ uvicorn==0.34.2
11
+ streamlit==1.49.1
superkart_sales_revenue_prediction_model_v1_0.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:518ece4da7aeae64791149e877fcc0685d327540441f7ecd2d09479a0c55a5ff
3
+ size 19050522