Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,48 @@
|
|
| 1 |
-
import pickle
|
| 2 |
-
import streamlit as st
|
| 3 |
-
from string import punctuation
|
| 4 |
-
import nltk
|
| 5 |
-
from nltk.corpus import stopwords
|
| 6 |
-
from nltk.stem import WordNetLemmatizer
|
| 7 |
-
from nltk import word_tokenize
|
| 8 |
-
|
| 9 |
-
nltk.download('stopwords')
|
| 10 |
-
nltk.download('punkt')
|
| 11 |
-
nltk.download('wordnet')
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
model = pickle.load(open('model.pkl', 'rb'))
|
| 15 |
-
vectorizer = pickle.load(open('vectorizer.pkl', 'rb'))
|
| 16 |
-
|
| 17 |
-
# Defining a function that will clean the text
|
| 18 |
-
def clean_text(text):
|
| 19 |
-
punc = list(punctuation)
|
| 20 |
-
stop = stopwords.words('english')
|
| 21 |
-
bad_tokens = punc + stop
|
| 22 |
-
tokens = word_tokenize(text)
|
| 23 |
-
lemma = WordNetLemmatizer()
|
| 24 |
-
word_tokens = [t for t in tokens if t.isalpha()]
|
| 25 |
-
clean_tokens = [lemma.lemmatize(t.lower()) for t in word_tokens if t not in bad_tokens]
|
| 26 |
-
return ' '.join(clean_tokens)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# defining the main function and creating the ui
|
| 30 |
-
def main():
|
| 31 |
-
st.set_page_config(page_title='Hate Speech Detector', page_icon='🤖')
|
| 32 |
-
st.header("Hate Speech Detection System")
|
| 33 |
-
text = st.text_area('Enter Text')
|
| 34 |
-
submit = st.button('Submit')
|
| 35 |
-
if submit:
|
| 36 |
-
itext = clean_text(text)
|
| 37 |
-
vectorized_text = vectorizer.transform([itext])
|
| 38 |
-
response = model.predict(vectorized_text)[0]
|
| 39 |
-
if response == 0:
|
| 40 |
-
st.write('Hate Speech')
|
| 41 |
-
|
| 42 |
-
st.write('Offensive Language')
|
| 43 |
-
else:
|
| 44 |
-
st.write('No hate speech or offensive language detected')
|
| 45 |
-
|
| 46 |
-
if __name__ == "__main__":
|
| 47 |
-
main()
|
| 48 |
-
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from string import punctuation
|
| 4 |
+
import nltk
|
| 5 |
+
from nltk.corpus import stopwords
|
| 6 |
+
from nltk.stem import WordNetLemmatizer
|
| 7 |
+
from nltk import word_tokenize
|
| 8 |
+
|
| 9 |
+
nltk.download('stopwords')
|
| 10 |
+
nltk.download('punkt')
|
| 11 |
+
nltk.download('wordnet')
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
model = pickle.load(open('model.pkl', 'rb'))
|
| 15 |
+
vectorizer = pickle.load(open('vectorizer.pkl', 'rb'))
|
| 16 |
+
|
| 17 |
+
# Defining a function that will clean the text
|
| 18 |
+
def clean_text(text):
|
| 19 |
+
punc = list(punctuation)
|
| 20 |
+
stop = stopwords.words('english')
|
| 21 |
+
bad_tokens = punc + stop
|
| 22 |
+
tokens = word_tokenize(text)
|
| 23 |
+
lemma = WordNetLemmatizer()
|
| 24 |
+
word_tokens = [t for t in tokens if t.isalpha()]
|
| 25 |
+
clean_tokens = [lemma.lemmatize(t.lower()) for t in word_tokens if t not in bad_tokens]
|
| 26 |
+
return ' '.join(clean_tokens)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# defining the main function and creating the ui
|
| 30 |
+
def main():
|
| 31 |
+
st.set_page_config(page_title='Hate Speech Detector', page_icon='🤖')
|
| 32 |
+
st.header("Hate Speech Detection System")
|
| 33 |
+
text = st.text_area('Enter Text')
|
| 34 |
+
submit = st.button('Submit')
|
| 35 |
+
if submit:
|
| 36 |
+
itext = clean_text(text)
|
| 37 |
+
vectorized_text = vectorizer.transform([itext])
|
| 38 |
+
response = model.predict(vectorized_text)[0]
|
| 39 |
+
if response == 0:
|
| 40 |
+
st.write('Hate Speech')
|
| 41 |
+
elif response == 1:
|
| 42 |
+
st.write('Offensive Language')
|
| 43 |
+
else:
|
| 44 |
+
st.write('No hate speech or offensive language detected')
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
main()
|
| 48 |
+
|