Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +16 -26
src/streamlit_app.py
CHANGED
|
@@ -2,47 +2,37 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
#
|
| 6 |
cache_dir = os.path.join(os.getcwd(), "hf_cache")
|
| 7 |
os.makedirs(cache_dir, exist_ok=True)
|
| 8 |
os.environ["TRANSFORMERS_CACHE"] = cache_dir
|
| 9 |
os.environ["HF_HOME"] = cache_dir
|
| 10 |
|
| 11 |
-
#
|
| 12 |
pipe = pipeline(
|
| 13 |
"text-classification",
|
| 14 |
-
model="t-Shr/SPAM_OR_HAM_SMS"
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# π§ Prediction function
|
| 18 |
def predict(text):
|
| 19 |
trust_score = 0.5
|
| 20 |
output = pipe(text)[0]
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
label
|
| 26 |
-
return label, round(model_prob, 4), round(fused_score, 4), risk_score
|
| 27 |
|
| 28 |
-
# π Streamlit UI
|
| 29 |
st.set_page_config(page_title="SMS Spam Detector", layout="centered")
|
| 30 |
st.title("π© Real-Time SMS Spam Detector")
|
| 31 |
-
st.
|
| 32 |
-
|
| 33 |
-
sms_text = st.text_area("βοΈ Enter SMS Text:", height=150)
|
| 34 |
|
| 35 |
if st.button("π Predict"):
|
| 36 |
-
if
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
else:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
if label == "SPAM":
|
| 42 |
-
st.markdown("### π₯ Prediction: **`SPAM`**")
|
| 43 |
-
else:
|
| 44 |
-
st.markdown("### π© Prediction: **`NOT SPAM`**")
|
| 45 |
-
|
| 46 |
-
st.metric("π Model Confidence", f"{confidence:.2f}")
|
| 47 |
-
st.metric("π Fused Score", f"{fused_score:.2f}")
|
| 48 |
-
st.metric("β οΈ Risk Score", f"{risk_score}/100")
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Redirect cache
|
| 6 |
cache_dir = os.path.join(os.getcwd(), "hf_cache")
|
| 7 |
os.makedirs(cache_dir, exist_ok=True)
|
| 8 |
os.environ["TRANSFORMERS_CACHE"] = cache_dir
|
| 9 |
os.environ["HF_HOME"] = cache_dir
|
| 10 |
|
| 11 |
+
# Load model (ensure it's public or token is handled securely)
|
| 12 |
pipe = pipeline(
|
| 13 |
"text-classification",
|
| 14 |
+
model="t-Shr/SPAM_OR_HAM_SMS" # π make sure it's public or token is handled
|
| 15 |
)
|
| 16 |
|
|
|
|
| 17 |
def predict(text):
|
| 18 |
trust_score = 0.5
|
| 19 |
output = pipe(text)[0]
|
| 20 |
+
prob = output['score'] if output['label'] == 'LABEL_1' else 1 - output['score']
|
| 21 |
+
fused = 0.7 * prob + 0.3 * (1 - trust_score)
|
| 22 |
+
risk = int(round(fused * 100))
|
| 23 |
+
label = "SPAM" if fused >= 0.5 else "NOT SPAM"
|
| 24 |
+
return label, round(prob, 4), round(fused, 4), risk
|
|
|
|
| 25 |
|
|
|
|
| 26 |
st.set_page_config(page_title="SMS Spam Detector", layout="centered")
|
| 27 |
st.title("π© Real-Time SMS Spam Detector")
|
| 28 |
+
sms = st.text_area("βοΈ Enter SMS:", height=150)
|
|
|
|
|
|
|
| 29 |
|
| 30 |
if st.button("π Predict"):
|
| 31 |
+
if sms.strip():
|
| 32 |
+
label, prob, fused, risk = predict(sms)
|
| 33 |
+
st.markdown(f"### {'π₯' if label == 'SPAM' else 'π©'} Prediction: `{label}`")
|
| 34 |
+
st.metric("Confidence", f"{prob:.2f}")
|
| 35 |
+
st.metric("Fused Score", f"{fused:.2f}")
|
| 36 |
+
st.metric("Risk Score", f"{risk}/100")
|
| 37 |
else:
|
| 38 |
+
st.warning("Please enter SMS text.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|