2025-02-26 05:37:10 -05:00
|
|
|
|
import os
|
|
|
|
|
|
import torch
|
2025-02-28 13:47:09 -05:00
|
|
|
|
import weaviate
|
2025-02-28 14:59:54 -05:00
|
|
|
|
import numpy as np
|
2025-02-28 14:58:24 -05:00
|
|
|
|
from datasets import Dataset
|
2025-02-28 14:59:54 -05:00
|
|
|
|
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, TrainingArguments, Trainer, DataCollatorForSeq2Seq
|
2025-02-28 15:02:51 -05:00
|
|
|
|
from weaviate.connect import ConnectionParams
|
2025-02-28 15:02:18 -05:00
|
|
|
|
import weaviate
|
2025-02-26 05:37:10 -05:00
|
|
|
|
|
2025-02-28 14:59:54 -05:00
|
|
|
|
# 1️⃣ Połączenie z Weaviate
|
2025-02-28 15:02:18 -05:00
|
|
|
|
client = weaviate.WeaviateClient(
|
2025-02-28 14:59:54 -05:00
|
|
|
|
connection_params=ConnectionParams.from_params(
|
2025-02-28 14:54:02 -05:00
|
|
|
|
http_host="weaviate",
|
|
|
|
|
|
http_port=8080,
|
|
|
|
|
|
http_secure=False,
|
|
|
|
|
|
grpc_host="weaviate",
|
|
|
|
|
|
grpc_port=50051,
|
|
|
|
|
|
grpc_secure=False,
|
|
|
|
|
|
)
|
2025-02-28 13:47:09 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-02-28 14:59:54 -05:00
|
|
|
|
# 2️⃣ Pobranie dokumentów z Weaviate
|
|
|
|
|
|
def fetch_documents():
|
|
|
|
|
|
query = client.query.get("Document", ["content", "fileName"]).do()
|
|
|
|
|
|
documents = []
|
|
|
|
|
|
for item in query["data"]["Get"]["Document"]:
|
|
|
|
|
|
file_name = item.get("fileName", "unknown_file")
|
|
|
|
|
|
content = item.get("content", "")
|
|
|
|
|
|
if content:
|
|
|
|
|
|
documents.append(f"fileName: {file_name}, content: {content}")
|
|
|
|
|
|
return documents
|
2025-02-28 13:47:09 -05:00
|
|
|
|
|
2025-02-28 14:59:54 -05:00
|
|
|
|
documents = fetch_documents()
|
2025-02-28 13:47:09 -05:00
|
|
|
|
|
2025-02-28 14:59:54 -05:00
|
|
|
|
# 3️⃣ Inicjalizacja modelu
|
|
|
|
|
|
model_name = "allegro/multislav-5lang"
|
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu"
|
2025-02-28 13:47:09 -05:00
|
|
|
|
|
2025-02-28 14:59:54 -05:00
|
|
|
|
model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device)
|
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
2025-02-26 05:37:10 -05:00
|
|
|
|
|
2025-02-28 14:59:54 -05:00
|
|
|
|
# 4️⃣ Przygotowanie danych treningowych
|
|
|
|
|
|
def create_training_data():
|
|
|
|
|
|
return Dataset.from_dict({"text": documents})
|
|
|
|
|
|
|
|
|
|
|
|
dataset = create_training_data()
|
2025-02-28 13:47:09 -05:00
|
|
|
|
split_dataset = dataset.train_test_split(test_size=0.25)
|
|
|
|
|
|
train_dataset = split_dataset["train"]
|
|
|
|
|
|
eval_dataset = split_dataset["test"]
|
|
|
|
|
|
|
2025-02-28 14:58:24 -05:00
|
|
|
|
# 5️⃣ Tokenizacja
|
2025-02-28 13:47:09 -05:00
|
|
|
|
def tokenize_function(examples):
|
|
|
|
|
|
return tokenizer(
|
|
|
|
|
|
examples["text"],
|
|
|
|
|
|
padding="max_length",
|
|
|
|
|
|
truncation=True,
|
2025-02-28 14:59:54 -05:00
|
|
|
|
max_length=512
|
2025-02-26 05:37:10 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-02-28 13:47:09 -05:00
|
|
|
|
tokenized_train = train_dataset.map(tokenize_function, batched=True)
|
|
|
|
|
|
tokenized_eval = eval_dataset.map(tokenize_function, batched=True)
|
|
|
|
|
|
|
2025-02-28 14:58:24 -05:00
|
|
|
|
# 6️⃣ Parametry treningu
|
2025-02-28 13:47:09 -05:00
|
|
|
|
training_args = TrainingArguments(
|
|
|
|
|
|
output_dir="./results",
|
2025-02-28 14:58:24 -05:00
|
|
|
|
evaluation_strategy="steps",
|
2025-02-28 13:47:09 -05:00
|
|
|
|
eval_steps=500,
|
|
|
|
|
|
save_steps=500,
|
2025-02-28 14:58:24 -05:00
|
|
|
|
learning_rate=2e-5,
|
2025-02-28 14:59:54 -05:00
|
|
|
|
per_device_train_batch_size=2,
|
|
|
|
|
|
per_device_eval_batch_size=2,
|
|
|
|
|
|
num_train_epochs=16,
|
2025-02-28 13:47:09 -05:00
|
|
|
|
weight_decay=0.01,
|
2025-02-28 14:59:54 -05:00
|
|
|
|
save_total_limit=2,
|
2025-02-28 13:47:09 -05:00
|
|
|
|
load_best_model_at_end=True,
|
|
|
|
|
|
metric_for_best_model="loss",
|
|
|
|
|
|
greater_is_better=False,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-02-28 14:58:24 -05:00
|
|
|
|
# 7️⃣ Data Collator
|
|
|
|
|
|
data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=model)
|
2025-02-28 13:47:09 -05:00
|
|
|
|
|
2025-02-28 14:58:24 -05:00
|
|
|
|
# 8️⃣ Trening
|
2025-02-28 13:47:09 -05:00
|
|
|
|
trainer = Trainer(
|
|
|
|
|
|
model=model,
|
|
|
|
|
|
args=training_args,
|
|
|
|
|
|
train_dataset=tokenized_train,
|
|
|
|
|
|
eval_dataset=tokenized_eval,
|
|
|
|
|
|
data_collator=data_collator,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
trainer.train()
|
|
|
|
|
|
|
2025-02-28 14:58:24 -05:00
|
|
|
|
# 9️⃣ Zapis modelu
|
2025-02-28 14:59:54 -05:00
|
|
|
|
model.save_pretrained("./models/allegro")
|
|
|
|
|
|
tokenizer.save_pretrained("./models/allegro")
|
2025-02-26 05:37:10 -05:00
|
|
|
|
|
2025-02-28 13:47:09 -05:00
|
|
|
|
print("✅ Model został wytrenowany i zapisany!")
|