MBG0903 commited on
Commit
93b8188
·
verified ·
1 Parent(s): 8740064

Upload folder using huggingface_hub

Browse files
Dockerfile ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 application named 'sales_prediction_api' from the 'app.py' file
16
+
17
+ ENTRYPOINT ["gunicorn", "-w", "4", "--bind", "0.0.0.0:7860", "app:sales_prediction_api"]
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Import necessary libraries
3
+ import numpy as np
4
+ import joblib # For loading the serialized model
5
+ import pandas as pd # For data manipulation
6
+ from flask import Flask, request, jsonify # For creating the Flask API
7
+
8
+ # Initialize flask app with a name
9
+ sales_prediction_api = Flask("Superkart Sales Predictor")
10
+
11
+ # Load the trained machine learning model
12
+ model = joblib.load("superkart_sales_prediction_model.joblib")
13
+
14
+ # Define a route for the home page
15
+ @sales_prediction_api.get("/")
16
+ def home():
17
+ """
18
+ This function handles GET requests to the root URL ('/') of the API.
19
+ It returns a simple welcome message.
20
+ """
21
+ return "Welcome to Superkart Sales Prediction API!"
22
+
23
+ # Define an endpoint for single property prediction (POST request)
24
+ @sales_prediction_api.post("/v1/predict")
25
+ def predict_sales():
26
+ """
27
+ This function handles POST requests to the '/v1/predi' endpoint.
28
+ It expects a JSON payload containing property details and returns
29
+ the predicted rental price as a JSON response.
30
+ """
31
+ # Get the JSON data from the request body
32
+ business_data = request.get_json()
33
+
34
+ # Extract relevant features from the JSON data
35
+ sample = {
36
+ 'Product_Weight': business_data['Product_Weight'] ,
37
+ 'Product_Sugar_Content': business_data['Product_Sugar_Content'],
38
+ 'Product_Allocated_Area': business_data['Product_Allocated_Area'],
39
+ 'Product_Type': business_data['Product_Type'],
40
+ 'Product_MRP': business_data['Product_MRP'],
41
+ 'Store_Establishment_Year': business_data['Store_Establishment_Year'],
42
+ 'Store_Size': business_data['Store_Size'],
43
+ 'Store_Location_City_Type': business_data['Store_Location_City_Type'],
44
+ 'Store_Type': business_data['Store_Type']
45
+ }
46
+
47
+ # Convert the extracted data into a Pandas DataFrame
48
+ input_data = pd.DataFrame([business_data])
49
+
50
+ # Make a sales prediction using the sales model
51
+ predicted_sales = model.predict(input_data)[0]
52
+
53
+ # Convert predicted_price to Python float
54
+ predicted_sales = float(predicted_sales)
55
+
56
+ # Return the actual sales
57
+ return jsonify({'Predicted sales': predicted_sales})
58
+
59
+ if __name__ == "__main__":
60
+ sales_prediction_api.run(debug=True)
61
+
62
+ # Define an endpoint for batch prediction (POST request)
63
+ @sales_prediction_api.post("/v1/predict/batch")
64
+ def predict_sales_batch():
65
+ """
66
+ This function handles POST requests to the '/v1/predict/batch' endpoint.
67
+ It expects a CSV file containing property details for multiple properties
68
+ and returns the predicted rental prices as a dictionary in the JSON response.
69
+ """
70
+ # Get the uploaded CSV file from the request
71
+ file = request.files['file']
72
+
73
+ # Read the CSV file into a Pandas DataFrame
74
+ input_data = pd.read_csv(file)
75
+
76
+ # Make predictions for all properties in the DataFrame (get log_prices)
77
+ predicted_log_sales = model.predict(input_data).tolist()
78
+
79
+ # Create a dictionary of predictions with property IDs as keys
80
+ Store_Type = input_data['Store_Type'].tolist()
81
+ output_dict = dict(zip(Store_Type, predicted_sales)) # Use actual prices
82
+
83
+ # Return the predictions dictionary as a JSON response
84
+ return output_dict
85
+
86
+ # Run the Flask application in debug mode if this script is executed directly
87
+ if __name__ == '__main__':
88
+ sales_prediction_api.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ Flask==3.1.0
2
+ joblib==1.4.2
3
+ pandas==2.2.2
4
+ scikit-learn==1.6.1
5
+ gunicorn==23.0.0
6
+ numpy==2.0.2
7
+ seaborn==0.13.2
8
+ xgboost==2.1.4
9
+ requests==2.32.3
10
+ streamlit==1.45.1
superkart_sales_prediction_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e9b8219f2405b2d0b7d27792cf7cb26b54083c77f9998caf16386f41cc56a7c8
3
+ size 63813587