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

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # 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
@@ -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
- # 🎨 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,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"{fused:.2f}")
53
- st.metric("⚠️ Risk Score", f"{risk}/100")
 
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")