Hunagypsy commited on
Commit
079759f
·
verified ·
1 Parent(s): 0f81579

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +18 -10
  2. app.py +54 -38
  3. requirements.txt +11 -1
Dockerfile CHANGED
@@ -1,16 +1,24 @@
1
- # Use a minimal base image with Python 3.9 installed
2
  FROM python:3.9-slim
3
 
4
- # Set the working directory inside the container to /app
5
- WORKDIR /app
6
 
7
- # Copy all files from the current directory on the host to the container's /app directory
8
- COPY . .
9
 
10
- # Install Python dependencies listed in requirements.txt
11
- RUN pip3 install -r requirements.txt
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- # Define the command to run the Streamlit app on port 8501 and make it accessible externally
14
- CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false"]
15
 
16
- # NOTE: Disable XSRF protection for easier external access in order to make batch predictions
 
 
1
  FROM python:3.9-slim
2
 
3
+ # Set the working directory inside the container
4
+ WORKDIR /app #Complete the code to mention the command in Docker to set the working directory
5
 
6
+ # Copy all files from the current directory to the container's working directory
7
+ COPY . . #Complete the code to mention the command in Docker to copy the files from the current directory to the container's working directory
8
 
9
+ # Install dependencies from the requirements file without using cache to reduce image size
10
+ # removed and replaced with below RUN pip install --no-cache-dir --upgrade -r requirements.txt #Complete the code to mention the command in Docker to install dependencies
11
+ RUN pip install --no-cache-dir --upgrade -r backend_files/requirements.txt
12
+
13
+
14
+ # new scripts
15
+ EXPOSE 7860
16
+
17
+ # Define the command to start the application using Gunicorn with 4 worker processes
18
+ # - `-w 4`: Uses 4 worker processes for handling requests
19
+ # - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
20
+ # - `app:app`: Runs the Flask app (assuming `app.py` contains the Flask instance named `app`)
21
+ # removed the following detail CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:superkart_api"]
22
+ CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "backend_files.app:superkart_api"]
23
 
 
 
24
 
 
app.py CHANGED
@@ -1,39 +1,55 @@
1
 
2
- import streamlit as st
3
- import requests
4
-
5
- st.title("Product Store Sales Prediction App") #Complete the code to define the title of the app.
6
-
7
- # Input fields for product and store data
8
- Product_Weight = st.number_input("Product Weight", min_value=0.0, value=12.66)
9
- Product_Sugar_Content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
10
- Product_Allocated_Area = st.selectbox("Product Allocated Area", ["Small", "Medium", "Large"]) #Complete the code to define the UI element for Product_Allocated_Area
11
- Product_MRP = st.number_input("Product MRP", min_value=0.0, value=100.0) #Complete the code to define the UI element for Product_MRP
12
- Store_Size = st.selectbox("Store Size", ["Small", "Medium", "High"]) #Complete the code to define the UI element for Store_Size
13
- Store_Location_City_Type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"]) #Complete the code to define the UI element for Store_Location_City_Type
14
- Store_Type = st.selectbox("Store Type", ["Type 1", "Type 2", "Type 3", "Type 4"]) #Complete the code to define the UI element for Store_Type
15
- Product_Id_char = st.text_input("Product ID (char)", value="FDX") #Complete the code to define the UI element for Product_Id_char
16
- Store_Age_Years = st.number_input("Store Age (Years)", min_value=0, value=5) #Complete the code to define the UI element for Store_Age_Years
17
- Product_Type_Category = st.selectbox("Product Type Category", ["Food", "Non-Food", "Drinks"]) #Complete the code to define the UI element for Product_Type_Category
18
-
19
- product_data = {
20
- "Product_Weight": Product_Weight,
21
- "Product_Sugar_Content": Product_Sugar_Content,
22
- "Product_Allocated_Area": Product_Allocated_Area,
23
- "Product_MRP": Product_MRP,
24
- "Store_Size": Store_Size,
25
- "Store_Location_City_Type": Store_Location_City_Type,
26
- "Store_Type": Store_Type,
27
- "Product_Id_char": Product_Id_char,
28
- "Store_Age_Years": Store_Age_Years,
29
- "Product_Type_Category": Product_Type_Category
30
- }
31
-
32
- if st.button("Predict", type='primary'):
33
- response = requests.post("https://Hunagypsy/my-docker-space.hf.space/v1/predict", json=product_data) # Complete the code to enter user name and space name to correctly define the endpoint
34
- if response.status_code == 200:
35
- result = response.json()
36
- predicted_sales = result["Sales"]
37
- st.write(f"Predicted Product Store Sales Total: ₹{predicted_sales:.2f}")
38
- else:
39
- st.error("Error in API request")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ superkart_api = Flask("superkart_api") #Complete the code to define the name of the app
10
+
11
+ # Load the trained churn prediction model
12
+ model = joblib.load("backend_files/tuned_gradient_boosting_regressor_model.joblib") #Complete the code to define the location of the serialized model
13
+
14
+ # Define a route for the home page
15
+ @superkart_api.get('/')
16
+ def home():
17
+ return "Welcome to the Superkart Sales Prediction API!" #Complete the code to define a welcome message
18
+
19
+ # Define an endpoint to predict churn for a single customer
20
+ @superkart_api.post('/v1/predict')
21
+ def predict_sales():
22
+ # Get JSON data from the request
23
+ data = request.get_json()
24
+
25
+ # Extract relevant customer features from the input data. The order of the column names matters.
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_MRP': data['Product_MRP'],
31
+ 'Store_Size': data['Store_Size'],
32
+ 'Store_Location_City_Type': data['Store_Location_City_Type'],
33
+ 'Store_Type': data['Store_Type'],
34
+ 'Product_Id_char': data['Product_Id_char'],
35
+ 'Store_Age_Years': data['Store_Age_Years'],
36
+ 'Product_Type_Category': data['Product_Type_Category']
37
+ }
38
+
39
+ # Convert the extracted data into a DataFrame
40
+ input_data = pd.DataFrame([sample])
41
+
42
+ # Make a churn prediction using the trained model
43
+ prediction = model.predict(input_data).tolist()[0]
44
+
45
+ # Return the prediction as a JSON response
46
+ return jsonify({'Sales': prediction})
47
+
48
+
49
+ # Run the Flask app in debug mode
50
+ # changed following if __name__ == '__main__':
51
+ # changed following superkart_api.run(debug=True)
52
+ if __name__ == '__main__':
53
+ import os
54
+ port = int(os.environ.get("PORT", 7860))
55
+ superkart_api.run(host="0.0.0.0", port=port)
requirements.txt CHANGED
@@ -1,2 +1,12 @@
 
 
 
 
 
 
 
 
 
1
  requests==2.32.3
2
- streamlit==1.45.0
 
 
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
+ Werkzeug==2.2.2
8
+ flask==2.2.2
9
+ gunicorn==20.1.0
10
  requests==2.32.3
11
+ uvicorn[standard]
12
+ streamlit==1.45