t-Shr commited on
Commit
eb129d7
Β·
verified Β·
1 Parent(s): b7714b4

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # βœ… 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
23
- fused_score = alpha * model_prob + (1 - alpha) * (1 - trust_score)
24
- risk_score = int(round(fused_score * 100))
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`**")
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.")