wu981526092 commited on
Commit
c79a5e4
·
1 Parent(s): 7166f8b
Files changed (1) hide show
  1. app.py +26 -25
app.py CHANGED
@@ -9,55 +9,56 @@ EXAMPLES = {
9
  "Race": "Steve thinks singaporeans are short, but he doesn't know any better",
10
  "Profession": "The football player's draft card said he was tough.",
11
  "Input your own": ""
12
- # Add more examples as needed
13
  }
14
- def format_results(results):
 
 
 
15
  formatted = ""
16
  for result in results:
17
  for text, pred in result.items():
18
  formatted += f"**Text**: {text}\n\n"
19
  formatted += "**Predictions**:\n"
20
- # Check if pred is a dictionary
21
  if isinstance(pred, dict):
22
  for token, labels in pred.items():
23
- if isinstance(labels, dict): # handling token-level output
24
  formatted += f"- Token: `{token}`\n"
25
  for label, score in labels.items():
26
  formatted += f" - Label: `{label}`, Score: `{score}`\n"
27
- else: # handling sentence-level output when labels is not a dictionary
28
  formatted += f"- Label: `{token}`, Score: `{labels}`\n"
29
- else: # handling edge cases where pred is not a dictionary
 
 
 
 
30
  formatted += f"Prediction score: {pred}\n"
31
  return formatted
32
 
33
-
34
-
35
  level = st.selectbox("Select the Bias Levels:", ("Sentence","Token"))
36
  dimension = st.selectbox("Select the Bias Dimensions:", ("All","Gender","Religion","Race","Profession"))
37
 
38
- detector = Detector(level,dimension)
39
-
40
  if st.button("Load Models"):
41
- st.text("Loading models...")
42
-
43
- dummy_sentence = "This is a dummy sentence."
44
- dummy_result = detector.predict([dummy_sentence]) # perform a dummy prediction
45
- if dummy_result:
46
- st.text("Models loaded successfully!")
47
- else:
48
- st.text("Failed to load models. Please check the server and/or model parameters.")
49
 
50
  example_type = st.selectbox("Choose an example type:", list(EXAMPLES.keys()))
51
  target_sentence = st.text_input("Input the sentence you want to detect:", value=EXAMPLES[example_type])
52
 
53
  if st.button("Detect"):
54
- results = detector.predict([target_sentence])
55
- if results:
56
- formatted_results = format_results(results)
57
- st.markdown(f"## Detection Results: \n\n {formatted_results}")
58
- else:
59
- st.text("Prediction failed. Please check the input and try again.")
60
-
61
 
62
 
63
 
 
9
  "Race": "Steve thinks singaporeans are short, but he doesn't know any better",
10
  "Profession": "The football player's draft card said he was tough.",
11
  "Input your own": ""
 
12
  }
13
+ if "detector" not in st.session_state:
14
+ st.session_state["detector"] = None
15
+
16
+ def format_results(results, bias_level):
17
  formatted = ""
18
  for result in results:
19
  for text, pred in result.items():
20
  formatted += f"**Text**: {text}\n\n"
21
  formatted += "**Predictions**:\n"
 
22
  if isinstance(pred, dict):
23
  for token, labels in pred.items():
24
+ if isinstance(labels, dict):
25
  formatted += f"- Token: `{token}`\n"
26
  for label, score in labels.items():
27
  formatted += f" - Label: `{label}`, Score: `{score}`\n"
28
+ else:
29
  formatted += f"- Label: `{token}`, Score: `{labels}`\n"
30
+ if bias_level == "Sentence": # sort the labels only for sentence level bias
31
+ sorted_pred = dict(sorted(pred.items(), key=lambda item: item[1], reverse=True))
32
+ for label, score in sorted_pred.items():
33
+ formatted += f"- Label: `{label}`, Score: `{score}`\n"
34
+ else:
35
  formatted += f"Prediction score: {pred}\n"
36
  return formatted
37
 
 
 
38
  level = st.selectbox("Select the Bias Levels:", ("Sentence","Token"))
39
  dimension = st.selectbox("Select the Bias Dimensions:", ("All","Gender","Religion","Race","Profession"))
40
 
 
 
41
  if st.button("Load Models"):
42
+ with st.spinner('Loading models...'):
43
+ st.session_state["detector"] = Detector(level, dimension)
44
+ dummy_sentence = "This is a dummy sentence."
45
+ dummy_result = st.session_state["detector"].predict([dummy_sentence])
46
+ if dummy_result:
47
+ st.success("Models loaded successfully!")
48
+ else:
49
+ st.error("Failed to load models. Please check the server and/or model parameters.")
50
 
51
  example_type = st.selectbox("Choose an example type:", list(EXAMPLES.keys()))
52
  target_sentence = st.text_input("Input the sentence you want to detect:", value=EXAMPLES[example_type])
53
 
54
  if st.button("Detect"):
55
+ with st.spinner('Detecting...'):
56
+ results = st.session_state["detector"].predict([target_sentence])
57
+ if results:
58
+ formatted_results = format_results(results, level) # pass the selected bias level to the function
59
+ st.markdown(f"## Detection Results: \n\n {formatted_results}")
60
+ else:
61
+ st.error("Prediction failed. Please check the input and try again.")
62
 
63
 
64