rahulsuren12 commited on
Commit
64c03bc
·
verified ·
1 Parent(s): d5662b4

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +40 -96
app.py CHANGED
@@ -2,49 +2,37 @@ import streamlit as st
2
  import pandas as pd
3
  import requests
4
 
5
- # ---------------------------------------------------------------
6
- # 🎯 App Header
7
- # ---------------------------------------------------------------
8
- st.set_page_config(page_title="SuperKart Sales Predictor", page_icon="🛒")
9
- st.title("🛒 SuperKart Total Sales Prediction App")
10
-
11
  st.markdown("""
12
- Welcome to the **SuperKart Sales Forecasting Tool**!
13
- Predict total sales for a single productstore combination, or upload a CSV for batch forecasting.
14
  """)
15
 
16
- # ---------------------------------------------------------------
17
- # 🧩 API Endpoint
18
- # ---------------------------------------------------------------
19
- BACKEND_URL = "https://rahulsuren12-TotalSalesPredictionBackend.hf.space"
20
-
21
- # ---------------------------------------------------------------
22
- # 🔹 Single Prediction
23
- # ---------------------------------------------------------------
24
- st.subheader("🔹 Online Prediction (Single Product)")
25
-
26
- col1, col2 = st.columns(2)
27
-
28
- with col1:
29
- product_weight = st.number_input("Product Weight (kg)", min_value=0.0, step=0.1)
30
- product_area = st.number_input("Product Allocated Area (sq. m.)", min_value=0.0, step=0.1)
31
- product_mrp = st.number_input("Product MRP (₹)", min_value=0.0, step=0.1)
32
- store_age = st.number_input("Store Age (years)", min_value=0, step=1)
33
-
34
- with col2:
35
- product_sugar = st.selectbox("Product Sugar Content", ["Low", "Regular", "No Sugar"])
36
- product_type = st.selectbox("Product Type", [
37
- "Dairy", "Soft Drinks", "Snacks", "Frozen Foods", "Baking Goods",
38
- "Household", "Others"
39
- ])
40
- store_size = st.selectbox("Store Size", ["Small", "Medium", "Large"])
41
- store_city_type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
42
- store_type = st.selectbox("Store Type", [
43
- "Supermarket Type1", "Supermarket Type2", "Departmental", "Grocery"
44
- ])
45
- store_id = st.selectbox("Store ID", ["ST001", "ST002", "ST003", "ST004"])
46
-
47
- # Prepare payload
48
  input_data = {
49
  "Product_Weight": product_weight,
50
  "Product_Allocated_Area": product_area,
@@ -58,59 +46,15 @@ input_data = {
58
  "Store_Id": store_id
59
  }
60
 
61
- if st.button("🔮 Predict Sales"):
62
- try:
63
- response = requests.post(f"{BACKEND_URL}/v1/sales", json=input_data)
64
- if response.status_code == 200:
65
- result = response.json()
66
- if "Predicted_Sales_Total" in result:
67
- st.success(f" Predicted Total Sales: {result['Predicted_Sales_Total']:,.2f}")
68
- else:
69
- st.error(f"⚠️ Unexpected response format: {result}")
70
- else:
71
- st.error(f"❌ Backend Error {response.status_code}: {response.text}")
72
- except requests.exceptions.RequestException as e:
73
- st.error(f"🚫 Connection failed: {e}")
74
-
75
- # ---------------------------------------------------------------
76
- # 🔹 Batch Prediction
77
- # ---------------------------------------------------------------
78
- st.subheader("📦 Batch Prediction (CSV Upload)")
79
-
80
- st.markdown("Upload a CSV file containing multiple product–store records for batch sales predictions.")
81
-
82
- uploaded_file = st.file_uploader("Choose a CSV file", type=["csv"])
83
-
84
- if uploaded_file is not None:
85
- if st.button("📈 Predict Batch Sales"):
86
- try:
87
- # Create proper file payload
88
- files = {"file": (uploaded_file.name, uploaded_file.getvalue(), "text/csv")}
89
- response = requests.post(f"{BACKEND_URL}/v1/salesbatch", files=files)
90
-
91
- if response.status_code == 200:
92
- result = response.json()
93
- if isinstance(result, dict) and "error" not in result:
94
- st.success("✅ Batch predictions completed successfully!")
95
- results_df = pd.DataFrame(list(result.items()), columns=["Product_Id", "Predicted_Sales_Total"])
96
- st.dataframe(results_df, use_container_width=True)
97
- csv_buffer = io.BytesIO()
98
- results_df.to_csv(csv_buffer, index=False)
99
- st.download_button(
100
- label="⬇️ Download Predictions as CSV",
101
- data=csv_buffer.getvalue(),
102
- file_name="SuperKart_Batch_Predictions.csv",
103
- mime="text/csv"
104
- )
105
- else:
106
- st.error(f"⚠️ Backend returned an error: {result.get('error', 'Unknown issue')}")
107
- else:
108
- # Non-200 HTTP status
109
- try:
110
- err_json = response.json()
111
- st.error(f"❌ Error {response.status_code}: {err_json.get('error', response.text)}")
112
- except Exception:
113
- st.error(f"❌ Error {response.status_code}: {response.text}")
114
-
115
- except requests.exceptions.RequestException as e:
116
- st.error(f"🚫 Network or connection error: {e}")
 
2
  import pandas as pd
3
  import requests
4
 
5
+ # Set the title of the Streamlit app
6
+ st.title("SuperKart Total Sales Prediction App")
 
 
 
 
7
  st.markdown("""
8
+ Welcome to the **SuperKart Sales Forecasting Tool**!
9
+ Predict total sales for a product-store combination or upload a batch of product records for multi-store forecasting.
10
  """)
11
 
12
+ # Section for online prediction
13
+ st.subheader("Online Prediction")
14
+
15
+ # Collect user input for property features
16
+ product_weight = st.number_input("Product Weight (kg)", min_value=0.0, step=0.1)
17
+ product_area = st.number_input("Product Allocated Area (sq. m.)", min_value=0.0, step=0.1)
18
+ product_mrp = st.number_input("Product MRP (₹)", min_value=0.0, step=0.1)
19
+ store_age = st.number_input("Store Age (years)", min_value=0, step=1)
20
+
21
+ product_sugar = st.selectbox("Product Sugar Content", ["Low", "Regular", "No Sugar"])
22
+ product_type = st.selectbox("Product Type", [
23
+ "Frozen Foods", "Dairy", "Canned", "Baking Goods", "Health and Hygiene",
24
+ "Snack Foods", "Meat", "Household", "Hard Drinks", "Fruits and Vegetables",
25
+ "Breads", "Soft Drinks", "Breakfast", "Starchy Foods", "Seafood", "Others"
26
+ ])
27
+ store_size = st.selectbox("Store Size", ["Small", "Medium", "Large"])
28
+ store_city_type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
29
+ store_type = st.selectbox("Store Type", [
30
+ "Supermarket Type1", "Supermarket Type2", "Departmental Store", "Food Mart"
31
+ ])
32
+ store_id = st.selectbox("Store ID", ["ST001", "ST002", "ST003", "ST004"])
33
+
34
+
35
+ # Convert the inputs into a dictionary for the backend
 
 
 
 
 
 
 
 
36
  input_data = {
37
  "Product_Weight": product_weight,
38
  "Product_Allocated_Area": product_area,
 
46
  "Store_Id": store_id
47
  }
48
 
49
+ # Make prediction when the "Predict" button is clicked
50
+ if st.button("Predict Sales"):
51
+ # Validate inputs before sending
52
+ if product_weight == 0 or product_area == 0 or product_mrp == 0:
53
+ st.warning("Please enter valid values for product details before predicting.")
54
+ else:
55
+ response = requests.post("https://rahulsuren12-TotalSalesPredictionBackend.hf.space/v1/sales", json=input_data) # Send data to Flask API
56
+ if response.status_code == 200:
57
+ prediction = response.json()['Predicted_Sales_Total']
58
+ st.success(f"Predicted Total Sales: ₹ {prediction:,.2f}")
59
+ else:
60
+ st.error("Error making prediction.")