Disha252001 commited on
Commit
2dedb84
·
verified ·
1 Parent(s): d0915a6

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +8 -10
  2. app.py +86 -69
  3. requirements.txt +8 -0
Dockerfile CHANGED
@@ -1,16 +1,14 @@
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 deployment_files into the container
7
+ COPY deployment_files/ ./
8
 
9
+ # Install dependencies
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
 
12
+ # Run the Flask app with Gunicorn
13
+ # Here: app.py is in deployment_files, Flask instance = store_sales_api
14
+ CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:store_sales_api"]
 
app.py CHANGED
@@ -1,70 +1,87 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import requests
4
-
5
- # Set the title of the Streamlit app
6
- st.title("SuperKart Store Sales Prediction")
7
-
8
- # Section for online prediction
9
- st.subheader("Online Prediction")
10
-
11
- # Collect user input for store/product features
12
- product_weight = st.number_input("Product Weight", min_value=0.0, value=1.0)
13
- product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, value=10.0)
14
- product_mrp = st.number_input("Product MRP", min_value=0.0, value=50.0)
15
- store_establishment_year = st.number_input("Store Establishment Year", min_value=1900, max_value=2025, value=2015)
16
- product_sugar_content = st.selectbox("Product Sugar Content", ["Low", "Medium", "High"])
17
- product_type = st.selectbox("Product Type", ["Dairy", "Beverages", "Snacks", "Others"])
18
- store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
19
- store_location_city_type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
20
- store_type = st.selectbox("Store Type", ["Type 1", "Type 2", "Type 3", "Type 4"])
21
- store_id = st.text_input("Store Id", "S001")
22
-
23
- # Feature engineering for Store_Age
24
- store_age = 2025 - store_establishment_year
25
-
26
- # Convert user input into a DataFrame
27
- input_data = pd.DataFrame([{
28
- 'Product_Weight': product_weight,
29
- 'Product_Allocated_Area': product_allocated_area,
30
- 'Product_MRP': product_mrp,
31
- 'Store_Age': store_age,
32
- 'Product_Sugar_Content': product_sugar_content,
33
- 'Product_Type': product_type,
34
- 'Store_Size': store_size,
35
- 'Store_Location_City_Type': store_location_city_type,
36
- 'Store_Type': store_type,
37
- 'Store_Id': store_id
38
- }])
39
-
40
- # Make prediction when the "Predict" button is clicked
41
- if st.button("Predict"):
42
- response = requests.post(
43
- "https://Disha252001-SuperKart-Frontend.hf.space/v1/sale",
44
- json=input_data.to_dict(orient='records')[0]
45
- )
46
- if response.status_code == 200:
47
- prediction = response.json()['Predicted Store Sales']
48
- st.success(f"Predicted Store Sales: {prediction}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  else:
50
- st.error("Error making prediction.")
51
-
52
- # Section for batch prediction
53
- st.subheader("Batch Prediction")
54
-
55
- # Allow users to upload a CSV file for batch prediction
56
- uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"])
57
-
58
- # Make batch prediction when the "Predict Batch" button is clicked
59
- if uploaded_file is not None:
60
- if st.button("Predict Batch"):
61
- response = requests.post(
62
- "https://Disha252001-SuperKart-Frontend.hf.space/v1/salebatch",
63
- files={"file": uploaded_file}
64
- )
65
- if response.status_code == 200:
66
- predictions = response.json()
67
- st.success("Batch predictions completed!")
68
- st.write(predictions) # Display the predictions
69
- else:
70
- st.error("Error making batch 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
+ store_sales_api = Flask("SuperKart Store Sales Predictor")
9
+
10
+ # Load the trained machine learning model
11
+ model = joblib.load("store_sales_prediction_model_v1_0.joblib")
12
+
13
+ # Define a route for the home page (GET request)
14
+ @store_sales_api.get('/')
15
+ def home():
16
+ """
17
+ Root endpoint to check if the API is running.
18
+ """
19
+ return "Welcome to the SuperKart Store Sales Prediction API!"
20
+
21
+ # Define an endpoint for single store sales prediction (POST request)
22
+ @store_sales_api.post('/v1/sale')
23
+ def predict_store_sales():
24
+ """
25
+ Handles POST requests for predicting sales of a single store.
26
+ Expects JSON input with store details.
27
+ """
28
+ # Get JSON input
29
+ store_data = request.get_json()
30
+
31
+ # Extract relevant features (update according to your dataset)
32
+ sample = {
33
+ 'Store_Type': store_data['Store_Type'],
34
+ 'Location_Type': store_data['Location_Type'],
35
+ 'Region_Code': store_data['Region_Code'],
36
+ 'Holiday': store_data['Holiday'],
37
+ 'Discount': store_data['Discount']
38
+ # Add any other features you used during training
39
+ }
40
+
41
+ # Convert into DataFrame
42
+ input_data = pd.DataFrame([sample])
43
+
44
+ # Make prediction (direct Store_Sales prediction)
45
+ predicted_sales = model.predict(input_data)[0]
46
+
47
+ # Convert to Python float and round
48
+ predicted_sales = round(float(predicted_sales), 2)
49
+
50
+ # Return JSON response
51
+ return jsonify({'Predicted Store Sales': predicted_sales})
52
+
53
+ # Define an endpoint for batch prediction (POST request)
54
+ @store_sales_api.post('/v1/salebatch')
55
+ def predict_store_sales_batch():
56
+ """
57
+ Handles batch predictions for multiple stores.
58
+ Expects a CSV file with store details.
59
+ """
60
+ # Get uploaded CSV file
61
+ file = request.files['file']
62
+
63
+ # Read CSV into DataFrame
64
+ input_data = pd.read_csv(file)
65
+
66
+ # Make predictions
67
+ predicted_sales = model.predict(input_data).tolist()
68
+
69
+ # Convert to float and round
70
+ predicted_sales = [round(float(sale), 2) for sale in predicted_sales]
71
+
72
+ # Use Store IDs if available
73
+ if 'Store_ID' in input_data.columns:
74
+ store_ids = input_data['Store_ID'].tolist()
75
  else:
76
+ store_ids = list(range(1, len(predicted_sales)+1)) # Fallback index
77
+
78
+ # Create dictionary of predictions
79
+ output_dict = dict(zip(store_ids, predicted_sales))
80
+
81
+ # Return JSON
82
+ return jsonify(output_dict)
83
+
84
+ # Run Flask app
85
+ if __name__ == '__main__':
86
+ store_sales_api.run(debug=True)
87
+
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,3 +1,11 @@
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]
11
  streamlit==1.43.2