Task-273 Implemented a basic chat-style UI for querying a model
Browse files- Pipfile +1 -0
- Pipfile.lock +17 -1
- app.py +91 -2
Pipfile
CHANGED
@@ -7,6 +7,7 @@ name = "pypi"
|
|
7 |
streamlit = "*"
|
8 |
pandas = "*"
|
9 |
numpy = "*"
|
|
|
10 |
|
11 |
[dev-packages]
|
12 |
|
|
|
7 |
streamlit = "*"
|
8 |
pandas = "*"
|
9 |
numpy = "*"
|
10 |
+
st-annotated-text = "*"
|
11 |
|
12 |
[dev-packages]
|
13 |
|
Pipfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
{
|
2 |
"_meta": {
|
3 |
"hash": {
|
4 |
-
"sha256": "
|
5 |
},
|
6 |
"pipfile-spec": 6,
|
7 |
"requires": {
|
@@ -186,6 +186,13 @@
|
|
186 |
"markers": "python_version >= '3.7'",
|
187 |
"version": "==3.1.44"
|
188 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
"idna": {
|
190 |
"hashes": [
|
191 |
"sha256:12f65c9b470abda6dc35cf8e63cc574b1c52b11df2c86030af0ac09b01b13ea9",
|
@@ -750,6 +757,15 @@
|
|
750 |
"markers": "python_version >= '3.7'",
|
751 |
"version": "==5.0.2"
|
752 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
753 |
"streamlit": {
|
754 |
"hashes": [
|
755 |
"sha256:62026dbdcb482790933f658b096d7dd58fa70da89c1f06fbc3658b91dcd4dab2",
|
|
|
1 |
{
|
2 |
"_meta": {
|
3 |
"hash": {
|
4 |
+
"sha256": "b114c11edb911ee8c918ef91522f2a4e6895019b452b37a052da3b4deb4c456e"
|
5 |
},
|
6 |
"pipfile-spec": 6,
|
7 |
"requires": {
|
|
|
186 |
"markers": "python_version >= '3.7'",
|
187 |
"version": "==3.1.44"
|
188 |
},
|
189 |
+
"htbuilder": {
|
190 |
+
"hashes": [
|
191 |
+
"sha256:58c0bc5502c1a46b42ae9e074c43ec0f6fdc24ed334936cb17e1ed5a8938aee2"
|
192 |
+
],
|
193 |
+
"markers": "python_version >= '3.7'",
|
194 |
+
"version": "==0.9.0"
|
195 |
+
},
|
196 |
"idna": {
|
197 |
"hashes": [
|
198 |
"sha256:12f65c9b470abda6dc35cf8e63cc574b1c52b11df2c86030af0ac09b01b13ea9",
|
|
|
757 |
"markers": "python_version >= '3.7'",
|
758 |
"version": "==5.0.2"
|
759 |
},
|
760 |
+
"st-annotated-text": {
|
761 |
+
"hashes": [
|
762 |
+
"sha256:712c45821f020eccafad3a58c10b9d2d51d449f4db999a06308ecf39a753a4df",
|
763 |
+
"sha256:b0134dcf734697cc3dbdb11862c4174071e7fb6f365af55a37c710d357fd88a5"
|
764 |
+
],
|
765 |
+
"index": "pypi",
|
766 |
+
"markers": "python_version >= '3.5'",
|
767 |
+
"version": "==4.0.2"
|
768 |
+
},
|
769 |
"streamlit": {
|
770 |
"hashes": [
|
771 |
"sha256:62026dbdcb482790933f658b096d7dd58fa70da89c1f06fbc3658b91dcd4dab2",
|
app.py
CHANGED
@@ -1,4 +1,93 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from annotated_text import annotated_text, annotation
|
3 |
+
import time
|
4 |
+
from random import randint, uniform
|
5 |
|
6 |
+
results_container = None
|
7 |
+
|
8 |
+
def output_results(res):
|
9 |
+
label_dict = {
|
10 |
+
'Gender': '#4A90E2',
|
11 |
+
'Race': '#E67E22',
|
12 |
+
'Sexuality': '#3B9C5A',
|
13 |
+
'Disability': '#8B5E3C',
|
14 |
+
'Religion': '#A347BA',
|
15 |
+
'Unspecified': '#A0A0A0'
|
16 |
+
|
17 |
+
}
|
18 |
+
with rc:
|
19 |
+
with st.chat_message(name = 'human', avatar=None):
|
20 |
+
at_list = []
|
21 |
+
if res['numerical_sentiment'] == 1:
|
22 |
+
# st.markdown('##### Category Results:')
|
23 |
+
for entry in res['category_sentiments'].keys():
|
24 |
+
if randint(0, 1) == 1:
|
25 |
+
val = res['category_sentiments'][entry]
|
26 |
+
perc = val * 100
|
27 |
+
at_list.append((entry, f'{perc:.2f}%', label_dict[entry]))
|
28 |
+
at_list.append(' ')
|
29 |
+
|
30 |
+
st.markdown(f"#### Text - *\"{res['raw_text']}\"*")
|
31 |
+
st.markdown(f"#### Classification - {':red' if res['numerical_sentiment'] == 1 else ':green'}[{res['text_sentiment']}]")
|
32 |
+
|
33 |
+
if len(at_list) > 0:
|
34 |
+
# st.html(annotated_text(at_list))
|
35 |
+
annotated_text(at_list)
|
36 |
+
|
37 |
+
def test_results(text):
|
38 |
+
test_val = int(randint(0, 1))
|
39 |
+
res_obj = {
|
40 |
+
'raw_text': text,
|
41 |
+
'text_sentiment': 'Discriminatory' if test_val == 1 else 'Non-Discriminatory',
|
42 |
+
'numerical_sentiment': test_val,
|
43 |
+
'category_sentiments': {
|
44 |
+
'Gender': None if test_val == 0 else uniform(0.0, 1.0),
|
45 |
+
'Race': None if test_val == 0 else uniform(0.0, 1.0),
|
46 |
+
'Sexuality': None if test_val == 0 else uniform(0.0, 1.0),
|
47 |
+
'Disability': None if test_val == 0 else uniform(0.0, 1.0),
|
48 |
+
'Religion': None if test_val == 0 else uniform(0.0, 1.0),
|
49 |
+
'Unspecified': None if test_val == 0 else uniform(0.0, 1.0)
|
50 |
+
}
|
51 |
+
}
|
52 |
+
return res_obj
|
53 |
+
|
54 |
+
|
55 |
+
def analyze_text(text):
|
56 |
+
res = None
|
57 |
+
with rc:
|
58 |
+
with st.spinner("Processing...", show_time=True) as spnr:
|
59 |
+
time.sleep(5)
|
60 |
+
res = test_results(text)
|
61 |
+
del spnr
|
62 |
+
|
63 |
+
if res is not None:
|
64 |
+
st.session_state.results.append(res)
|
65 |
+
output_results(res)
|
66 |
+
|
67 |
+
pri_container = st.container()
|
68 |
+
cols = pri_container.columns([1, 8, 1])
|
69 |
+
cols[1].subheader('NLPinitiative - Discriminatory Text Classifier')
|
70 |
+
|
71 |
+
pri_container.markdown(
|
72 |
+
"""The NLPinitiative Discriminatory Text Classifier is an advanced
|
73 |
+
natural language processing tool designed to detect and flag potentially
|
74 |
+
discriminatory or harmful language. By analyzing text for biased, offensive,
|
75 |
+
or exclusionary content, this classifier helps promote more inclusive and
|
76 |
+
respectful communication. Simply enter your text below, and the model will
|
77 |
+
assess it based on linguistic patterns and context. While the tool provides
|
78 |
+
valuable insights, we encourage users to review flagged content thoughtfully
|
79 |
+
and consider context when interpreting results."""
|
80 |
+
)
|
81 |
+
|
82 |
+
st.divider()
|
83 |
+
rc = st.container()
|
84 |
+
|
85 |
+
if "results" not in st.session_state:
|
86 |
+
st.session_state.results = []
|
87 |
+
|
88 |
+
with rc:
|
89 |
+
for result in st.session_state.results:
|
90 |
+
output_results(result)
|
91 |
+
|
92 |
+
if entry := st.chat_input('Enter text to classify'):
|
93 |
+
analyze_text(entry)
|