Spaces:
Runtime error
Runtime error
eerrffuunn
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,55 @@
|
|
1 |
-
import
|
2 |
-
from
|
|
|
3 |
import torch
|
4 |
|
5 |
-
# Load the
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
)
|
27 |
|
28 |
-
#
|
29 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import RobertaForSequenceClassification, RobertaTokenizer, Trainer, TrainingArguments
|
2 |
+
from datasets import Dataset
|
3 |
+
import pandas as pd
|
4 |
import torch
|
5 |
|
6 |
+
# Load the dataset
|
7 |
+
df = pd.read_csv("processed_step3.csv")
|
8 |
+
|
9 |
+
# Prepare the dataset for Hugging Face
|
10 |
+
def preprocess_data(row):
|
11 |
+
return {"text": row["full_text"], "labels": row["narratives"]}
|
12 |
+
|
13 |
+
# Create a Dataset object
|
14 |
+
hf_dataset = Dataset.from_pandas(df).map(preprocess_data)
|
15 |
+
|
16 |
+
# Load pre-trained tokenizer and model
|
17 |
+
tokenizer = RobertaTokenizer.from_pretrained("roberta-base")
|
18 |
+
model = RobertaForSequenceClassification.from_pretrained(
|
19 |
+
"roberta-base", num_labels=len(set(df["narratives"])))
|
20 |
+
|
21 |
+
# Tokenize the data
|
22 |
+
def tokenize_function(examples):
|
23 |
+
return tokenizer(examples["text"], padding="max_length", truncation=True)
|
24 |
+
|
25 |
+
hf_dataset = hf_dataset.map(tokenize_function, batched=True)
|
26 |
+
|
27 |
+
# Set Hugging Face TrainingArguments
|
28 |
+
training_args = TrainingArguments(
|
29 |
+
output_dir="./results",
|
30 |
+
evaluation_strategy="epoch",
|
31 |
+
save_strategy="epoch",
|
32 |
+
per_device_train_batch_size=8,
|
33 |
+
num_train_epochs=3,
|
34 |
+
load_best_model_at_end=True,
|
35 |
+
logging_dir="./logs",
|
36 |
+
logging_steps=10,
|
37 |
+
push_to_hub=True, # Push to Hugging Face Model Hub
|
38 |
+
hub_model_id="eerrffuunn/semeval-task"
|
39 |
+
)
|
40 |
+
|
41 |
+
# Trainer for training the model
|
42 |
+
trainer = Trainer(
|
43 |
+
model=model,
|
44 |
+
args=training_args,
|
45 |
+
train_dataset=hf_dataset["train"],
|
46 |
+
eval_dataset=hf_dataset["validation"],
|
47 |
+
tokenizer=tokenizer
|
48 |
)
|
49 |
|
50 |
+
# Train the model
|
51 |
+
trainer.train()
|
52 |
+
|
53 |
+
# Save the model and tokenizer
|
54 |
+
trainer.save_model("semeval_model")
|
55 |
+
tokenizer.save_pretrained("semeval_model")
|