Amit commited on
Commit
f14967e
·
1 Parent(s): ce00e36

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/local/bin/python3.9
2
+
3
+ import streamlit as st
4
+
5
+ import datetime
6
+ print(datetime.datetime.now(),"Program start.")
7
+
8
+ #import nltk
9
+ #from nltk.corpus import stopwords
10
+ import re
11
+ #import pandas as pd
12
+ #import nltk
13
+
14
+ import pickle
15
+ #import re
16
+ #import string
17
+ import numpy as np
18
+ #import pandas as pd
19
+
20
+ #from sklearn.preprocessing import LabelEncoder
21
+ #from sklearn.model_selection import train_test_split
22
+
23
+ #from tensorflow.keras.utils import to_categorical
24
+ from tensorflow.keras.preprocessing.text import Tokenizer
25
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
26
+
27
+ #from tensorflow.keras.optimizers import Adam
28
+ #from tensorflow.keras.models import Sequential
29
+ #from tensorflow.keras.callbacks import EarlyStopping
30
+ #from tensorflow.keras.layers import Dense, LSTM, Embedding, Bidirectional
31
+
32
+ if "model_loaded" not in st.session_state:
33
+ with open('model.pkl', 'rb') as f:
34
+ clf2 = pickle.load(f)
35
+ st.session_state.model_loaded=clf2
36
+ else:
37
+ clf2=st.session_state.model_loaded
38
+
39
+ print(datetime.datetime.now(),"Finished import.")
40
+ st.text("Hate Speech Detector")
41
+ sentence=st.text_input('Sentence to analyze')
42
+
43
+ labels=['Homophobe', 'Sexist', 'OtherHate', 'NotHate', 'Religion', 'Racist']
44
+
45
+ # LIB LIB str_punc = string.punctuation.replace(',', '').replace("'",'')
46
+ def clean(text):
47
+ global str_punc
48
+ text = re.sub(r'[^a-zA-Z ]', '', text)
49
+ text = text.lower()
50
+ return text
51
+
52
+ tokenizer = Tokenizer()
53
+ # LIB LIB le = LabelEncoder()
54
+
55
+ print(datetime.datetime.now(),"Program. About to load the model.")
56
+
57
+ print(datetime.datetime.now(),"Program. Finished loading the model.")
58
+ if sentence:
59
+ print("*************\nSentence:",sentence)
60
+ sentence = clean(sentence)
61
+ sentence = tokenizer.texts_to_sequences([sentence])
62
+ sentence = pad_sequences(sentence, maxlen=256, truncating='pre')
63
+ p=clf2.predict(sentence)
64
+ print("Prediction:",p)
65
+ a=np.argmax(p)
66
+ print("ArgMax:",a)
67
+ result=labels[a]
68
+ #result = le.inverse_transform(np.argmax(clf2.predict(sentence), axis=-1))[0]
69
+ proba = np.max(clf2.predict(sentence))
70
+ print(f"{result} : {proba}\n\n")
71
+ st.text(f"{result}")
72
+ print(datetime.datetime.now(),"Program end.")