mdj1412 commited on
Commit
fe7c35d
1 Parent(s): 509d266

Upload 5 files

Browse files
Files changed (2) hide show
  1. app.py +38 -30
  2. klue_roberta-small-2400.pt +3 -0
app.py CHANGED
@@ -28,7 +28,7 @@ description = "It is a program that classifies whether it is positive or negativ
28
 
29
  def tokenized_data(tokenizer, inputs):
30
  return tokenizer.batch_encode_plus(
31
- inputs,
32
  return_tensors="pt",
33
  padding="max_length",
34
  max_length=64,
@@ -44,35 +44,42 @@ examples = []
44
  df = pd.read_csv('examples.csv', sep='\t', index_col='Unnamed: 0')
45
  for i in range(2):
46
  idx = random.randint(0, 50)
47
- examples.append(df.iloc[idx, 0])
48
- examples.append(df.iloc[idx, 1])
49
-
50
-
51
-
52
-
53
- model_kor = gr.Interface.load("models/cardiffnlp/twitter-roberta-base-sentiment")
54
- model_eng = gr.Interface.load("models/mdj1412/movie_review_score_discriminator_eng")
55
-
56
-
57
-
58
- def builder(version, inputs):
59
- if version == 'Eng':
60
- model_name = "roberta-base"
61
- step = 1900
62
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  else:
64
- model_name = "klue/roberta-small"
65
- step = 2400
66
 
67
- tokenizer = AutoTokenizer.from_pretrained(model_name)
68
- inputs = tokenized_data(tokenizer, inputs)
69
- file_name = "{}-{}.pt".format(model_name, step)
70
- state_dict = torch.load(file_name)
71
- model = AutoModelForSequenceClassification.from_pretrained(
72
- model_name, num_labels=2, id2label=id2label, label2id=label2id,
73
- state_dict=state_dict
74
- )
75
-
76
  model.eval()
77
  with torch.no_grad():
78
  logits = model(input_ids=inputs['input_ids'],
@@ -84,11 +91,11 @@ def builder(version, inputs):
84
 
85
 
86
  def builder2(inputs):
87
- return model_eng(inputs)
88
 
89
 
90
  demo = gr.Interface(builder, inputs=[gr.inputs.Dropdown(['Eng', 'Kor']), "text"], outputs="text",
91
- title=title, description=description, examples=[examples])
92
 
93
  # demo2 = gr.Interface(builder2, inputs="text", outputs="text",
94
  # title=title, theme="peach",
@@ -101,5 +108,6 @@ demo = gr.Interface(builder, inputs=[gr.inputs.Dropdown(['Eng', 'Kor']), "text"]
101
  # description=description, examples=examples)
102
 
103
  if __name__ == "__main__":
 
104
  demo.launch()
105
  # demo3.launch()
 
28
 
29
  def tokenized_data(tokenizer, inputs):
30
  return tokenizer.batch_encode_plus(
31
+ [inputs],
32
  return_tensors="pt",
33
  padding="max_length",
34
  max_length=64,
 
44
  df = pd.read_csv('examples.csv', sep='\t', index_col='Unnamed: 0')
45
  for i in range(2):
46
  idx = random.randint(0, 50)
47
+ examples.append(['Eng', df.iloc[idx, 0]])
48
+ examples.append(['Kor', df.iloc[idx, 1]])
49
+
50
+
51
+ eng_model_name = "roberta-base"
52
+ eng_step = 1900
53
+ eng_tokenizer = AutoTokenizer.from_pretrained(eng_model_name)
54
+ eng_file_name = "{}-{}.pt".format(eng_model_name, eng_step)
55
+ eng_state_dict = torch.load(eng_file_name)
56
+ eng_model = AutoModelForSequenceClassification.from_pretrained(
57
+ eng_model_name, num_labels=2, id2label=id2label, label2id=label2id,
58
+ state_dict=eng_state_dict
59
+ )
60
+
61
+
62
+ kor_model_name = "klue_roberta-small"
63
+ kor_step = 2400
64
+ kor_tokenizer = AutoTokenizer.from_pretrained(kor_model_name.replace('_', '/'))
65
+ kor_file_name = "{}-{}.pt".format(kor_model_name, kor_step)
66
+ kor_state_dict = torch.load(kor_file_name)
67
+ kor_model = AutoModelForSequenceClassification.from_pretrained(
68
+ kor_model_name.replace('_', '/'), num_labels=2, id2label=id2label, label2id=label2id,
69
+ state_dict=kor_state_dict
70
+ )
71
+
72
+
73
+ def builder(lang, text):
74
+ if lang == 'Eng':
75
+ model = eng_model
76
+ tokenizer = eng_tokenizer
77
  else:
78
+ model = kor_model
79
+ tokenizer = kor_tokenizer
80
 
81
+ inputs = tokenized_data(tokenizer, text)
82
+
 
 
 
 
 
 
 
83
  model.eval()
84
  with torch.no_grad():
85
  logits = model(input_ids=inputs['input_ids'],
 
91
 
92
 
93
  def builder2(inputs):
94
+ return eng_model(inputs)
95
 
96
 
97
  demo = gr.Interface(builder, inputs=[gr.inputs.Dropdown(['Eng', 'Kor']), "text"], outputs="text",
98
+ title=title, description=description, examples=examples)
99
 
100
  # demo2 = gr.Interface(builder2, inputs="text", outputs="text",
101
  # title=title, theme="peach",
 
108
  # description=description, examples=examples)
109
 
110
  if __name__ == "__main__":
111
+ # print(examples)
112
  demo.launch()
113
  # demo3.launch()
klue_roberta-small-2400.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1b572a576888999c3696750507168b1ec8c194b93e3b0a5fb69d5932cb61a410
3
+ size 272408049