Parthi07 commited on
Commit
f1b60a0
·
verified ·
1 Parent(s): fa39f29

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +77 -0
  3. 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 -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:product_sales_prediction_api"]
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import joblib
3
+ from flask import Flask, request, jsonify
4
+
5
+ # Initiate flask app with a name
6
+ product_sales_prediction_api = Flask("SuperKart Product Sales Prediction")
7
+
8
+ # Load the trained sales prediction model
9
+ model = joblib.load("deployment files/superkart_product_sales_prediction_model_v1_0.joblib")
10
+
11
+ # Define a route for the home page (GET request)
12
+
13
+ @product_sales_prediction_api.get("/")
14
+ def home():
15
+ """
16
+ This function handles GET requests to the root URL ('/') of the API.
17
+ It returns a simple welcome message.
18
+ """
19
+ return "Welcome to SuperKart Product Sales Prediction API!"
20
+
21
+ # Define an endpoint for single property prediction (POST request)
22
+ @product_sales_prediction_api.post("/v1/sales")
23
+ def predict_product_sales():
24
+ """
25
+ This function handles POST requests to the '/v1/sales' endpoint.
26
+ It expects a JSON payload containing property details and returns
27
+ the predicted product sales price as a JSON response.
28
+ """
29
+ # Get the JSON data from the request body
30
+ product_data = requests.get_json()
31
+
32
+ # Extract relevant features from the JSON data
33
+ sample = {
34
+ 'Product_Weight': product_data['Product_Weight'],
35
+ 'Product_Sugar_Content' : product_data['Product_Sugar_Content'],
36
+ 'Product_Allocated_Area' : product_data['Product_Allocated_Area'],
37
+ 'Product_Type' : product_data['Product_Type'],
38
+ 'Product_MRP' : product_data['Product_MRP'],
39
+ 'Store_Size' : product_data['Store_Size'],
40
+ 'Store_Location_City_Type' : product_data['Store_Location_City_Type'],
41
+ 'Store_Type' : product_data['Store_Type'],
42
+ 'Store_Age' : 2025- product_data['Store_Age']
43
+ }
44
+ # Convert the extracted data into a Pandas DataFrame
45
+ input_data = pd.DataFrame([sample])
46
+
47
+ # Make prediction (get product_store_sales_price)
48
+ predicted_sales_price = model.predict(input_data)[0]
49
+
50
+ predicted_sales_price = round(float(predicted_sales_price), 2)
51
+
52
+ return jsonify({'Predicted Product Sales Price',predicted_sales_price})
53
+
54
+ # Define an endpoint for batch prediction (POST request)
55
+ @product_sales_prediction_api.post('v1/salesbatch')
56
+ def predict_product_sales_batch():
57
+ """
58
+ This function handles POST requests to the '/v1/salesbatch' endpoint.
59
+ It expects a CSV file containing property details for multiple properties
60
+ and returns the predicted rental prices as a dictionary in the JSON response.
61
+ """
62
+ # Get the uploaded CSV file from the request
63
+ file = requests.files['file']
64
+ # Read the CSV file into a Pandas DataFrame
65
+ input_data = pd.read_csv(file)
66
+ # Make predictions for all properties in the DataFrame (get prodict_sales)
67
+ predicted_product_sales = model.predict(input_data).tolist()
68
+ # Create a dictionary of predictions with product IDs as keys
69
+ product_ids=input_data['Product_Id'].tolist()
70
+ output_dict=dict(zip(product_ids,predicted_product_sales))
71
+
72
+ # Return the predictions dictionary as a JSON response
73
+ return output_dict
74
+
75
+ # Run the Flask application in debug mode if this script is executed directly
76
+ if __name__ == "__main__":
77
+ product_sales_prediction_api.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ numpy==2.0.2
2
+ pandas==2.2.2
3
+ scikit-learn==1.6.1
4
+ joblib==1.4.2
5
+ xgboost==2.1.4
6
+ requests==2.32.3
7
+ Werkzeug==2.2.2
8
+ flask==2.2.2
9
+ gunicorn==20.1.0
10
+ uvicorn[standard]