Sandhya-2025 commited on
Commit
ba7bd7f
·
verified ·
1 Parent(s): c758a54

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +9 -9
  2. app.py +62 -71
  3. requirements.txt +5 -0
Dockerfile CHANGED
@@ -1,16 +1,16 @@
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
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:rental_price_predictor_api"]
app.py CHANGED
@@ -1,71 +1,62 @@
1
-
2
- import streamlit as st
3
- import pandas as pd
4
- import requests
5
-
6
- # Set the title of the Streamlit app
7
- st.title("Superkart Product sales revenue Predictor")
8
-
9
- # Section for online prediction
10
- st.subheader("Online Prediction")
11
-
12
- # Collect user input for property features
13
-
14
-
15
- # --- User Inputs ---
16
- # col1, col2 = st.columns(2)
17
- # with col1:
18
- # Product_Weight = st.number_input("Product Weight", min_value=0.0, max_value=100.0, step=0.1)
19
- # Product_Allocated_Area = st.number_input("Allocated Display Area Ratio", min_value=0.0, max_value=1.0, step=0.01)
20
- # Product_MRP = st.number_input("Product MRP", min_value=1.0, max_value=10000.0, step=1.0)
21
-
22
- # with col2:
23
- # Product_Sugar_Content = st.selectbox("Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
24
- # Product_Type = st.selectbox("Product Type", [
25
- # "Meat","Snack Foods","Hard Drinks","Dairy","Canned","Soft Drinks",
26
- # "Health and Hygiene","Baking Goods","Bread","Breakfast","Frozen Foods",
27
- # "Fruits and Vegetables","Household","Seafood","Starchy Foods","Others"
28
- # ])
29
- # Store_Size = st.selectbox("Store Size", ["High", "Medium", "Low"])
30
- # Store_Location_City_Type = st.selectbox("City Type", ["Tier 1", "Tier 2", "Tier 3"])
31
- # Store_Type = st.selectbox("Store Type", [
32
- # "Departmental Store","Supermarket Type 1","Supermarket Type 2","Food Mart"
33
- # ])
34
-
35
- Product_Weight = st.number_input("Product Weight", min_value=0.0, max_value=100.0, step=0.1)
36
- Product_Allocated_Area = st.number_input("Allocated Display Area Ratio", min_value=0.0, max_value=1.0, step=0.01)
37
- Product_MRP = st.number_input("Product MRP", min_value=1.0, max_value=10000.0, step=1.0)
38
- Product_Sugar_Content = st.selectbox("Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
39
- Product_Type = st.selectbox("Product Type", [
40
- "Meat","Snack Foods","Hard Drinks","Dairy","Canned","Soft Drinks",
41
- "Health and Hygiene","Baking Goods","Bread","Breakfast","Frozen Foods",
42
- "Fruits and Vegetables","Household","Seafood","Starchy Foods","Others"
43
- ])
44
- Store_Size = st.selectbox("Store Size", ["High", "Medium", "Low"])
45
- Store_Location_City_Type = st.selectbox("City Type", ["Tier 1", "Tier 2", "Tier 3"])
46
- Store_Type = st.selectbox("Store Type", [
47
- "Departmental Store","Supermarket Type 1","Supermarket Type 2","Food Mart"
48
- ])
49
- Store_Id = st.selectbox("Store Id", ["OUT001","OUT002","OUT003","OUT004"])
50
-
51
- # Convert user input into a DataFrame
52
- input_data = pd.DataFrame([{
53
- 'Product_Weight': Product_Weight,
54
- 'Product_Sugar_Content': Product_Sugar_Content,
55
- 'Product_Allocated_Area': Product_Allocated_Area,
56
- 'Product_Type': Product_Type,
57
- 'Product_MRP': Product_MRP,
58
- 'Store_Size': Store_Size,
59
- 'Store_Location_City_Type': Store_Location_City_Type,
60
- 'Store_Type': Store_Type,
61
- 'Store_Id': Store_Id
62
- }])
63
-
64
- # Make prediction when the "Predict" button is clicked
65
- if st.button("Predict"):
66
- response = requests.post("https://Sandhya-2025-Sandhya-2025/SuperKartRevenuePredictionBackend.hf.space/v1/salesRevenue", json=input_data.to_dict(orient='records')[0]) # Send data to Flask API
67
- if response.status_code == 200:
68
- prediction = response.json()['Predicted Price (in dollars)']
69
- st.success(f"Predicted Rental Price (in dollars): {prediction}")
70
- else:
71
- st.error("Error making prediction.")
 
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
+ # Initialize the Flask application
8
+ superkart_sales_revenue_predictor_api = Flask("Superkart Product sales revenue Predictor")
9
+
10
+ # Load the trained machine learning model
11
+ model = joblib.load("superkart_revenue_prediction_model_v1_0.joblib")
12
+
13
+ # Define a route for the home page (GET request)
14
+ @superkart_sales_revenue_predictor_api.get('/')
15
+ def home():
16
+ """
17
+ This function handles GET requests to the root URL ('/') of the API.
18
+ It returns a simple welcome message.
19
+ """
20
+ return "Welcome to the Superkart sale revenue Prediction API!"
21
+
22
+ # Define an endpoint for single property prediction (POST request)
23
+ @superkart_sales_revenue_predictor_api.post('/v1/salesRevenue')
24
+ def predict_product_sales_revenue():
25
+ """
26
+ This function handles POST requests to the '/v1/salesRevenue' endpoint.
27
+ It expects a JSON payload containing property details and returns
28
+ the predicted rental price as a JSON response.
29
+ """
30
+ # Get the JSON data from the request body
31
+ sales_data = request.get_json()
32
+
33
+ # Extract relevant features from the JSON data
34
+ sample = {
35
+ 'Product_Weight': sales_data['Product_Weight'],
36
+ 'Product_Sugar_Content': sales_data['Product_Sugar_Content'],
37
+ 'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
38
+ 'Product_Type': sales_data['Product_Type'],
39
+ 'Product_MRP': sales_data['Product_MRP'],
40
+ 'Store_Size': sales_data['Store_Size'],
41
+ 'Store_Location_City_Type': sales_data['Store_Location_City_Type'],
42
+ 'Store_Type': sales_data['Store_Type'],
43
+ 'Store_Id': sales_data['Store_Id']
44
+ }
45
+
46
+ # Convert the extracted data into a Pandas DataFrame
47
+ input_data_flask = pd.DataFrame([sample])
48
+
49
+ # Make prediction (get log_price)
50
+ predicted_price = model.predict(input_data_flask)[0]
51
+
52
+ # Convert predicted_price to Python float
53
+ predicted_price = round(float(predicted_price), 2)
54
+ # The conversion above is needed as we convert the model prediction (log price) to actual price using np.exp, which returns predictions as NumPy float32 values.
55
+ # When we send this value directly within a JSON response, Flask's jsonify function encounters a datatype error
56
+
57
+ # Return the actual price
58
+ return jsonify({'Predicted Price (in dollars)': predicted_price})
59
+
60
+ # Run the Flask application in debug mode if this script is executed directly
61
+ if __name__ == '__main__':
62
+ superkart_sales_revenue_predictor_api.run(debug=True)
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -3,4 +3,9 @@ numpy==2.0.2
3
  scikit-learn==1.6.1
4
  xgboost==2.1.4
5
  joblib==1.4.2
 
 
 
 
 
6
  streamlit==1.43.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]
11
  streamlit==1.43.2