Spaces:
Sleeping
Sleeping
Download app.py from Suneclipse2005/lighting-llm-trainer: direct link, hf CLI and curl.
- Browser
- Download file 4.57 kB
-
https://huggingface.co/spaces/Suneclipse2005/lighting-llm-trainer/resolve/main/app.py
- Command line
-
hf download hf://spaces/Suneclipse2005/lighting-llm-trainer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Suneclipse2005/lighting-llm-trainer/resolve/main/app.py
4.57 kB
| import gradio as gr | |
| import spaces | |
| import torch | |
| import os | |
| from datasets import load_dataset | |
| from transformers import ( | |
| AutoModelForCausalLM, | |
| AutoTokenizer, | |
| TrainingArguments, | |
| BitsAndBytesConfig | |
| ) | |
| from peft import LoraConfig, get_peft_model | |
| from trl import SFTTrainer | |
| HF_USERNAME = "Suneclipse2005" | |
| DATASET_ID = f"{HF_USERNAME}/lighting-llm-dataset" | |
| OUTPUT_MODEL = f"{HF_USERNAME}/lighting-llm-lora" | |
| def run_training_zerogpu(base_model, lora_r, epochs, learning_rate, progress=gr.Progress(track_tqdm=True)): | |
| yield f"⚡ ZeroGPU Alocado com Sucesso! Inicializando ambiente com Torch {torch.__version__} (CUDA: {torch.cuda.is_available()})\n" | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| yield f"📦 Carregando Tokenizer e Pesos Base de: {base_model}...\n" | |
| tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True) | |
| if tokenizer.pad_token is None: | |
| tokenizer.pad_token = tokenizer.eos_token | |
| # 4-bit QLoRA | |
| if torch.cuda.is_available(): | |
| bnb_config = BitsAndBytesConfig( | |
| load_in_4bit=True, | |
| bnb_4bit_quant_type="nf4", | |
| bnb_4bit_compute_dtype=torch.float16, | |
| bnb_4bit_use_double_quant=True | |
| ) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| base_model, | |
| quantization_config=bnb_config, | |
| device_map="auto", | |
| trust_remote_code=True | |
| ) | |
| else: | |
| model = AutoModelForCausalLM.from_pretrained(base_model, device_map="auto", trust_remote_code=True) | |
| # LoRA Config | |
| peft_config = LoraConfig( | |
| r=int(lora_r), | |
| lora_alpha=int(lora_r * 2), | |
| lora_dropout=0.05, | |
| bias="none", | |
| task_type="CAUSAL_LM", | |
| target_modules=["q_proj", "k_proj", "v_proj", "o_proj"] | |
| ) | |
| model = get_peft_model(model, peft_config) | |
| yield f"📚 Carregando dataset internacional do Hugging Face Hub: {DATASET_ID}...\n" | |
| dataset = load_dataset(DATASET_ID, split="train") | |
| def format_prompts(example): | |
| return {"text": tokenizer.apply_chat_template(example["messages"], tokenize=False)} | |
| formatted_dataset = dataset.map(format_prompts) | |
| training_args = TrainingArguments( | |
| output_dir="./lighting_lora_zerogpu", | |
| per_device_train_batch_size=2, | |
| gradient_accumulation_steps=4, | |
| learning_rate=float(learning_rate), | |
| num_train_epochs=int(epochs), | |
| logging_steps=2, | |
| fp16=torch.cuda.is_available(), | |
| optim="paged_adamw_8bit" if torch.cuda.is_available() else "adamw_torch", | |
| report_to="none" | |
| ) | |
| trainer = SFTTrainer( | |
| model=model, | |
| train_dataset=formatted_dataset, | |
| dataset_text_field="text", | |
| max_seq_length=1024, | |
| tokenizer=tokenizer, | |
| args=training_args, | |
| peft_config=peft_config | |
| ) | |
| yield f"🎯 Iniciando treino de {epochs} épocas no ZeroGPU...\n" | |
| trainer.train() | |
| yield f"☁️ Publicando modelo ajustado no Hugging Face Hub: {OUTPUT_MODEL}...\n" | |
| trainer.model.push_to_hub(OUTPUT_MODEL, private=False) | |
| tokenizer.push_to_hub(OUTPUT_MODEL, private=False) | |
| yield f"✅ Treino concluído com Sucesso no Hugging Face ZeroGPU! Modelo disponível em: https://huggingface.co/{OUTPUT_MODEL}\n" | |
| with gr.Blocks(title="Lighting LLM ZeroGPU Trainer", theme=gr.themes.Base()) as demo: | |
| gr.Markdown("# 💡 LuxLighting ZeroGPU Studio Trainer") | |
| gr.Markdown("Treinador de modelos de linguagem para iluminação de Cinema, Fotografia, Teatro e Concertos acelerado por **Hugging Face ZeroGPU**.") | |
| with gr.Row(): | |
| base_model_input = gr.Dropdown( | |
| choices=["Qwen/Qwen2.5-0.5B-Instruct", "meta-llama/Llama-3.2-1B-Instruct", "HuggingFaceTB/SmolLM2-360M-Instruct", "mistralai/Mistral-7B-Instruct-v0.3"], | |
| value="Qwen/Qwen2.5-0.5B-Instruct", | |
| label="Modelo Base" | |
| ) | |
| lora_r_input = gr.Slider(minimum=8, maximum=64, step=8, value=16, label="LoRA Rank (r)") | |
| epochs_input = gr.Slider(minimum=1, maximum=10, step=1, value=3, label="Épocas de Treino") | |
| lr_input = gr.Number(value=0.0002, label="Learning Rate") | |
| train_btn = gr.Button("⚡ Executar Treino no ZeroGPU", variant="primary") | |
| output_logs = gr.Textbox(label="Logs de Treino ao Vivo (ZeroGPU)", lines=12) | |
| train_btn.click( | |
| fn=run_training_zerogpu, | |
| inputs=[base_model_input, lora_r_input, epochs_input, lr_input], | |
| outputs=[output_logs] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |