singhina's picture
Fix Streamlit async loop & permission issues + backend linkage
2603cfa
import os
# Redirect HOME so Streamlit writes under /tmp
os.environ["HOME"] = "/tmp"
# Disable usage stats
os.environ["STREAMLIT_GATHER_USAGE_STATS"] = "false"
# Use tmp for config/cache
os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/.streamlit"
os.environ["STREAMLIT_CACHE_DIR"] = "/tmp/.streamlit"
# Patch asyncio loop to avoid RuntimeError
import nest_asyncio
nest_asyncio.apply()
import streamlit as st
import requests
import pandas as pd
# Page config
st.set_page_config(page_title="ExtraaLearn Lead Converter", layout="centered")
st.title("πŸŽ“ ExtraaLearn Lead Conversion")
st.write("Enter lead details and click Predict.")
# Inputs
age = st.number_input("Age", 18, 100, 30)
visits = st.number_input("Website Visits", 0, 50, 1)
time_spent = st.number_input("Time Spent on Website (s)", 0, 5000, 300)
pages = st.number_input("Page Views per Visit", 1, 20, 3)
occ = st.selectbox("Current Occupation", ["Professional","Unemployed","Student"])
first_int = st.selectbox("First Interaction", ["Website","Mobile App"])
profile = st.selectbox("Profile Completed", ["Low","Medium","High"])
print1 = st.checkbox("Saw Newspaper Ad")
print2 = st.checkbox("Saw Magazine Ad")
digital = st.checkbox("Saw Digital Ad")
edu_chan = st.checkbox("Heard via Education Channels")
referral = st.checkbox("Heard via Referral")
last_act = st.selectbox("Last Activity", ["Email Activity","Phone Activity","Website Activity"])
if st.button("Predict"):
payload = {
"age": age,
"website_visits": visits,
"time_spent_on_website": time_spent,
"page_views_per_visit": pages,
"current_occupation": occ,
"first_interaction": first_int,
"profile_completed": profile,
"print_media_type1": int(print1),
"print_media_type2": int(print2),
"digital_media": int(digital),
"educational_channels": int(edu_chan),
"referral": int(referral),
"last_activity": last_act
}
resp = requests.post("$BACKEND_URL", json=payload)
if resp.ok:
res = resp.json()
st.success(f"Conversion: {res['prediction']} (Prob: {res['probability']:.2f})")
else:
st.error(f"Error {resp.status_code}: {resp.text}")