vamsikrishna1516 commited on
Commit
d1b73db
·
verified ·
1 Parent(s): 88b2116

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +10 -10
  2. app.py +89 -67
  3. requirements.txt +9 -1
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:superkart_sales_prediction_api"]
app.py CHANGED
@@ -1,69 +1,91 @@
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 Sales Prediction App")
7
-
8
- st.write("Predict sales revenue based on product and customer details.")
9
-
10
- # Section: Online prediction
11
- st.subheader("📦 Online Prediction")
12
-
13
- # Sample input fields (must match features used in your trained model)
14
- product_category = st.selectbox("Product Category", ["Electronics", "Clothing", "Home", "Sports", "Others"])
15
- product_price = st.number_input("Product Price ($)", min_value=0.0, step=10.0, value=100.0)
16
- discount_applied = st.selectbox("Discount Applied", ["Yes", "No"])
17
- customer_age = st.slider("Customer Age", 18, 70, 30)
18
- customer_gender = st.selectbox("Customer Gender", ["Male", "Female"])
19
- customer_income = st.number_input("Customer Income ($)", min_value=0.0, step=1000.0, value=50000.0)
20
- store_region = st.selectbox("Store Region", ["North", "South", "East", "West"])
21
- units_sold = st.number_input("Units Sold", min_value=1, value=1)
22
-
23
- # Prepare the input as a dictionary
24
- input_data = pd.DataFrame([{
25
- "product_category": product_category,
26
- "product_price": product_price,
27
- "discount_applied": discount_applied,
28
- "customer_age": customer_age,
29
- "customer_gender": customer_gender,
30
- "customer_income": customer_income,
31
- "store_region": store_region,
32
- "units_sold": units_sold
33
- }])
34
-
35
- # API call to Hugging Face backend
36
- if st.button("Predict Sales"):
37
  try:
38
- response = requests.post(
39
- "https://vamsikrishna1516-SuperKartSalesPredictionBackend.hf.space/v1/sales",
40
- json=input_data.to_dict(orient='records')[0]
41
- )
42
- if response.status_code == 200:
43
- predicted = response.json()['Predicted Sales']
44
- st.success(f"💰 Predicted Sales: ${predicted}")
45
- else:
46
- st.error("❌ Error from backend. Please try again.")
 
 
 
 
 
 
 
 
 
47
  except Exception as e:
48
- st.error(f"⚠️ Request failed: {e}")
49
-
50
- # Section: Batch prediction
51
- st.subheader("📄 Batch Prediction (CSV Upload)")
52
-
53
- uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"])
54
-
55
- if uploaded_file is not None:
56
- if st.button("Predict Batch Sales"):
57
- try:
58
- response = requests.post(
59
- "https://vamsikrishna1516-SuperKartSalesPredictionBackend.hf.space/v1/salesbatch",
60
- files={"file": uploaded_file}
61
- )
62
- if response.status_code == 200:
63
- results = response.json()
64
- st.success(" Batch predictions received!")
65
- st.write(pd.DataFrame(results.items(), columns=["ID", "Predicted Sales ($)"]))
66
- else:
67
- st.error("❌ Failed to get predictions from backend.")
68
- except Exception as e:
69
- st.error(f"⚠️ Request failed: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 the Flask application
9
+ superkart_sales_prediction_api = Flask("SuperKart Sales Predictor")
10
+
11
+ # Load the trained machine learning model
12
+ model = joblib.load("superkart_sales_prediction_model_v1_0.joblib")
13
+
14
+ # Define the required feature columns
15
+ REQUIRED_FEATURES = [
16
+ 'Product_Weight',
17
+ 'Product_Sugar_Content',
18
+ 'Product_Allocated_Area',
19
+ 'Product_MRP',
20
+ 'Store_Size',
21
+ 'Store_Location_City_Type',
22
+ 'Store_Type',
23
+ 'Product_Id_Group',
24
+ 'Store_Age',
25
+ 'Product_Type_Category',
26
+ 'Store_Id'
27
+ ]
28
+
29
+ # Define a route for the home page (GET request)
30
+ @superkart_sales_prediction_api.get('/')
31
+ def home():
32
+ return "Welcome to the SuperKart Sales Prediction API!"
33
+
34
+ # Define an endpoint for single product prediction (POST request)
35
+ @superkart_sales_prediction_api.post('/v1/sales')
36
+ def predict_sales():
37
  try:
38
+ data = request.get_json()
39
+
40
+ # Validate input fields
41
+ missing_fields = [field for field in REQUIRED_FEATURES if field not in data]
42
+ if missing_fields:
43
+ return jsonify({
44
+ "error": "Missing required fields",
45
+ "missing_fields": missing_fields
46
+ }), 400
47
+
48
+ # Prepare input data
49
+ input_data = pd.DataFrame([{key: data[key] for key in REQUIRED_FEATURES}])
50
+
51
+ # Make prediction
52
+ prediction = model.predict(input_data).tolist()[0]
53
+
54
+ return jsonify({'Sales': prediction})
55
+
56
  except Exception as e:
57
+ return jsonify({"error": "Internal server error", "message": str(e)}), 500
58
+
59
+ # Define an endpoint for batch prediction (POST request)
60
+ @superkart_sales_prediction_api.post('/v1/salesbatch')
61
+ def predict_sales_batch():
62
+ try:
63
+ file = request.files.get('file')
64
+ if file is None:
65
+ return jsonify({"error": "CSV file not provided"}), 400
66
+
67
+ input_data = pd.read_csv(file)
68
+
69
+ # Check for missing columns
70
+ missing_cols = [col for col in REQUIRED_FEATURES if col not in input_data.columns]
71
+ if missing_cols:
72
+ return jsonify({
73
+ "error": "Missing columns in CSV file",
74
+ "missing_columns": missing_cols
75
+ }), 400
76
+
77
+ # Predict
78
+ predictions = model.predict(input_data[REQUIRED_FEATURES]).tolist()
79
+ predicted_sales = [round(float(pred), 2) for pred in predictions]
80
+
81
+ ids = input_data['id'].tolist() if 'id' in input_data.columns else list(range(1, len(predicted_sales) + 1))
82
+ output_dict = dict(zip(ids, predicted_sales))
83
+
84
+ return jsonify(output_dict)
85
+
86
+ except Exception as e:
87
+ return jsonify({"error": "Internal server error", "message": str(e)}), 500
88
+
89
+ # Run the API
90
+ if __name__ == '__main__':
91
+ superkart_sales_prediction_api.run(debug=True)
requirements.txt CHANGED
@@ -1,3 +1,11 @@
 
1
  numpy==2.0.2
2
- requests==2.32.3
 
 
 
 
 
 
 
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