t-Shr commited on
Commit
e637464
Β·
verified Β·
1 Parent(s): 6bbeead

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +22 -27
src/streamlit_app.py CHANGED
@@ -1,52 +1,47 @@
1
  import os
2
  import streamlit as st
3
  from transformers import pipeline
 
4
 
5
- # 🧩 Ensure cache is writable (for Docker or hosted env)
6
  os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
7
  os.environ["HF_HOME"] = "/tmp/huggingface"
8
 
9
- from huggingface_hub import login
10
- login(token=os.environ.get("HF_TOKEN"))
 
11
 
12
- # πŸ” Load pipeline using Hugging Face token
13
  pipe = pipeline(
14
  "text-classification",
15
- model="t-Shr/SPAM_OR_HAM_SMS",
16
- use_auth_token=os.environ.get("HF_TOKEN") # Make sure HF_TOKEN is set
17
  )
18
 
19
- # 🧠 Prediction function
20
- def predict(text, trust_score=0.5):
 
21
  output = pipe(text)[0]
22
- label_raw = output['label']
23
- model_prob = output['score'] if label_raw == 'LABEL_1' else 1 - output['score']
24
-
25
  alpha = 0.7
26
- fused = alpha * model_prob + (1 - alpha) * (1 - trust_score)
27
- risk = int(round(fused * 100))
28
- label = "SPAM" if fused >= 0.5 else "NOT SPAM"
 
29
 
30
- return label, round(model_prob, 4), round(fused, 4), risk
31
-
32
- # 🌐 Streamlit UI setup
33
  st.set_page_config(page_title="SMS Spam Detector", layout="centered")
34
  st.title("πŸ“© Real-Time SMS Spam Detector")
 
35
 
36
- st.markdown("Enter an SMS message to check if it's likely spam. The system will show:")
37
- st.markdown("- **πŸ“Š Model Confidence**")
38
- st.markdown("- **πŸ” Fused Score**")
39
- st.markdown("- **⚠️ Risk Score out of 100**")
40
-
41
- # πŸ“ Text input
42
  sms_text = st.text_area("βœ‰οΈ Enter SMS Text:", height=150)
43
 
44
- # πŸ” Predict button
45
  if st.button("πŸ” Predict"):
46
  if sms_text.strip() == "":
47
  st.warning("Please enter some text.")
48
  else:
49
- label, confidence, fused_score, risk_score = predict(sms_text)
50
 
51
  if label == "SPAM":
52
  st.markdown("### πŸŸ₯ Prediction: **`SPAM`**")
@@ -54,5 +49,5 @@ if st.button("πŸ” Predict"):
54
  st.markdown("### 🟩 Prediction: **`NOT SPAM`**")
55
 
56
  st.metric("πŸ“Š Model Confidence", f"{confidence:.2f}")
57
- st.metric("πŸ” Fused Score", f"{fused_score:.2f}")
58
- st.metric("⚠️ Risk Score", f"{risk_score}/100")
 
1
  import os
2
  import streamlit as st
3
  from transformers import pipeline
4
+ from huggingface_hub import login
5
 
6
+ # Optional if running in container: ensure HF cache is writeable
7
  os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
8
  os.environ["HF_HOME"] = "/tmp/huggingface"
9
 
10
+ # βœ… Login once; not needed if you're already logged in via CLI
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
+ # 🧠 Spam Prediction Logic
21
+ def predict(text):
22
+ trust_score = 0.5 # Fixed for now
23
  output = pipe(text)[0]
24
+ model_prob = output['score'] if output['label'] == 'LABEL_1' else 1 - output['score']
 
 
25
  alpha = 0.7
26
+ fused_score = alpha * model_prob + (1 - alpha) * (1 - trust_score)
27
+ risk_score = int(round(fused_score * 100))
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
+ # 🎨 UI Config
 
 
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, fused, risk = predict(sms_text)
45
 
46
  if label == "SPAM":
47
  st.markdown("### πŸŸ₯ Prediction: **`SPAM`**")
 
49
  st.markdown("### 🟩 Prediction: **`NOT SPAM`**")
50
 
51
  st.metric("πŸ“Š Model Confidence", f"{confidence:.2f}")
52
+ st.metric("πŸ” Fused Score", f"{fused:.2f}")
53
+ st.metric("⚠️ Risk Score", f"{risk}/100")