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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import streamlit as st
|
4 |
+
import json
|
5 |
+
import torch
|
6 |
+
from streamlit_option_menu import option_menu
|
7 |
+
import time
|
8 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
9 |
+
from streamlit_lottie import st_lottie
|
10 |
+
from streamlit_lottie import st_lottie_spinner
|
11 |
+
import speech_recognition as s
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
with st.sidebar:
|
17 |
+
selected = option_menu(
|
18 |
+
menu_title="Main Menu",
|
19 |
+
options=["Home","Information"]
|
20 |
+
)
|
21 |
+
|
22 |
+
################# Animation of the title
|
23 |
+
|
24 |
+
st.markdown(
|
25 |
+
"""
|
26 |
+
<style>
|
27 |
+
.sidebar{
|
28 |
+
background-image: linear-gradient(#2e7bcf,#2e7bcf);
|
29 |
+
color: white;
|
30 |
+
}
|
31 |
+
</style>
|
32 |
+
""",
|
33 |
+
unsafe_allow_html=True,
|
34 |
+
)
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
st.markdown(
|
39 |
+
f"""
|
40 |
+
<style>
|
41 |
+
.stApp {{
|
42 |
+
background-image: url("https://img.freepik.com/free-vector/seamless-gold-rhombus-grid-pattern-black-background_53876-97589.jpg?w=1060&t=st=1671168593~exp=1671169193~hmac=34116cdbd09587f0c6d4c289e5b48129239d316204877b7f5191f0c6e50d715d");
|
43 |
+
background-attachment: fixed;
|
44 |
+
background-size: cover
|
45 |
+
}}
|
46 |
+
</style>
|
47 |
+
""",
|
48 |
+
unsafe_allow_html=True
|
49 |
+
)
|
50 |
+
|
51 |
+
|
52 |
+
if selected == "Home":
|
53 |
+
|
54 |
+
col1,col2,col3,col4,col5,col6,col7,col8 = st.columns(8)
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
def load_lottiefile(filepath: str):
|
60 |
+
with open(filepath, "r") as f:
|
61 |
+
return json.load(f)
|
62 |
+
|
63 |
+
|
64 |
+
Tbr = load_lottiefile("images/t.json")
|
65 |
+
with col1:
|
66 |
+
st_lottie(Tbr,
|
67 |
+
loop=True,
|
68 |
+
speed=0.28,
|
69 |
+
key="hello")
|
70 |
+
|
71 |
+
|
72 |
+
Obr = load_lottiefile("images/o.json")
|
73 |
+
with col2:
|
74 |
+
st_lottie(Obr,
|
75 |
+
speed=0.28,
|
76 |
+
key="O")
|
77 |
+
|
78 |
+
|
79 |
+
Xbr = load_lottiefile("images/x.json")
|
80 |
+
with col3:
|
81 |
+
st_lottie(Xbr,
|
82 |
+
speed=0.28,
|
83 |
+
key="X")
|
84 |
+
|
85 |
+
|
86 |
+
Ibr = load_lottiefile("images/i.json")
|
87 |
+
with col4:
|
88 |
+
st_lottie(Ibr,
|
89 |
+
speed=0.28,
|
90 |
+
key="I")
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
Cbr = load_lottiefile("images/c.json")
|
95 |
+
with col5:
|
96 |
+
st_lottie(Cbr,
|
97 |
+
speed=0.28,
|
98 |
+
key="C")
|
99 |
+
|
100 |
+
|
101 |
+
with col6:
|
102 |
+
st_lottie(Ibr,
|
103 |
+
speed=0.28,
|
104 |
+
key="II")
|
105 |
+
|
106 |
+
|
107 |
+
with col7:
|
108 |
+
st_lottie(Tbr,
|
109 |
+
speed=0.28,
|
110 |
+
key="TT")
|
111 |
+
|
112 |
+
|
113 |
+
Ybr = load_lottiefile("images/y.json")
|
114 |
+
with col8:
|
115 |
+
st_lottie(Ybr,
|
116 |
+
speed=0.28,
|
117 |
+
key="Y")
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
|
123 |
+
########################END OF ANIMATIONS
|
124 |
+
|
125 |
+
|
126 |
+
|
127 |
+
st.markdown(f'<h1 style="color:#00ddb3;font-size:40px;border:2px solid white;margin-bottom:10px; text-align:center">{"Analysis of Text/Speech"}</h1>', unsafe_allow_html=True)
|
128 |
+
|
129 |
+
tokenizer = AutoTokenizer.from_pretrained('unitary/toxic-bert')
|
130 |
+
model = AutoModelForSequenceClassification.from_pretrained('unitary/toxic-bert')
|
131 |
+
|
132 |
+
labels=('Toxic','Severe Toxic','Obscene/Sexual','Threat/Violent','Insulting','Identity Hate')
|
133 |
+
|
134 |
+
st.markdown(f'<h1 style="color:#febf00;font-size:30px; text-shadow: 2px 2px 4px #ff0000; text-transform: uppercase;line-height: 1;">{"👉 Input Method"}</h1>', unsafe_allow_html=True)
|
135 |
+
|
136 |
+
t1= st.radio("",
|
137 |
+
('Text','Speech'))
|
138 |
+
|
139 |
+
st.markdown(
|
140 |
+
"""
|
141 |
+
<style>
|
142 |
+
.stProgress > div > div > div > div {
|
143 |
+
background-image: linear-gradient(to right, #5fde , #00cc42);
|
144 |
+
}
|
145 |
+
</style>""",
|
146 |
+
unsafe_allow_html=True,
|
147 |
+
)
|
148 |
+
|
149 |
+
bar = st.progress(0)
|
150 |
+
|
151 |
+
if t1 == "Text":
|
152 |
+
|
153 |
+
st.markdown(f'<h1 style="color:#febf00; text-shadow: 2px 2px 2px #ff0000;font-size:25px;">{"Enter Text : "}</h1>', unsafe_allow_html=True)
|
154 |
+
|
155 |
+
text = st.text_area("👇")
|
156 |
+
if st.button("Done"):
|
157 |
+
with st.spinner("Analysing Toxicity of the Text"):
|
158 |
+
time.sleep(1.5)
|
159 |
+
tokens = tokenizer.encode(text, return_tensors='pt', max_length=512, truncation=True)
|
160 |
+
|
161 |
+
result = model(tokens)
|
162 |
+
|
163 |
+
toxic_array=[]
|
164 |
+
|
165 |
+
for i in range(6):
|
166 |
+
toxic_array.append((labels[i],round(float(result.logits[0][i]),3)))
|
167 |
+
bar.progress(50)
|
168 |
+
bar.progress(100)
|
169 |
+
|
170 |
+
max_= max(toxic_array[1:5], key= lambda tup:tup[1])
|
171 |
+
st.success("Result", icon="✅")
|
172 |
+
|
173 |
+
if ((toxic_array[0][1]<=0) and (max_[1]<0)):
|
174 |
+
st.markdown(f'<h1 style="color:#00e500;text-shadow:1px 1px 1px orange;font-size:25px;">{"Not Toxic"}</h1>', unsafe_allow_html=True)
|
175 |
+
|
176 |
+
else:
|
177 |
+
st.markdown(f'<h1 style="color:#EC2001;text-shadow:1px 1px #36024c;font-size:30px;">⭕{max_[0]}</h1>', unsafe_allow_html=True)
|
178 |
+
|
179 |
+
|
180 |
+
count=0
|
181 |
+
for i in range(1,6):
|
182 |
+
if toxic_array[i][1]>0:
|
183 |
+
count+=1
|
184 |
+
if count>1:
|
185 |
+
st.markdown(f'<h1 style="color:#febf00;font-size:28px;">{"Other Toxicity:"}</h1>', unsafe_allow_html=True)
|
186 |
+
|
187 |
+
for i in range(1,6):
|
188 |
+
if (toxic_array[i][1]>0) and (toxic_array[i]!=max_):
|
189 |
+
st.markdown(f'<h1 style="color:#EC2001;text-shadow:1px 1px #36024c;font-size:25px;">⭕{toxic_array[i][0]}</h1>', unsafe_allow_html=True)
|
190 |
+
|
191 |
+
|
192 |
+
|
193 |
+
|
194 |
+
else:
|
195 |
+
st.markdown(f'<h1 style="color:#febf00;text-shadow: 2px 2px 3px #ff0000;font-size:23px;">{"🤏 Click on the Mic to Record"}</h1>', unsafe_allow_html=True)
|
196 |
+
|
197 |
+
|
198 |
+
if st.button("🎙️"):
|
199 |
+
|
200 |
+
sr=s.Recognizer()
|
201 |
+
with s.Microphone() as m:
|
202 |
+
st.markdown(f'<h1 style="color:white; margin-bottom:5px;font-size:25px;">{"Speak Now..."}</h1>', unsafe_allow_html=True)
|
203 |
+
|
204 |
+
|
205 |
+
audio=sr.listen(m)
|
206 |
+
query=sr.recognize_google(audio,language='eng-in')
|
207 |
+
#st.image(query)
|
208 |
+
st.markdown(f'<h1 style="color:black;margin-bottom:7px;border-radius:10px;background-color:orange;padding:10px;border:2px solid white;font-size:20px;">{query}</h1>', unsafe_allow_html=True)
|
209 |
+
|
210 |
+
tokens = tokenizer.encode(query, return_tensors='pt', max_length=512, truncation=True)
|
211 |
+
result = model(tokens)
|
212 |
+
toxic_array=[]
|
213 |
+
with st.spinner("Analysing Toxicity of the Text"):
|
214 |
+
time.sleep(1.5)
|
215 |
+
for i in range(6):
|
216 |
+
toxic_array.append((labels[i],round(float(result.logits[0][i]),3)))
|
217 |
+
bar.progress(50)
|
218 |
+
bar.progress(100)
|
219 |
+
max_= max(toxic_array[1:5], key= lambda tup:tup[1])
|
220 |
+
st.success('Result', icon="✅")
|
221 |
+
|
222 |
+
if ((toxic_array[0][1]<=0) and (max_[1]<0)):
|
223 |
+
st.markdown(f'<h1 style="color:#00e500;font-size:25px;">{"Not Toxic"}</h1>', unsafe_allow_html=True)
|
224 |
+
|
225 |
+
else:
|
226 |
+
st.markdown(f'<h1 style="color:#ffff;font-size:30px;">⭕{max_[0]}</h1>', unsafe_allow_html=True)
|
227 |
+
count=0
|
228 |
+
for i in range(1,6):
|
229 |
+
if toxic_array[i][1]>0:
|
230 |
+
count+=1
|
231 |
+
if count>1:
|
232 |
+
st.markdown(f'<h1 style="color:#EC2001;font-size:28px;">{"Other Toxicity:"}</h1>', unsafe_allow_html=True)
|
233 |
+
|
234 |
+
for i in range(1,6):
|
235 |
+
if (toxic_array[i][1]>0) and (toxic_array[i]!=max_):
|
236 |
+
st.markdown(f'<h1 style="color:#ffff;font-size:25px;">⭕{toxic_array[i][0]}</h1>', unsafe_allow_html=True)
|
237 |
+
|
238 |
+
|
239 |
+
|
240 |
+
|
241 |
+
if selected == "Information":
|
242 |
+
st.markdown(f'<h1 style="color:#ffff;text-shadow:4px 4px 4px black; text-align:center;font-size:32px;">{"This Application uses BERT Model for Analysing Toxicity of Text/Speech"}</h1>', unsafe_allow_html=True)
|
243 |
+
|
244 |
+
st.markdown(f'<h1 style="color:#febf00;margin-top:5rem;text-shadow:4px 4px 4px black; text-align:center;font-size:27px;">{"Made by :"}</h1>', unsafe_allow_html=True)
|
245 |
+
|
246 |
+
st.markdown(f'<h1 style="color:#ffff;margin-top:0.3rem;text-shadow:4px 4px 4px black;text-align:center;font-size:25px;">{"🪶Aman Kaintura"}</h1>', unsafe_allow_html=True)
|
247 |
+
st.markdown(f'<h1 style="color:#ffff;margin-top:1rem;text-shadow:4px 4px 4px black; text-align:center;font-size:25px;">{"🪶Yuvraj Chakravarty"}</h1>', unsafe_allow_html=True)
|
248 |
+
st.markdown(f'<h1 style="color:#ffff;margin-top:1rem;text-shadow:4px 4px 4px black; text-align:center;font-size:25px;">{"🪶Bharat Kumar"}</h1>', unsafe_allow_html=True)
|
249 |
+
|
250 |
+
|
251 |
+
|
252 |
+
|
253 |
+
st.markdown("""
|
254 |
+
<style>
|
255 |
+
.css-1lsmgbg.egzxvld0
|
256 |
+
{
|
257 |
+
visibility: hidden;
|
258 |
+
}
|
259 |
+
</style>
|
260 |
+
""",unsafe_allow_html=True)
|
261 |
+
|