Spaces:
Running
Running
Create flan_t5_finetuned_model
Browse filesMODEL_NAME: google/flan-t5-base
- flan_t5_finetuned_model +92 -0
flan_t5_finetuned_model
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Untitled4.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/19SAJcA_N4eQVyeNjT1iFdgpyLvvtSSEw
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
!pip install transformers datasets accelerate -q
|
| 11 |
+
|
| 12 |
+
from google.colab import files
|
| 13 |
+
uploaded = files.upload()
|
| 14 |
+
|
| 15 |
+
from datasets import Dataset
|
| 16 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration, Trainer, TrainingArguments, DataCollatorForSeq2Seq, Seq2SeqTrainingArguments
|
| 17 |
+
import pandas as pd
|
| 18 |
+
import torch
|
| 19 |
+
|
| 20 |
+
# Load CSV file (adjust filename if needed)
|
| 21 |
+
df = pd.read_csv("flan_t5_true_false_dataset.csv")
|
| 22 |
+
|
| 23 |
+
# Convert to Hugging Face Dataset
|
| 24 |
+
dataset = Dataset.from_pandas(df)
|
| 25 |
+
|
| 26 |
+
# Load tokenizer and model
|
| 27 |
+
model_name = "google/flan-t5-base"
|
| 28 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 29 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 30 |
+
|
| 31 |
+
# Preprocessing
|
| 32 |
+
def preprocess(example):
|
| 33 |
+
inputs = tokenizer(example["input"], padding="max_length", truncation=True, max_length=256)
|
| 34 |
+
with tokenizer.as_target_tokenizer():
|
| 35 |
+
labels = tokenizer(example["output"], padding="max_length", truncation=True, max_length=64)
|
| 36 |
+
inputs["labels"] = labels["input_ids"]
|
| 37 |
+
return inputs
|
| 38 |
+
|
| 39 |
+
# Tokenize dataset
|
| 40 |
+
tokenized_dataset = dataset.map(preprocess, batched=True)
|
| 41 |
+
|
| 42 |
+
# Define training arguments
|
| 43 |
+
training_args = Seq2SeqTrainingArguments(
|
| 44 |
+
output_dir="./flan_t5_finetuned_model",
|
| 45 |
+
per_device_train_batch_size=4,
|
| 46 |
+
per_device_eval_batch_size=4, # Added evaluation batch size
|
| 47 |
+
num_train_epochs=3,
|
| 48 |
+
save_steps=500,
|
| 49 |
+
logging_steps=100,
|
| 50 |
+
save_total_limit=1,
|
| 51 |
+
fp16=torch.cuda.is_available()
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Trainer setup
|
| 55 |
+
trainer = Trainer(
|
| 56 |
+
model=model,
|
| 57 |
+
args=training_args,
|
| 58 |
+
train_dataset=tokenized_dataset,
|
| 59 |
+
tokenizer=tokenizer,
|
| 60 |
+
data_collator=DataCollatorForSeq2Seq(tokenizer, model)
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Start training
|
| 64 |
+
trainer.train()
|
| 65 |
+
|
| 66 |
+
!zip -r flan_t5_finetuned_model.zip flan_t5_finetuned_model
|
| 67 |
+
files.download("flan_t5_finetuned_model.zip")
|
| 68 |
+
|
| 69 |
+
import pandas as pd
|
| 70 |
+
|
| 71 |
+
data = [
|
| 72 |
+
{
|
| 73 |
+
"input": f"Convert this fact into a true/false question: The moon is made of cheese {i}.",
|
| 74 |
+
"output": f"The moon is made of cheese {i}. True or False?"
|
| 75 |
+
}
|
| 76 |
+
for i in range(150)
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
df = pd.DataFrame(data)
|
| 80 |
+
df.to_csv("flan_t5_eval.csv", index=False)
|
| 81 |
+
|
| 82 |
+
from google.colab import files
|
| 83 |
+
files.download('flan_t5_eval.csv')
|
| 84 |
+
|
| 85 |
+
!pip install transformers datasets bert-score sentence-transformers -q
|
| 86 |
+
|
| 87 |
+
from google.colab import files
|
| 88 |
+
uploaded = files.upload()
|
| 89 |
+
|
| 90 |
+
EVAL_CSV = "/content/flan_t5_eval.csv"
|
| 91 |
+
|
| 92 |
+
!ls -l ./flan_t5_finetuned
|