-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
148 lines (126 loc) · 6.4 KB
/
main.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#from copyreg import pickle
import streamlit as st
import pickle
#import numpy as np
#from sklearn.tree import DecisionTreeClassifier
# Set page configuration
st.set_page_config(page_title="Hate Speech Detection", layout="wide")
#st.title("Welcome to Aditi Srivastava's Webpage")
# Define the main title and subtitle
#st.subheader("Author: Aditi Srivastava")
st.title("Hate Speech Detection")
st.markdown(
"""
</style>
<div style='text-align: center;'><em>Enter any speech for detection</em></div>
"""
, unsafe_allow_html=True
)
# Create input text box for speech
speech = st.text_input( '')
#check if speech is provided
if st.button("check for hate and offensive language"):
if not speech:
st.error("Please enter a speech for detection.")
else:
# Load the pre-trained model and feature vectorizer
model = pickle.load(open('hate_speech_model.pkl',"rb"))
cv = pickle.load(open("cv.pkl","rb"))
# Preprocess the input speech
data = cv.transform([speech]).toarray()
# Make predictions
pred = model.predict(data)
if pred == "Offensive Language":
st.error("Offensive Language Detected")
st.image("offensive_image.png", caption="Be mindful when it comes to your words. A string of some that do not mean much to you, may stick with someone else for a lifetime.", width=300)
st.info("This speech contains offensive language. Please be cautious when using such language.")
# st.balloons()
st.markdown("---")
# st.subheader("Additional Information")
# st.write("Prediction Probability: ", model.predict_proba(data)[0, 1])
#st.write("Decision Path: ", model.decision_path(data))
elif pred == "No Hate and Offensive language":
st.success("No Hate and Offensive Language was detected")
st.image("non_hate_image.png", caption="Kind words are a creative force, a power that concurs in the building up of all that is good, and energy that showers blessings upon the world.", width=300)
st.success("This speech does not contain hate or offensive language. Continue spreading positivity!")
st.markdown("---")
#st.subheader("Additional Information")
#st.write("Prediction Probability: ", model.predict_proba(data)[0, 1])
#st.write("Decision Path: ", model.decision_path(data))
elif pred == "Hate Speech":
st.error("Hate Speech Detected")
st.image("hate_speech_image.png", caption="Be careful with your words. Once they are said, they can be only forgiven, not forgotten.", width=300)
st.error("This speech contains hate speech. Please promote respect and tolerance in communication.")
st.markdown("---")
# st.subheader("Additional Information")
# st.write("Prediction Probability: ", model.predict_proba(data)[0, 1])
# st.write("Decision Path: ", model.decision_path(data))
else:
st.warning("Not Found")
# Add a separator for better visual division
st.markdown("---")
# Add a section for example speeches
st.subheader("Example Speeches")
example_speeches = [
"I hate you!",
"This is a friendly message.",
"You're the worst!",
"He is a stupid boy!",
"I completely disagree with you.",
]
selected_example = st.selectbox("Select an example speech", example_speeches)
if st.button("Check Example Speech"):
st.text_area("Selected Example Speech", selected_example, height=100)
# Load the pre-trained model and feature vectorizer
model = pickle.load(open("hate_speech_model.pkl", "rb"))
cv = pickle.load(open("cv.pkl", "rb"))
# Preprocess the example speech
data = cv.transform([selected_example]).toarray()
# Make predictions
pred = model.predict(data)
# Display the prediction result
if pred == "Offensive Language":
st.error("Offensive Language Detected")
elif pred == "No Hate and Offensive language":
st.success("No Hate and Offensive Language was detected")
elif pred == "Hate Speech":
st.error("Hate Speech Detected")
else:
st.warning("Not Found")
# Chatbot-style interaction
st.subheader("Chatbot")
st.markdown(
"""
This chatbot can assist you with hate speech detection. Type your question or choose an option below:
"""
)
st.markdown("---")
selected_option = st.selectbox("Select an option", ["Select One", "What is hate speech?", "How does the model detect hate speech?", "What can I do to combat hate speech?"])
if selected_option == "Select One":
st.markdown("Please select an option from the dropdown.")
elif selected_option == "What is hate speech?":
st.markdown("Hate speech refers to any form of communication, whether written, spoken, or symbolic, that offends, threatens, or insults individuals or groups based on attributes such as race, religion, ethnic origin, sexual orientation, disability, or gender.")
st.markdown("It is important to be aware of the impact of hate speech and to promote respectful and inclusive communication.")
elif selected_option == "How does the model detect hate speech?":
st.markdown("The model uses machine learning techniques to analyze the text of a speech and classify it into categories such as offensive language, hate speech, or no hate and offensive language. It is trained on a dataset of labeled examples to learn patterns and features that distinguish between different types of speech.")
elif selected_option == "What can I do to combat hate speech?":
st.markdown("Here are some ways you can combat hate speech:")
st.markdown("- Educate yourself about different cultures, religions, and communities to foster understanding and empathy.")
st.markdown("- Promote respectful and inclusive language in your interactions.")
st.markdown("- Speak up against hate speech when you encounter it.")
st.markdown("- Support organizations and initiatives that work towards combating hate speech and promoting tolerance.")
m = st.markdown("""
<style>
div.stButton > button:first-child {
background-color: green ;
padding: 13px 50px;
display: block;
margin: 0 auto;
}
</style>""", unsafe_allow_html=True)
n = st.markdown("""
<style>
div.stimage {
size: 200px;
}
</style>""", unsafe_allow_html=True)