Spaces:
Sleeping
Sleeping
Update streamlit app
Browse files- src/streamlit_app.py +41 -83
src/streamlit_app.py
CHANGED
|
@@ -1,108 +1,66 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
-
import pandas as pd
|
| 4 |
import json
|
| 5 |
-
from datetime import datetime
|
| 6 |
|
| 7 |
-
# Page
|
| 8 |
-
st.set_page_config(
|
| 9 |
-
page_title="SuperKart Sales Forecasting",
|
| 10 |
-
page_icon="🛒",
|
| 11 |
-
layout="wide"
|
| 12 |
-
)
|
| 13 |
|
| 14 |
-
# API
|
| 15 |
API_BASE_URL = "https://hackstert-greatlearningproject.hf.space"
|
| 16 |
|
| 17 |
-
|
| 18 |
-
st.
|
| 19 |
-
st.write("Enter product and store details to get a sales forecast")
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
def call_api(endpoint, data=None, method="GET"):
|
| 23 |
-
try:
|
| 24 |
-
url = f"{API_BASE_URL}{endpoint}"
|
| 25 |
-
if method == "GET":
|
| 26 |
-
response = requests.get(url)
|
| 27 |
-
elif method == "POST":
|
| 28 |
-
response = requests.post(url, json=data)
|
| 29 |
-
|
| 30 |
-
if response.status_code == 200:
|
| 31 |
-
return response.json(), None
|
| 32 |
-
else:
|
| 33 |
-
return None, f"API Error: {response.status_code}"
|
| 34 |
-
except Exception as e:
|
| 35 |
-
return None, f"Connection Error: {str(e)}"
|
| 36 |
-
|
| 37 |
-
# Create input form
|
| 38 |
with st.form("prediction_form"):
|
| 39 |
col1, col2 = st.columns(2)
|
| 40 |
|
| 41 |
with col1:
|
| 42 |
-
st.subheader("Product
|
| 43 |
-
product_weight = st.number_input("
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
"Baking Goods", "Snack Foods", "Frozen Foods", "Breakfast"
|
| 48 |
-
])
|
| 49 |
-
product_mrp = st.number_input("Maximum Retail Price ($)", min_value=1.0, max_value=500.0, value=150.0)
|
| 50 |
|
| 51 |
with col2:
|
| 52 |
-
st.subheader("Store
|
| 53 |
-
store_id = st.selectbox("Store ID", [f"OUT{str(i).zfill(3)}" for i in range(1, 46)])
|
| 54 |
-
store_establishment_year = st.number_input("Establishment Year", min_value=1985, max_value=2024, value=2009)
|
| 55 |
store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
"Grocery Store", "Departmental Store"
|
| 60 |
-
])
|
| 61 |
-
|
| 62 |
-
# Submit button
|
| 63 |
-
submitted = st.form_submit_button("🔮 Predict Sales", type="primary")
|
| 64 |
|
| 65 |
-
if
|
| 66 |
-
# Prepare data
|
| 67 |
-
|
| 68 |
"Product_Weight": product_weight,
|
|
|
|
| 69 |
"Product_Sugar_Content": product_sugar_content,
|
| 70 |
"Product_Type": product_type,
|
| 71 |
-
"Product_MRP": product_mrp,
|
| 72 |
-
"Store_Id": store_id,
|
| 73 |
-
"Store_Establishment_Year": store_establishment_year,
|
| 74 |
"Store_Size": store_size,
|
| 75 |
-
"
|
| 76 |
-
"
|
|
|
|
| 77 |
}
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
if result:
|
| 84 |
-
st.success("Prediction completed successfully!")
|
| 85 |
-
st.metric("Predicted Sales", result["prediction_formatted"])
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
st.
|
| 90 |
-
|
| 91 |
-
st.
|
| 92 |
-
|
| 93 |
-
st.error(f"
|
| 94 |
-
|
| 95 |
-
# API Health Check Section
|
| 96 |
-
st.markdown("---")
|
| 97 |
-
st.subheader("🏥 API Health Check")
|
| 98 |
|
|
|
|
| 99 |
if st.button("Check API Status"):
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
st.error(f"API health check failed: {error}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
| 3 |
import json
|
|
|
|
| 4 |
|
| 5 |
+
# Page config
|
| 6 |
+
st.set_page_config(page_title="SuperKart Sales Forecasting", layout="wide")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# API URL - your working backend
|
| 9 |
API_BASE_URL = "https://hackstert-greatlearningproject.hf.space"
|
| 10 |
|
| 11 |
+
st.title("SuperKart Sales Forecasting")
|
| 12 |
+
st.write("Enter product details to get sales prediction")
|
|
|
|
| 13 |
|
| 14 |
+
# Simple form
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
with st.form("prediction_form"):
|
| 16 |
col1, col2 = st.columns(2)
|
| 17 |
|
| 18 |
with col1:
|
| 19 |
+
st.subheader("Product Info")
|
| 20 |
+
product_weight = st.number_input("Weight (kg)", value=12.5)
|
| 21 |
+
product_mrp = st.number_input("Price ($)", value=150.0)
|
| 22 |
+
product_sugar_content = st.selectbox("Sugar Content", ["Low Sugar", "Regular"])
|
| 23 |
+
product_type = st.selectbox("Product Type", ["Dairy", "Soft Drinks", "Meat", "Fruits and Vegetables"])
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
with col2:
|
| 26 |
+
st.subheader("Store Info")
|
|
|
|
|
|
|
| 27 |
store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
|
| 28 |
+
store_type = st.selectbox("Store Type", ["Supermarket Type1", "Grocery Store"])
|
| 29 |
+
store_location = st.selectbox("City Type", ["Tier 1", "Tier 2", "Tier 3"])
|
| 30 |
+
store_year = st.number_input("Establishment Year", value=2009)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
if st.form_submit_button("Predict Sales"):
|
| 33 |
+
# Prepare data
|
| 34 |
+
data = {
|
| 35 |
"Product_Weight": product_weight,
|
| 36 |
+
"Product_MRP": product_mrp,
|
| 37 |
"Product_Sugar_Content": product_sugar_content,
|
| 38 |
"Product_Type": product_type,
|
|
|
|
|
|
|
|
|
|
| 39 |
"Store_Size": store_size,
|
| 40 |
+
"Store_Type": store_type,
|
| 41 |
+
"Store_Location_City_Type": store_location,
|
| 42 |
+
"Store_Establishment_Year": store_year
|
| 43 |
}
|
| 44 |
|
| 45 |
+
try:
|
| 46 |
+
# Call API
|
| 47 |
+
response = requests.post(f"{API_BASE_URL}/predict", json=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
if response.status_code == 200:
|
| 50 |
+
result = response.json()
|
| 51 |
+
st.success(f"Predicted Sales: {result['prediction_formatted']}")
|
| 52 |
+
else:
|
| 53 |
+
st.error(f"API Error: {response.status_code}")
|
| 54 |
+
except Exception as e:
|
| 55 |
+
st.error(f"Connection Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
# Health check
|
| 58 |
if st.button("Check API Status"):
|
| 59 |
+
try:
|
| 60 |
+
response = requests.get(f"{API_BASE_URL}/")
|
| 61 |
+
if response.status_code == 200:
|
| 62 |
+
st.success("API is healthy!")
|
| 63 |
+
else:
|
| 64 |
+
st.error("API not responding")
|
| 65 |
+
except:
|
| 66 |
+
st.error("Cannot connect to API")
|
|
|