Spaces:
Sleeping
Sleeping
| import os | |
| import streamlit as st | |
| from transformers import pipeline | |
| # Redirect cache | |
| cache_dir = os.path.join(os.getcwd(), "/tmp/hf_cache") | |
| os.makedirs(cache_dir, exist_ok=True) | |
| os.environ["TRANSFORMERS_CACHE"] = cache_dir | |
| os.environ["HF_HOME"] = cache_dir | |
| # Load model (ensure it's public or token is handled securely) | |
| pipe = pipeline( | |
| "text-classification", | |
| model="t-Shr/SPAM_OR_HAM_SMS" # π make sure it's public or token is handled | |
| ) | |
| def predict(text): | |
| trust_score = 0.5 | |
| output = pipe(text)[0] | |
| prob = output['score'] if output['label'] == 'LABEL_1' else 1 - output['score'] | |
| fused = 0.7 * prob + 0.3 * (1 - trust_score) | |
| risk = int(round(fused * 100)) | |
| label = "SPAM" if fused >= 0.5 else "NOT SPAM" | |
| return label, round(prob, 4), round(fused, 4), risk | |
| st.set_page_config(page_title="SMS Spam Detector", layout="centered") | |
| st.title("π© Real-Time SMS Spam Detector") | |
| sms = st.text_area("βοΈ Enter SMS:", height=150) | |
| if st.button("π Predict"): | |
| if sms.strip(): | |
| label, prob, fused, risk = predict(sms) | |
| st.markdown(f"### {'π₯' if label == 'SPAM' else 'π©'} Prediction: `{label}`") | |
| st.metric("Confidence", f"{prob:.2f}") | |
| st.metric("Fused Score", f"{fused:.2f}") | |
| st.metric("Risk Score", f"{risk}/100") | |
| else: | |
| st.warning("Please enter SMS text.") | |