Spaces:
Sleeping
Sleeping
| 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""" | |
| <style> | |
| body {{ | |
| background-color: {bg_color}; | |
| }} | |
| </style> | |
| """, | |
| 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}") | |