abdulrahman305 commited on
Commit
5fba6b8
·
verified ·
1 Parent(s): cf6a4c8

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +7 -11
  2. app.py +51 -28
  3. requirements.txt +2 -3
Dockerfile CHANGED
@@ -4,17 +4,13 @@ FROM python:3.9-slim
4
  # Set the working directory inside the container to /app
5
  WORKDIR /app
6
 
7
- # Copy the requirements file first to leverage Docker cache
8
- COPY requirements.txt .
9
-
10
- # Install Python dependencies
11
- RUN pip install --no-cache-dir -r requirements.txt
12
-
13
- # Copy the rest of the application code
14
  COPY . .
15
 
16
- # Expose the port the Flask app runs on
17
- EXPOSE 5000
 
 
 
18
 
19
- # Define the command to run the Flask app
20
- CMD ["python", "app.py"]
 
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
 
app.py CHANGED
@@ -1,33 +1,56 @@
1
- import joblib
2
- from flask import Flask, request, jsonify
3
  import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- app = Flask(__name__)
6
-
7
- # Load the serialized model
8
- model = joblib.load('best_regression_model.pkl')
9
-
10
- @app.route('/')
11
- def home():
12
- return "SuperKart Sales Forecasting Backend is running!"
13
-
14
- @app.route('/predict', methods=['POST'])
15
- def predict():
16
  try:
17
- # Get data from the request (assuming JSON format)
18
- data = request.get_json(force=True)
19
- df_pred = pd.DataFrame(data)
20
-
21
- # Make predictions
22
- predictions = model.predict(df_pred)
23
-
24
- # Return predictions as JSON
25
- return jsonify(predictions.tolist())
26
 
 
 
 
 
 
27
  except Exception as e:
28
- return jsonify({'error': str(e)})
29
-
30
- if __name__ == '__main__':
31
- # Run the Flask app
32
- # Using 0.0.0.0 to make it accessible externally within the Docker container
33
- app.run(host='0.0.0.0', port=5000)
 
1
+ import streamlit as st
2
+ import requests
3
  import pandas as pd
4
+ import json
5
+
6
+ st.title("SuperKart Sales Forecasting")
7
+
8
+ st.write("Enter the product and store details to get a sales forecast.")
9
+
10
+ # Input fields for features (based on the columns in your X_train)
11
+ product_weight = st.number_input("Product Weight", min_value=0.1)
12
+ product_sugar_content = st.selectbox("Product Sugar Content", ['Low Sugar', 'Regular', 'No Sugar'])
13
+ product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0)
14
+ product_type = st.selectbox("Product Type", ['Frozen Foods', 'Dairy', 'Canned', 'Baking Goods', 'Health and Hygiene', 'Household', 'Snack Foods', 'Fruits and Vegetables', 'Meat', 'Soft Drinks', 'Breads', 'Hard Drinks', 'Others', 'Starchy Foods', 'Breakfast', 'Seafood'])
15
+ product_mrp = st.number_input("Product MRP", min_value=0.1)
16
+ store_id = st.selectbox("Store ID", ['OUT004', 'OUT003', 'OUT001', 'OUT002'])
17
+ store_size = st.selectbox("Store Size", ['Medium', 'High', 'Small'])
18
+ store_location_city_type = st.selectbox("Store Location City Type", ['Tier 2', 'Tier 1', 'Tier 3'])
19
+ store_type = st.selectbox("Store Type", ['Supermarket Type2', 'Departmental Store', 'Supermarket Type1', 'Food Mart'])
20
+ store_age = st.number_input("Store Age (Years)", min_value=0)
21
+
22
+ # Create a dictionary with the input data
23
+ input_data = {
24
+ 'Product_Weight': product_weight,
25
+ 'Product_Sugar_Content': product_sugar_content,
26
+ 'Product_Allocated_Area': product_allocated_area,
27
+ 'Product_Type': product_type,
28
+ 'Product_MRP': product_mrp,
29
+ 'Store_Id': store_id,
30
+ 'Store_Size': store_size,
31
+ 'Store_Location_City_Type': store_location_city_type,
32
+ 'Store_Type': store_type,
33
+ 'Store_Age': store_age
34
+ }
35
+
36
+ # Create a DataFrame from the input data
37
+ input_df = pd.DataFrame([input_data])
38
+
39
+ # Button to trigger prediction
40
+ if st.button("Predict Sales"):
41
+ # Convert DataFrame to JSON
42
+ json_data = json.dumps(input_df.to_dict(orient='records'))
43
+
44
+ # Replace with your backend Space URL
45
+ backend_url = "abdulrahman305/SuperKart"
46
 
 
 
 
 
 
 
 
 
 
 
 
47
  try:
48
+ response = requests.post(backend_url, json=json.loads(json_data))
 
 
 
 
 
 
 
 
49
 
50
+ if response.status_code == 200:
51
+ predictions = response.json()
52
+ st.success(f"Predicted Sales: {predictions[0]:.2f}")
53
+ else:
54
+ st.error(f"Error predicting sales: {response.text}")
55
  except Exception as e:
56
+ st.error(f"An error occurred: {e}")
 
 
 
 
 
requirements.txt CHANGED
@@ -1,4 +1,3 @@
1
- flask==3.0.3
 
2
  pandas==2.2.2
3
- scikit-learn==1.6.1
4
- joblib==1.4.2
 
1
+ streamlit==1.36.0
2
+ requests==2.32.3
3
  pandas==2.2.2