Download app.py from shravanda/ExtraaLearnLeadFrontend: direct link, hf CLI and curl.
- Browser
- Download file 4.75 kB
-
https://huggingface.co/spaces/shravanda/ExtraaLearnLeadFrontend/resolve/main/app.py
- Command line
-
hf download hf://spaces/shravanda/ExtraaLearnLeadFrontend/app.py
-
curl -L -o app.py https://huggingface.co/spaces/shravanda/ExtraaLearnLeadFrontend/resolve/main/app.py
4.75 kB
| import pandas as pd | |
| import requests | |
| import streamlit as st | |
| # URL of the Flask backend deployed in the Hugging Face Docker space | |
| API_URL = "https://shravanda-extraalearnleadbackend.hf.space" | |
| st.set_page_config(page_title="ExtraaLearn Lead Conversion Predictor", page_icon="๐", layout="centered") | |
| st.title("๐ ExtraaLearn Lead Conversion Predictor") | |
| st.write("Predict whether a lead is likely to convert into a paid customer, so the sales team " | |
| "can prioritise follow-ups.") | |
| tab_single, tab_batch = st.tabs(["Single lead", "Batch scoring (CSV)"]) | |
| with tab_single: | |
| with st.form("lead_form"): | |
| c1, c2 = st.columns(2) | |
| with c1: | |
| age = st.number_input("Age", min_value=18, max_value=100, value=45) | |
| current_occupation = st.selectbox("Current occupation", ["Professional", "Unemployed", "Student"]) | |
| first_interaction = st.selectbox("First interaction", ["Website", "Mobile App"]) | |
| profile_completed = st.selectbox("Profile completed", ["High", "Medium", "Low"], | |
| help="Low: 0-50%, Medium: 50-75%, High: 75-100%") | |
| last_activity = st.selectbox("Last activity", ["Email Activity", "Phone Activity", "Website Activity"]) | |
| with c2: | |
| website_visits = st.number_input("Website visits", min_value=0, max_value=100, value=3) | |
| time_spent_on_website = st.number_input("Time spent on website (seconds)", min_value=0, | |
| max_value=10000, value=600) | |
| page_views_per_visit = st.number_input("Page views per visit", min_value=0.0, max_value=50.0, | |
| value=2.8, step=0.1) | |
| st.markdown("**How did the lead hear about ExtraaLearn?**") | |
| f1, f2, f3, f4, f5 = st.columns(5) | |
| print_media_type1 = f1.checkbox("Newspaper") | |
| print_media_type2 = f2.checkbox("Magazine") | |
| digital_media = f3.checkbox("Digital ads") | |
| educational_channels = f4.checkbox("Edu channels") | |
| referral = f5.checkbox("Referral") | |
| submitted = st.form_submit_button("Predict", type="primary") | |
| if submitted: | |
| yes_no = lambda flag: "Yes" if flag else "No" | |
| lead = { | |
| "age": int(age), "current_occupation": current_occupation, "first_interaction": first_interaction, | |
| "profile_completed": profile_completed, "website_visits": int(website_visits), | |
| "time_spent_on_website": int(time_spent_on_website), | |
| "page_views_per_visit": float(page_views_per_visit), "last_activity": last_activity, | |
| "print_media_type1": yes_no(print_media_type1), "print_media_type2": yes_no(print_media_type2), | |
| "digital_media": yes_no(digital_media), "educational_channels": yes_no(educational_channels), | |
| "referral": yes_no(referral), | |
| } | |
| try: | |
| response = requests.post(f"{API_URL}/v1/lead", json=lead, timeout=60) | |
| response.raise_for_status() | |
| result = response.json() | |
| prob = result["conversion_probability"] | |
| if result["prediction"] == 1: | |
| st.success(f"โ **Likely to convert**: conversion probability {prob:.0%}. " | |
| "Prioritise this lead for a call from a counsellor.") | |
| else: | |
| st.warning(f"โ ๏ธ **Unlikely to convert**: conversion probability {prob:.0%}. " | |
| "Keep nurturing through low-cost channels (email, content).") | |
| st.progress(prob) | |
| except requests.RequestException as e: | |
| st.error(f"Could not reach the prediction API: {e}") | |
| with tab_batch: | |
| st.write("Upload a CSV with the same columns as the ExtraaLearn leads data (an `ID` column is optional).") | |
| uploaded = st.file_uploader("Leads CSV", type="csv") | |
| if uploaded is not None and st.button("Score leads", type="primary"): | |
| try: | |
| response = requests.post(f"{API_URL}/v1/leadbatch", | |
| files={"file": (uploaded.name, uploaded.getvalue(), "text/csv")}, | |
| timeout=120) | |
| response.raise_for_status() | |
| scored = pd.DataFrame(response.json()).sort_values("conversion_probability", ascending=False) | |
| st.metric("Leads likely to convert", f"{scored['prediction'].sum()} of {len(scored)}") | |
| st.dataframe(scored, use_container_width=True, hide_index=True) | |
| st.download_button("Download scored leads", scored.to_csv(index=False), | |
| file_name="scored_leads.csv", mime="text/csv") | |
| except requests.RequestException as e: | |
| st.error(f"Could not reach the prediction API: {e}") | |