Zekun Wu commited on
Commit
9385cae
1 Parent(s): d1ba853
Files changed (2) hide show
  1. app.py +40 -10
  2. bias_detector/bias_detector.py +2 -2
app.py CHANGED
@@ -3,10 +3,6 @@ from bias_detector import Detector
3
 
4
  st.title("Multidimensional Multilevel Bias Detection")
5
 
6
- level = st.selectbox("Select the Bias Levels:", ("Token","Sentence"))
7
- dimension = st.selectbox("Select the Bias Dimensions:", ("All","Gender","Religion","Race","Profession"))
8
- detector = Detector(level,dimension)
9
- target_sentence = st.text_input("Input the sentence you want to detect:")
10
 
11
  def format_results(results):
12
  formatted = ""
@@ -14,13 +10,47 @@ def format_results(results):
14
  for text, pred in result.items():
15
  formatted += f"**Text**: {text}\n\n"
16
  formatted += "**Predictions**:\n"
17
- for token, labels in pred.items():
18
- formatted += f"- Token: `{token}`\n"
19
- for label, score in labels.items():
20
- formatted += f" - Label: `{label}`, Score: `{score}`\n"
 
 
 
 
 
 
 
21
  return formatted
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  if st.button("Detect"):
24
  results = detector.predict([target_sentence])
25
- formatted_results = format_results(results)
26
- st.markdown(f"## Detection Results: \n\n {formatted_results}")
 
 
 
 
 
 
 
 
 
3
 
4
  st.title("Multidimensional Multilevel Bias Detection")
5
 
 
 
 
 
6
 
7
  def format_results(results):
8
  formatted = ""
 
10
  for text, pred in result.items():
11
  formatted += f"**Text**: {text}\n\n"
12
  formatted += "**Predictions**:\n"
13
+ # Check if pred is a dictionary
14
+ if isinstance(pred, dict):
15
+ for token, labels in pred.items():
16
+ if isinstance(labels, dict): # handling token-level output
17
+ formatted += f"- Token: `{token}`\n"
18
+ for label, score in labels.items():
19
+ formatted += f" - Label: `{label}`, Score: `{score}`\n"
20
+ else: # handling sentence-level output when labels is not a dictionary
21
+ formatted += f"- Label: `{token}`, Score: `{labels}`\n"
22
+ else: # handling edge cases where pred is not a dictionary
23
+ formatted += f"Prediction score: {pred}\n"
24
  return formatted
25
 
26
+
27
+
28
+ level = st.selectbox("Select the Bias Levels:", ("Token","Sentence"))
29
+ dimension = st.selectbox("Select the Bias Dimensions:", ("All","Gender","Religion","Race","Profession"))
30
+
31
+ detector = Detector(level,dimension)
32
+
33
+ if st.button("Load Models"):
34
+ st.text("Loading models...")
35
+
36
+ dummy_sentence = "This is a dummy sentence."
37
+ dummy_result = detector.predict([dummy_sentence]) # perform a dummy prediction
38
+ if dummy_result:
39
+ st.text("Models loaded successfully!")
40
+ else:
41
+ st.text("Failed to load models. Please check the server and/or model parameters.")
42
+
43
+ target_sentence = st.text_input("Input the sentence you want to detect:")
44
+
45
  if st.button("Detect"):
46
  results = detector.predict([target_sentence])
47
+ if results:
48
+ formatted_results = format_results(results)
49
+ st.markdown(f"## Detection Results: \n\n {formatted_results}")
50
+ else:
51
+ st.text("Prediction failed. Please check the input and try again.")
52
+
53
+
54
+
55
+
56
+
bias_detector/bias_detector.py CHANGED
@@ -26,7 +26,7 @@ class Detector:
26
  "Religion": "wu981526092/Token-Level-Religion-Bias-Detector",
27
  },
28
  "Sentence": {
29
- "All":None,
30
  "Religion": "wu981526092/Sentence-Level-Religion-Bias-Detector",
31
  "Profession": "wu981526092/Sentence-Level-Profession-Bias-Detector",
32
  "Race": "wu981526092/Sentence-Level-Race-Bias-Detector",
@@ -67,7 +67,7 @@ class Detector:
67
  # Create the API endpoint from the model path
68
  self.API_URL = f"https://api-inference.huggingface.co/models/{self.model_path}"
69
  API_token = os.getenv("BIAS_DETECTOR_API_KEY")
70
- #API_token = "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"
71
  # Add authorization token (if required)
72
  self.headers = {"Authorization": f"Bearer {API_token}"} # Replace `your_api_token` with your token
73
 
 
26
  "Religion": "wu981526092/Token-Level-Religion-Bias-Detector",
27
  },
28
  "Sentence": {
29
+ "All": "wu981526092/Sentence-Level-Multidimensional-Bias-Detector",
30
  "Religion": "wu981526092/Sentence-Level-Religion-Bias-Detector",
31
  "Profession": "wu981526092/Sentence-Level-Profession-Bias-Detector",
32
  "Race": "wu981526092/Sentence-Level-Race-Bias-Detector",
 
67
  # Create the API endpoint from the model path
68
  self.API_URL = f"https://api-inference.huggingface.co/models/{self.model_path}"
69
  API_token = os.getenv("BIAS_DETECTOR_API_KEY")
70
+ API_token = "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"
71
  # Add authorization token (if required)
72
  self.headers = {"Authorization": f"Bearer {API_token}"} # Replace `your_api_token` with your token
73