Lokiiparihar commited on
Commit
e670d86
·
verified ·
1 Parent(s): 181b7b0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+
4
+ API_URL = "https://lokiiparihar-superkart-api.hf.space/predict" # use the working API
5
+
6
+ st.set_page_config(page_title="Superkart Sales Prediction", layout="centered")
7
+
8
+ st.title("Sales Prediction App")
9
+ st.write("This tool predicts Superkart sales. Enter the required information below.")
10
+
11
+ # Model Choice
12
+ model_choice = st.selectbox(
13
+ "Select Model",
14
+ options=["dt", "xgb"],
15
+ format_func=lambda x: {
16
+ "dt": "Decision Tree",
17
+ "xgb": "XGBoost",
18
+ "rf": "Random Forest",
19
+ "lr": "Linear Regression",
20
+ }.get(x, x),
21
+ )
22
+
23
+ # Inputs (set defaults so the API call has valid values)
24
+ col1, col2 = st.columns(2)
25
+
26
+ with col1:
27
+ product_weight = st.number_input("Product Weight", min_value=0.0, value=12.5, step=0.1)
28
+ sugar = st.selectbox("Sugar Content", [0, 1, 2], index=0)
29
+ area = st.number_input("Allocated Area", min_value=0.0, value=0.08, step=0.01)
30
+ product_type = st.number_input("Product Type Code", min_value=0, value=0, step=1)
31
+
32
+ with col2:
33
+ mrp = st.number_input("Product MRP", min_value=0.0, value=249.99, step=1.0)
34
+ store_size = st.selectbox("Store Size Code", [0, 1, 2], index=1)
35
+ city = st.selectbox("City Type Code", [0, 1, 2], index=0)
36
+ store_type = st.number_input("Store Type Code", min_value=0, value=1, step=1)
37
+ store_age = st.number_input("Store Age", min_value=0, value=15, step=1)
38
+
39
+ # Build payload EXACTLY as your working notebook request expects
40
+ sample = {
41
+ "Product_Weight": float(product_weight),
42
+ "Product_Sugar_Content": float(sugar),
43
+ "Product_Allocated_Area": float(area),
44
+ "Product_Type": int(product_type),
45
+ "Product_MRP": float(mrp),
46
+ "Store_Size": int(store_size),
47
+ "Store_Location_City_Type": int(city),
48
+ "Store_Type": int(store_type),
49
+ "Store_Age": int(store_age),
50
+ "model": model_choice,
51
+ }
52
+
53
+ st.subheader("Payload being sent")
54
+ st.json(sample)
55
+
56
+ if st.button("Predict", type="primary"):
57
+ try:
58
+ headers = {"Content-Type": "application/json"}
59
+ with st.spinner("Calling prediction API..."):
60
+ response = requests.post(API_URL, json=sample, headers=headers, timeout=30)
61
+
62
+ st.write("Status Code:", response.status_code)
63
+
64
+ # Show raw response for debugging
65
+ st.write("Raw Response:")
66
+ st.code(response.text)
67
+
68
+ if response.headers.get("content-type", "").startswith("application/json"):
69
+ result = response.json()
70
+ st.write("Parsed JSON:")
71
+ st.json(result)
72
+
73
+ # Try common keys (your API might return a different one)
74
+ pred_key = next((k for k in ["Prediction", "prediction", "pred", "result", "output"] if k in result), None)
75
+ if pred_key:
76
+ st.success(f"Prediction: {result[pred_key]}")
77
+ else:
78
+ st.info("Prediction key not found. See JSON above.")
79
+ else:
80
+ st.error("API did not return JSON. See raw response above.")
81
+
82
+ except requests.exceptions.RequestException as e:
83
+ st.error(f"Request failed: {e}")
84
+
85
+ # IMPORTANT:
86
+ # Streamlit apps do NOT use app.run(). Remove any Flask-related code.
87
+ # if __name__ == '__main__':
88
+ # app.run(debug=True)