Spaces:
Build error
Build error
| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| from tensorflow.keras import layers | |
| import tensorflow_hub as hub | |
| from googletrans import Translator | |
| import bert | |
| class DCNNBERTEmbedding(tf.keras.Model): | |
| def __init__(self, | |
| nb_filters=50, | |
| FFN_units=512, | |
| nb_classes=2, | |
| dropout_rate=0.1, | |
| name="dcnn"): | |
| super(DCNNBERTEmbedding, self).__init__(name=name) | |
| self.bert_layer = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1", trainable = False) | |
| self.bigram = layers.Conv1D(filters=nb_filters, | |
| kernel_size=2, | |
| padding="valid", | |
| activation="relu") | |
| self.trigram = layers.Conv1D(filters=nb_filters, | |
| kernel_size=3, | |
| padding="valid", | |
| activation="relu") | |
| self.fourgram = layers.Conv1D(filters=nb_filters, | |
| kernel_size=4, | |
| padding="valid", | |
| activation="relu") | |
| self.pool = layers.GlobalMaxPool1D() | |
| self.dense_1 = layers.Dense(units=FFN_units, activation="relu") | |
| self.dropout = layers.Dropout(rate=dropout_rate) | |
| if nb_classes == 2: | |
| self.last_dense = layers.Dense(units=1, activation="sigmoid") | |
| else: | |
| self.last_dense = layers.Dense(units=nb_classes, activation="softmax") | |
| def embed_with_bert(self, all_tokens): | |
| _, embs = self.bert_layer([all_tokens[:, 0, :], | |
| all_tokens[:, 1, :], | |
| all_tokens[:, 2, :]]) | |
| return embs | |
| def call(self, inputs, training): | |
| x = self.embed_with_bert(inputs) | |
| x_1 = self.bigram(x) | |
| x_1 = self.pool(x_1) | |
| x_2 = self.trigram(x) | |
| x_2 = self.pool(x_2) | |
| x_3 = self.fourgram(x) | |
| x_3 = self.pool(x_3) | |
| merged = tf.concat([x_1, x_2, x_3], axis=-1) | |
| merged = self.dense_1(merged) | |
| merged = self.dropout(merged, training) | |
| output = self.last_dense(merged) | |
| return output | |
| NB_FILTERS = 100 | |
| FFN_UNITS = 256 | |
| NB_CLASSES = 2 | |
| DROPOUT_RATE = 0.2 | |
| BATCH_SIZE = 32 | |
| NB_EPOCHS = 5 | |
| Dcnn = DCNNBERTEmbedding(nb_filters=NB_FILTERS, | |
| FFN_units=FFN_UNITS, | |
| nb_classes=NB_CLASSES, | |
| dropout_rate=DROPOUT_RATE) | |
| if NB_CLASSES == 2: | |
| Dcnn.compile(loss="binary_crossentropy", | |
| optimizer="adam", | |
| metrics=["accuracy"]) | |
| else: | |
| Dcnn.compile(loss="sparse_categorical_crossentropy", | |
| optimizer="adam", | |
| metrics=["sparse_categorical_accuracy"]) | |
| checkpoint_path = "./" | |
| ckpt = tf.train.Checkpoint(Dcnn=Dcnn) | |
| ckpt_manager = tf.train.CheckpointManager(ckpt, checkpoint_path, max_to_keep=1) | |
| if ckpt_manager.latest_checkpoint: | |
| ckpt.restore(ckpt_manager.latest_checkpoint) | |
| FullTokenizer = bert.bert_tokenization.FullTokenizer | |
| bert_layer = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1",trainable=False) | |
| vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy() | |
| do_lower_case = bert_layer.resolved_object.do_lower_case.numpy() | |
| tokenizer = FullTokenizer(vocab_file, do_lower_case) | |
| def encode_sentence(sent): | |
| return ["[CLS]"] + tokenizer.tokenize(sent) + ["[SEP]"] | |
| def get_ids(tokens): | |
| return tokenizer.convert_tokens_to_ids(tokens) | |
| def get_mask(tokens): | |
| return np.char.not_equal(tokens, "[PAD]").astype(int) | |
| def get_segments(tokens): | |
| seg_ids = [] | |
| current_seg_id = 0 | |
| for tok in tokens: | |
| seg_ids.append(current_seg_id) | |
| if tok == "[SEP]": | |
| current_seg_id = 1 - current_seg_id | |
| return seg_ids | |
| def get_prediction(sentence): | |
| tokens = encode_sentence(sentence) | |
| input_ids = get_ids(tokens) | |
| input_mask = get_mask(tokens) | |
| segment_ids = get_segments(tokens) | |
| inputs = tf.stack( | |
| [ | |
| tf.cast(input_ids, dtype=tf.int32), | |
| tf.cast(input_mask, dtype=tf.int32), | |
| tf.cast(segment_ids, dtype=tf.int32), | |
| ], axis = 0) | |
| inputs = tf.expand_dims(inputs, 0) | |
| output = Dcnn(inputs, training=False) | |
| return "Probabilidade de suicídio: {:.2%}".format(output[0][0]) | |
| def translate(sent): | |
| translator = Translator() | |
| text = translator.translate(sent, dest = 'en', src = 'auto') | |
| return text.text | |
| def predict(text): | |
| sent = translate(text) | |
| prediction = get_prediction(sent) | |
| return prediction | |
| inputs = gr.inputs.Textbox(lines = 5, label = 'Como está se sentindo?') | |
| #outputs = gr.outputs.Label(num_top_classes = 1, labels = lambda x: f'Probabilidade de suicídio: {x[0]:.2%}') | |
| app = gr.Interface(fn = predict, inputs = inputs, outputs = 'text', title = 'IA para classificação de risco de suicídio - Versão 0.0.1') | |
| app.launch() | |