Shalyn commited on
Commit
2116765
·
verified ·
1 Parent(s): 4a58514

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +75 -0
  3. requirements.txt +8 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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:churn_predictor_api"]
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
+ import pandas as pd
3
+ from flask import Flask, request, jsonify
4
+
5
+ #initialise flask app
6
+ sales_forecast_api = Flask('Sales forecasting')
7
+
8
+ # load the model
9
+
10
+ model = joblib.load('deployment_files/sales_forecast_v1_0.joblib')
11
+
12
+ #define home page
13
+ @sales_forecast_api.get('/')
14
+ def home():
15
+ return 'Welcome to the sales forecase api'
16
+
17
+ #define an endpoint for prediction
18
+ @sales_forecast_api.post('/v1/sales')
19
+ def sales_predict():
20
+ #get data from json request
21
+ sales_data = request.get_json()
22
+
23
+ #get relevant details
24
+ sample = {
25
+ 'Product_Weight' = sales_data['Product_Weight']
26
+ 'Product_Sugar_Content'= sales_data['Product_Sugar_Content']
27
+ 'Product_Allocated_Area' = sales_data['Product_Allocated_Area']
28
+ 'Product_Type' = sales_data['Product_Type']
29
+ 'Product_MRP' = sales_data['Product_MRP']
30
+ 'Store_Establishment_Year' = sales_data['Store_Establishment_Year']
31
+ 'Store_Size' = sales_data['Store_Size']
32
+ 'Store_Location_City_Type' = sales_data['Store_Location_City_Type']
33
+ 'Store_Type' = sales_data['Store_Type']
34
+ }
35
+
36
+ input_data = pd.DataFrame([sample])
37
+
38
+ #convert the categorical to dummies
39
+ categorical_columns_for_dummies = ['Product_Sugar_Content','Product_Type','Store_Size','Store_Location_City_Type','Store_Type']
40
+ input_df_dummies = pd.get_dummies(input_data, columns=categorical_columns_for_dummies, drop_first=True))
41
+
42
+ #make model to predict
43
+ prediction = model.predict(input_df_dummies.reindex(columns=X_train.columns, fill_value=0))
44
+
45
+ return jsonify({'Prediction':prediction})
46
+
47
+
48
+ #defining endpoint for batch
49
+ @sales_forecast_api.post('/v1/salesbatch')
50
+
51
+ def sales_batch_predict():
52
+ #get the file from the request
53
+ file = request.files['file']
54
+ #read the file to df
55
+ input_data = pd.read_csv(file)
56
+
57
+ #convert the categorical to dummies
58
+ categorical_columns_for_dummies = ['Product_Sugar_Content','Product_Type','Store_Size','Store_Location_City_Type','Store_Type']
59
+ input_df_dummies = pd.get_dummies(input_data, columns=categorical_columns_for_dummies, drop_first=True))
60
+ input_df_aligned =input_df_dummies.reindex(columns=X_train.columns, fill_value=0)
61
+
62
+ #predict
63
+ predictions = [
64
+ model.predict(input_df_aligned.drop(['Product_Id','Store_Id'],axis=1)).ToList()
65
+ ]
66
+ product_id_list = input_data.Product_Id.values.ToList()
67
+ store_id_list = input_data.Store_Id.values.ToList()
68
+ output_dict = dict(zip(product_id_list,store_id_list, predictions))
69
+
70
+
71
+ retrun output_dict
72
+
73
+ #run the flask app in debug mode
74
+ if __name__ == '__main__':
75
+ app.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ scikit-learn==1.4.2
2
+ pandas==2.0.3
3
+ numpy==1.25.2
4
+ matplotlib==3.7.1
5
+ seaborn==0.13.1
6
+ joblib==1.3.2
7
+ huggingface_hub==0.20.3
8
+ Flask==3.0.2