Lokiiparihar commited on
Commit
d4470ae
·
verified ·
1 Parent(s): d993a8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -53
app.py CHANGED
@@ -1,62 +1,129 @@
1
  import streamlit as st
2
- import pandas as pd
3
- import requests # Import requests for API calls
4
 
5
- # --- Configuration for Backend API --- #
6
- # This URL should point to your deployed backend Hugging Face Space
7
- BACKEND_API_URL = "https://lokiiparihar-superkart-api-t.hf.space" # Replace with your actual backend space URL
 
8
  PREDICT_ENDPOINT = f"{BACKEND_API_URL}/v1/sales"
9
 
10
- # Streamlit UI for Sales Prediction
 
 
11
  st.title("SuperKart Sales Prediction App")
12
- st.write("This tool predicts the sales revenue for a specific product in a SuperKart store.")
13
-
14
- st.subheader("Enter the product and store details:")
15
-
16
- # Collect user input for SuperKart sales prediction
17
- product_id = st.selectbox("Product ID Prefix", ['FD', 'NC', 'DR'])
18
- product_weight = st.number_input("Product Weight", min_value=0.0, value=12.0)
19
- product_sugar_content = st.selectbox("Product Sugar Content", ['Low Sugar', 'Regular', 'No Sugar'])
20
- product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, value=0.05)
21
- product_type = st.selectbox("Product Type", ['Perishables', 'Non Perishables'])
22
- product_mrp = st.number_input("Product MRP", min_value=0.0, value=150.0)
23
- store_id = st.selectbox("Store ID", ['OUT004', 'OUT003', 'OUT001', 'OUT002'])
24
- store_size = st.selectbox("Store Size", ['Medium', 'High', 'Small'])
25
- store_location_city_type = st.selectbox("Store Location City Type", ['Tier 2', 'Tier 1', 'Tier 3'])
26
- store_current_age = st.number_input("Store Current Age (Years)", min_value=0, value=15)
27
-
28
- # Convert user input into a dictionary for JSON payload
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  input_data = {
30
- 'Product_Id': product_id,
31
- 'Product_Weight': product_weight,
32
- 'Product_Sugar_Content': product_sugar_content,
33
- 'Product_Allocated_Area': product_allocated_area,
34
- 'Product_Type': product_type,
35
- 'Product_MRP': product_mrp,
36
- 'Store_Id': store_id,
37
- 'Store_Size': store_size,
38
- 'Store_Location_City_Type': store_location_city_type,
39
- 'Store_Current_Age': store_current_age
 
40
  }
41
 
42
- # Predict button
 
 
43
  if st.button("Predict Sales"):
44
- try:
45
- # Make a POST request to the backend API
46
- response = requests.post(PREDICT_ENDPOINT, json=input_data)
47
- response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
48
-
49
- predicted_sales = response.json().get('Predicted Sales')
50
- if predicted_sales is not None:
51
- st.write(f"The predicted sales revenue is ${predicted_sales:.2f}.")
52
- else:
53
- st.error("Prediction failed: Unexpected response from backend.")
54
- st.json(response.json()) # Display full response for debugging
55
-
56
- except requests.exceptions.ConnectionError:
57
- st.error("Could not connect to the backend API. Please ensure the backend Space is running and accessible.")
58
- except requests.exceptions.RequestException as e:
59
- st.error(f"An error occurred during the API request: {e}")
60
- st.text(response.text) # Display raw response text for debugging
61
- except Exception as e:
62
- st.error(f"An unexpected error occurred: {e}")
 
 
 
 
 
 
1
  import streamlit as st
2
+ import requests
 
3
 
4
+ # ----------------------------------
5
+ # Backend API Configuration
6
+ # ----------------------------------
7
+ BACKEND_API_URL = "https://lokiiparihar-superkart-api-t.hf.space"
8
  PREDICT_ENDPOINT = f"{BACKEND_API_URL}/v1/sales"
9
 
10
+ # ----------------------------------
11
+ # Streamlit UI
12
+ # ----------------------------------
13
  st.title("SuperKart Sales Prediction App")
14
+ st.write(
15
+ "This tool predicts the sales revenue for a specific product in a SuperKart store "
16
+ "using a trained machine learning model."
17
+ )
18
+
19
+ st.subheader("Enter the product and store details")
20
+
21
+ # ----------------------------------
22
+ # User Inputs
23
+ # ----------------------------------
24
+ product_id = st.selectbox("Product ID Prefix", ["FD", "NC", "DR"])
25
+
26
+ product_weight = st.number_input(
27
+ "Product Weight",
28
+ min_value=0.0,
29
+ value=12.0
30
+ )
31
+
32
+ product_sugar_content = st.selectbox(
33
+ "Product Sugar Content",
34
+ ["Low Sugar", "Regular", "No Sugar"]
35
+ )
36
+
37
+ product_allocated_area = st.number_input(
38
+ "Product Allocated Area",
39
+ min_value=0.0,
40
+ value=0.05
41
+ )
42
+
43
+ product_type = st.selectbox(
44
+ "Product Type",
45
+ ["Perishables", "Non Perishables"]
46
+ )
47
+
48
+ product_mrp = st.number_input(
49
+ "Product MRP",
50
+ min_value=0.0,
51
+ value=150.0
52
+ )
53
+
54
+ store_id = st.selectbox(
55
+ "Store ID",
56
+ ["OUT004", "OUT003", "OUT001", "OUT002"]
57
+ )
58
+
59
+ store_size = st.selectbox(
60
+ "Store Size",
61
+ ["Small", "Medium", "High"]
62
+ )
63
+
64
+ store_location_city_type = st.selectbox(
65
+ "Store Location City Type",
66
+ ["Tier 1", "Tier 2", "Tier 3"]
67
+ )
68
+
69
+ store_type = st.selectbox(
70
+ "Store Type",
71
+ [
72
+ "Grocery Store",
73
+ "Supermarket Type1",
74
+ "Supermarket Type2",
75
+ "Supermarket Type3"
76
+ ]
77
+ )
78
+
79
+ store_current_age = st.number_input(
80
+ "Store Current Age (Years)",
81
+ min_value=0,
82
+ value=15
83
+ )
84
+
85
+ # ----------------------------------
86
+ # Payload for API
87
+ # ----------------------------------
88
  input_data = {
89
+ "Product_Id": product_id,
90
+ "Product_Weight": product_weight,
91
+ "Product_Sugar_Content": product_sugar_content,
92
+ "Product_Allocated_Area": product_allocated_area,
93
+ "Product_Type": product_type,
94
+ "Product_MRP": product_mrp,
95
+ "Store_Id": store_id,
96
+ "Store_Size": store_size,
97
+ "Store_Location_City_Type": store_location_city_type,
98
+ "Store_Type": store_type, # ✅ REQUIRED
99
+ "Store_Current_Age": store_current_age
100
  }
101
 
102
+ # ----------------------------------
103
+ # Predict Button
104
+ # ----------------------------------
105
  if st.button("Predict Sales"):
106
+ with st.spinner("Predicting sales..."):
107
+ try:
108
+ response = requests.post(
109
+ PREDICT_ENDPOINT,
110
+ json=input_data,
111
+ timeout=10
112
+ )
113
+
114
+ if response.status_code == 200:
115
+ result = response.json()
116
+ st.success(
117
+ f"Predicted Sales Revenue: ${result['Predicted Sales']:.2f}"
118
+ )
119
+ else:
120
+ st.error("Prediction failed")
121
+ st.json(response.json())
122
+
123
+ except requests.exceptions.ConnectionError:
124
+ st.error("Could not connect to backend API.")
125
+ except requests.exceptions.Timeout:
126
+ st.error("Request timed out. Please try again.")
127
+ except Exception as e:
128
+ st.error(f"Unexpected error: {e}")
129
+