Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import gradio as gr
|
3 |
+
import pickle
|
4 |
+
import os
|
5 |
+
|
6 |
+
# define variables
|
7 |
+
checkpoint_folder = './'
|
8 |
+
bi_gru_name = "test_25_128_2564_15_32_10_16_05_D_64_15.pickle"
|
9 |
+
tokenizer_path = os.path.join(checkpoint_folder, 'tokenizer_tP4suwZ.pickle')
|
10 |
+
model_path = os.path.join(checkpoint_folder, bi_gru_name)
|
11 |
+
INDEX_CLASS = {0:'suicide', 1:'non-suicide'}
|
12 |
+
MAX_SEQ_DF = 64
|
13 |
+
|
14 |
+
|
15 |
+
# load the tokenizer from pickle
|
16 |
+
with open(tokenizer_path, 'rb') as tokenizer_file:
|
17 |
+
tokenizer = pickle.load(tokenizer_file)
|
18 |
+
# load the mode from pickle and read the model
|
19 |
+
with open(model_path, 'rb') as model_file:
|
20 |
+
model_json = pickle.load(model_file)
|
21 |
+
model = tf.keras.models.model_from_json(model_json)
|
22 |
+
|
23 |
+
# define prediction function
|
24 |
+
def predict(input_text):
|
25 |
+
# preprocessing
|
26 |
+
tokenized_data = tokenizer.texts_to_sequences([input_text])
|
27 |
+
text_data_padded = tf.keras.preprocessing.sequence.pad_sequences(tokenized_data, maxlen = MAX_SEQ_DF, padding = 'post')
|
28 |
+
|
29 |
+
# make prediction
|
30 |
+
pred = model.predict(text_data_padded)
|
31 |
+
prediction = INDEX_CLASS[round(pred[0][0])]
|
32 |
+
|
33 |
+
return prediction
|
34 |
+
|
35 |
+
|
36 |
+
gr_intrfc = gr.Interface(fn=predict, inputs="text", title='Suicide Detection',
|
37 |
+
examples=['I Just want it to stop, what the point anyway',
|
38 |
+
'Thanks for the help, i do nont feel depressed anymore'],
|
39 |
+
outputs="text", theme='dark')
|
40 |
+
gr_intrfc.launch(debug=True, )
|
41 |
+
|