Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +62 -0
  3. requirements.txt +13 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11.7-slim
2
+
3
+ # Set the working directory inside the container to /app
4
+ WORKDIR /app
5
+
6
+ # Copy all files from the current directory on the host to the container's /app directory
7
+ COPY . .
8
+
9
+ # Install Python dependencies listed in requirements
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ #Define the command to run the Flask application on port 7860 and make it accessible externally
13
+ # - '-w 4' : Uses 4 worker processes to handle requests.
14
+ # - '-b 0.0.0.0:7860': Binds the Flask app to the specified host and port.
15
+ # - 'app:sales_prediction_api': Specifies the entry point of the Flask app.
16
+ CMD ["gunicorn", "w", "4", "-b", "0.0.0.0:7860" "app:sales_prediction_api"]
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import numpy as np
3
+ from flask import Flask, request, jsonify
4
+ import joblib
5
+ import pandas as pd
6
+
7
+ # Inititialize Flask app with name
8
+ sales_prediction_api = Flask("Sales Predictor")
9
+
10
+ # Load the trained model predictor model
11
+ dt_model = joblib.load("decision_tree_model.pkl")
12
+ xgb_model = joblib.load("xgboost_model.pkl")
13
+
14
+ # Define a route for the home page
15
+ @sales_prediction_api.route('/')
16
+ def home():
17
+ return "Sales Prediction API"
18
+
19
+ # Define an endpoint to predict sales
20
+ @sales_prediction_api.post('/predict')
21
+ def predict():
22
+ # Get the data from the request
23
+ data = request.get_json()
24
+
25
+ # Extract relevant features from the input data
26
+ sample = {
27
+ 'Product_Weight': data['Product_Weight'],
28
+ 'Product_Sugar_Content': data['Product_Sugar_Content'],
29
+ 'Product_Allocated_Area': data['Product_Allocated_Area'],
30
+ 'Product_Type': data['Product_Type'],
31
+ 'Product_MRP': data['Product_MRP'],
32
+ 'Store_Size': data['Store_Size'],
33
+ 'Store_Location_City_Type': data['Store_Location_City_Type'],
34
+ 'Store_Type': data['Store_Type'],
35
+ 'Store_Age': data['Store_Age']
36
+ }
37
+
38
+ #convert the extracted data into a dataframe
39
+ sample_df = pd.DataFrame(sample, index=[0])
40
+
41
+ # --------------------------------
42
+ # Model selection logic (FIXED)
43
+ # --------------------------------
44
+ model_choice = data.get("model", "dt")
45
+
46
+ if model_choice == "dt":
47
+ prediction = dt_model.predict(sample_df)[0]
48
+
49
+ else :
50
+ prediction = xgb_model.predict(sample_df)[0]
51
+
52
+
53
+ # --------------------------------
54
+ # Response
55
+ # --------------------------------
56
+ return jsonify({
57
+ "model_used": model_choice,
58
+ "prediction": float(prediction)
59
+ })
60
+
61
+ if __name__ == '__main__':
62
+ sales_prediction_api.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pandas==2.2.2
2
+ numpy==2.0.2
3
+ scikit-learn==1.6.1
4
+ seaborn==0.13.2
5
+ joblib==1.4.2
6
+ xgboost==2.1.4
7
+ joblib==1.4.2
8
+ Werkzeug==2.2.2
9
+ flask==2.2.2
10
+ gunicorn==20.1.0
11
+ requests==2.32.3
12
+ uvicorn[standard]
13
+ streamlit==1.43.2