nam194 commited on
Commit
988450a
1 Parent(s): 4e55f8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -24
app.py CHANGED
@@ -1,15 +1,15 @@
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:
@@ -24,20 +24,18 @@ def cvt2cls(data):
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]
@@ -55,12 +53,9 @@ def review_company(sent: str):
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):
66
  return np.fliplr(x)
@@ -71,17 +66,17 @@ with gr.Blocks() as demo:
71
  with gr.Tab("Review Company"):
72
  text_input = gr.Textbox()
73
  text_output = gr.Textbox()
74
- text_button = gr.Button("Flip")
75
- with gr.Tab("Flip Image"):
76
  with gr.Row():
77
  image_input = gr.Image()
78
  image_output = gr.Image()
79
- image_button = gr.Button("Flip")
80
 
81
- with gr.Accordion("Open for More!"):
82
- gr.Markdown("Look at me...")
83
 
84
- text_button.click(flip_text, inputs=text_input, outputs=text_output)
85
  image_button.click(flip_image, inputs=image_input, outputs=image_output)
86
 
87
  demo.launch()
 
1
  import numpy as np
2
  import gradio as gr
3
+ from imports import *
4
  from huggingface_hub import login
5
  login(token="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")
6
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
  dict_ = {
8
  0: "negative",
9
  1: "positive",
10
  2: "neutral"}
11
+ tokenizer_sent = AutoTokenizer.from_pretrained("nam194/sentiment", use_fast=False)
12
+ model_sent = AutoModelForSequenceClassification.from_pretrained("nam194/sentiment", num_labels=3, use_auth_token=True).to(device)
 
13
  def cvt2cls(data):
14
  data = list(set(data))
15
  try:
 
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
+ config = RobertaConfig.from_pretrained("nam194/ner", num_labels=20)
28
+ tokenizer_topic = AutoTokenizer.from_pretrained("nam194/ner", use_fast=False)
29
+ model_topic = PhoBertLstmCrf.from_pretrained("nam194/ner", config=config, from_tf=False).to(device)
 
 
30
  model_topic.resize_token_embeddings(len(tokenizer_topic))
31
 
32
 
33
+ def sentiment(sent: str):
34
  try:
35
+ sent_ = normalize(text=sent_) # segment input sentence, maybe raise ConnectionError: HTTPConnectionPool())
36
  except:
37
  pass
38
+ input_sent = torch.tensor([tokenizer_sent.encode(sent_)]).to(device)
39
  with torch.no_grad():
40
  out_sent = model_sent(input_sent)
41
  logits_sent = out_sent.logits.softmax(dim=-1).tolist()[0]
 
53
  for idx, batch in enumerate(dump_iter):
54
  batch = { k:v.to(device) for k, v in batch.items() }
55
  outputs = model_topic(**batch)
56
+ pred_topic = list(set([topic_tags[i] for i in cvt2cls(outputs["tags"][0])]))
57
+ return {"sentiment": pred_sent, "topic": pred_topic}
58
+
 
 
 
59
 
60
  def flip_image(x):
61
  return np.fliplr(x)
 
66
  with gr.Tab("Review Company"):
67
  text_input = gr.Textbox()
68
  text_output = gr.Textbox()
69
+ text_button = gr.Button("Predict")
70
+ with gr.Tab("Extract infomation from resume"):
71
  with gr.Row():
72
  image_input = gr.Image()
73
  image_output = gr.Image()
74
+ image_button = gr.Button("Predict")
75
 
76
+ # with gr.Accordion("Open for More!"):
77
+ # gr.Markdown("Look at me...")
78
 
79
+ text_button.click(sentiment, inputs=text_input, outputs=text_output)
80
  image_button.click(flip_image, inputs=image_input, outputs=image_output)
81
 
82
  demo.launch()