customer360AI / app.py
Vishu2006's picture
Upload 31 files
bfde63f verified
Raw History Blame Contribute Delete
4.06 kB
import os
import sys
import json
import gradio as gr
# Ensure project root is in sys.path
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
if PROJECT_ROOT not in sys.path:
sys.path.insert(0, PROJECT_ROOT)
from ml.inference.unified_inference import inference_service
LIVE_API_KEY = os.getenv("MODEL_API_KEY", "c360_live_key_9f8a2b7c4e1d")
def predict_gradio(
customer_id, name, recency_days, frequency_purchases, monetary_total_spend,
website_visits_30d, product_views_30d, cart_additions_30d, cart_abandonments_30d,
support_tickets_30d, engagement_change_pct, preferred_channel, preferred_category,
api_key_input
):
# Verify API Key
if api_key_input != LIVE_API_KEY:
return {"error": "Unauthorized: Invalid API Key. Please provide the correct X-API-Key."}
cust_dict = {
"customer_id": customer_id or "C1001",
"name": name or "Sample Customer",
"recency_days": int(recency_days),
"frequency_purchases": int(frequency_purchases),
"monetary_total_spend": float(monetary_total_spend),
"website_visits_30d": int(website_visits_30d),
"product_views_30d": int(product_views_30d),
"cart_additions_30d": int(cart_additions_30d),
"cart_abandonments_30d": int(cart_abandonments_30d),
"email_opens_30d": 5,
"email_clicks_30d": 2,
"support_tickets_30d": int(support_tickets_30d),
"engagement_change_pct": float(engagement_change_pct),
"preferred_channel": preferred_channel,
"preferred_category": preferred_category
}
res = inference_service.predict_customer_decision(cust_dict)
return res
# Build Gradio UI
with gr.Blocks(title="Customer360 AI -- Live Model Decision Studio") as demo:
gr.Markdown("# 🧠 Customer360 AI -- Live Model Decision Studio")
gr.Markdown("Real-time Churn Prediction, Purchase Propensity, Customer Lifetime Value & Next Best Action Engine.")
with gr.Row():
with gr.Column():
api_key_input = gr.Textbox(label="Live API Key", value="c360_live_key_9f8a2b7c4e1d", type="password")
customer_id = gr.Textbox(label="Customer ID", value="C1024")
name = gr.Textbox(label="Customer Name", value="Ananya Sharma")
recency_days = gr.Number(label="Days Inactive (Recency)", value=74)
frequency_purchases = gr.Number(label="Total Purchases", value=12)
monetary_total_spend = gr.Number(label="Lifetime Spend (INR)", value=45000)
website_visits_30d = gr.Number(label="30-Day Website Visits", value=3)
product_views_30d = gr.Number(label="30-Day Product Views", value=5)
cart_additions_30d = gr.Number(label="30-Day Cart Additions", value=3)
cart_abandonments_30d = gr.Number(label="30-Day Cart Abandonments", value=2)
support_tickets_30d = gr.Number(label="30-Day Support Tickets", value=3)
engagement_change_pct = gr.Number(label="Engagement Trend %", value=-65.0)
preferred_channel = gr.Dropdown(label="Preferred Channel", choices=["WhatsApp", "Email", "Push", "SMS"], value="WhatsApp")
preferred_category = gr.Dropdown(label="Preferred Category", choices=["Electronics", "Fashion", "Home & Kitchen", "Beauty & Care", "Sports & Fitness"], value="Electronics")
btn = gr.Button("🚀 Run Live ML Inference", variant="primary")
with gr.Column():
output_json = gr.JSON(label="ML Prediction & Next Best Action Result")
btn.click(
fn=predict_gradio,
inputs=[
customer_id, name, recency_days, frequency_purchases, monetary_total_spend,
website_visits_30d, product_views_30d, cart_additions_30d, cart_abandonments_30d,
support_tickets_30d, engagement_change_pct, preferred_channel, preferred_category,
api_key_input
],
outputs=output_json
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))