Spaces:
Runtime error
Runtime error
Create new file
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
import tensorflow_text as text
|
| 3 |
+
from tensorflow.train import Checkpoint
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
import gradio
|
| 7 |
+
from Model import Transformer
|
| 8 |
+
|
| 9 |
+
vocab = []
|
| 10 |
+
with open("vocab.txt", mode = "r", encoding = "utf-8") as file:
|
| 11 |
+
for token in file:
|
| 12 |
+
vocab.append(token.replace("\n", ""))
|
| 13 |
+
|
| 14 |
+
tokenizer = text.FastBertTokenizer(vocab, support_detokenization = True)
|
| 15 |
+
|
| 16 |
+
VOCAB_SIZE = len(vocab)
|
| 17 |
+
D_MODEL = 256
|
| 18 |
+
NB_LAYERS = 6
|
| 19 |
+
FFN_UNITS = 2048
|
| 20 |
+
NB_PROJ = 8
|
| 21 |
+
DROPOUT_RATE = 0.1
|
| 22 |
+
|
| 23 |
+
transformer = Transformer(vocab_size_enc = VOCAB_SIZE,
|
| 24 |
+
vocab_size_dec = 1,
|
| 25 |
+
d_model = D_MODEL,
|
| 26 |
+
nb_layers = NB_LAYERS,
|
| 27 |
+
FFN_units = FFN_UNITS,
|
| 28 |
+
nb_proj = NB_PROJ,
|
| 29 |
+
dropout_rate = DROPOUT_RATE)
|
| 30 |
+
|
| 31 |
+
ckpt = Checkpoint()
|
| 32 |
+
ckpt.restore("ckpt-10")
|
| 33 |
+
print("Checkpoint Restaurado")
|
| 34 |
+
|
| 35 |
+
def evaluate(sentence):
|
| 36 |
+
ragged = tokenizer.tokenize([sentence])
|
| 37 |
+
ragged = trimer.trim([ragged])[0]
|
| 38 |
+
count = ragged.bounding_shape()[0]
|
| 39 |
+
starts = tf.fill([count,1], START)
|
| 40 |
+
ends = tf.fill([count,1], END)
|
| 41 |
+
inputs = tf.concat([starts, ragged, ends], axis=1)
|
| 42 |
+
inputs, _ = text.pad_model_inputs(inputs, max_seq_length = MAX_LENGTH + 2, pad_value = PAD)
|
| 43 |
+
|
| 44 |
+
prediction = transformer(inputs, False)
|
| 45 |
+
|
| 46 |
+
prediction = tf.round(prediction)
|
| 47 |
+
if prediction == 0:
|
| 48 |
+
return"Negative"
|
| 49 |
+
else:
|
| 50 |
+
return"Positive"
|
| 51 |
+
|
| 52 |
+
app = gr.Interface(fn = evaluate, title = "IMDb Sentiment Classifier", description = "Write a sentence with a positive or negative sentiment", inputs = "text", outputs = "text")
|
| 53 |
+
app.launch(share = True)
|
| 54 |
+
|
| 55 |
+
|