import streamlit as st import requests # ------------------------------- # Page Configuration # ------------------------------- st.set_page_config(page_title="Bot Detector", layout="centered") # ------------------------------- # Title and Description # ------------------------------- st.title("🕵️ Bot Click Detector") st.markdown("Detect fraudulent ad clicks using machine learning and interpret model decisions using SHAP explanations.") # ------------------------------- # Input Fields # ------------------------------- st.subheader("🔍 Enter Session Info") region_options = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"] browser_options = ["Brave", "Chrome", "Edge", "Safari", "UCBrowser"] device_options = ["Android", "iOS"] region = st.selectbox("🌍 Region", region_options, index=2) browser = st.selectbox("🌐 Browser", browser_options, index=1) device = st.selectbox("📱 Device", device_options, index=0) click_url = st.text_input( "🔗 Full Click URL", value="/ad_click?n=1&f=1&d=www.amazon.ca&sld=0&st=mobile_search_intl&nt=0&r=0&adx=none&adx_name=none&ttc=5314&q=sh%20rug%20gasmaker%20matty&ct=CA&kl=wt-wt&kp=-1" ) single_input = { "region": region, "browser": browser, "device": device, "d": click_url } # ------------------------------- # Prediction and Output # ------------------------------- if st.button("🚀 Predict"): with st.spinner("Sending data to model and generating SHAP explanation..."): try: response = requests.post( "https://Sheltonmaharesh-DuckDuckGoBackend.hf.space/v1/predict", json=single_input ) if response.status_code == 200: result = response.json() # 🎨 Change background based on prediction if result["Prediction"] == "Bot Attack": bg_color = "#ffe6e6" # Light red else: bg_color = "#e6ffe6" # Light green # Inject background color st.markdown( f""" """, unsafe_allow_html=True ) # 🧠 Display Results st.success(f"🧠 Prediction: {result['Prediction']}") st.markdown("### 🔍 SHAP Explanation") st.text(result["SHAP Explanation"]) else: st.error(f"❌ Backend returned error {response.status_code}: {response.text}") except Exception as e: st.error(f"🚨 Prediction failed: {e}")