import numpy as np
import pandas as pd
import streamlit as st
import json
import torch
from streamlit_option_menu import option_menu
import time
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from streamlit_lottie import st_lottie
from streamlit_lottie import st_lottie_spinner
import speech_recognition as s
with st.sidebar:
selected = option_menu(
menu_title="Main Menu",
options=["Home","Information"]
)
################# Animation of the title
st.markdown(
"""
""",
unsafe_allow_html=True,
)
st.markdown(
f"""
""",
unsafe_allow_html=True
)
if selected == "Home":
col1,col2,col3,col4,col5,col6,col7,col8 = st.columns(8)
def load_lottiefile(filepath: str):
with open(filepath, "r") as f:
return json.load(f)
Tbr = load_lottiefile("t.json")
with col1:
st_lottie(Tbr,
loop=True,
speed=0.28,
key="hello")
Obr = load_lottiefile("o.json")
with col2:
st_lottie(Obr,
speed=0.28,
key="O")
Xbr = load_lottiefile("x.json")
with col3:
st_lottie(Xbr,
speed=0.28,
key="X")
Ibr = load_lottiefile("i.json")
with col4:
st_lottie(Ibr,
speed=0.28,
key="I")
Cbr = load_lottiefile("c.json")
with col5:
st_lottie(Cbr,
speed=0.28,
key="C")
with col6:
st_lottie(Ibr,
speed=0.28,
key="II")
with col7:
st_lottie(Tbr,
speed=0.28,
key="TT")
Ybr = load_lottiefile("y.json")
with col8:
st_lottie(Ybr,
speed=0.28,
key="Y")
########################END OF ANIMATIONS
st.markdown(f'
{"Analysis of Text/Speech"}
', unsafe_allow_html=True)
tokenizer = AutoTokenizer.from_pretrained('unitary/toxic-bert')
model = AutoModelForSequenceClassification.from_pretrained('unitary/toxic-bert')
labels=('Toxic','Severe Toxic','Obscene/Sexual','Threat/Violent','Insulting','Identity Hate')
st.markdown(f'{"👉 Input Method"}
', unsafe_allow_html=True)
t1= st.radio("",
('Text','Speech'))
st.markdown(
"""
""",
unsafe_allow_html=True,
)
bar = st.progress(0)
if t1 == "Text":
st.markdown(f'{"Enter Text : "}
', unsafe_allow_html=True)
text = st.text_area("👇")
if st.button("Done"):
with st.spinner("Analysing Toxicity of the Text"):
time.sleep(1.5)
tokens = tokenizer.encode(text, return_tensors='pt', max_length=512, truncation=True)
result = model(tokens)
toxic_array=[]
for i in range(6):
toxic_array.append((labels[i],round(float(result.logits[0][i]),3)))
bar.progress(50)
bar.progress(100)
max_= max(toxic_array[1:5], key= lambda tup:tup[1])
st.success("Result", icon="✅")
if ((toxic_array[0][1]<=0) and (max_[1]<0)):
st.markdown(f'{"Not Toxic"}
', unsafe_allow_html=True)
else:
st.markdown(f'⭕{max_[0]}
', unsafe_allow_html=True)
count=0
for i in range(1,6):
if toxic_array[i][1]>0:
count+=1
if count>1:
st.markdown(f'{"Other Toxicity:"}
', unsafe_allow_html=True)
for i in range(1,6):
if (toxic_array[i][1]>0) and (toxic_array[i]!=max_):
st.markdown(f'⭕{toxic_array[i][0]}
', unsafe_allow_html=True)
else:
st.markdown(f'{"🤏 Click on the Mic to Record"}
', unsafe_allow_html=True)
if st.button("🎙️"):
sr=s.Recognizer()
with s.Microphone() as m:
st.markdown(f'{"Speak Now..."}
', unsafe_allow_html=True)
audio=sr.listen(m)
query=sr.recognize_google(audio,language='eng-in')
#st.image(query)
st.markdown(f'{query}
', unsafe_allow_html=True)
tokens = tokenizer.encode(query, return_tensors='pt', max_length=512, truncation=True)
result = model(tokens)
toxic_array=[]
with st.spinner("Analysing Toxicity of the Text"):
time.sleep(1.5)
for i in range(6):
toxic_array.append((labels[i],round(float(result.logits[0][i]),3)))
bar.progress(50)
bar.progress(100)
max_= max(toxic_array[1:5], key= lambda tup:tup[1])
st.success('Result', icon="✅")
if ((toxic_array[0][1]<=0) and (max_[1]<0)):
st.markdown(f'{"Not Toxic"}
', unsafe_allow_html=True)
else:
st.markdown(f'⭕{max_[0]}
', unsafe_allow_html=True)
count=0
for i in range(1,6):
if toxic_array[i][1]>0:
count+=1
if count>1:
st.markdown(f'{"Other Toxicity:"}
', unsafe_allow_html=True)
for i in range(1,6):
if (toxic_array[i][1]>0) and (toxic_array[i]!=max_):
st.markdown(f'⭕{toxic_array[i][0]}
', unsafe_allow_html=True)
if selected == "Information":
st.markdown(f'{"This Application uses BERT Model for Analysing Toxicity of Text/Speech"}
', unsafe_allow_html=True)
st.markdown(f'{"Made by :"}
', unsafe_allow_html=True)
st.markdown(f'{"🪶Aman Kaintura"}
', unsafe_allow_html=True)
st.markdown(f'{"🪶Yuvraj Chakravarty"}
', unsafe_allow_html=True)
st.markdown(f'{"🪶Bharat Kumar"}
', unsafe_allow_html=True)
st.markdown("""
""",unsafe_allow_html=True)