yokesh1999 commited on
Commit
2f5c4ac
·
verified ·
1 Parent(s): 4deddf3

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. Dockerfile +9 -7
  2. app.py +76 -99
  3. requirements.txt +8 -1
  4. superkart_sales_model_v1_0.joblib +3 -0
Dockerfile CHANGED
@@ -1,14 +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"]
 
 
 
 
 
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:house_price_api"]
app.py CHANGED
@@ -1,100 +1,77 @@
1
- import streamlit as st
2
  import pandas as pd
3
- import requests
4
-
5
- # Streamlit UI for SuperKart Sales Revenue Forecasting
6
- st.title("SuperKart Sales Revenue Forecasting App")
7
- st.write("This app predicts the total sales revenue for product-store combinations based on product and store features.")
8
- st.write("Adjust the values below to get a sales prediction.")
9
-
10
- # Collect user input using sliders and selectboxes
11
- col1, col2 = st.columns(2)
12
-
13
- with col1:
14
- st.subheader("Product Features")
15
- Product_Weight = st.slider("Product Weight", 5.0, 25.0, 12.5, 0.1)
16
- Product_Sugar_Content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
17
- Product_Allocated_Area = st.slider("Product Allocated Area (ratio)", 0.0, 0.5, 0.05, 0.01)
18
- Product_Type = st.selectbox("Product Type", [
19
- "Meat", "Snack Foods", "Hard Drinks", "Dairy", "Canned", "Soft Drinks",
20
- "Health and Hygiene", "Baking Goods", "Bread", "Breakfast", "Frozen Foods",
21
- "Fruits and Vegetables", "Household", "Seafood", "Starchy Foods", "Others"
22
- ])
23
- Product_MRP = st.slider("Product MRP (Maximum Retail Price)", 50.0, 300.0, 150.0, 1.0)
24
-
25
- with col2:
26
- st.subheader("Store Features")
27
- Store_Establishment_Year = st.slider("Store Establishment Year", 1985, 2015, 2000, 1)
28
- Store_Size = st.selectbox("Store Size", ["Small", "Medium", "High"])
29
- Store_Location_City_Type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
30
- Store_Type = st.selectbox("Store Type", [
31
- "Departmental Store", "Supermarket Type1", "Supermarket Type2", "Food Mart"
32
- ])
33
-
34
- # Create input data dictionary
35
- input_data = {
36
- 'Product_Weight': Product_Weight,
37
- 'Product_Sugar_Content': Product_Sugar_Content,
38
- 'Product_Allocated_Area': Product_Allocated_Area,
39
- 'Product_Type': Product_Type,
40
- 'Product_MRP': Product_MRP,
41
- 'Store_Establishment_Year': Store_Establishment_Year,
42
- 'Store_Size': Store_Size,
43
- 'Store_Location_City_Type': Store_Location_City_Type,
44
- 'Store_Type': Store_Type
45
- }
46
-
47
- if st.button("Predict Sales Revenue", type='primary'):
48
- # Replace with your Hugging Face backend space URL
49
- api_url = "https://<----user-name---->-<---repo name--->.hf.space/v1/sales" # Enter user name and space name
50
- try:
51
- response = requests.post(api_url, json=input_data, timeout=30)
52
- if response.status_code == 200:
53
- result = response.json()
54
- predicted_sales = result["Predicted_Sales_Total"]
55
- st.success(f"💰 Predicted Sales Revenue: **${predicted_sales:,.2f}**")
56
- st.info(f"This prediction is for a {Product_Type} product in a {Store_Size} {Store_Type} located in {Store_Location_City_Type}.")
57
- else:
58
- st.error(f"Error in API request: Status Code {response.status_code}")
59
- st.error(f"Response: {response.text}")
60
- except requests.exceptions.RequestException as e:
61
- st.error(f"Error connecting to API: {str(e)}")
62
- st.info("Please ensure your backend API is deployed and the URL is correct.")
63
-
64
- # Batch Prediction
65
- st.subheader("Batch Prediction")
66
- st.write("Upload a CSV file with product and store features to get predictions for multiple combinations.")
67
-
68
- file = st.file_uploader("Upload CSV file", type=["csv"])
69
- if file is not None:
70
- # Display preview of uploaded file
71
- df_preview = pd.read_csv(file)
72
- st.write("Preview of uploaded file:")
73
- st.dataframe(df_preview.head())
74
-
75
- if st.button("Predict for Batch", type='primary'):
76
- # Replace with your Hugging Face backend space URL
77
- api_url = "https://<----user-name---->-<---repo name--->.hf.space/v1/salesbatch" # Enter user name and space name
78
- try:
79
- files = {"file": (file.name, file, "text/csv")}
80
- response = requests.post(api_url, files=files, timeout=60)
81
- if response.status_code == 200:
82
- result = response.json()
83
- result_df = pd.DataFrame(result)
84
- st.header("Batch Prediction Results")
85
- st.dataframe(result_df)
86
-
87
- # Download button
88
- csv = result_df.to_csv(index=False).encode('utf-8')
89
- st.download_button(
90
- label="Download Predictions as CSV",
91
- data=csv,
92
- file_name="predictions.csv",
93
- mime="text/csv"
94
- )
95
- else:
96
- st.error(f"Error in API request: Status Code {response.status_code}")
97
- st.error(f"Response: {response.text}")
98
- except requests.exceptions.RequestException as e:
99
- st.error(f"Error connecting to API: {str(e)}")
100
- st.info("Please ensure your backend API is deployed and the URL is correct.")
 
1
+ import joblib
2
  import pandas as pd
3
+ from flask import Flask, request, jsonify
4
+ import numpy as np
5
+
6
+ # Initialize Flask app
7
+ sales_forecast_api = Flask("SuperKart Sales Forecast Predictor")
8
+
9
+ # Load the trained SuperKart sales model
10
+ model = joblib.load("superkart_sales_model_v1_0.joblib")
11
+
12
+ # Define a route for the home page
13
+ @sales_forecast_api.get('/')
14
+ def home():
15
+ return "Welcome to the SuperKart Sales Revenue Forecasting API!"
16
+
17
+ # Define an endpoint to predict sales for a single product-store combination
18
+ @sales_forecast_api.post('/v1/sales')
19
+ def predict_sales():
20
+ # Get JSON data from the request
21
+ sales_data = request.get_json()
22
+
23
+ # Extract relevant features from the input data
24
+ # Note: Store_Age will be calculated from Store_Establishment_Year
25
+ current_year = 2024
26
+ store_age = current_year - sales_data['Store_Establishment_Year']
27
+
28
+ sample = {
29
+ 'Product_Weight': sales_data['Product_Weight'],
30
+ 'Product_Sugar_Content': sales_data['Product_Sugar_Content'],
31
+ 'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
32
+ 'Product_Type': sales_data['Product_Type'],
33
+ 'Product_MRP': sales_data['Product_MRP'],
34
+ 'Store_Establishment_Year': sales_data['Store_Establishment_Year'],
35
+ 'Store_Size': sales_data['Store_Size'],
36
+ 'Store_Location_City_Type': sales_data['Store_Location_City_Type'],
37
+ 'Store_Type': sales_data['Store_Type'],
38
+ 'Store_Age': store_age
39
+ }
40
+
41
+ # Convert the extracted data into a DataFrame
42
+ input_data = pd.DataFrame([sample])
43
+
44
+ # Make a prediction using the trained model
45
+ prediction = model.predict(input_data).tolist()[0]
46
+
47
+ # Return the prediction as a JSON response
48
+ return jsonify({'Predicted_Sales_Total': prediction})
49
+
50
+ # Define an endpoint to predict sales for a batch of product-store combinations
51
+ @sales_forecast_api.post('/v1/salesbatch')
52
+ def predict_sales_batch():
53
+ # Get the uploaded CSV file from the request
54
+ file = request.files['file']
55
+
56
+ # Read the file into a DataFrame
57
+ input_data = pd.read_csv(file)
58
+
59
+ # Calculate Store_Age if not present
60
+ if 'Store_Age' not in input_data.columns:
61
+ current_year = 2024
62
+ input_data['Store_Age'] = current_year - input_data['Store_Establishment_Year']
63
+
64
+ # Make predictions for the batch data
65
+ predictions = model.predict(input_data).tolist()
66
+
67
+ # Add predictions to the DataFrame
68
+ input_data['Predicted_Sales_Total'] = predictions
69
+
70
+ # Convert results to dictionary
71
+ result = input_data.to_dict(orient="records")
72
+
73
+ return jsonify(result)
74
+
75
+ # Run the Flask app in debug mode
76
+ if __name__ == '__main__':
77
+ sales_forecast_api.run(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,3 +1,10 @@
1
  pandas==2.2.2
 
 
 
 
 
 
 
2
  requests==2.28.1
3
- streamlit==1.43.2
 
1
  pandas==2.2.2
2
+ numpy==2.0.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]
superkart_sales_model_v1_0.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:200c1855aa4114e035256f3c29e366623272deda030dc23af2613f70315a8f4b
3
+ size 23837399