Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +74 -34
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,80 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import requests
|
| 4 |
|
| 5 |
+
# Set the title and header of the app
|
| 6 |
+
st.set_page_config(page_title="ExtraaLearn Lead Conversion Predictor", layout="wide")
|
| 7 |
+
st.title("🚀 ExtraaLearn Lead Conversion Predictor")
|
| 8 |
+
st.markdown("### Predict whether a lead will convert to a paid customer.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# --- API Configuration ---
|
| 11 |
+
# REPLACE THIS WITH YOUR ACTUAL DEPLOYED API URL
|
| 12 |
+
API_URL = "https://<your-username>-<your-space-name>.hf.space/predict"
|
| 13 |
|
| 14 |
+
# --- User Input Form ---
|
| 15 |
+
st.header("Lead Information")
|
| 16 |
+
with st.form("lead_form"):
|
| 17 |
+
|
| 18 |
+
col1, col2, col3 = st.columns(3)
|
| 19 |
+
|
| 20 |
+
with col1:
|
| 21 |
+
age = st.slider("Age", 18, 65, 30)
|
| 22 |
+
current_occupation = st.radio("Current Occupation", ['Professional', 'Unemployed', 'Student'])
|
| 23 |
+
first_interaction = st.radio("First Interaction", ['Website', 'Mobile App'])
|
| 24 |
+
|
| 25 |
+
with col2:
|
| 26 |
+
profile_completed = st.selectbox("Profile Completed", ['Low', 'Medium', 'High'])
|
| 27 |
+
website_visits = st.slider("Website Visits", 0, 30, 3)
|
| 28 |
+
time_spent_on_website = st.slider("Time Spent on Website (seconds)", 0, 2600, 500)
|
| 29 |
+
|
| 30 |
+
with col3:
|
| 31 |
+
page_views_per_visit = st.number_input("Pages Viewed per Visit", 0.0, 20.0, 3.0)
|
| 32 |
+
last_activity = st.selectbox("Last Activity", ['Email Activity', 'Phone Activity', 'Website Activity'])
|
| 33 |
+
|
| 34 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
| 35 |
+
st.write("---")
|
| 36 |
+
st.subheader("Source of Information")
|
| 37 |
+
print_media_type1 = st.checkbox("Seen on Newspaper Ad?")
|
| 38 |
+
print_media_type2 = st.checkbox("Seen on Magazine Ad?")
|
| 39 |
+
digital_media = st.checkbox("Seen on Digital Ad?")
|
| 40 |
+
educational_channels = st.checkbox("Heard on Educational Channels?")
|
| 41 |
+
referral = st.checkbox("Referred by someone?")
|
| 42 |
|
| 43 |
+
# Submit button for the form
|
| 44 |
+
submit_button = st.form_submit_button(label='Predict Lead Conversion')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# --- Prediction Logic ---
|
| 47 |
+
if submit_button:
|
| 48 |
+
# Prepare data to be sent to the API
|
| 49 |
+
input_data = {
|
| 50 |
+
"age": age,
|
| 51 |
+
"current_occupation": current_occupation,
|
| 52 |
+
"first_interaction": first_interaction,
|
| 53 |
+
"profile_completed": profile_completed,
|
| 54 |
+
"website_visits": website_visits,
|
| 55 |
+
"time_spent_on_website": time_spent_on_website,
|
| 56 |
+
"page_views_per_visit": page_views_per_visit,
|
| 57 |
+
"last_activity": last_activity,
|
| 58 |
+
"print_media_type1": "Yes" if print_media_type1 else "No",
|
| 59 |
+
"print_media_type2": "Yes" if print_media_type2 else "No",
|
| 60 |
+
"digital_media": "Yes" if digital_media else "No",
|
| 61 |
+
"educational_channels": "Yes" if educational_channels else "No",
|
| 62 |
+
"referral": "Yes" if referral else "No"
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
with st.spinner("Analyzing lead data..."):
|
| 66 |
+
try:
|
| 67 |
+
response = requests.post(API_URL, json=input_data)
|
| 68 |
+
if response.status_code == 200:
|
| 69 |
+
prediction = response.json()
|
| 70 |
+
st.success("✅ Prediction Successful!")
|
| 71 |
+
|
| 72 |
+
# Display the prediction result
|
| 73 |
+
st.write(f"The model predicts this lead is **{prediction['prediction_label']}**.")
|
| 74 |
+
st.progress(int(prediction['probabilities']['Converted'] * 100), text="Conversion Probability")
|
| 75 |
+
st.info(f"Probability of Conversion: **{prediction['probabilities']['Converted']:.2f}**")
|
| 76 |
+
else:
|
| 77 |
+
st.error(f"❌ API call failed with status code: {response.status_code}")
|
| 78 |
+
st.json(response.json())
|
| 79 |
+
except requests.exceptions.RequestException as e:
|
| 80 |
+
st.error(f"❌ An error occurred while connecting to the API: {e}")
|