azratuni commited on
Commit
60e3188
·
verified ·
1 Parent(s): 3029c6f

low-margin uncertainty detection

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +28 -7
src/streamlit_app.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
 
3
- # ✅ Set writable directories for cache and metrics
4
  os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf"
5
  os.environ["HF_HOME"] = "/tmp/hf"
6
  os.environ["STREAMLIT_HOME"] = "/tmp/.streamlit"
@@ -10,14 +10,15 @@ os.makedirs("/tmp/.streamlit", exist_ok=True)
10
  import streamlit as st
11
  from transformers import pipeline
12
 
13
- # 🔐 Load your Hugging Face token from Space secrets
14
  HF_TOKEN = os.getenv("HF_TOKEN")
15
 
16
  # 🔁 Load the private model using the token
17
  classifier = pipeline(
18
  "text-classification",
19
  model="azratuni/isl-classifier",
20
- token=HF_TOKEN
 
21
  )
22
 
23
  # 🧠 Streamlit UI
@@ -30,11 +31,31 @@ text_input = st.text_area("Input text", placeholder="e.g. Muslims are terrorists
30
  if st.button("Classify"):
31
  if text_input.strip():
32
  with st.spinner("Classifying..."):
33
- result = classifier(text_input)[0]
34
- label = result["label"] # Now already in friendly form
35
- score = result["score"]
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  st.success(f"**Prediction:** {label}")
38
- st.write(f"**Confidence:** {score:.2%}")
 
 
 
 
 
 
 
 
 
39
  else:
40
  st.warning("Please enter some text.")
 
1
  import os
2
 
3
+ # ✅ Fix: Use writable directories for cache and metrics
4
  os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf"
5
  os.environ["HF_HOME"] = "/tmp/hf"
6
  os.environ["STREAMLIT_HOME"] = "/tmp/.streamlit"
 
10
  import streamlit as st
11
  from transformers import pipeline
12
 
13
+ # 🔐 Load Hugging Face token from Space secrets
14
  HF_TOKEN = os.getenv("HF_TOKEN")
15
 
16
  # 🔁 Load the private model using the token
17
  classifier = pipeline(
18
  "text-classification",
19
  model="azratuni/isl-classifier",
20
+ token=HF_TOKEN,
21
+ return_all_scores=True # ✅ Return all class scores
22
  )
23
 
24
  # 🧠 Streamlit UI
 
31
  if st.button("Classify"):
32
  if text_input.strip():
33
  with st.spinner("Classifying..."):
34
+ scores = classifier(text_input)[0]
35
+ scores = sorted(scores, key=lambda x: x["score"], reverse=True)
36
+
37
+ top_label = scores[0]["label"]
38
+ top_score = scores[0]["score"]
39
+ second_score = scores[1]["score"]
40
+ margin = top_score - second_score
41
+
42
+ label_map = {
43
+ "LABEL_0": "Not Islamophobic",
44
+ "LABEL_1": "Islamophobic"
45
+ }
46
+
47
+ label = label_map.get(top_label, top_label)
48
 
49
  st.success(f"**Prediction:** {label}")
50
+ st.write(f"**Confidence:** {top_score:.2%}")
51
+
52
+ # ⚠️ Uncertainty warning if margin is low
53
+ if margin < 0.15:
54
+ st.warning("⚠️ The model is uncertain. Both classes received similar confidence scores.")
55
+
56
+ # 🧾 Optional: show both scores
57
+ st.markdown("**Score breakdown:**")
58
+ for s in scores:
59
+ st.write(f"- {label_map.get(s['label'], s['label'])}: {s['score']:.2%}")
60
  else:
61
  st.warning("Please enter some text.")