Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app (2).py +102 -0
- requirements (2).txt +10 -0
app (2).py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
# Load trained model
|
| 6 |
+
model = joblib.load("grit_elite_predictor.pkl")
|
| 7 |
+
|
| 8 |
+
# Prediction function
|
| 9 |
+
def predict(total_spend, recency_days, used_discount, visit_time, num_items, num_prices, num_channels):
|
| 10 |
+
visit_morning = int(visit_time == "Morning (7–10am)")
|
| 11 |
+
visit_noon = int(visit_time == "Midday (10am–1pm)")
|
| 12 |
+
visit_afternoon = int(visit_time == "Afternoon (1–5pm)")
|
| 13 |
+
|
| 14 |
+
input_data = pd.DataFrame([{
|
| 15 |
+
'TotalSpend_Capped': total_spend,
|
| 16 |
+
'Recency': recency_days,
|
| 17 |
+
'DiscountPct': 0.20 if used_discount else 0.0,
|
| 18 |
+
'NumUniqueItems': num_items,
|
| 19 |
+
'NumUniquePricePoints': num_prices,
|
| 20 |
+
'NumUniqueChannels': num_channels,
|
| 21 |
+
'Visit_Morning': visit_morning,
|
| 22 |
+
'Visit_Noon': visit_noon,
|
| 23 |
+
'Visit_Afternoon': visit_afternoon,
|
| 24 |
+
}])
|
| 25 |
+
|
| 26 |
+
prob = model.predict_proba(input_data)[0][1]
|
| 27 |
+
pred = int(prob >= 0.25)
|
| 28 |
+
return f"Prediction: {'Elite' if pred == 1 else 'Non-Elite'} (Confidence: {prob:.2%})"
|
| 29 |
+
|
| 30 |
+
# Custom CSS
|
| 31 |
+
custom_css = """
|
| 32 |
+
body {
|
| 33 |
+
background-color: #fef6ee !important;
|
| 34 |
+
font-family: 'Raleway', sans-serif;
|
| 35 |
+
}
|
| 36 |
+
h1 {
|
| 37 |
+
text-align: center;
|
| 38 |
+
font-size: 32px;
|
| 39 |
+
color: #151515;
|
| 40 |
+
margin-top: 10px;
|
| 41 |
+
margin-bottom: 20px;
|
| 42 |
+
}
|
| 43 |
+
label, .gr-checkbox label, .gr-radio label {
|
| 44 |
+
color: white !important;
|
| 45 |
+
}
|
| 46 |
+
button {
|
| 47 |
+
background-color: #4b4b4b !important;
|
| 48 |
+
color: white !important;
|
| 49 |
+
}
|
| 50 |
+
#logo-container {
|
| 51 |
+
padding: 20px;
|
| 52 |
+
display: flex;
|
| 53 |
+
justify-content: center;
|
| 54 |
+
align-items: center;
|
| 55 |
+
}
|
| 56 |
+
#logo-container img {
|
| 57 |
+
max-height: 80px;
|
| 58 |
+
width: auto;
|
| 59 |
+
max-width: 100%;
|
| 60 |
+
object-fit: contain;
|
| 61 |
+
}
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
# App layout
|
| 65 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 66 |
+
gr.Markdown("<h1>Grit Elite Customer Predictor</h1>")
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
with gr.Column():
|
| 70 |
+
total_spend = gr.Slider(0, 500, step=10, label="Total Spend (First 90 Days)")
|
| 71 |
+
recency_days = gr.Slider(0, 90, step=1, label="Recency (Days Between First & Last Visit)")
|
| 72 |
+
used_discount = gr.Checkbox(label="Used Loyalty Discount?")
|
| 73 |
+
visit_time = gr.Radio(["Morning (7–10am)", "Midday (10am–1pm)", "Afternoon (1–5pm)"], label="Typical Visit Time")
|
| 74 |
+
num_items = gr.Slider(1, 15, step=1, label="Number of Unique Items")
|
| 75 |
+
num_prices = gr.Slider(1, 15, step=1, label="Unique Price Points Tried")
|
| 76 |
+
num_channels = gr.Slider(1, 4, step=1, label="Channels Used (POS, Mobile, Web, etc.)")
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
clear_btn = gr.Button("Clear")
|
| 80 |
+
submit_btn = gr.Button("Submit")
|
| 81 |
+
|
| 82 |
+
with gr.Column():
|
| 83 |
+
with gr.Group():
|
| 84 |
+
with gr.Row(elem_id="logo-container"):
|
| 85 |
+
gr.Image("GC-Logo-Gold2.png", show_label=False, show_download_button=False)
|
| 86 |
+
output = gr.Textbox(label="Prediction Output", interactive=False)
|
| 87 |
+
gr.HTML(
|
| 88 |
+
'<iframe style="border-radius:12px" src="https://open.spotify.com/embed/playlist/37A3Lq5UUDp7za3PzRUBed?utm_source=generator" width="100%" height="80" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>'
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
submit_btn.click(
|
| 92 |
+
fn=predict,
|
| 93 |
+
inputs=[total_spend, recency_days, used_discount, visit_time, num_items, num_prices, num_channels],
|
| 94 |
+
outputs=output
|
| 95 |
+
)
|
| 96 |
+
clear_btn.click(
|
| 97 |
+
fn=lambda: [0, 0, False, None, 1, 1, 1],
|
| 98 |
+
inputs=[],
|
| 99 |
+
outputs=[total_spend, recency_days, used_discount, visit_time, num_items, num_prices, num_channels]
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
demo.launch()
|
requirements (2).txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==3.44.1
|
| 2 |
+
Pillow
|
| 3 |
+
yake
|
| 4 |
+
pandas
|
| 5 |
+
scikit-learn
|
| 6 |
+
shap
|
| 7 |
+
xgboost
|
| 8 |
+
matplotlib
|
| 9 |
+
numpy
|
| 10 |
+
streamlit
|