Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,65 @@
|
|
1 |
import numpy as np
|
2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
|
5 |
-
def flip_text(x):
|
6 |
-
return x[::-1]
|
7 |
|
8 |
|
9 |
def flip_image(x):
|
@@ -11,8 +67,8 @@ def flip_image(x):
|
|
11 |
|
12 |
|
13 |
with gr.Blocks() as demo:
|
14 |
-
gr.Markdown("
|
15 |
-
with gr.Tab("
|
16 |
text_input = gr.Textbox()
|
17 |
text_output = gr.Textbox()
|
18 |
text_button = gr.Button("Flip")
|
|
|
1 |
import numpy as np
|
2 |
import gradio as gr
|
3 |
+
from huggingface_hub import login
|
4 |
+
login(token="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")
|
5 |
+
|
6 |
+
dict_ = {
|
7 |
+
0: "negative",
|
8 |
+
1: "positive",
|
9 |
+
2: "neutral"}
|
10 |
+
path_sent = "/content/drive/MyDrive/company-review-analysis-model/sentiment_checkpoint"
|
11 |
+
tokenizer_sent = AutoTokenizer.from_pretrained(path_sent, use_fast=False)
|
12 |
+
model_sent = AutoModelForSequenceClassification.from_pretrained(path_sent, num_labels=3).to(device)
|
13 |
+
def cvt2cls(data):
|
14 |
+
data = list(set(data))
|
15 |
+
try:
|
16 |
+
data.remove(20)
|
17 |
+
except:
|
18 |
+
pass
|
19 |
+
for i, num in enumerate(data):
|
20 |
+
if num == 20:
|
21 |
+
continue
|
22 |
+
if num>=10:
|
23 |
+
data[i] -= 10
|
24 |
+
return data
|
25 |
+
ner_tags = {0: 'B-chỗ để xe', 1: 'B-con người', 2: 'B-công việc', 3: 'B-cơ sở vật chất', 4: 'B-dự án', 5: 'B-lương', 6: 'B-môi trường làm việc', 7: 'B-ot/thời gian', 8: 'B-văn phòng', 9: 'B-đãi ngộ', 10: 'I-chỗ để xe', 11: 'I-con người', 12: 'I-công việc', 13: 'I-cơ sở vật chất', 14: 'I-dự án', 15: 'I-lương', 16: 'I-môi trường làm việc', 17: 'I-ot/thời gian', 18: 'I-văn phòng', 19: 'I-đãi ngộ', 20: 'O'}
|
26 |
+
topic_tags = {0: 'chỗ để xe', 1: 'con người', 2: 'công việc', 3: 'cơ sở vật chất', 4: 'dự án', 5: 'lương', 6: 'môi trường làm việc', 7: 'ot/thời gian', 8: 'văn phòng', 9: 'đãi ngộ'}
|
27 |
+
path_topic = "/content/drive/MyDrive/company-review-analysis-model/topic_checkpoint"
|
28 |
+
num_labels = 20
|
29 |
+
config = RobertaConfig.from_pretrained(path_topic, num_labels=num_labels)
|
30 |
+
tokenizer_topic = AutoTokenizer.from_pretrained(path_topic, use_fast=False)
|
31 |
+
model_topic = PhoBertLstmCrf.from_pretrained(path_topic, config=config, from_tf=False).to(device)
|
32 |
+
model_topic.resize_token_embeddings(len(tokenizer_topic))
|
33 |
+
|
34 |
+
|
35 |
+
def review_company(sent: str):
|
36 |
+
try:
|
37 |
+
sent = normalize(text=sent) # segment input sentence, maybe raise ConnectionError: HTTPConnectionPool())
|
38 |
+
except:
|
39 |
+
pass
|
40 |
+
input_sent = torch.tensor([tokenizer_sent.encode(sent)]).to(device)
|
41 |
+
with torch.no_grad():
|
42 |
+
out_sent = model_sent(input_sent)
|
43 |
+
logits_sent = out_sent.logits.softmax(dim=-1).tolist()[0]
|
44 |
+
pred_sent = dict_[np.argmax(logits_sent)]
|
45 |
+
|
46 |
+
try:
|
47 |
+
sent = replace_all(text=sent) # segment input sentence, maybe raise ConnectionError: HTTPConnectionPool())
|
48 |
+
except:
|
49 |
+
pass
|
50 |
+
sent_segment = rdrsegmenter.tokenize(sent)
|
51 |
+
dump = [[i, 'O'] for s in sent_segment for i in s]
|
52 |
+
dump_set = NerDataset(feature_for_phobert([dump], tokenizer=tokenizer_topic, use_crf=True))
|
53 |
+
dump_iter = DataLoader(dump_set, batch_size=1)
|
54 |
+
with torch.no_grad():
|
55 |
+
for idx, batch in enumerate(dump_iter):
|
56 |
+
batch = { k:v.to(device) for k, v in batch.items() }
|
57 |
+
outputs = model_topic(**batch)
|
58 |
+
return list(set([topic_tags[i] for i in cvt2cls(outputs["tags"][0])]))
|
59 |
+
|
60 |
+
return pred_sent
|
61 |
|
62 |
|
|
|
|
|
63 |
|
64 |
|
65 |
def flip_image(x):
|
|
|
67 |
|
68 |
|
69 |
with gr.Blocks() as demo:
|
70 |
+
gr.Markdown("Demo projects Review Company and Resume parser phase 1.")
|
71 |
+
with gr.Tab("Review Company"):
|
72 |
text_input = gr.Textbox()
|
73 |
text_output = gr.Textbox()
|
74 |
text_button = gr.Button("Flip")
|