UncloudMe commited on
Commit
745aa9d
·
verified ·
1 Parent(s): 6a3182a

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +94 -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 7860 and make it accessible externally
14
+ CMD ["streamlit", "run", "app.py", "--server.port=7860", "--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,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datetime import datetime
2
+ import streamlit as st
3
+ import pandas as pd
4
+ import requests
5
+
6
+ # Set the title of the Streamlit app
7
+ st.title("SuperKart Sales Forecast System")
8
+
9
+ # Section for online prediction
10
+ st.subheader("Online Prediction")
11
+
12
+ # Collect user input for Product and Store features
13
+ Product_Id = st.text_input("Product_Id")
14
+ Product_Weight = st.number_input("Product_Weight", min_value=1.0,max_value=100.0, value=1.0)
15
+ Product_Sugar_Content = st.selectbox("Product_Sugar_Content", ["No Sugar", "Low Sugar", "Regular"])
16
+ Product_Allocated_Area = st.number_input("Product_Allocated_Area", min_value=0.0,max_value=1.0,value=.5)
17
+ Product_Type = st.selectbox("Product_Type", ["Fruits and Vegetables", "Snack Foods", "Frozen Foods","Dairy", "Household", "Baking Goods","Canned", "Health and Hygiene", "Meat","Soft Drinks", "Breads", "Hard Drinks", "Others", "Starchy Foods","Breakfast", "Seafood"])
18
+ Product_MRP = st.number_input("Product_MRP", min_value=0.0)
19
+ #Store_Id = st.selectbox("Store_Id", ["OUT001", "OUT002", "OUT003","OUT004"])
20
+ Store_Establishment_Year = st.number_input("Store_Establishment_Year", min_value=1987,max_value=2025,value=2009)
21
+ Store_Size = st.selectbox("Store_Size", ["Small", "Medium", "High"])
22
+ Store_Location_City_Type = st.selectbox("Store_Location_City_Type", ["Tier 1", "Tier 2", "Tier 3"])
23
+ Store_Type = st.selectbox("Store_Type", ["Supermarket Type1", "Supermarket Type2", "Departmental Store","Food Mart"])
24
+
25
+ # Convert user input into a DataFrame
26
+ input_data = pd.DataFrame([{
27
+ 'Product_Id':Product_Id,
28
+ 'Product_Weight': Product_Weight,
29
+ 'Product_Sugar_Content': Product_Sugar_Content,
30
+ 'Product_Allocated_Area': Product_Allocated_Area,
31
+ 'Product_Type': Product_Type,
32
+ 'Product_MRP': Product_MRP,
33
+ #'Store_Id': Store_Id,
34
+ 'Store_Establishment_Year': Store_Establishment_Year,
35
+ 'Store_Size': Store_Size,
36
+ 'Store_Location_City_Type': Store_Location_City_Type,
37
+ 'Store_Type': Store_Type
38
+ }])
39
+
40
+ # Extract the Product_Code and Store_Age before feeding to the model
41
+ input_data["Product_Code"] = input_data["Product_Id"].str[:2]
42
+ input_data.drop("Product_Id", axis=1, inplace=True)
43
+
44
+ current_year = datetime.now().year
45
+ input_data["Store_Age"] = current_year - input_data["Store_Establishment_Year"]
46
+ input_data.drop("Store_Establishment_Year", axis=1, inplace=True)
47
+
48
+ # Make prediction when the "Predict" button is clicked
49
+ if st.button("Forecast"):
50
+ try:
51
+ response = requests.post(
52
+ "https://UncloudMe-SK_Sales_Forecast.hf.space/salespredict",
53
+ json=input_data.to_dict(orient='records')[0],
54
+ timeout=30 # add timeout for safety
55
+ )
56
+
57
+ if response.status_code == 200:
58
+ prediction = response.json().get('Predicted Sales', None)
59
+ if prediction is not None:
60
+ st.success(f"Predicted Sales: {prediction}")
61
+ else:
62
+ st.error("No prediction found in response.")
63
+ else:
64
+ # show backend error text if available
65
+ st.error(f"Error {response.status_code}: {response.text}")
66
+
67
+ except requests.exceptions.RequestException as e:
68
+ # catch all connection, timeout, DNS, etc. errors
69
+ st.error(f"Connection error: {str(e)}")
70
+
71
+
72
+ #if st.button("Forecast"):
73
+ # response = requests.post("https://UncloudMe-SK_Sales_Forecast.hf.space/salespredict", json=input_data.to_dict(orient='records')[0]) # Send data to Flask API
74
+ # if response.status_code == 200:
75
+ # prediction = response.json()['Predicted Sales']
76
+ # st.success(f"Predicted Sales: {prediction}")
77
+ # else:
78
+ # st.error("Error making prediction.")
79
+
80
+ # Section for batch prediction
81
+ st.subheader("Batch Prediction")
82
+ # Allow users to upload a CSV file for batch prediction
83
+ uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"])
84
+
85
+ # Make batch prediction when the "Predict Batch" button is clicked
86
+ if uploaded_file is not None:
87
+ if st.button("Predict Batch"):
88
+ response = requests.post("https://UncloudMe-SK_Sales_Forecast.hf.space/salespredictbatch", files={"file": uploaded_file}) # Send file to Flask API
89
+ if response.status_code == 200:
90
+ predictions = response.json()
91
+ st.success("Batch predictions completed!")
92
+ st.write(predictions) # Display the predictions
93
+ else:
94
+ st.error("Error making batch prediction.")
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ pandas==2.2.2
2
+ requests==2.28.1
3
+ streamlit==1.43.2