Spaces:
Runtime error
Runtime error
Commit
·
c79a5e4
1
Parent(s):
7166f8b
update
Browse files
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 |
-
|
|
|
|
|
|
|
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):
|
24 |
formatted += f"- Token: `{token}`\n"
|
25 |
for label, score in labels.items():
|
26 |
formatted += f" - Label: `{label}`, Score: `{score}`\n"
|
27 |
-
else:
|
28 |
formatted += f"- Label: `{token}`, Score: `{labels}`\n"
|
29 |
-
|
|
|
|
|
|
|
|
|
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.
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
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 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
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 |
|