simnid commited on
Commit
70d1f02
·
verified ·
1 Parent(s): 51f3cab

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +99 -0
  3. requirements.txt +3 -0
Dockerfile ADDED
@@ -0,0 +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
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")
7
+
8
+ # Section for single prediction
9
+ st.subheader("Single Product-Store Prediction")
10
+
11
+ # Collect user input for product and store features
12
+ product_weight = st.number_input("Product Weight", min_value=0.1, max_value=20.0, value=12.65, step=0.1)
13
+ product_allocated_area = st.number_input("Product Allocated Area Ratio", min_value=0.0, max_value=1.0, value=0.07, step=0.01)
14
+ product_mrp = st.number_input("Product MRP", min_value=10, max_value=200, value=147, step=1)
15
+ product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
16
+ product_type = st.selectbox("Product Type", [
17
+ "Fruits and Vegetables", "Household", "Snack Foods", "Meat",
18
+ "Hard Drinks", "Dairy", "Canned", "Soft Drinks",
19
+ "Health and Hygiene", "Baking Goods", "Bread", "Breakfast",
20
+ "Frozen Foods", "Seafood", "Starchy Foods", "Others"
21
+ ])
22
+ store_size = st.selectbox("Store Size", ["High", "Medium", "Low"])
23
+ store_location_city_type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
24
+ store_type = st.selectbox("Store Type", ["Departmental Store", "Supermarket Type 1", "Supermarket Type 2", "Food Mart"])
25
+ store_age = st.number_input("Store Age (years)", min_value=1, max_value=50, value=10, step=1)
26
+ product_category_code = st.number_input("Product Category Code", min_value=1, max_value=100, value=10, step=1)
27
+
28
+ # Convert user input into the format expected by the API
29
+ input_data = {
30
+ 'Product_Weight': float(product_weight),
31
+ 'Product_Allocated_Area': float(product_allocated_area),
32
+ 'Product_MRP': int(product_mrp),
33
+ 'Product_Sugar_Content': product_sugar_content,
34
+ 'Product_Type': product_type,
35
+ 'Store_Size': store_size,
36
+ 'Store_Location_City_Type': store_location_city_type,
37
+ 'Store_Type': store_type,
38
+ 'Store_Age': int(store_age),
39
+ 'Product_Category_Code': int(product_category_code)
40
+ }
41
+
42
+ # Make prediction when the "Predict" button is clicked
43
+ if st.button("Predict Sales"):
44
+ try:
45
+ # Replace with your actual backend URL
46
+ backend_url = "https://simnid-superkartsalesbackend.hf.space/v1/predict"
47
+ response = requests.post(backend_url, json=input_data)
48
+
49
+ if response.status_code == 200:
50
+ prediction = response.json()['Predicted Sales Total']
51
+ st.success(f"Predicted Sales Total: ${prediction:,.2f}")
52
+ else:
53
+ st.error(f"Error making prediction. Status code: {response.status_code}")
54
+ st.write(response.text)
55
+ except Exception as e:
56
+ st.error(f"Error connecting to API: {e}")
57
+
58
+ # Section for batch prediction
59
+ st.subheader("Batch Prediction")
60
+
61
+ # Allow users to upload a CSV file for batch prediction
62
+ uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"])
63
+
64
+ # Make batch prediction when the "Predict Batch" button is clicked
65
+ if uploaded_file is not None:
66
+ if st.button("Predict Batch"):
67
+ try:
68
+ # Replace with your actual backend batch URL
69
+ batch_backend_url = "https://simnid-superkartsalesbackend.hf.space/v1/predictbatch"
70
+ response = requests.post(batch_backend_url, files={"file": uploaded_file})
71
+
72
+ if response.status_code == 200:
73
+ predictions = response.json()
74
+ st.success("Batch predictions completed!")
75
+
76
+ # Display predictions in a nice format
77
+ predictions_df = pd.DataFrame.from_dict(predictions, orient='index', columns=['Predicted Sales'])
78
+ st.dataframe(predictions_df)
79
+
80
+ # Add download button
81
+ csv = predictions_df.to_csv(index=True)
82
+ st.download_button(
83
+ label="Download Predictions as CSV",
84
+ data=csv,
85
+ file_name="superkart_predictions.csv",
86
+ mime="text/csv"
87
+ )
88
+ else:
89
+ st.error(f"Error making batch prediction. Status code: {response.status_code}")
90
+ except Exception as e:
91
+ st.error(f"Error connecting to API: {e}")
92
+
93
+ # Add some information about the app
94
+ st.sidebar.header("About")
95
+ st.sidebar.info("""
96
+ This app predicts sales totals for SuperKart products using a machine learning model.
97
+ - **Single Prediction**: Enter details for one product-store combination
98
+ - **Batch Prediction**: Upload a CSV file with multiple records
99
+ """)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ pandas==2.2.2
2
+ requests==2.28.1
3
+ streamlit==1.43.2