spamorham_SMS / src /streamlit_app.py
t-Shr's picture
Update src/streamlit_app.py
f382f8d verified
Raw
History Blame Contribute Delete
1.35 kB
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.")