nishithworld commited on
Commit
3f187d4
·
verified ·
1 Parent(s): a0c5b71

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +13 -0
  2. app.py +106 -0
  3. requirements.txt +11 -0
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-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
13
+ CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:product_store_sales_predictor_api"]
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Initialize the Flask application
3
+ product_store_sales_predictor_api = Flask("SuperKart Product Store Sales Predictor")
4
+
5
+ # Load the trained machine learning model
6
+ model = joblib.load("saved_model_path")
7
+
8
+ # Define a route for the home page (GET request)
9
+ @product_store_sales_predictor_api.get('/')
10
+ def home():
11
+ """
12
+ This function handles GET requests to the root URL ('/') of the API.
13
+ It returns a simple welcome message.
14
+ """
15
+ return "Welcome to SuperKart Product Store Sales Predictor API!"
16
+
17
+ # Define an endpoint for single property prediction (POST request)
18
+ @product_store_sales_predictor_api.post('/v1/productsales')
19
+ def predict_product_sales():
20
+ """
21
+ This function handles POST requests to the '/v1/productsales' endpoint.
22
+ It expects a JSON payload containing Product and store details and returns
23
+ the predicted sales price as a JSON response.
24
+ """
25
+ # Get the JSON data from the request body
26
+ product_data = request.get_json()
27
+
28
+ # Extract relevant features from the JSON data
29
+ sample = {
30
+ 'Product_Weight': product_data['Product_Weight'],
31
+ 'Product_Allocated_Area': product_data['Product_Allocated_Area'],
32
+ 'Product_MRP': product_data['Product_MRP'],
33
+ 'Store_Age': product_data['Store_Age'],
34
+ 'Product_Identifier': product_data['Product_Identifier'],
35
+ 'Product_Sugar_Content_No Sugar': product_data['Product_Sugar_Content_No Sugar'],
36
+ 'Product_Sugar_Content_Regular': product_data['Product_Sugar_Content_Regular'],
37
+ 'Product_Sugar_Content_reg': product_data['Product_Sugar_Content_reg'],
38
+ 'Product_Type_Breads': product_data['Product_Type_Breads'],
39
+ 'Product_Type_Breakfast': product_data['Product_Type_Breakfast'],
40
+ 'Product_Type_Canned': product_data['Product_Type_Canned'],
41
+ 'Product_Type_Dairy': product_data['Product_Type_Dairy'],
42
+ 'Product_Type_Frozen Foods': product_data['Product_Type_Frozen Foods'],
43
+ 'Product_Type_Fruits and Vegetables': product_data['Product_Type_Fruits and Vegetables'],
44
+ 'Product_Type_Hard Drinks': product_data['Product_Type_Hard Drinks'],
45
+ 'Product_Type_Health and Hygiene': product_data['Product_Type_Health and Hygiene'],
46
+ 'Product_Type_Household': product_data['Product_Type_Household'],
47
+ 'Product_Type_Meat': product_data['Product_Type_Meat'],
48
+ 'Product_Type_Others': product_data['Product_Type_Others'],
49
+ 'Product_Type_Seafood': product_data['Product_Type_Seafood'],
50
+ 'Product_Type_Snack Foods': product_data['Product_Type_Snack Foods'],
51
+ 'Product_Type_Soft Drinks': product_data['Product_Type_Soft Drinks'],
52
+ 'Product_Type_Starchy Foods': product_data['Product_Type_Starchy Foods'],
53
+ 'Store_Id_OUT002': product_data['Store_Id_OUT002'],
54
+ 'Store_Id_OUT003': product_data['Store_Id_OUT003'],
55
+ 'Store_Id_OUT004': product_data['Store_Id_OUT004'],
56
+ 'Store_Size_Medium': product_data['Store_Size_Medium'],
57
+ 'Store_Size_Small': product_data['Store_Size_Small'],
58
+ 'Store_Location_City_Type_Tier 2': product_data['Store_Location_City_Type_Tier 2'],
59
+ 'Store_Location_City_Type_Tier 3': product_data['Store_Location_City_Type_Tier 3'],
60
+ 'Store_Type_Food Mart': product_data['Store_Type_Food Mart'],
61
+ 'Store_Type_Supermarket Type1': product_data['Store_Type_Supermarket Type1'],
62
+ 'Store_Type_Supermarket Type2': product_data['Store_Type_Supermarket Type2']
63
+ }
64
+
65
+ # Convert the extracted data into a Pandas DataFrame
66
+ input_data = pd.DataFrame([sample])
67
+
68
+ # Make prediction (get log_price)
69
+ predicted_sales = model.predict(input_data)[0]
70
+
71
+ # Convert predicted_price to Python float
72
+ predicted_sales = round(float(predicted_sales), 2)
73
+ # 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.
74
+ # When we send this value directly within a JSON response, Flask's jsonify function encounters a datatype error
75
+
76
+ # Return the actual price
77
+ return jsonify({'Predicted Product Store Sales Total': predicted_sales})
78
+
79
+
80
+ # Define an endpoint for batch prediction (POST request)
81
+ @product_store_sales_predictor_api.post('/v1/productsalesbatch')
82
+ def predict_product_sales_batch():
83
+ """
84
+ This function handles POST requests to the '/v1/productsalesbatch' endpoint.
85
+ It expects a CSV file containing product and store details for multiple entries
86
+ and returns the predicted sales as a dictionary in the JSON response.
87
+ """
88
+ # Get the uploaded CSV file from the request
89
+ file = request.files['file']
90
+
91
+ # Read the CSV file into a Pandas DataFrame
92
+ input_data = pd.read_csv(file)
93
+
94
+ # Make predictions for all products in the DataFrame
95
+ predicted_sales = model.predict(input_data).tolist()
96
+
97
+ # Create a dictionary of predictions with Product_Id as keys
98
+ product_ids = input_data['Product_Id'].tolist() # Assuming 'Product_Id' is the product ID column
99
+ output_dict = dict(zip(product_ids, predicted_sales))
100
+
101
+ # Return the predictions dictionary as a JSON response
102
+ return output_dict
103
+
104
+ # Run the Flask application in debug mode if this script is executed directly
105
+ if __name__ == '__main__':
106
+ product_store_sales_predictor_api.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pandas==2.2.2
2
+ numpy==2.0.2
3
+ scikit-learn==1.6.1
4
+ xgboost==2.1.4
5
+ joblib==1.4.2
6
+ Werkzeug==2.2.2
7
+ flask==2.2.2
8
+ gunicorn==20.1.0
9
+ requests==2.28.1
10
+ uvicorn[standard]
11
+ streamlit==1.43.2