Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import zipfile
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import mindspore
|
7 |
+
import mindspore.nn as nn
|
8 |
+
import mindspore.numpy as mnp
|
9 |
+
import mindspore.ops as ops
|
10 |
+
import mindspore.dataset as dataset
|
11 |
+
from mindspore import Tensor
|
12 |
+
from mindspore import load_checkpoint, load_param_into_net
|
13 |
+
from mindspore.common.initializer import Uniform, HeUniform
|
14 |
+
|
15 |
+
|
16 |
+
def load_glove():
|
17 |
+
embeddings = []
|
18 |
+
tokens = []
|
19 |
+
with open("./lstm/glove.6B.100d.txt", encoding='utf-8') as gf:
|
20 |
+
for glove in gf:
|
21 |
+
word, embedding = glove.split(maxsplit=1)
|
22 |
+
tokens.append(word)
|
23 |
+
embeddings.append(np.fromstring(embedding, dtype=np.float32, sep=' '))
|
24 |
+
# 添加 <unk>, <pad> 两个特殊占位符对应的embedding
|
25 |
+
embeddings.append(np.random.rand(100))
|
26 |
+
embeddings.append(np.zeros((100,), np.float32))
|
27 |
+
|
28 |
+
vocab = dataset.text.Vocab.from_list(tokens, special_tokens=["<unk>", "<pad>"], special_first=False)
|
29 |
+
embeddings = np.array(embeddings).astype(np.float32)
|
30 |
+
return vocab, embeddings
|
31 |
+
|
32 |
+
class RNN(nn.Cell):
|
33 |
+
def __init__(self, embeddings, hidden_dim, output_dim, n_layers,
|
34 |
+
bidirectional, dropout, pad_idx):
|
35 |
+
super().__init__()
|
36 |
+
vocab_size, embedding_dim = embeddings.shape
|
37 |
+
self.embedding = nn.Embedding(vocab_size, embedding_dim, embedding_table=Tensor(embeddings), padding_idx=pad_idx)
|
38 |
+
self.rnn = nn.LSTM(embedding_dim,
|
39 |
+
hidden_dim,
|
40 |
+
num_layers=n_layers,
|
41 |
+
bidirectional=bidirectional,
|
42 |
+
dropout=dropout,
|
43 |
+
batch_first=True)
|
44 |
+
weight_init = HeUniform(math.sqrt(5))
|
45 |
+
bias_init = Uniform(1 / math.sqrt(hidden_dim * 2))
|
46 |
+
self.fc = nn.Dense(hidden_dim * 2, output_dim, weight_init=weight_init, bias_init=bias_init)
|
47 |
+
self.dropout = nn.Dropout(1 - dropout)
|
48 |
+
self.sigmoid = ops.Sigmoid()
|
49 |
+
|
50 |
+
def construct(self, inputs):
|
51 |
+
embedded = self.dropout(self.embedding(inputs))
|
52 |
+
_, (hidden, _) = self.rnn(embedded)
|
53 |
+
hidden = self.dropout(mnp.concatenate((hidden[-2, :, :], hidden[-1, :, :]), axis=1))
|
54 |
+
output = self.fc(hidden)
|
55 |
+
return self.sigmoid(output)
|
56 |
+
|
57 |
+
score_map = {
|
58 |
+
1: "Positive",
|
59 |
+
0: "Negative"
|
60 |
+
}
|
61 |
+
|
62 |
+
def predict_sentiment(model, vocab, sentence):
|
63 |
+
model.set_train(False)
|
64 |
+
tokenized = sentence.lower().split()
|
65 |
+
indexed = vocab.tokens_to_ids(tokenized)
|
66 |
+
tensor = mindspore.Tensor(indexed, mindspore.int32)
|
67 |
+
tensor = tensor.expand_dims(0)
|
68 |
+
prediction = model(tensor)
|
69 |
+
return prediction.asnumpy()
|
70 |
+
|
71 |
+
def prefict_emotion(sentence):
|
72 |
+
# 加载网路
|
73 |
+
hidden_size = 256
|
74 |
+
output_size = 1
|
75 |
+
num_layers = 2
|
76 |
+
bidirectional = True
|
77 |
+
dropout = 0.5
|
78 |
+
lr = 0.00
|
79 |
+
|
80 |
+
vocab, embeddings = load_glove()
|
81 |
+
pad_idx = vocab.tokens_to_ids('<pad>')
|
82 |
+
net = RNN(embeddings, hidden_size, output_size, num_layers, bidirectional, dropout, pad_idx)
|
83 |
+
|
84 |
+
# 将模型参数存入parameter的字典中
|
85 |
+
param_dict = load_checkpoint("./lstm/sentiment-analysis.ckpt")
|
86 |
+
|
87 |
+
# 将参数加载到网络中
|
88 |
+
load_param_into_net(net, param_dict)
|
89 |
+
model = Model(net)
|
90 |
+
|
91 |
+
# 预测
|
92 |
+
pred = predict_sentiment(model, vocab, sentence)
|
93 |
+
result = {
|
94 |
+
"Positive 🙂": pred,
|
95 |
+
"Negative 🙃": 1-pred,
|
96 |
+
}
|
97 |
+
return result
|
98 |
+
|
99 |
+
gr.Interface(
|
100 |
+
fn=prefict_emotion,
|
101 |
+
inputs=gr.inputs.Textbox(
|
102 |
+
lines=3,
|
103 |
+
placeholder="Type a phrase that has some emotion",
|
104 |
+
label="Input Text",
|
105 |
+
),
|
106 |
+
outputs="label",
|
107 |
+
title="Sentiment Analysis",
|
108 |
+
examples=[
|
109 |
+
"This film is terrible",
|
110 |
+
"This film is great",
|
111 |
+
],
|
112 |
+
).launch(share=True)
|