Spaces:
Sleeping
Sleeping
Upload streamlit_app.py with huggingface_hub
Browse files- streamlit_app.py +108 -0
streamlit_app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import json
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
# Page configuration
|
| 8 |
+
st.set_page_config(
|
| 9 |
+
page_title="SuperKart Sales Forecasting",
|
| 10 |
+
page_icon="๐",
|
| 11 |
+
layout="wide"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# API Configuration
|
| 15 |
+
API_BASE_URL = "https://hackstert-greatlearningproject.hf.space"
|
| 16 |
+
|
| 17 |
+
# Title
|
| 18 |
+
st.title("๐ SuperKart Sales Forecasting System")
|
| 19 |
+
st.write("Enter product and store details to get a sales forecast")
|
| 20 |
+
|
| 21 |
+
# Helper function to make API calls
|
| 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 Information")
|
| 43 |
+
product_weight = st.number_input("Product Weight (kg)", min_value=0.1, max_value=50.0, value=12.5)
|
| 44 |
+
product_sugar_content = st.selectbox("Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
|
| 45 |
+
product_type = st.selectbox("Product Type", [
|
| 46 |
+
"Dairy", "Soft Drinks", "Meat", "Fruits and Vegetables", "Household",
|
| 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 Information")
|
| 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 |
+
store_location_city_type = st.selectbox("City Type", ["Tier 1", "Tier 2", "Tier 3"])
|
| 57 |
+
store_type = st.selectbox("Store Type", [
|
| 58 |
+
"Supermarket Type1", "Supermarket Type2", "Supermarket Type3",
|
| 59 |
+
"Grocery Store", "Departmental Store"
|
| 60 |
+
])
|
| 61 |
+
|
| 62 |
+
# Submit button
|
| 63 |
+
submitted = st.form_submit_button("๐ฎ Predict Sales", type="primary")
|
| 64 |
+
|
| 65 |
+
if submitted:
|
| 66 |
+
# Prepare data for API call
|
| 67 |
+
prediction_data = {
|
| 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 |
+
"Store_Location_City_Type": store_location_city_type,
|
| 76 |
+
"Store_Type": store_type
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
# Make API call
|
| 80 |
+
with st.spinner("Predicting sales..."):
|
| 81 |
+
result, error = call_api("/predict", prediction_data, "POST")
|
| 82 |
+
|
| 83 |
+
if result:
|
| 84 |
+
st.success("Prediction completed successfully!")
|
| 85 |
+
st.metric("Predicted Sales", result["prediction_formatted"])
|
| 86 |
+
|
| 87 |
+
# Show additional details
|
| 88 |
+
with st.expander("View Details"):
|
| 89 |
+
st.write(f"Raw Prediction: ${result['prediction']:.2f}")
|
| 90 |
+
st.write(f"Model Version: {result['model_version']}")
|
| 91 |
+
st.write(f"Timestamp: {result['timestamp']}")
|
| 92 |
+
else:
|
| 93 |
+
st.error(f"Prediction failed: {error}")
|
| 94 |
+
|
| 95 |
+
# API Health Check Section
|
| 96 |
+
st.markdown("---")
|
| 97 |
+
st.subheader("๐ฅ API Health Check")
|
| 98 |
+
|
| 99 |
+
if st.button("Check API Status"):
|
| 100 |
+
with st.spinner("Checking API health..."):
|
| 101 |
+
result, error = call_api("/")
|
| 102 |
+
|
| 103 |
+
if result:
|
| 104 |
+
st.success("API is healthy!")
|
| 105 |
+
st.write(f"Status: {result['status']}")
|
| 106 |
+
st.write(f"Message: {result['message']}")
|
| 107 |
+
else:
|
| 108 |
+
st.error(f"API health check failed: {error}")
|