File size: 4,211 Bytes
120f3a5
377b74f
a189dd1
5e9bb68
82221ca
120f3a5
377b74f
a189dd1
 
 
 
aa4fbce
 
 
 
377b74f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e9bb68
 
 
 
 
 
 
 
 
 
377b74f
5e9bb68
377b74f
 
5e9bb68
 
 
82221ca
 
5e9bb68
 
 
 
 
 
 
82221ca
5e9bb68
a189dd1
5e9bb68
aa4fbce
a189dd1
 
 
 
 
 
 
 
 
 
 
 
5e9bb68
377b74f
a189dd1
 
 
 
 
 
 
aa4fbce
a189dd1
377b74f
 
 
 
 
 
 
 
 
 
a189dd1
 
aa4fbce
a189dd1
377b74f
 
aa4fbce
a189dd1
 
aa4fbce
 
377b74f
a189dd1
aa4fbce
 
377b74f
 
46e80bf
5e9bb68
 
 
 
 
 
 
 
377b74f
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import streamlit as st
import pandas as pd
from annotated_text import annotated_text
import time
from scripts.predict import InferenceHandler

history_df = pd.DataFrame(data=[], columns=['Text', 'Classification', 'Gender', 'Race', 'Sexuality', 'Disability', 'Religion', 'Unspecified'])
rc = None

@st.cache_data
def load_inference_handler(api_token):
    try:
        return InferenceHandler(api_token)
    except:
        return None

def extract_data(json_obj):
    row_data = []

    row_data.append(json_obj['raw_text'])
    row_data.append(json_obj['text_sentiment'])
    cat_dict = json_obj['category_sentiments']
    for cat in cat_dict.keys():
        raw_val = cat_dict[cat]
        val = f'{raw_val * 100: .2f}%' if raw_val is not None else 'N/A'
        row_data.append(val)
    
    return row_data

def load_history():
    for result in st.session_state.results:
        history_df.loc[len(history_df)] = extract_data(result)

def output_results(res):
    label_dict = {
        'Gender': '#4A90E2',
        'Race': '#E67E22',
        'Sexuality': '#3B9C5A',  
        'Disability': '#8B5E3C',
        'Religion': '#A347BA',  
        'Unspecified': '#A0A0A0'
    }

    with rc:
        st.markdown('### Results')
        with st.container(border=True):
            at_list = []
            if res['numerical_sentiment'] == 1:
                for entry in res['category_sentiments'].keys():
                    val = res['category_sentiments'][entry]
                    if val > 0.0:
                        perc = val * 100
                        at_list.append((entry, f'{perc:.2f}%', label_dict[entry]))

            st.markdown(f"#### Text - *\"{res['raw_text']}\"*")
            st.markdown(f"#### Classification - {':red' if res['numerical_sentiment'] == 1 else ':green'}[{res['text_sentiment']}]")

            if len(at_list) > 0:
                annotated_text(at_list)

@st.cache_data
def analyze_text(text):
    st.write(f'Text: {text}')
    if ih:
        res = None
        with rc:
            with st.spinner("Processing...", show_time=True) as spnr:
                time.sleep(5)
                res = ih.classify_text(text)
                del spnr

        if res is not None:
            st.session_state.results.append(res)
            history_df.loc[-1] = extract_data(res)
            output_results(res)

st.title('NLPinitiative Text Classifier')

st.sidebar.write("")
API_KEY = st.sidebar.text_input(
    "Enter your HuggingFace API Token",
    help="You can get your free API token in your settings page: https://huggingface.co/settings/tokens",
    type="password",
)
ih = load_inference_handler(API_KEY)

tab1, tab2 = st.tabs(['Classifier', 'About This App'])

if "results" not in st.session_state:
    st.session_state.results = []
    
load_history()

with tab1:
    "Text Classifier for determining if entered text is discriminatory (and the categories of discrimination) or Non-Discriminatory."

    hist_container = st.container()
    hist_expander = hist_container.expander('History')

    rc = st.container()
    text_form = st.form(key='classifier', clear_on_submit=True, enter_to_submit=True)
    with text_form:
        entry = None
        text_area = st.text_area('Enter text to classify', value='', disabled=True if ih is None else False)
        form_btn = st.form_submit_button('submit', disabled=True if ih is None else False)
        if form_btn and text_area is not None and len(text_area) > 0:
            analyze_text(text_area)

    with hist_expander:
        st.dataframe(history_df, hide_index=True)
    

with tab2:
    st.markdown(
    """The NLPinitiative Discriminatory Text Classifier is an advanced 
    natural language processing tool designed to detect and flag potentially 
    discriminatory or harmful language. By analyzing text for biased, offensive, 
    or exclusionary content, this classifier helps promote more inclusive and 
    respectful communication. Simply enter your text below, and the model will 
    assess it based on linguistic patterns and context. While the tool provides 
    valuable insights, we encourage users to review flagged content thoughtfully 
    and consider context when interpreting results."""
)