Dodanie testu na koniec treningu

This commit is contained in:
l.gabrysiak 2025-02-25 18:01:28 +01:00
parent 5aef37828d
commit bb183461ba
1 changed files with 21 additions and 1 deletions

22
hft.py
View File

@ -192,7 +192,7 @@ trainer.train()
def generate_answer(question, model, tokenizer, source_mapper, max_length=200): def generate_answer(question, model, tokenizer, source_mapper, max_length=200):
inputs = tokenizer(question, return_tensors="pt", truncation=True, max_length=512) inputs = tokenizer(question, return_tensors="pt", truncation=True, max_length=512)
outputs = model.generate( outputs = model.base_model.generate(
**inputs, **inputs,
max_length=max_length, max_length=max_length,
num_return_sequences=1, num_return_sequences=1,
@ -205,3 +205,23 @@ def generate_answer(question, model, tokenizer, source_mapper, max_length=200):
# Pobierz źródło z ostatniego tokena # Pobierz źródło z ostatniego tokena
last_token_id = outputs.sequences[0][-1].item() last_token_id = outputs.sequences[0][-1].item()
source_idx = model.source_embeddi source_idx = model.source_embeddi
# Po zakończeniu treningu modelu
# Przygotowanie niezbędnych komponentów
model.eval() # Przełącz model w tryb ewaluacji
model = model.to("cuda" if torch.cuda.is_available() else "cpu") # Przenieś model na GPU, jeśli jest dostępne
# Przykładowe pytanie
question = "Ile dni urlopu przysługuje pracownikowi?"
# Generowanie odpowiedzi
answer = generate_answer(question, model, tokenizer, source_mapper)
# Wyświetlenie wyniku
print("Pytanie:", question)
print("Odpowiedź:", answer)