File size: 2,504 Bytes
40c82a6
 
 
33e27e7
40c82a6
0add53e
5580be6
 
 
 
0add53e
 
 
40c82a6
 
 
 
 
 
9385cae
 
 
 
 
 
 
 
 
 
 
40c82a6
 
9385cae
 
5580be6
9385cae
 
 
 
 
 
 
 
 
 
 
 
 
 
0add53e
 
9385cae
40c82a6
 
9385cae
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import streamlit as st
from bias_detector import Detector

st.title("Multidimensional Bias Multilevel Detection")

EXAMPLES = {
    "Gender": "The baby loved the presence of his caring mommy.",
    "Religion": "The orange church had a very good reputation.",
    "Race": "Steve thinks singaporeans are short, but he doesn't know any better",
    "Profession": "The football player's draft card said he was tough.",
    "Input your own": ""
    # Add more examples as needed
}
def format_results(results):
    formatted = ""
    for result in results:
        for text, pred in result.items():
            formatted += f"**Text**: {text}\n\n"
            formatted += "**Predictions**:\n"
            # Check if pred is a dictionary
            if isinstance(pred, dict):
                for token, labels in pred.items():
                    if isinstance(labels, dict):  # handling token-level output
                        formatted += f"- Token: `{token}`\n"
                        for label, score in labels.items():
                            formatted += f"    - Label: `{label}`, Score: `{score}`\n"
                    else:  # handling sentence-level output when labels is not a dictionary
                        formatted += f"- Label: `{token}`, Score: `{labels}`\n"
            else:  # handling edge cases where pred is not a dictionary
                formatted += f"Prediction score: {pred}\n"
    return formatted



level = st.selectbox("Select the Bias Levels:", ("Sentence","Token"))
dimension = st.selectbox("Select the Bias Dimensions:", ("All","Gender","Religion","Race","Profession"))

detector = Detector(level,dimension)

if st.button("Load Models"):
    st.text("Loading models...")

    dummy_sentence = "This is a dummy sentence."
    dummy_result = detector.predict([dummy_sentence])  # perform a dummy prediction
    if dummy_result:
        st.text("Models loaded successfully!")
    else:
        st.text("Failed to load models. Please check the server and/or model parameters.")

example_type = st.selectbox("Choose an example type:", list(EXAMPLES.keys()))
target_sentence = st.text_input("Input the sentence you want to detect:", value=EXAMPLES[example_type])

if st.button("Detect"):
    results = detector.predict([target_sentence])
    if results:
        formatted_results = format_results(results)
        st.markdown(f"## Detection Results: \n\n {formatted_results}")
    else:
        st.text("Prediction failed. Please check the input and try again.")