Spaces:
Sleeping
Sleeping
grpathak22
commited on
Create modelrun.py
Browse files- modelrun.py +56 -0
modelrun.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tokenizer = T5Tokenizer.from_pretrained('google/mt5-base')
|
2 |
+
model = MT5ForConditionalGeneration.from_pretrained("google/mt5-base")
|
3 |
+
#st.write(model)
|
4 |
+
|
5 |
+
df = pd.read_csv('proverbs.csv')
|
6 |
+
df
|
7 |
+
dataset = Dataset.from_pandas(df)
|
8 |
+
|
9 |
+
def preprocess_function(examples):
|
10 |
+
inputs = examples['Proverb']
|
11 |
+
targets = examples['Meaning']
|
12 |
+
model_inputs = tokenizer(inputs, max_length=128, truncation=True, padding="max_length")
|
13 |
+
with tokenizer.as_target_tokenizer():
|
14 |
+
labels = tokenizer(targets, max_length=128, truncation=True, padding="max_length")
|
15 |
+
model_inputs["labels"] = labels["input_ids"]
|
16 |
+
return model_inputs
|
17 |
+
|
18 |
+
|
19 |
+
tokenized_dataset = dataset.map(preprocess_function, batched=True)
|
20 |
+
|
21 |
+
|
22 |
+
dataset_split = tokenized_dataset.train_test_split(test_size=0.2)
|
23 |
+
|
24 |
+
|
25 |
+
train_dataset = dataset_split['train']
|
26 |
+
test_dataset = dataset_split['test']
|
27 |
+
|
28 |
+
|
29 |
+
print(f"Training dataset size: {len(train_dataset)}")
|
30 |
+
print(f"Testing dataset size: {len(test_dataset)}")
|
31 |
+
|
32 |
+
training_args = TrainingArguments(
|
33 |
+
output_dir="./results",
|
34 |
+
evaluation_strategy="epoch",
|
35 |
+
learning_rate=2e-5,
|
36 |
+
per_device_train_batch_size=4,
|
37 |
+
per_device_eval_batch_size=4,
|
38 |
+
num_train_epochs=3,
|
39 |
+
weight_decay=0.01,
|
40 |
+
save_total_limit=2,
|
41 |
+
save_steps=500,
|
42 |
+
)
|
43 |
+
|
44 |
+
# Initialize Trainer
|
45 |
+
trainer = Trainer(
|
46 |
+
model=model,
|
47 |
+
args=training_args,
|
48 |
+
train_dataset=tokenized_dataset,
|
49 |
+
eval_dataset=tokenized_dataset, # Typically you'd have a separate eval dataset
|
50 |
+
)
|
51 |
+
|
52 |
+
# Fine-tune the model
|
53 |
+
trainer.train()
|
54 |
+
|
55 |
+
model.save_pretrained("./fine-tuned-mt5-marathi-proverbs")
|
56 |
+
tokenizer.save_pretrained("./fine-tuned-mt5-marathi-proverbs")
|