File size: 2,399 Bytes
a83f80b
9660558
 
 
 
a83f80b
d564f5f
 
 
 
 
c49cab1
d564f5f
 
766e63e
9660558
 
 
 
 
 
 
 
 
 
 
 
7c020ac
 
 
 
 
 
9660558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c020ac
 
 
 
9660558
 
 
 
 
7c020ac
 
9660558
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import gradio as gr
import torch
import os
from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer
from transformers import AutoTokenizer

README = """
    # Movie Review Score Discriminator
    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.
    ## Usage

"""


model = "roberta-base"
learning_rate = 5e-5
batch_size_train = 64
step = 1900

file_name = "model-{}.pt".format(step)
state_dict = torch.load(os.path.join(file_name))

id2label = {0: "NEGATIVE", 1: "POSITIVE"}
label2id = {"NEGATIVE": 0, "POSITIVE": 1}


title = "Movie Review Score Discriminator"
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."
examples = ["the greatest musicians ", "cold movie "]



def tokenized_data(tokenizer, inputs):
    return tokenizer.batch_encode_plus(
            inputs,
            return_tensors="pt",
            padding="max_length",
            max_length=64,
            truncation=True)


def greet(text):
    tokenizer = AutoTokenizer.from_pretrained(model)
    model = AutoModelForSequenceClassification.from_pretrained(
        model, num_labels=2, id2label=id2label, label2id=label2id,
        state_dict=state_dict
    )
    inputs = tokenized_data(tokenizer, text)


    # 모델의 매개변수 Tensor를 mps Tensor로 변환
    # model.to(device)
    # evaluation mode or training mode
    model.eval()

    with torch.no_grad():
        # logits.shape = torch.Size([ batch_size, 2 ])
        logits = model(input_ids=inputs[0], attention_mask=inputs[1]).logits

    return logits

demo1 = gr.Interface.load("models/cardiffnlp/twitter-roberta-base-sentiment", inputs="text", outputs="text", 
                         title=title, theme="peach",
                         allow_flagging="auto",
                         description=description, examples=examples)
# demo = gr.Interface(fn=greet, inputs="text", outputs="text")

demo2 = gr.Interface(fn=greet, inputs="text", outputs="text", 
                         title=title, theme="peach",
                         allow_flagging="auto",
                         description=description, examples=examples)
    
if __name__ == "__main__":
    demo2.launch()