Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +12 -17
src/streamlit_app.py
CHANGED
|
@@ -1,25 +1,22 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
from transformers import pipeline
|
| 4 |
-
from huggingface_hub import login
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
os.
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# β
|
| 11 |
-
# Remove this line if using HF Spaces or if already logged in
|
| 12 |
-
# login(token=os.environ.get("HF_TOKEN"))
|
| 13 |
-
|
| 14 |
-
# β
Load model (no use_auth_token needed if logged in)
|
| 15 |
pipe = pipeline(
|
| 16 |
"text-classification",
|
| 17 |
model="t-Shr/SPAM_OR_HAM_SMS"
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# π§
|
| 21 |
def predict(text):
|
| 22 |
-
trust_score = 0.5
|
| 23 |
output = pipe(text)[0]
|
| 24 |
model_prob = output['score'] if output['label'] == 'LABEL_1' else 1 - output['score']
|
| 25 |
alpha = 0.7
|
|
@@ -28,20 +25,18 @@ def predict(text):
|
|
| 28 |
label = "SPAM" if fused_score >= 0.5 else "NOT SPAM"
|
| 29 |
return label, round(model_prob, 4), round(fused_score, 4), risk_score
|
| 30 |
|
| 31 |
-
#
|
| 32 |
st.set_page_config(page_title="SMS Spam Detector", layout="centered")
|
| 33 |
st.title("π© Real-Time SMS Spam Detector")
|
| 34 |
st.markdown("Enter an SMS message to check if it's likely spam.")
|
| 35 |
|
| 36 |
-
# π User Input
|
| 37 |
sms_text = st.text_area("βοΈ Enter SMS Text:", height=150)
|
| 38 |
|
| 39 |
-
# π Prediction
|
| 40 |
if st.button("π Predict"):
|
| 41 |
if sms_text.strip() == "":
|
| 42 |
st.warning("Please enter some text.")
|
| 43 |
else:
|
| 44 |
-
label, confidence,
|
| 45 |
|
| 46 |
if label == "SPAM":
|
| 47 |
st.markdown("### π₯ Prediction: **`SPAM`**")
|
|
@@ -49,5 +44,5 @@ if st.button("π Predict"):
|
|
| 49 |
st.markdown("### π© Prediction: **`NOT SPAM`**")
|
| 50 |
|
| 51 |
st.metric("π Model Confidence", f"{confidence:.2f}")
|
| 52 |
-
st.metric("π Fused Score", f"{
|
| 53 |
-
st.metric("β οΈ Risk Score", f"{
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
from transformers import pipeline
|
|
|
|
| 4 |
|
| 5 |
+
# β
Set HF cache directory to a local writable path
|
| 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 pipeline (no token needed if logged in)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
model_prob = output['score'] if output['label'] == 'LABEL_1' else 1 - output['score']
|
| 22 |
alpha = 0.7
|
|
|
|
| 25 |
label = "SPAM" if fused_score >= 0.5 else "NOT SPAM"
|
| 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.markdown("Enter an SMS message to check if it's likely spam.")
|
| 32 |
|
|
|
|
| 33 |
sms_text = st.text_area("βοΈ Enter SMS Text:", height=150)
|
| 34 |
|
|
|
|
| 35 |
if st.button("π Predict"):
|
| 36 |
if sms_text.strip() == "":
|
| 37 |
st.warning("Please enter some text.")
|
| 38 |
else:
|
| 39 |
+
label, confidence, fused_score, risk_score = predict(sms_text)
|
| 40 |
|
| 41 |
if label == "SPAM":
|
| 42 |
st.markdown("### π₯ Prediction: **`SPAM`**")
|
|
|
|
| 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")
|