Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,9 @@ from dataclasses import dataclass, field
|
|
| 7 |
import gradio as gr
|
| 8 |
from datasets import load_dataset
|
| 9 |
from huggingface_hub import HfApi, Repository
|
| 10 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer,
|
|
|
|
|
|
|
| 11 |
|
| 12 |
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 13 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
|
@@ -59,46 +61,44 @@ class HuggingFaceModelProcessor:
|
|
| 59 |
os.makedirs(self.DOWNLOAD_FOLDER, exist_ok=True)
|
| 60 |
os.makedirs(self.OUTPUT_FOLDER, exist_ok=True)
|
| 61 |
|
| 62 |
-
# ----------------- Entrenamiento -----------------
|
| 63 |
-
def
|
| 64 |
-
print(f"Iniciando entrenamiento de {model_id} con dataset {dataset_name}")
|
| 65 |
dataset = load_dataset(dataset_name, split="train", token=token)
|
| 66 |
|
| 67 |
tokenizer = AutoTokenizer.from_pretrained(model_id, token=token)
|
| 68 |
-
model =
|
| 69 |
|
|
|
|
| 70 |
def tokenize_function(examples):
|
| 71 |
return tokenizer(examples["text"], truncation=True, padding="max_length", max_length=512)
|
| 72 |
|
| 73 |
tokenized_dataset = dataset.map(tokenize_function, batched=True, remove_columns=dataset.column_names)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
num_train_epochs=1,
|
| 81 |
-
per_device_train_batch_size=2,
|
| 82 |
-
save_total_limit=1,
|
| 83 |
-
logging_dir=f"{outdir}/logs",
|
| 84 |
-
logging_steps=10,
|
| 85 |
-
save_steps=200,
|
| 86 |
-
report_to=[],
|
| 87 |
)
|
| 88 |
|
| 89 |
-
|
| 90 |
model=model,
|
| 91 |
-
args=
|
| 92 |
train_dataset=tokenized_dataset,
|
| 93 |
-
|
| 94 |
)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
-
print("Entrenamiento finalizado.")
|
| 101 |
-
return
|
| 102 |
|
| 103 |
# ----------------- Conversi贸n a FP16 -----------------
|
| 104 |
def _convert_to_fp16(self, model_dir: str, out_fp16_path: str):
|
|
@@ -167,8 +167,8 @@ class HuggingFaceModelProcessor:
|
|
| 167 |
output_config=output_config
|
| 168 |
)
|
| 169 |
|
| 170 |
-
# 1. Entrenar modelo
|
| 171 |
-
trained_dir = self.
|
| 172 |
logs.append("Entrenamiento completado")
|
| 173 |
|
| 174 |
# 2. Convertir a FP16
|
|
@@ -194,7 +194,7 @@ class HuggingFaceModelProcessor:
|
|
| 194 |
processor = HuggingFaceModelProcessor()
|
| 195 |
|
| 196 |
with gr.Blocks() as demo:
|
| 197 |
-
gr.Markdown("## Pipeline Autom谩tica GGUF con entrenamiento y publicaci贸n HF")
|
| 198 |
model_input = gr.Textbox(label="ID del modelo HF (para entrenamiento)", placeholder="ochoa/your-model")
|
| 199 |
repo_input = gr.Textbox(label="Nombre del repo HF para publicar", placeholder="usuario/nuevo-modelo")
|
| 200 |
token_input = gr.Textbox(label="Tu token HF", type="password")
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
from datasets import load_dataset
|
| 9 |
from huggingface_hub import HfApi, Repository
|
| 10 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments
|
| 11 |
+
from trl import PPOTrainer, PPOConfig
|
| 12 |
+
from trl import AutoModelForCausalLMWithValueHead
|
| 13 |
|
| 14 |
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 15 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
|
|
|
| 61 |
os.makedirs(self.DOWNLOAD_FOLDER, exist_ok=True)
|
| 62 |
os.makedirs(self.OUTPUT_FOLDER, exist_ok=True)
|
| 63 |
|
| 64 |
+
# ----------------- Entrenamiento con PPO -----------------
|
| 65 |
+
def _train_model_with_ppo(self, model_id: str, dataset_name: str, outdir: str, token: str):
|
| 66 |
+
print(f"Iniciando entrenamiento con PPO de {model_id} con dataset {dataset_name}")
|
| 67 |
dataset = load_dataset(dataset_name, split="train", token=token)
|
| 68 |
|
| 69 |
tokenizer = AutoTokenizer.from_pretrained(model_id, token=token)
|
| 70 |
+
model = AutoModelForCausalLMWithValueHead.from_pretrained(model_id, token=token)
|
| 71 |
|
| 72 |
+
# Tokenizaci贸n
|
| 73 |
def tokenize_function(examples):
|
| 74 |
return tokenizer(examples["text"], truncation=True, padding="max_length", max_length=512)
|
| 75 |
|
| 76 |
tokenized_dataset = dataset.map(tokenize_function, batched=True, remove_columns=dataset.column_names)
|
| 77 |
|
| 78 |
+
# Configuraci贸n de PPO
|
| 79 |
+
ppo_config = PPOConfig(
|
| 80 |
+
model_name=model_id,
|
| 81 |
+
train_batch_size=2,
|
| 82 |
+
gradient_accumulation_steps=1,
|
| 83 |
+
learning_rate=5e-6,
|
| 84 |
+
max_length=512,
|
| 85 |
num_train_epochs=1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
)
|
| 87 |
|
| 88 |
+
ppo_trainer = PPOTrainer(
|
| 89 |
model=model,
|
| 90 |
+
args=ppo_config,
|
| 91 |
train_dataset=tokenized_dataset,
|
| 92 |
+
tokenizer=tokenizer,
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# Entrenamiento
|
| 96 |
+
ppo_trainer.train()
|
| 97 |
+
ppo_trainer.save_model(outdir)
|
| 98 |
+
tokenizer.save_pretrained(outdir)
|
| 99 |
|
| 100 |
+
print("Entrenamiento PPO finalizado.")
|
| 101 |
+
return outdir
|
| 102 |
|
| 103 |
# ----------------- Conversi贸n a FP16 -----------------
|
| 104 |
def _convert_to_fp16(self, model_dir: str, out_fp16_path: str):
|
|
|
|
| 167 |
output_config=output_config
|
| 168 |
)
|
| 169 |
|
| 170 |
+
# 1. Entrenar modelo con PPO
|
| 171 |
+
trained_dir = self._train_model_with_ppo(model_id, DATASET_NAME, outdir, token)
|
| 172 |
logs.append("Entrenamiento completado")
|
| 173 |
|
| 174 |
# 2. Convertir a FP16
|
|
|
|
| 194 |
processor = HuggingFaceModelProcessor()
|
| 195 |
|
| 196 |
with gr.Blocks() as demo:
|
| 197 |
+
gr.Markdown("## Pipeline Autom谩tica GGUF con entrenamiento PPO y publicaci贸n HF")
|
| 198 |
model_input = gr.Textbox(label="ID del modelo HF (para entrenamiento)", placeholder="ochoa/your-model")
|
| 199 |
repo_input = gr.Textbox(label="Nombre del repo HF para publicar", placeholder="usuario/nuevo-modelo")
|
| 200 |
token_input = gr.Textbox(label="Tu token HF", type="password")
|