Commit Β·
433f2f5
1
Parent(s): 361ab09
Update app file
Browse files- streamlit_app/app_streamlit.py +18 -154
streamlit_app/app_streamlit.py
CHANGED
|
@@ -5,159 +5,23 @@ import pandas as pd
|
|
| 5 |
import streamlit as st
|
| 6 |
import requests
|
| 7 |
|
| 8 |
-
# ----------------------------
|
| 9 |
-
#
|
| 10 |
-
# ----------------------------
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
BACKEND_URL = os.getenv("BACKEND_URL", "").rstrip("/")
|
| 13 |
-
if BACKEND_URL == "":
|
| 14 |
-
st.warning(
|
| 15 |
-
"β οΈ BACKEND_URL is not set. "
|
| 16 |
-
"Set an environment variable `BACKEND_URL` to your backend Space base URL, "
|
| 17 |
-
"e.g. https://<username>-<backendspace>.hf.space"
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
st.set_page_config(page_title="ExtraaLearn β Lead Conversion", page_icon="π", layout="wide")
|
| 21 |
-
st.title("π ExtraaLearn β Lead Conversion (Frontend)")
|
| 22 |
-
|
| 23 |
-
with st.expander("βΉοΈ Instructions", expanded=False):
|
| 24 |
-
st.markdown(
|
| 25 |
-
"""
|
| 26 |
-
- Set environment variable **`BACKEND_URL`** to your backend Space URL (without trailing slash).
|
| 27 |
-
- Use the **Single Prediction** tab for an individual lead.
|
| 28 |
-
- Use the **Batch Scoring** tab to upload a CSV and get predictions back.
|
| 29 |
-
"""
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
# ----------------------------
|
| 33 |
-
# Utilities
|
| 34 |
-
# ----------------------------
|
| 35 |
-
def ping_backend():
|
| 36 |
-
if not BACKEND_URL:
|
| 37 |
-
return False, {"error": "BACKEND_URL not set"}
|
| 38 |
-
try:
|
| 39 |
-
r = requests.get(f"{BACKEND_URL}/", timeout=10)
|
| 40 |
-
return r.ok, r.json()
|
| 41 |
-
except Exception as e:
|
| 42 |
-
return False, {"error": str(e)}
|
| 43 |
-
|
| 44 |
-
def post_json(endpoint: str, payload):
|
| 45 |
-
url = f"{BACKEND_URL}{endpoint}"
|
| 46 |
-
r = requests.post(url, json=payload, timeout=60)
|
| 47 |
-
r.raise_for_status()
|
| 48 |
-
return r
|
| 49 |
-
|
| 50 |
-
# ----------------------------
|
| 51 |
-
# Health check
|
| 52 |
-
# ----------------------------
|
| 53 |
-
colA, colB = st.columns([1, 2])
|
| 54 |
-
with colA:
|
| 55 |
-
if st.button("π Check Backend Health"):
|
| 56 |
-
ok, info = ping_backend()
|
| 57 |
-
if ok:
|
| 58 |
-
st.success(f"Backend OK Β· model_loaded={info.get('model_loaded')}")
|
| 59 |
-
else:
|
| 60 |
-
st.error(f"Backend unreachable: {info}")
|
| 61 |
-
|
| 62 |
-
# ----------------------------
|
| 63 |
-
# Tabs
|
| 64 |
-
# ----------------------------
|
| 65 |
-
tab1, tab2 = st.tabs(["Single Prediction", "Batch Scoring"])
|
| 66 |
-
|
| 67 |
-
# ---- Tab 1: Single ----
|
| 68 |
-
with tab1:
|
| 69 |
-
st.subheader("Single Prediction")
|
| 70 |
-
c1, c2, c3 = st.columns(3)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
with c2:
|
| 79 |
-
current_occupation = st.selectbox("Current Occupation", ["Professional", "Student", "Unemployed"])
|
| 80 |
-
first_interaction = st.selectbox("First Interaction", ["Website", "Mobile App"])
|
| 81 |
-
last_activity = st.selectbox("Last Activity", ["Email Activity", "Phone Activity", "Website Activity"])
|
| 82 |
-
profile_completed = st.selectbox(
|
| 83 |
-
"Profile Completed",
|
| 84 |
-
["Low (0-50%)", "Medium (50-75%)", "High (75-100%)"]
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
with c3:
|
| 88 |
-
print_media_type1 = st.selectbox("Saw Newspaper Ad (print_media_type1)", ["No", "Yes"])
|
| 89 |
-
print_media_type2 = st.selectbox("Saw Magazine Ad (print_media_type2)", ["No", "Yes"])
|
| 90 |
-
digital_media = st.selectbox("Saw Digital Ad", ["No", "Yes"])
|
| 91 |
-
educational_channels = st.selectbox("Heard via Educational Channels", ["No", "Yes"])
|
| 92 |
-
referral = st.selectbox("Referred by Someone", ["No", "Yes"])
|
| 93 |
-
|
| 94 |
-
if st.button("π Predict"):
|
| 95 |
-
if not BACKEND_URL:
|
| 96 |
-
st.error("Please set BACKEND_URL to your backend Space URL.")
|
| 97 |
-
else:
|
| 98 |
-
payload = {
|
| 99 |
-
"age": age,
|
| 100 |
-
"website_visits": website_visits,
|
| 101 |
-
"time_spent_on_website": time_spent_on_website,
|
| 102 |
-
"page_views_per_visit": page_views_per_visit,
|
| 103 |
-
"print_media_type1": print_media_type1,
|
| 104 |
-
"print_media_type2": print_media_type2,
|
| 105 |
-
"digital_media": digital_media,
|
| 106 |
-
"educational_channels": educational_channels,
|
| 107 |
-
"referral": referral,
|
| 108 |
-
"current_occupation": current_occupation,
|
| 109 |
-
"first_interaction": first_interaction,
|
| 110 |
-
"last_activity": last_activity,
|
| 111 |
-
"profile_completed": profile_completed
|
| 112 |
-
}
|
| 113 |
-
try:
|
| 114 |
-
start = time.time()
|
| 115 |
-
r = post_json("/predict", payload)
|
| 116 |
-
elapsed = time.time() - start
|
| 117 |
-
result = r.json()
|
| 118 |
-
st.success(f"β
Inference OK Β· {elapsed:.2f}s")
|
| 119 |
-
st.metric("Conversion Probability", f"{result.get('probability', 0.0):.3f}")
|
| 120 |
-
pred = result.get("prediction", 0)
|
| 121 |
-
st.write("Prediction:", "β
Will Convert" if pred == 1 else "β Unlikely to Convert")
|
| 122 |
-
st.code(json.dumps(payload, indent=2), language="json")
|
| 123 |
-
except requests.HTTPError as http_err:
|
| 124 |
-
st.error(f"HTTP error: {http_err} Β· {r.text}")
|
| 125 |
-
except Exception as e:
|
| 126 |
-
st.error(f"Request failed: {e}")
|
| 127 |
-
|
| 128 |
-
# ---- Tab 2: Batch ----
|
| 129 |
-
with tab2:
|
| 130 |
-
st.subheader("Batch Scoring (CSV)")
|
| 131 |
-
st.caption("The CSV must include the same columns used during model training.")
|
| 132 |
-
|
| 133 |
-
sample_schema = [
|
| 134 |
-
"age","website_visits","time_spent_on_website","page_views_per_visit",
|
| 135 |
-
"print_media_type1","print_media_type2","digital_media","educational_channels","referral",
|
| 136 |
-
"current_occupation","first_interaction","last_activity","profile_completed"
|
| 137 |
-
]
|
| 138 |
-
st.text_area("Expected columns", ", ".join(sample_schema), height=70, disabled=True)
|
| 139 |
-
|
| 140 |
-
up = st.file_uploader("Upload CSV", type=["csv"])
|
| 141 |
-
if up is not None:
|
| 142 |
-
try:
|
| 143 |
-
df = pd.read_csv(up)
|
| 144 |
-
st.write("Preview:", df.head())
|
| 145 |
-
if st.button("π€ Send to Backend"):
|
| 146 |
-
if not BACKEND_URL:
|
| 147 |
-
st.error("Please set BACKEND_URL to your backend Space URL.")
|
| 148 |
-
else:
|
| 149 |
-
records = df.to_dict(orient="records")
|
| 150 |
-
start = time.time()
|
| 151 |
-
r = post_json("/predict-batch", {"records": records})
|
| 152 |
-
elapsed = time.time() - start
|
| 153 |
-
out = pd.DataFrame(r.json())
|
| 154 |
-
st.success(f"β
Batch inference OK Β· {elapsed:.2f}s")
|
| 155 |
-
st.dataframe(out.head(20), use_container_width=True)
|
| 156 |
-
st.download_button(
|
| 157 |
-
"β¬οΈ Download Results",
|
| 158 |
-
out.to_csv(index=False).encode("utf-8"),
|
| 159 |
-
file_name="predictions.csv",
|
| 160 |
-
mime="text/csv"
|
| 161 |
-
)
|
| 162 |
-
except Exception as e:
|
| 163 |
-
st.error(f"Failed to process CSV: {e}")
|
|
|
|
| 5 |
import streamlit as st
|
| 6 |
import requests
|
| 7 |
|
| 8 |
+
# -----------------------------------------------------
|
| 9 |
+
# MUST COME FIRST BEFORE ANY OTHER STREAMLIT COMMAND
|
| 10 |
+
# -----------------------------------------------------
|
| 11 |
+
st.set_page_config(
|
| 12 |
+
page_title="ExtraaLearn β Lead Conversion",
|
| 13 |
+
page_icon="π",
|
| 14 |
+
layout="wide"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# -----------------------------------------------------
|
| 18 |
+
# Backend API URL (from HF Environment Variable)
|
| 19 |
+
# -----------------------------------------------------
|
| 20 |
BACKEND_URL = os.getenv("BACKEND_URL", "").rstrip("/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
if not BACKEND_URL:
|
| 23 |
+
st.warning(
|
| 24 |
+
"β οΈ BACKEND_URL is not set. Set an environment variable BACKEND_URL "
|
| 25 |
+
"to your backend Space URL (without trailing slash). "
|
| 26 |
+
"Example: https://<username>-<backendspace>.hf.space"
|
| 27 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|