Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import sys
|
3 |
+
import dataset
|
4 |
+
import engine
|
5 |
+
from model import BERTBaseUncased
|
6 |
+
from tokenizer import tokenizer
|
7 |
+
|
8 |
+
|
9 |
+
config = {
|
10 |
+
"device":
|
11 |
+
}
|
12 |
+
|
13 |
+
T = tokenizer.TweetTokenizer(
|
14 |
+
preserve_handles=True, preserve_hashes=True, preserve_case=False, preserve_url=False)
|
15 |
+
|
16 |
+
def preprocess(text):
|
17 |
+
tokens = T.tokenize(text)
|
18 |
+
print(tokens, file=sys.stderr)
|
19 |
+
ptokens = []
|
20 |
+
for index, token in enumerate(tokens):
|
21 |
+
if "@" in token:
|
22 |
+
if index > 0:
|
23 |
+
# check if previous token was mention
|
24 |
+
if "@" in tokens[index-1]:
|
25 |
+
pass
|
26 |
+
else:
|
27 |
+
ptokens.append("mention_0")
|
28 |
+
else:
|
29 |
+
ptokens.append("mention_0")
|
30 |
+
else:
|
31 |
+
ptokens.append(token)
|
32 |
+
|
33 |
+
print(ptokens, file=sys.stderr)
|
34 |
+
return " ".join(ptokens)
|
35 |
+
|
36 |
+
|
37 |
+
def sentence_prediction(sentence):
|
38 |
+
sentence = preprocess(sentence)
|
39 |
+
model_path = config.MODEL_PATH
|
40 |
+
|
41 |
+
test_dataset = dataset.BERTDataset(
|
42 |
+
review=[sentence],
|
43 |
+
target=[0]
|
44 |
+
)
|
45 |
+
|
46 |
+
test_data_loader = torch.utils.data.DataLoader(
|
47 |
+
test_dataset,
|
48 |
+
batch_size=config.VALID_BATCH_SIZE,
|
49 |
+
num_workers=3
|
50 |
+
)
|
51 |
+
|
52 |
+
device = config.device
|
53 |
+
|
54 |
+
model = BERTBaseUncased()
|
55 |
+
model.load_state_dict(torch.load(
|
56 |
+
model_path, map_location=torch.device(device)))
|
57 |
+
model.to(device)
|
58 |
+
|
59 |
+
outputs, [] = engine.predict_fn(test_data_loader, model, device)
|
60 |
+
print(outputs)
|
61 |
+
return outputs[0]
|
62 |
+
|
63 |
+
demo = gr.Interface(
|
64 |
+
fn=sentence_prediction,
|
65 |
+
inputs=gr.Textbox(placeholder="Enter a sentence here..."),
|
66 |
+
outputs="label",
|
67 |
+
interpretation="default",
|
68 |
+
examples=[["!"]])
|
69 |
+
|
70 |
+
demo.launch()
|