Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +45 -39
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,46 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Load the Hugging Face pipeline (with private token if needed)
|
| 6 |
+
pipe = pipeline(
|
| 7 |
+
"text-classification",
|
| 8 |
+
model="t-Shr/SPAM_OR_HAM_SMS", # Replace with your model path
|
| 9 |
+
use_auth_token=os.environ.get("HF_TOKEN") # or just paste the token as string if running locally
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
# π§ Custom prediction logic with trust score fusion
|
| 13 |
+
def predict(text, trust_score=0.5):
|
| 14 |
+
output = pipe(text)[0]
|
| 15 |
+
model_prob = output['score'] if output['label'] == 'LABEL_1' else 1 - output['score']
|
| 16 |
+
|
| 17 |
+
# Fused score = weighted confidence + inverse trust
|
| 18 |
+
alpha = 0.7
|
| 19 |
+
fused_score = alpha * model_prob + (1 - alpha) * (1 - trust_score)
|
| 20 |
+
risk_score = int(round(fused_score * 100))
|
| 21 |
+
label = "SPAM" if fused_score >= 0.5 else "HAM"
|
| 22 |
+
|
| 23 |
+
return label, round(model_prob, 4), round(fused_score, 4), risk_score
|
| 24 |
+
|
| 25 |
+
# Streamlit UI
|
| 26 |
+
st.set_page_config(page_title="π© SMS Spam Classifier", layout="centered")
|
| 27 |
+
st.title("π© Real-Time SMS Spam Classifier")
|
| 28 |
+
st.markdown("Detect whether an SMS is **spam** or **ham**, with model confidence, fused score and risk score.")
|
| 29 |
+
|
| 30 |
+
# Text input
|
| 31 |
+
sms_text = st.text_area("βοΈ Enter SMS Text:", height=150)
|
| 32 |
+
|
| 33 |
+
# Trust score slider
|
| 34 |
+
trust_score = st.slider("π Trust Score (user reliability)", 0.0, 1.0, 0.5, step=0.01)
|
| 35 |
+
|
| 36 |
+
# Predict button
|
| 37 |
+
if st.button("π Predict"):
|
| 38 |
+
if sms_text.strip() == "":
|
| 39 |
+
st.warning("Please enter some text to analyze.")
|
| 40 |
+
else:
|
| 41 |
+
label, confidence, fused_score, risk_score = predict(sms_text, trust_score)
|
| 42 |
+
|
| 43 |
+
st.markdown(f"### β
Prediction: `{label}`")
|
| 44 |
+
st.metric(label="π Model Confidence", value=f"{confidence:.2f}")
|
| 45 |
+
st.metric(label="π Fused Score", value=f"{fused_score:.2f}")
|
| 46 |
+
st.metric(label="β οΈ Risk Score", value=f"{risk_score}/100")
|