TokenTutor commited on
Commit
5c8db15
·
verified ·
1 Parent(s): f70990c

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +81 -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 --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:superkart_product_sales_prediction_api"]
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
+ import pandas as pd
3
+ from flask import Flask, request, jsonify
4
+
5
+ # Initialize Flask app with a name
6
+ superkart_product_sales_prediction_api = Flask("SuperKart Sales Forecast API")
7
+
8
+ # Load the trained churn prediction model
9
+ model = joblib.load("superkart_product_sales_prediction_model_v1_0.joblib")
10
+
11
+ # Define a route for the home page
12
+ @superkart_product_sales_prediction_api.get('/')
13
+ def home():
14
+ return "Welcome to the SuperKart Sales Forecast API"
15
+
16
+ # Define an endpoint to predict churn for a single customer
17
+ @superkart_product_sales_prediction_api.post('/v1/forecast')
18
+ def predict_sales_forecast():
19
+ """
20
+ This function handles POST requests to the '/v1/forecast' endpoint.
21
+ It expects a JSON payload containing property details and returns
22
+ the predicted rental price as a JSON response.
23
+ """
24
+
25
+ # Get JSON data from the request
26
+ product_data = request.get_json()
27
+
28
+ # Extract relevant customer features from the input data
29
+ sample = {
30
+ 'Product_Weight': product_data.get('Product_Weight'),
31
+ 'Product_Sugar_Content': product_data.get('Product_Sugar_Content'),
32
+ 'Product_Allocated_Area': product_data.get('Product_Allocated_Area'),
33
+ 'Product_Type': product_data.get('Product_Type'),
34
+ 'Product_MRP': product_data.get('Product_MRP'),
35
+ 'Store_Id': product_data.get('Store_Id'),
36
+ 'Store_Establishment_Year': product_data.get('Store_Establishment_Year'),
37
+ 'Store_Size': product_data.get('Store_Size'),
38
+ 'Store_Location_City_Type': product_data.get('Store_Location_City_Type'),
39
+ 'Store_Type': product_data.get('Store_Type')
40
+ }
41
+
42
+ # Convert the extracted data into a DataFrame
43
+ input_data = pd.DataFrame([sample])
44
+
45
+ # Make a churn prediction using the trained model
46
+ predicted_sales = model.predict(input_data).tolist()[0]
47
+
48
+ # Convert predicted_price to Python float
49
+ predicted_sales = round(float(predicted_sales), 2)
50
+
51
+
52
+ # Return the prediction as a JSON response
53
+ return jsonify({'Prediction': predicted_sales})
54
+
55
+ @superkart_product_sales_prediction_api.post('/v1/forecastbatch')
56
+ def predict_sales_forecast_batch():
57
+ """
58
+ This function handles POST requests to the '/v1/forecastbatch' endpoint.
59
+ It expects a CSV file containing product and store details for multiple products
60
+ and returns the predicted sales forecast prices as a dictionary in the JSON response.
61
+ """
62
+ # Get the uploaded CSV file from the request
63
+ file = request.files['file']
64
+
65
+ # Read the CSV file into a Pandas DataFrame
66
+ input_data = pd.read_csv(file)
67
+
68
+ # Make predictions for all properties in the DataFrame (get log_prices)
69
+ predicted_sales = model.predict(input_data).tolist()
70
+
71
+
72
+ # Create a dictionary of predictions with property IDs as keys
73
+ product_ids = input_data['Product_Id'].tolist() # Assuming 'id' is the property ID column
74
+ output_dict = dict(zip(product_ids, predicted_sales)) # Use actual prices
75
+
76
+ # Return the predictions dictionary as a JSON response
77
+ return output_dict
78
+
79
+ # Run the Flask app in debug mode
80
+ if __name__ == '__main__':
81
+ app.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
+ joblib==1.4.2
5
+ Werkzeug==2.2.2
6
+ flask==2.2.2
7
+ gunicorn==20.1.0
8
+ requests==2.28.1
9
+ uvicorn[standard]
10
+ streamlit==1.43.2