u2jyothibhat commited on
Commit
073a82f
·
verified ·
1 Parent(s): d6ce99d

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -0
  2. app.py +68 -0
  3. final_xgb_pipeline.joblib +3 -0
  4. requirements.txt +10 -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_predictor_api"]
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import numpy as np
3
+ import joblib # For loading the serialized model
4
+ import pandas as pd # For data manipulation
5
+ from flask import Flask, request, jsonify # For creating the Flask API
6
+
7
+ import pandas as pd
8
+ import numpy as np
9
+ from sklearn.base import BaseEstimator, TransformerMixin
10
+ from sklearn.preprocessing import PowerTransformer, OrdinalEncoder
11
+ from sklearn.pipeline import Pipeline
12
+ from sklearn.compose import ColumnTransformer
13
+
14
+ print( " Trying to load XGBoost model using joblib")
15
+ model = joblib.load("final_xgb_pipeline.joblib")
16
+
17
+ print("Model loaded successfully!")
18
+
19
+ # Initialize the Flask application
20
+ sales_predictor_api = Flask("SuperKart Sales Prediction")
21
+
22
+ # Define a route for the home page (GET request)
23
+ @sales_predictor_api.get('/')
24
+ def home():
25
+ """
26
+ This function handles GET requests to the root URL ('/') of the API.
27
+ It returns a simple welcome message.
28
+ """
29
+ return "Welcome to the SuperKart Sales Prediction API!"
30
+
31
+ # Define an endpoint for single property prediction (POST request)
32
+ @sales_predictor_api.post('/v1/sales')
33
+ def predict_sales():
34
+ """
35
+ This function handles POST requests to the '/v1/sales' endpoint.
36
+ It expects a JSON payload containing property details and returns
37
+ the predicted rental price as a JSON response.
38
+ """
39
+ # Get the JSON data from the request body
40
+ property_data = request.get_json()
41
+
42
+ # Extract relevant features from the JSON data
43
+ sample = {
44
+
45
+ 'Product_Weight': property_data['Product_Weight'],
46
+ 'Product_Allocated_Area': property_data['Product_Allocated_Area'],
47
+ 'Product_MRP': property_data['Product_MRP'],
48
+ 'Product_Sugar_Content': property_data['Product_Sugar_Content'],
49
+ 'Product_Type': property_data['Product_Type'],
50
+ 'Store_Establishment_Year': property_data['Store_Establishment_Year'],
51
+ 'Store_Size': property_data['Store_Size'],
52
+ 'Store_Location_City_Type': property_data['Store_Location_City_Type'],
53
+ # 'pid_c2': property_data['pid_c2'],
54
+ 'Store_Type': property_data['Store_Type']
55
+ }
56
+ # print( ' recevied request from client ')
57
+ # Convert the extracted data into a Pandas DataFrame
58
+ input_data = pd.DataFrame([sample])
59
+ # print("data recevied = ", input_data)
60
+ # Make prediction (get log_price)
61
+ predicted_sales = model.predict(input_data)[0]
62
+ predicted_sales = float(predicted_sales) # convert to native float
63
+ # print ("Sales predicted = ", predicted_sales)
64
+ return jsonify({'Predicted Sales': predicted_sales})
65
+
66
+ # Run the Flask application in debug mode if this script is executed directly
67
+ if __name__ == '__main__':
68
+ sales_predictor_api.run(debug=True)
final_xgb_pipeline.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:531a82dd8b5ebed88101f1fd4019019fd635c3438e3ba22052ad0b78345c980d
3
+ size 265806
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]