mdj1412 commited on
Commit
9660558
1 Parent(s): 7c020ac

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -4
app.py CHANGED
@@ -1,4 +1,8 @@
1
  import gradio as gr
 
 
 
 
2
 
3
  README = """
4
  # Movie Review Score Discriminator
@@ -9,19 +13,63 @@ README = """
9
  """
10
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  title = "Movie Review Score Discriminator"
13
  description = "It is a program that classifies whether it is positive or negative by entering movie reviews. You can choose between the Korean version and the English version."
14
  examples = ["the greatest musicians ", "cold movie "]
15
 
16
 
17
- def greet(name):
18
- return "Hello " + name + "!"
19
 
20
- demo = gr.Interface.load("models/cardiffnlp/twitter-roberta-base-sentiment", inputs="text", outputs="text",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  title=title, theme="peach",
22
  allow_flagging="auto",
23
  description=description, examples=examples)
24
  # demo = gr.Interface(fn=greet, inputs="text", outputs="text")
 
 
 
 
 
25
 
26
  if __name__ == "__main__":
27
- demo.launch()
 
1
  import gradio as gr
2
+ import torch
3
+ import os
4
+ from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer
5
+ from transformers import AutoTokenizer
6
 
7
  README = """
8
  # Movie Review Score Discriminator
 
13
  """
14
 
15
 
16
+ model = "roberta-base"
17
+ learning_rate = 5e-5
18
+ batch_size_train = 64
19
+ step = 1900
20
+
21
+ file_name = "model-{}.pt".format(step)
22
+ state_dict = torch.load(os.path.join(file_name))
23
+
24
+ id2label = {0: "NEGATIVE", 1: "POSITIVE"}
25
+ label2id = {"NEGATIVE": 0, "POSITIVE": 1}
26
+
27
+
28
  title = "Movie Review Score Discriminator"
29
  description = "It is a program that classifies whether it is positive or negative by entering movie reviews. You can choose between the Korean version and the English version."
30
  examples = ["the greatest musicians ", "cold movie "]
31
 
32
 
 
 
33
 
34
+ def tokenized_data(tokenizer, inputs):
35
+ return tokenizer.batch_encode_plus(
36
+ inputs,
37
+ return_tensors="pt",
38
+ padding="max_length",
39
+ max_length=64,
40
+ truncation=True)
41
+
42
+
43
+ def greet(text):
44
+ tokenizer = AutoTokenizer.from_pretrained(model)
45
+ model = AutoModelForSequenceClassification.from_pretrained(
46
+ model, num_labels=2, id2label=id2label, label2id=label2id,
47
+ state_dict=state_dict
48
+ )
49
+ inputs = tokenized_data(tokenizer, text)
50
+
51
+
52
+ # 모델의 매개변수 Tensor를 mps Tensor로 변환
53
+ # model.to(device)
54
+ # evaluation mode or training mode
55
+ model.eval()
56
+
57
+ with torch.no_grad():
58
+ # logits.shape = torch.Size([ batch_size, 2 ])
59
+ logits = model(input_ids=inputs[0], attention_mask=inputs[1]).logits
60
+
61
+ return logits
62
+
63
+ demo1 = gr.Interface.load("models/cardiffnlp/twitter-roberta-base-sentiment", inputs="text", outputs="text",
64
  title=title, theme="peach",
65
  allow_flagging="auto",
66
  description=description, examples=examples)
67
  # demo = gr.Interface(fn=greet, inputs="text", outputs="text")
68
+
69
+ demo2 = gr.Interface(fn=greet, inputs="text", outputs="text",
70
+ title=title, theme="peach",
71
+ allow_flagging="auto",
72
+ description=description, examples=examples)
73
 
74
  if __name__ == "__main__":
75
+ demo2.launch()