ShanRaja commited on
Commit
aa9fe93
·
verified ·
1 Parent(s): f78d0a6

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +11 -9
  2. app.py +67 -52
  3. requirements.txt +6 -11
Dockerfile CHANGED
@@ -1,16 +1,18 @@
 
 
1
  FROM python:3.10-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:t_sales_predictor_api"]
 
1
+
2
+ # Use the official Streamlit image
3
  FROM python:3.10-slim
4
 
5
+ # Set working directory
6
  WORKDIR /app
7
 
8
+ # Copy app files
9
  COPY . .
10
 
11
+ # Install dependencies
12
+ RUN pip install --no-cache-dir -r requirements.txt
13
+
14
+ # Expose Streamlit default port
15
+ EXPOSE 7860
16
 
17
+ # Run the Streamlit app
18
+ CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.enableCORS=false"]
 
 
 
app.py CHANGED
@@ -1,53 +1,68 @@
1
- import numpy as np
2
- import joblib
3
  import pandas as pd
4
- from flask import Flask, request, jsonify
5
-
6
- # Initialize the Flask application
7
- t_sales_predictor_api = Flask("SuperKart Sales Predictor")
8
-
9
- # Load the trained machine learning model
10
- model = joblib.load("t_superkart_sales_prediction_model_v1_0.joblib")
11
-
12
- # Define a route for the home page (GET request)
13
- @t_sales_predictor_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 the SuperKart Sales Prediction API!"
20
-
21
- # Define an endpoint for single property prediction (POST request)
22
- @t_sales_predictor_api.post('/v1/sales')
23
- def predict_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 rental price as a JSON response.
28
- """
29
- # Get the JSON data from the request body
30
- data = request.get_json()
31
- # Extract relevant features from the JSON data
32
- sample = {
33
- 'Product_Id': data['product_id'],
34
- 'Product_Weight': data['product_weight'],
35
- 'Product_Sugar_Content': data['product_sugar_content'],
36
- 'Product_Allocated_Area': data['product_allocated_area'],
37
- 'Product_Type': data['product_type'],
38
- 'Product_MRP': data['product_mrp'],
39
- 'Store_Id': data['store_id'],
40
- 'Store_Establishment_Year': data['store_establishment_year'],
41
- 'Store_Size': data['store_size'],
42
- 'Store_Location_City_Type': data['store_location_city_type'],
43
- 'Store_Type': data['store_type'],
44
- }
45
-
46
- # Convert the extracted data into a Pandas DataFrame
47
- input_data = pd.DataFrame([sample])
48
-
49
- # Make prediction
50
- pred = model.predict(input_data)[0]
51
-
52
- # Return the actual price
53
- return jsonify({'Predicted Sales': round(float(pred), 2)})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
 
2
  import pandas as pd
3
+ import joblib
4
+
5
+ # Load trained model
6
+ @st.cache_resource
7
+ def load_model():
8
+ return joblib.load("t_superkart_sales_prediction_model_v1_0.joblib")
9
+
10
+ model = load_model()
11
+
12
+ st.set_page_config(page_title="SuperKart Sales Predictor", layout="wide")
13
+ st.title("🛒 SuperKart Sales Prediction App")
14
+
15
+ st.write("Fill in the product and store details below to predict sales.")
16
+
17
+ # Input fields
18
+ col1, col2 = st.columns(2)
19
+
20
+ with col1:
21
+ product_weight = st.number_input("Product Weight", min_value=0.0, step=0.01)
22
+ product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, step=0.001)
23
+ product_mrp = st.number_input("Product MRP", min_value=0.0, step=0.01)
24
+ product_store_sales_total = st.number_input("Product Store Sales Total", min_value=0.0, step=0.01)
25
+ store_age = st.number_input("Store Age (Years)", min_value=0, step=1)
26
+
27
+ with col2:
28
+ product_sugar_content = st.selectbox("Sugar Content", ["no sugar", "regular"])
29
+ product_type = st.selectbox(
30
+ "Product Type",
31
+ ["breads", "breakfast", "canned", "dairy", "frozen foods",
32
+ "fruits and vegetables", "hard drinks", "health and hygiene",
33
+ "household", "meat", "others", "seafood", "snack foods",
34
+ "soft drinks", "starchy foods"]
35
+ )
36
+ store_size = st.selectbox("Store Size", ["small", "medium"])
37
+ store_location_city_type = st.selectbox("Store Location City Type", ["tier 2", "tier 3"])
38
+ store_type = st.selectbox("Store Type", ["food mart", "supermarket type1", "supermarket type2"])
39
+ product_group_code = st.selectbox("Product Group Code", ["fd", "nc"])
40
+
41
+ # Convert inputs to DataFrame
42
+ input_data = {
43
+ "Product_Weight": product_weight,
44
+ "Product_Allocated_Area": product_allocated_area,
45
+ "Product_MRP": product_mrp,
46
+ "Product_Store_Sales_Total": product_store_sales_total,
47
+ "Store_Age": store_age,
48
+ f"Product_Sugar_Content_{product_sugar_content}": True,
49
+ f"Product_Type_{product_type}": True,
50
+ f"Store_Size_{store_size}": True,
51
+ f"Store_Location_City_Type_{store_location_city_type}": True,
52
+ f"Store_Type_{store_type}": True,
53
+ f"Product_Group_Code_{product_group_code}": True
54
+ }
55
+
56
+ input_df = pd.DataFrame([input_data]).astype(object)
57
+
58
+ # Align with model features
59
+ if hasattr(model, "feature_names_in_"):
60
+ input_df = input_df.reindex(columns=model.feature_names_in_, fill_value=0)
61
+
62
+ # Prediction
63
+ if st.button("Predict"):
64
+ try:
65
+ prediction = model.predict(input_df)[0]
66
+ st.success(f"Predicted Product Sales: {prediction:.2f}")
67
+ except Exception as e:
68
+ st.error(f"Prediction failed: {e}")
requirements.txt CHANGED
@@ -1,11 +1,6 @@
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]
11
- streamlit==1.43.2
 
1
+ streamlit
2
+ requests
3
+ pandas
4
+ scikit-learn
5
+ joblib
6
+