micole66 commited on
Commit
8d9bc51
1 Parent(s): bc8cc98

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import XLMRobertaTokenizer, XLMRobertaForSequenceClassification, Trainer, TrainingArguments
2
+ import datasets
3
+
4
+ # Load the pre-trained XLM-Roberta-Large model and tokenizer
5
+ model_name = 'xlm-roberta-large'
6
+ tokenizer = XLMRobertaTokenizer.from_pretrained(model_name)
7
+ model = XLMRobertaForSequenceClassification.from_pretrained(model_name, num_labels=2)
8
+
9
+ # Load the sentiment analysis dataset
10
+ dataset = datasets.load_dataset('imdb')
11
+
12
+ # Tokenize the dataset
13
+ def tokenize(batch):
14
+ return tokenizer(batch['text'], padding=True, truncation=True)
15
+
16
+ dataset = dataset.map(tokenize, batched=True)
17
+
18
+ # Fine-tune the model on the dataset
19
+ training_args = TrainingArguments(
20
+ output_dir='./results',
21
+ evaluation_strategy='epoch',
22
+ learning_rate=2e-5,
23
+ per_device_train_batch_size=8,
24
+ per_device_eval_batch_size=8,
25
+ num_train_epochs=3,
26
+ weight_decay=0.01,
27
+ push_to_hub=False,
28
+ logging_dir='./logs',
29
+ logging_steps=10,
30
+ load_best_model_at_end=True,
31
+ metric_for_best_model='accuracy'
32
+ )
33
+
34
+ trainer = Trainer(
35
+ model=model,
36
+ args=training_args,
37
+ train_dataset=dataset['train'],
38
+ eval_dataset=dataset['test']
39
+ )
40
+
41
+ trainer.train()
42
+
43
+ import torch
44
+
45
+ # Load the fine-tuned XLM-Roberta-Large model
46
+ model_path = './results/checkpoint-1000'
47
+ model = XLMRobertaForSequenceClassification.from_pretrained(model_path)
48
+
49
+ # Create a function that takes a text input and returns the predicted sentiment label
50
+ def predict_sentiment(text):
51
+ inputs = tokenizer(text, padding=True, truncation=True, return_tensors='pt')
52
+ outputs = model(**inputs)
53
+ logits = outputs.logits
54
+ predicted_class = torch.argmax(logits, dim=1)
55
+ return 'positive' if predicted_class == 1 else 'negative'
56
+
57
+ import gradio as gr
58
+
59
+ # Create a Gradio interface for the predict_sentiment function
60
+ iface = gr.Interface(
61
+ fn=predict_sentiment,
62
+ inputs=gr.inputs.Textbox(placeholder='Enter text here...'),
63
+ outputs=gr.outputs.Textbox(placeholder='Sentiment prediction...')
64
+ )
65
+
66
+ # Launch the interface
67
+ iface.launch()