File size: 1,367 Bytes
002956e
55efbe6
780b9c9
272c38a
 
b1dd89b
3f44762
 
 
de1ce9e
 
5108918
 
 
f4731d9
 
 
426e741
 
 
 
 
 
 
46845f7
426e741
 
 
 
 
 
 
 
 
 
 
 
b1dd89b
 
 
 
 
 
cc156dc
c807b76
b1dd89b
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
import gradio as gr

from datasets import load_dataset
imdb = load_dataset("imdb")

from transformers import AutoTokenizer, pipeline
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")

def preprocess_function(examples):
    return tokenizer(examples["text"], truncation=True)
    
tokenized_imdb = imdb.map(preprocess_function, batched=True)

from transformers import DataCollatorWithPadding
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)

from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer
model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased", num_labels=2)

training_args = TrainingArguments(
    output_dir="./results",
    learning_rate=2e-5,
    per_device_train_batch_size=16,
    per_device_eval_batch_size=16,
    num_train_epochs=0.01,
    weight_decay=0.01,
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_imdb["train"],
    eval_dataset=tokenized_imdb["test"],
    tokenizer=tokenizer,
    data_collator=data_collator,
)

trainer.train()

def greet(text):
    pipe = pipeline("sentiment-analysis", tokenizer=tokenizer, model=model)
    return pipe(text)[0]['label']
    
iface = gr.Interface(fn=greet, inputs=gr.inputs.Textbox(placeholder="Please enter the sentence...", lines=5), outputs="text")

iface.launch()