Spaces:
Build error
Build error
Download prueba.py from iespsurmreqmer/fine_tunning: direct link, hf CLI and curl.
- Browser
- Download file 2.81 kB
-
https://huggingface.co/spaces/iespsurmreqmer/fine_tunning/resolve/main/prueba.py
- Command line
-
hf download hf://spaces/iespsurmreqmer/fine_tunning/prueba.py
-
curl -L -o prueba.py https://huggingface.co/spaces/iespsurmreqmer/fine_tunning/resolve/main/prueba.py
2.81 kB
| # 2️⃣ Importar librerías | |
| from datasets import load_dataset | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer | |
| import evaluate | |
| import numpy as np | |
| # 3️⃣ Cargar dataset educativo | |
| dataset = load_dataset("haider0941/Educational_Noneducational_Dataset") | |
| print(dataset['train'][0]) | |
| print("----------3----------") | |
| # Arreglo del dataset | |
| def fix_dataset(example): | |
| # Extrae la primera clave (el texto) | |
| text = list(example.keys())[0] # la clave que no sea 'educational' | |
| label = example['educational'] # etiqueta | |
| return {"text": text, "label": 0 if label == "educational" else 1} | |
| dataset = dataset.map(fix_dataset) | |
| print(dataset["train"][0]) | |
| # {'text': 'What is photosynthesis?', 'label': 0} | |
| print("--------Dataset fix--------------") | |
| # 4️⃣ Definir modelo y tokenizer | |
| model_name = "distilbert-base-uncased" # modelo ligero | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| print("----------4----------") | |
| # 5️⃣ Tokenizar dataset | |
| def tokenize_function(example): | |
| return tokenizer(example["text"], padding="max_length", truncation=True, max_length=128) | |
| tokenized_datasets = dataset.map(tokenize_function, batched=True) | |
| print("----------5----------") | |
| # 6️⃣ Convertir etiquetas a enteros → ELIMINADO, ya existe columna 'label' | |
| # label2id = {"educational":0, "noneducational":1} | |
| # tokenized_datasets = tokenized_datasets.map(lambda x: {"label": label2id[x["response_name"]]}, batched=False) | |
| print("----------6----------") | |
| # 7️⃣ Preparar modelo para clasificación | |
| model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2) | |
| print("----------7----------") | |
| # 8️⃣ Definir métricas | |
| accuracy = evaluate.load("accuracy") | |
| f1 = evaluate.load("f1") | |
| def compute_metrics(eval_pred): | |
| logits, labels = eval_pred | |
| predictions = logits.argmax(axis=-1) | |
| return {"accuracy": accuracy.compute(predictions=predictions, references=labels)["accuracy"], | |
| "f1": f1.compute(predictions=predictions, references=labels)["f1"]} | |
| print("----------8----------") | |
| # 9️⃣ Definir argumentos de entrenamiento | |
| training_args = TrainingArguments( | |
| output_dir="./results", | |
| evaluation_strategy="epoch", | |
| learning_rate=5e-5, | |
| per_device_train_batch_size=16, | |
| per_device_eval_batch_size=16, | |
| num_train_epochs=3, | |
| weight_decay=0.01, | |
| ) | |
| print("----------9----------") | |
| # 🔟 Inicializar Trainer | |
| trainer = Trainer( | |
| model=model, | |
| args=training_args, | |
| train_dataset=tokenized_datasets["train"], | |
| eval_dataset=tokenized_datasets["test"], | |
| compute_metrics=compute_metrics, | |
| ) | |
| print("----------10----------") | |
| # 1️⃣1️⃣ Entrenar modelo | |
| trainer.train() | |
| print("----------11----------") | |
| # 1️⃣2️⃣ Evaluar modelo | |
| trainer.evaluate() | |
| print("----------12----------") | |