Spaces:
Runtime error
Runtime error
File size: 1,830 Bytes
23b37f9 fc2f5ac 23b37f9 f8abd11 0b0db39 23b37f9 fc2f5ac 23b37f9 fc2f5ac 23b37f9 d316c6f 23b37f9 d316c6f b1a3221 b3d7597 23b37f9 07e273e 23b37f9 07e273e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | from src.Vocabulary import Vocabulary
from src.RNNmodel import RNN
import torch
import gradio as gr
import torchtext.vocab as vocab1
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
word_embedding = vocab1.Vectors(name = "word2vec.txt",
unk_init = torch.Tensor.normal_)
vocab = Vocabulary()
words_list = list(word_embedding.stoi.keys())
for word in words_list:
vocab.add(word)
INPUT_DIM = word_embedding.vectors.shape[0]
EMBEDDING_DIM = 100
HIDDEN_DIM = 256
N_LAYERS = 2
BIDIRECTIONAL = True
DROPOUT = 0.3
PAD_IDX = vocab["<pad>"]
UNK_IDX = vocab["<unk>"]
model = RNN(INPUT_DIM,
EMBEDDING_DIM,
HIDDEN_DIM,
N_LAYERS,
BIDIRECTIONAL,
DROPOUT,
PAD_IDX)
model.embedding.weight.data.copy_(word_embedding.vectors)
model.embedding.weight.data[UNK_IDX] = torch.zeros(EMBEDDING_DIM)
model.embedding.weight.data[PAD_IDX] = torch.zeros(EMBEDDING_DIM)
model.to(device)
model.load_state_dict(torch.load('Best_model_RNN.pt', map_location=torch.device('cpu')))
def predict_sentiment(model, sentence, vocab, device):
model.eval()
corpus = [sentence]
tensor = vocab.corpus_to_tensor(corpus)[0].to(device)
tensor = tensor.unsqueeze(1)
length = [len(tensor)]
length_tensor = torch.LongTensor(length)
prediction = torch.sigmoid(model(tensor, length_tensor))
return prediction.item()
def predict(text):
pre = predict_sentiment(model, text, vocab, device)
return {'Positive':(pre),
"Negative": (1- (pre))}
demo = gr.Interface(
fn=predict,
inputs=gr.components.Textbox(label='Nhập comment'),
outputs=gr.components.Label(label='Prediction'),
allow_flagging='never'
)
def run():
demo.launch(share=True)
if __name__ == "__main__":
run()
|