Spaces:
Sleeping
Sleeping
Deploy Streamlit frontend
Browse files- app.py +98 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, pandas as pd, requests, streamlit as st
|
| 2 |
+
|
| 3 |
+
API_BASE = (os.environ.get("API_BASE") or "https://Amitgupta2982-superkart-backend.hf.space").rstrip("/")
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="SuperKart • Sales Price Predictor", page_icon="🛒", layout="wide")
|
| 6 |
+
st.title("🛒 SuperKart — Sales Price Predictor")
|
| 7 |
+
st.caption("Enter details to predict sales price, or upload a CSV to batch-score.")
|
| 8 |
+
|
| 9 |
+
# ---- Health ----
|
| 10 |
+
with st.sidebar:
|
| 11 |
+
st.header("⚙️ Backend")
|
| 12 |
+
api_base = st.text_input("API Base", value=API_BASE)
|
| 13 |
+
try:
|
| 14 |
+
r = requests.get(f"{api_base}/health", timeout=10)
|
| 15 |
+
st.success(f"Health: {r.status_code} • {r.text}")
|
| 16 |
+
except Exception as e:
|
| 17 |
+
st.error(f"Health check failed: {e}")
|
| 18 |
+
|
| 19 |
+
tab_single, tab_batch = st.tabs(["🔮 Single Prediction", "📦 Batch Prediction"])
|
| 20 |
+
|
| 21 |
+
# ---- Single ----
|
| 22 |
+
with tab_single:
|
| 23 |
+
col1, col2, col3 = st.columns(3)
|
| 24 |
+
with col1:
|
| 25 |
+
w = st.number_input("Product_Weight (kg)", 0.0, step=0.01, value=0.35)
|
| 26 |
+
sugar = st.number_input("Product_Sugar_Content (g)", 0.0, step=0.1, value=12.0)
|
| 27 |
+
area = st.number_input("Product_Allocated_Area (m²)", 0.0, step=0.1, value=1.2)
|
| 28 |
+
with col2:
|
| 29 |
+
ptype = st.selectbox("Product_Type", ["Snack","Grocery","Beverage","Personal Care","Other"])
|
| 30 |
+
mrp = st.number_input("Product_MRP", 0.0, step=1.0, value=199.0)
|
| 31 |
+
ssize = st.selectbox("Store_Size", ["Small","Medium","Large"], index=1)
|
| 32 |
+
with col3:
|
| 33 |
+
city = st.selectbox("Store_Location_City_Type", ["Tier 1","Tier 2","Tier 3"], index=1)
|
| 34 |
+
stype = st.selectbox("Store_Type", ["Supermarket","Hypermarket","Grocery","Convenience"])
|
| 35 |
+
year = st.number_input("Store_Establishment_Year", min_value=1900, max_value=2100, value=2015, step=1)
|
| 36 |
+
|
| 37 |
+
if st.button("Predict Price", type="primary"):
|
| 38 |
+
payload = {
|
| 39 |
+
"Product_Weight": w,
|
| 40 |
+
"Product_Sugar_Content": sugar,
|
| 41 |
+
"Product_Allocated_Area": area,
|
| 42 |
+
"Product_Type": ptype,
|
| 43 |
+
"Product_MRP": mrp,
|
| 44 |
+
"Store_Size": ssize,
|
| 45 |
+
"Store_Location_City_Type": city,
|
| 46 |
+
"Store_Type": stype,
|
| 47 |
+
"Store_Establishment_Year": year,
|
| 48 |
+
}
|
| 49 |
+
try:
|
| 50 |
+
r = requests.post(f"{api_base}/v1/salesprice", json=payload, timeout=20)
|
| 51 |
+
if r.status_code == 200:
|
| 52 |
+
st.success(f"Predicted Price: **{r.json().get('Predicted Price')}**")
|
| 53 |
+
else:
|
| 54 |
+
st.error(f"{r.status_code}: {r.text}")
|
| 55 |
+
except Exception as e:
|
| 56 |
+
st.error(str(e))
|
| 57 |
+
|
| 58 |
+
# ---- Batch ----
|
| 59 |
+
with tab_batch:
|
| 60 |
+
st.caption("CSV must include columns: Product_Weight, Product_Sugar_Content, Product_Allocated_Area, "
|
| 61 |
+
"Product_Type, Product_MRP, Store_Size, Store_Location_City_Type, Store_Type, Store_Establishment_Year")
|
| 62 |
+
sample = pd.DataFrame([{
|
| 63 |
+
"Product_Weight": 0.35,
|
| 64 |
+
"Product_Sugar_Content": 12.0,
|
| 65 |
+
"Product_Allocated_Area": 1.2,
|
| 66 |
+
"Product_Type": "Snack",
|
| 67 |
+
"Product_MRP": 199.0,
|
| 68 |
+
"Store_Size": "Medium",
|
| 69 |
+
"Store_Location_City_Type": "Tier 2",
|
| 70 |
+
"Store_Type": "Supermarket",
|
| 71 |
+
"Store_Establishment_Year": 2015,
|
| 72 |
+
}])
|
| 73 |
+
st.download_button("Download sample CSV", data=sample.to_csv(index=False).encode("utf-8"),
|
| 74 |
+
file_name="superkart_sample.csv", mime="text/csv")
|
| 75 |
+
|
| 76 |
+
file = st.file_uploader("Upload CSV", type=["csv"])
|
| 77 |
+
if file and st.button("Run Batch Prediction", type="primary"):
|
| 78 |
+
try:
|
| 79 |
+
df = pd.read_csv(file)
|
| 80 |
+
st.write("Preview:", df.head())
|
| 81 |
+
csv_bytes = df.to_csv(index=False).encode("utf-8")
|
| 82 |
+
r = requests.post(f"{api_base}/v1/salespricebatch",
|
| 83 |
+
files={"file": ("batch.csv", csv_bytes, "text/csv")},
|
| 84 |
+
timeout=30)
|
| 85 |
+
if r.status_code == 200:
|
| 86 |
+
out = pd.DataFrame(r.json())
|
| 87 |
+
st.success("Done ✅")
|
| 88 |
+
st.dataframe(out, use_container_width=True)
|
| 89 |
+
st.download_button("Download predictions", out.to_csv(index=False).encode("utf-8"),
|
| 90 |
+
"predictions.csv", "text/csv")
|
| 91 |
+
else:
|
| 92 |
+
st.error(f"{r.status_code}: {r.text}")
|
| 93 |
+
except Exception as e:
|
| 94 |
+
st.error(str(e))
|
| 95 |
+
|
| 96 |
+
st.markdown("---")
|
| 97 |
+
st.caption("SuperKart • Streamlit frontend for the Sales Price Prediction API")
|
| 98 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
streamlit==1.43.2
|
| 3 |
+
pandas==2.2.2
|
| 4 |
+
requests==2.32.3
|