Spaces:
Runtime error
Runtime error
File size: 2,269 Bytes
24b97e8 77a2a1e d29bb72 9242c1b d29bb72 24b97e8 d29bb72 0ec3c66 d29bb72 9242c1b d29bb72 9242c1b d29bb72 9242c1b d29bb72 9242c1b d29bb72 9242c1b d29bb72 77a2a1e d29bb72 12ddec0 d4034ee 36c6aa0 9242c1b 36c6aa0 12ddec0 36c6aa0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import os
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoModelForSeq2SeqLM, AutoTokenizer, Trainer, TrainingArguments, EncoderDecoderCache
from datasets import load_dataset
from huggingface_hub import login
hf_token = os.getenv("HF_TOKEN")
if hf_token is None:
raise ValueError("Il token HF_TOKEN non è impostato nelle variabili d'ambiente")
login(hf_token)
# Carica il dataset dal file JSONL
dataset = load_dataset("json", data_files="data.jsonl")
# Definisci il modello e il tokenizer
#model_name = "meta-llama/Llama-2-7b-hf"
model_name = "slarkprime/Llama-2-7b-QLoRA"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Sposta il modello su GPU se disponibile
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
# Tokenizza il dataset
def preprocess_data(example):
inputs = tokenizer(example["text"], truncation=True, max_length=256)
inputs["labels"] = inputs["input_ids"].copy()
return inputs
tokenized_dataset = dataset.map(preprocess_data, batched=True)
# Configura i parametri di addestramento
training_args = TrainingArguments(
output_dir="./results",
eval_strategy="no",
learning_rate=2e-5,
per_device_train_batch_size=4,
num_train_epochs=3,
weight_decay=0.01,
fp16=torch.cuda.is_available(), # Usa mixed precision se GPU è disponibile
)
# Inizializza il Trainer
def start_training():
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_dataset["train"]
)
trainer.train()
trainer.push_to_hub("to_validate_model")
return "Training completato e caricato"
def answer_question(question):
inputs = tokenizer(question, return_tensors="pt", truncation=True, max_length=128).to(device)
outputs = model.generate(inputs["input_ids"], max_length=50, pad_token_id=tokenizer.eos_token_id)
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
return answer
iface = gr.Interface(fn=answer_question, inputs="text", outputs="text")
train_interface = gr.Interface(fn=start_training, inputs=[], outputs="text")
app = gr.TabbedInterface([iface, train_interface], ["Q&A", "Avvia Training"])
iface.launch() |