Update app.py
Browse files
app.py
CHANGED
@@ -1 +1,64 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration, Trainer, TrainingArguments, DataCollatorForSeq2Seq
|
5 |
+
from datasets import Dataset
|
6 |
+
|
7 |
+
# ✅ 1. Ielādē treniņa datus
|
8 |
+
with open("train.json", "r", encoding="utf-8") as f:
|
9 |
+
train_data = json.load(f)
|
10 |
+
|
11 |
+
# ✅ 2. Pārveido datus Hugging Face formātā
|
12 |
+
dataset = Dataset.from_list([{"input_text": d["question"], "target_text": d["answer"]} for d in train_data])
|
13 |
+
|
14 |
+
# ✅ 3. Izvēlies modeli (piemēram, `mT5-small` vai `facebook/opt-1.3b`)
|
15 |
+
model_name = "google/mt5-small"
|
16 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
+
|
18 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
19 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name).to(device)
|
20 |
+
|
21 |
+
# ✅ 4. Sagatavo datus treniņam
|
22 |
+
def preprocess_data(examples):
|
23 |
+
model_inputs = tokenizer(examples["input_text"], max_length=128, truncation=True)
|
24 |
+
labels = tokenizer(examples["target_text"], max_length=128, truncation=True)
|
25 |
+
model_inputs["labels"] = labels["input_ids"]
|
26 |
+
return model_inputs
|
27 |
+
|
28 |
+
dataset = dataset.map(preprocess_data, batched=True)
|
29 |
+
|
30 |
+
# ✅ 5. Definē trenēšanas parametrus
|
31 |
+
training_args = TrainingArguments(
|
32 |
+
output_dir="./trained_model",
|
33 |
+
evaluation_strategy="epoch",
|
34 |
+
save_strategy="epoch",
|
35 |
+
learning_rate=5e-5,
|
36 |
+
per_device_train_batch_size=4,
|
37 |
+
per_device_eval_batch_size=4,
|
38 |
+
num_train_epochs=3, # Trenē 3 epohas
|
39 |
+
weight_decay=0.01,
|
40 |
+
push_to_hub=True, # Augšupielādē Hugging Face Hub
|
41 |
+
logging_dir="./logs"
|
42 |
+
)
|
43 |
+
|
44 |
+
data_collator = DataCollatorForSeq2Seq(tokenizer, model=model)
|
45 |
+
|
46 |
+
# ✅ 6. Izveido `Trainer` klasi
|
47 |
+
trainer = Trainer(
|
48 |
+
model=model,
|
49 |
+
args=training_args,
|
50 |
+
train_dataset=dataset,
|
51 |
+
tokenizer=tokenizer,
|
52 |
+
data_collator=data_collator
|
53 |
+
)
|
54 |
+
|
55 |
+
# ✅ 7. Sāc trenēšanu
|
56 |
+
trainer.train()
|
57 |
+
|
58 |
+
# ✅ 8. Saglabā trenēto modeli
|
59 |
+
model.save_pretrained("./trained_model")
|
60 |
+
tokenizer.save_pretrained("./trained_model")
|
61 |
+
|
62 |
+
# ✅ 9. Augšupielādē Hugging Face Hub (ja nepieciešams)
|
63 |
+
trainer.push_to_hub()
|
64 |
+
|