finalyearproject/hatespeechdetection
1
1import streamlit as st2import tensorflow as tf3import numpy as np4import time5from transformers import DistilBertTokenizer, TFDistilBertModel6from keras.models import load_model7from streamlit_option_menu import option_menu8 9 10 11 12new_model = tf.keras.models.load_model("DL_model_DistilBert_Lstm.h5",custom_objects = {'TFDistilBertModel': TFDistilBertModel})13 14tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')15bert_model = TFDistilBertModel.from_pretrained('distilbert-base-uncased')16 17MAX_LEN = 12818 19 20 21st.set_page_config(22 23 menu_items={24 'Get Help': None,25 'Report a bug': None,26 'About' :27 '''28 # Thanks for using this app29 30 This app is built as a Final Year Project31 32 '''33 }34 35)36 37# sidebar for navigation38with st.sidebar:39 40 selected = option_menu('User Guide',41 42 ["To determine whether a statement constitutes hate speech or not, simply enter it into the provided textbox and click on the 'Predict' button.", "1. Enter the statement you want to check for hate speech in the textbox provided on the screen.",43 "2. Click on the 'Predict' button to initiate the analysis process.", "3. Wait for the system to process the statement.", "4. Once the process is complete, the model will output a prediction indicating whether the statement contains hate speech or not.",44 "5. Review the prediction and use it to make informed decisions about the use of the statement.", "Note: The accuracy of the prediction may vary depending on the complexity of the statement and the accuracy of the model. Therefore, it is recommended that you use your own discretion when interpreting the results and take appropriate actions as necessary."45 ],46 )47 48 49 50def main():51 st.title("Hate Speech Detection using Machine Learning")52 53 st.image("hatespeech2.jpg")54 55 56 st.write(57 "This app employs the DistilBERT model to categorize speech into three distinct categories, namely Non-Risky, Potentially Risky, and Risky, thereby determining whether the speech constitutes hate speech or not.")58 59 html_temp = """60 <div style="background-color:blue;padding:10px">61 <h2 style="color:white;text-align:center;">Built upon DistilBERT Model</h2>62 </div>63 """64 st.markdown(html_temp, unsafe_allow_html=True)65 66 67 68 69 text = st.text_area("Enter your statement")70 71 72 if st.button("Predict"):73 text_tokenized = tokenizer.encode_plus(74 text,75 max_length=MAX_LEN,76 padding='max_length',77 truncation=True,78 return_token_type_ids=False,79 return_tensors='tf'80 )81 prediction = new_model.predict([text_tokenized['input_ids'].numpy()])82 83 pred = np.argmax(prediction)84 85 st.header("AI thinks that...")86 87 88 if pred == 0:89 col1, col2 = st.columns(2)90 col1.metric("Statement", value="Non-Risky")91 col2.metric("Confidence Level", value=f"{np.round(np.max(prediction) * 100)}%")92 93 94 95 96 elif pred == 1:97 col1, col2 = st.columns(2)98 col1.metric("Statement", value="Potentially Risky")99 col2.metric("Confidence Level", value=f"{np.round(np.max(prediction) * 100)}%")100 101 102 103 104 else:105 col1, col2 = st.columns(2)106 col1.metric("Statement", value="Risky")107 col2.metric("Confidence Level", value=f"{np.round(np.max(prediction) * 100)}%")108 109 110 111 112 113 114if __name__ == '__main__':115 main()116 117 