yokesh1999 commited on
Commit
a645397
·
verified ·
1 Parent(s): 1472a29

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -0
  2. app.py +77 -0
  3. requirements.txt +10 -0
  4. superkart_sales_model_v1_0.joblib +3 -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:sales_forecast_api"]
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
+ import pandas as pd
3
+ from flask import Flask, request, jsonify
4
+ import numpy as np
5
+
6
+ # Initialize Flask app
7
+ sales_forecast_api = Flask("SuperKart Sales Forecast Predictor")
8
+
9
+ # Load the trained SuperKart sales model
10
+ model = joblib.load("superkart_sales_model_v1_0.joblib")
11
+
12
+ # Define a route for the home page
13
+ @sales_forecast_api.get('/')
14
+ def home():
15
+ return "Welcome to the SuperKart Sales Revenue Forecasting API!"
16
+
17
+ # Define an endpoint to predict sales for a single product-store combination
18
+ @sales_forecast_api.post('/v1/sales')
19
+ def predict_sales():
20
+ # Get JSON data from the request
21
+ sales_data = request.get_json()
22
+
23
+ # Extract relevant features from the input data
24
+ # Note: Store_Age will be calculated from Store_Establishment_Year
25
+ current_year = 2024
26
+ store_age = current_year - sales_data['Store_Establishment_Year']
27
+
28
+ sample = {
29
+ 'Product_Weight': sales_data['Product_Weight'],
30
+ 'Product_Sugar_Content': sales_data['Product_Sugar_Content'],
31
+ 'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
32
+ 'Product_Type': sales_data['Product_Type'],
33
+ 'Product_MRP': sales_data['Product_MRP'],
34
+ 'Store_Establishment_Year': sales_data['Store_Establishment_Year'],
35
+ 'Store_Size': sales_data['Store_Size'],
36
+ 'Store_Location_City_Type': sales_data['Store_Location_City_Type'],
37
+ 'Store_Type': sales_data['Store_Type'],
38
+ 'Store_Age': store_age
39
+ }
40
+
41
+ # Convert the extracted data into a DataFrame
42
+ input_data = pd.DataFrame([sample])
43
+
44
+ # Make a prediction using the trained model
45
+ prediction = model.predict(input_data).tolist()[0]
46
+
47
+ # Return the prediction as a JSON response
48
+ return jsonify({'Predicted_Sales_Total': prediction})
49
+
50
+ # Define an endpoint to predict sales for a batch of product-store combinations
51
+ @sales_forecast_api.post('/v1/salesbatch')
52
+ def predict_sales_batch():
53
+ # Get the uploaded CSV file from the request
54
+ file = request.files['file']
55
+
56
+ # Read the file into a DataFrame
57
+ input_data = pd.read_csv(file)
58
+
59
+ # Calculate Store_Age if not present
60
+ if 'Store_Age' not in input_data.columns:
61
+ current_year = 2024
62
+ input_data['Store_Age'] = current_year - input_data['Store_Establishment_Year']
63
+
64
+ # Make predictions for the batch data
65
+ predictions = model.predict(input_data).tolist()
66
+
67
+ # Add predictions to the DataFrame
68
+ input_data['Predicted_Sales_Total'] = predictions
69
+
70
+ # Convert results to dictionary
71
+ result = input_data.to_dict(orient="records")
72
+
73
+ return jsonify(result)
74
+
75
+ # Run the Flask app in debug mode
76
+ if __name__ == '__main__':
77
+ sales_forecast_api.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
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]
superkart_sales_model_v1_0.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:200c1855aa4114e035256f3c29e366623272deda030dc23af2613f70315a8f4b
3
+ size 23837399