Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,74 @@
|
|
| 1 |
"""
|
| 2 |
-
PromptWizard Qwen Training
|
| 3 |
-
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import spaces
|
| 8 |
import torch
|
| 9 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
from datasets import load_dataset, Dataset
|
| 11 |
from peft import LoraConfig, get_peft_model, TaskType
|
| 12 |
-
import
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
def check_gpu_status():
|
| 16 |
return "๐ Zero GPU Ready - GPU will be allocated when training starts"
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
def train_model(model_name, num_epochs, batch_size, learning_rate, progress=gr.Progress()):
|
| 20 |
progress(0, desc="Initializing...")
|
| 21 |
output_log = []
|
| 22 |
|
| 23 |
try:
|
|
|
|
| 24 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
output_log.append(f"๐ฎ Using device: {device}")
|
| 26 |
if device == "cuda":
|
| 27 |
output_log.append(f"โ
GPU: {torch.cuda.get_device_name(0)}")
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
progress(0.1, desc="Loading Gita dataset...")
|
| 31 |
-
output_log.append("\n๐ Loading
|
| 32 |
|
| 33 |
-
# Load CSV from Hugging Face repo
|
| 34 |
dataset = load_dataset("rahul7star/Gita", split="train")
|
| 35 |
-
output_log.append(f" Loaded {len(dataset)}
|
| 36 |
-
|
| 37 |
-
# Preview columns
|
| 38 |
output_log.append(f" Columns: {dataset.column_names}")
|
| 39 |
|
| 40 |
-
#
|
| 41 |
def format_example(item):
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
text = item["content"]
|
| 49 |
-
else:
|
| 50 |
-
# fallback: join all columns
|
| 51 |
-
text = " ".join(str(v) for v in item.values())
|
| 52 |
|
| 53 |
prompt = f"""<|system|>
|
| 54 |
-
You are a
|
| 55 |
<|user|>
|
| 56 |
-
{
|
| 57 |
<|assistant|>
|
| 58 |
"""
|
| 59 |
return {"text": prompt}
|
| 60 |
|
| 61 |
-
|
| 62 |
-
output_log.append(f" Formatted {len(
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
progress(0.3, desc="Loading model...")
|
| 66 |
-
model_name = "Qwen/Qwen2.5-0.5B"
|
| 67 |
-
output_log.append(f"\n๐ค Loading {model_name}
|
| 68 |
|
| 69 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 70 |
if tokenizer.pad_token is None:
|
|
@@ -74,16 +78,16 @@ You are a spiritual teacher interpreting Bhagavad Gita verses with deep meaning.
|
|
| 74 |
model_name,
|
| 75 |
trust_remote_code=True,
|
| 76 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 77 |
-
low_cpu_mem_usage=True
|
| 78 |
)
|
| 79 |
-
|
| 80 |
if device == "cuda":
|
| 81 |
model = model.to(device)
|
|
|
|
| 82 |
output_log.append(" โ
Model loaded successfully")
|
| 83 |
|
| 84 |
-
#
|
| 85 |
progress(0.4, desc="Configuring LoRA...")
|
| 86 |
-
output_log.append("\nโ๏ธ Setting up LoRA for efficient
|
| 87 |
|
| 88 |
lora_config = LoraConfig(
|
| 89 |
task_type=TaskType.CAUSAL_LM,
|
|
@@ -91,72 +95,93 @@ You are a spiritual teacher interpreting Bhagavad Gita verses with deep meaning.
|
|
| 91 |
lora_alpha=16,
|
| 92 |
lora_dropout=0.1,
|
| 93 |
target_modules=["q_proj", "v_proj"],
|
| 94 |
-
bias="none"
|
| 95 |
)
|
| 96 |
model = get_peft_model(model, lora_config)
|
|
|
|
| 97 |
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 98 |
output_log.append(f" Trainable params: {trainable_params:,}")
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
progress(0.5, desc="Tokenizing
|
| 102 |
def tokenize_fn(examples):
|
| 103 |
return tokenizer(
|
| 104 |
examples["text"],
|
| 105 |
padding="max_length",
|
| 106 |
truncation=True,
|
| 107 |
-
max_length=256
|
| 108 |
)
|
| 109 |
-
|
|
|
|
| 110 |
output_log.append(" โ
Tokenization done")
|
| 111 |
|
| 112 |
-
#
|
| 113 |
progress(0.6, desc="Setting up training...")
|
|
|
|
| 114 |
training_args = TrainingArguments(
|
| 115 |
-
output_dir=
|
| 116 |
num_train_epochs=num_epochs,
|
| 117 |
per_device_train_batch_size=batch_size,
|
| 118 |
gradient_accumulation_steps=2,
|
| 119 |
warmup_steps=10,
|
| 120 |
logging_steps=5,
|
| 121 |
-
save_strategy="
|
| 122 |
fp16=device == "cuda",
|
| 123 |
optim="adamw_torch",
|
| 124 |
learning_rate=learning_rate,
|
| 125 |
-
max_steps=
|
| 126 |
)
|
| 127 |
|
| 128 |
trainer = Trainer(
|
| 129 |
model=model,
|
| 130 |
args=training_args,
|
| 131 |
-
train_dataset=
|
| 132 |
tokenizer=tokenizer,
|
| 133 |
)
|
| 134 |
|
| 135 |
-
#
|
| 136 |
-
progress(0.7, desc="Training
|
| 137 |
-
output_log.append("\n๐ Starting training...\n" + "="*50)
|
| 138 |
train_result = trainer.train()
|
| 139 |
|
| 140 |
-
progress(0.
|
| 141 |
-
output_log.append("
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
progress(1.0, desc="Complete!")
|
|
|
|
|
|
|
| 147 |
except Exception as e:
|
| 148 |
output_log.append(f"\nโ Error: {e}")
|
| 149 |
|
| 150 |
return "\n".join(output_log)
|
| 151 |
|
| 152 |
|
| 153 |
-
# === Gradio
|
| 154 |
def create_interface():
|
| 155 |
-
with gr.Blocks(title="PromptWizard Gita Trainer") as demo:
|
| 156 |
gr.Markdown("""
|
| 157 |
# ๐ง PromptWizard Qwen Fine-tuning โ Gita Edition
|
| 158 |
-
|
| 159 |
-
|
| 160 |
""")
|
| 161 |
|
| 162 |
with gr.Row():
|
|
@@ -164,32 +189,30 @@ def create_interface():
|
|
| 164 |
gpu_status = gr.Textbox(
|
| 165 |
label="GPU Status",
|
| 166 |
value=check_gpu_status(),
|
| 167 |
-
interactive=False
|
| 168 |
)
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
choices=["Qwen/Qwen2.5-0.5B", "Qwen/Qwen2.5-1.5B"],
|
| 172 |
value="Qwen/Qwen2.5-0.5B",
|
| 173 |
-
|
| 174 |
)
|
| 175 |
-
|
| 176 |
num_epochs = gr.Slider(1, 3, value=1, step=1, label="Epochs")
|
| 177 |
batch_size = gr.Slider(1, 4, value=2, step=1, label="Batch Size")
|
| 178 |
learning_rate = gr.Number(value=5e-5, label="Learning Rate")
|
| 179 |
-
train_btn = gr.Button("๐ Start
|
| 180 |
|
| 181 |
with gr.Column():
|
| 182 |
output = gr.Textbox(
|
| 183 |
-
label="Training
|
| 184 |
-
lines=
|
| 185 |
-
max_lines=
|
| 186 |
-
value="Click 'Start
|
| 187 |
)
|
| 188 |
|
| 189 |
train_btn.click(
|
| 190 |
fn=train_model,
|
| 191 |
inputs=[model_name, num_epochs, batch_size, learning_rate],
|
| 192 |
-
outputs=output
|
| 193 |
)
|
| 194 |
|
| 195 |
return demo
|
|
@@ -197,4 +220,4 @@ def create_interface():
|
|
| 197 |
|
| 198 |
if __name__ == "__main__":
|
| 199 |
demo = create_interface()
|
| 200 |
-
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
+
PromptWizard Qwen Training โ Gita Edition
|
| 3 |
+
Fine-tunes Qwen using rahul7star/Gita dataset (.csv)
|
| 4 |
+
Uploads trained model to rahul7star/Qwen0.5-3B-Gita on Hugging Face Hub
|
| 5 |
"""
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
import spaces
|
| 9 |
import torch
|
| 10 |
+
from transformers import (
|
| 11 |
+
AutoModelForCausalLM,
|
| 12 |
+
AutoTokenizer,
|
| 13 |
+
Trainer,
|
| 14 |
+
TrainingArguments,
|
| 15 |
+
)
|
| 16 |
from datasets import load_dataset, Dataset
|
| 17 |
from peft import LoraConfig, get_peft_model, TaskType
|
| 18 |
+
from huggingface_hub import HfApi, HfFolder, Repository
|
| 19 |
+
import os, tempfile, shutil
|
| 20 |
|
| 21 |
+
|
| 22 |
+
# === GPU check (Zero GPU compatible) ===
|
| 23 |
def check_gpu_status():
|
| 24 |
return "๐ Zero GPU Ready - GPU will be allocated when training starts"
|
| 25 |
|
| 26 |
+
|
| 27 |
+
# === Main Training ===
|
| 28 |
+
@spaces.GPU(duration=300)
|
| 29 |
def train_model(model_name, num_epochs, batch_size, learning_rate, progress=gr.Progress()):
|
| 30 |
progress(0, desc="Initializing...")
|
| 31 |
output_log = []
|
| 32 |
|
| 33 |
try:
|
| 34 |
+
# ==== Device ====
|
| 35 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 36 |
output_log.append(f"๐ฎ Using device: {device}")
|
| 37 |
if device == "cuda":
|
| 38 |
output_log.append(f"โ
GPU: {torch.cuda.get_device_name(0)}")
|
| 39 |
|
| 40 |
+
# ==== Load dataset ====
|
| 41 |
+
progress(0.1, desc="Loading rahul7star/Gita dataset...")
|
| 42 |
+
output_log.append("\n๐ Loading dataset from rahul7star/Gita...")
|
| 43 |
|
|
|
|
| 44 |
dataset = load_dataset("rahul7star/Gita", split="train")
|
| 45 |
+
output_log.append(f" Loaded {len(dataset)} samples from CSV")
|
|
|
|
|
|
|
| 46 |
output_log.append(f" Columns: {dataset.column_names}")
|
| 47 |
|
| 48 |
+
# ==== Format data ====
|
| 49 |
def format_example(item):
|
| 50 |
+
# Use "text" or "content" column if available
|
| 51 |
+
text = (
|
| 52 |
+
item.get("text")
|
| 53 |
+
or item.get("content")
|
| 54 |
+
or " ".join(str(v) for v in item.values())
|
| 55 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
prompt = f"""<|system|>
|
| 58 |
+
You are a wise teacher interpreting Bhagavad Gita with deep insights.
|
| 59 |
<|user|>
|
| 60 |
+
{text}
|
| 61 |
<|assistant|>
|
| 62 |
"""
|
| 63 |
return {"text": prompt}
|
| 64 |
|
| 65 |
+
dataset = dataset.map(format_example)
|
| 66 |
+
output_log.append(f" โ
Formatted {len(dataset)} examples")
|
| 67 |
|
| 68 |
+
# ==== Model ====
|
| 69 |
+
progress(0.3, desc="Loading model & tokenizer...")
|
| 70 |
+
model_name = "Qwen/Qwen2.5-0.5B"
|
| 71 |
+
output_log.append(f"\n๐ค Loading model: {model_name}")
|
| 72 |
|
| 73 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 74 |
if tokenizer.pad_token is None:
|
|
|
|
| 78 |
model_name,
|
| 79 |
trust_remote_code=True,
|
| 80 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 81 |
+
low_cpu_mem_usage=True,
|
| 82 |
)
|
|
|
|
| 83 |
if device == "cuda":
|
| 84 |
model = model.to(device)
|
| 85 |
+
|
| 86 |
output_log.append(" โ
Model loaded successfully")
|
| 87 |
|
| 88 |
+
# ==== LoRA ====
|
| 89 |
progress(0.4, desc="Configuring LoRA...")
|
| 90 |
+
output_log.append("\nโ๏ธ Setting up LoRA for efficient fine-tuning...")
|
| 91 |
|
| 92 |
lora_config = LoraConfig(
|
| 93 |
task_type=TaskType.CAUSAL_LM,
|
|
|
|
| 95 |
lora_alpha=16,
|
| 96 |
lora_dropout=0.1,
|
| 97 |
target_modules=["q_proj", "v_proj"],
|
| 98 |
+
bias="none",
|
| 99 |
)
|
| 100 |
model = get_peft_model(model, lora_config)
|
| 101 |
+
|
| 102 |
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 103 |
output_log.append(f" Trainable params: {trainable_params:,}")
|
| 104 |
|
| 105 |
+
# ==== Tokenization ====
|
| 106 |
+
progress(0.5, desc="Tokenizing dataset...")
|
| 107 |
def tokenize_fn(examples):
|
| 108 |
return tokenizer(
|
| 109 |
examples["text"],
|
| 110 |
padding="max_length",
|
| 111 |
truncation=True,
|
| 112 |
+
max_length=256,
|
| 113 |
)
|
| 114 |
+
|
| 115 |
+
dataset = dataset.map(tokenize_fn, batched=True)
|
| 116 |
output_log.append(" โ
Tokenization done")
|
| 117 |
|
| 118 |
+
# ==== Training arguments ====
|
| 119 |
progress(0.6, desc="Setting up training...")
|
| 120 |
+
output_dir = "./qwen-gita-lora"
|
| 121 |
training_args = TrainingArguments(
|
| 122 |
+
output_dir=output_dir,
|
| 123 |
num_train_epochs=num_epochs,
|
| 124 |
per_device_train_batch_size=batch_size,
|
| 125 |
gradient_accumulation_steps=2,
|
| 126 |
warmup_steps=10,
|
| 127 |
logging_steps=5,
|
| 128 |
+
save_strategy="epoch",
|
| 129 |
fp16=device == "cuda",
|
| 130 |
optim="adamw_torch",
|
| 131 |
learning_rate=learning_rate,
|
| 132 |
+
max_steps=100,
|
| 133 |
)
|
| 134 |
|
| 135 |
trainer = Trainer(
|
| 136 |
model=model,
|
| 137 |
args=training_args,
|
| 138 |
+
train_dataset=dataset,
|
| 139 |
tokenizer=tokenizer,
|
| 140 |
)
|
| 141 |
|
| 142 |
+
# ==== Train ====
|
| 143 |
+
progress(0.7, desc="Training...")
|
| 144 |
+
output_log.append("\n๐ Starting training...\n" + "=" * 50)
|
| 145 |
train_result = trainer.train()
|
| 146 |
|
| 147 |
+
progress(0.85, desc="Saving model...")
|
| 148 |
+
output_log.append("\n๐พ Saving model locally...")
|
| 149 |
+
trainer.save_model(output_dir)
|
| 150 |
+
tokenizer.save_pretrained(output_dir)
|
| 151 |
+
|
| 152 |
+
# ==== Upload to HF Hub ====
|
| 153 |
+
progress(0.9, desc="Uploading to Hugging Face Hub...")
|
| 154 |
+
hf_repo = "rahul7star/Qwen0.5-3B-Gita"
|
| 155 |
+
output_log.append(f"\nโ๏ธ Uploading fine-tuned model to: {hf_repo}")
|
| 156 |
+
|
| 157 |
+
api = HfApi()
|
| 158 |
+
token = HfFolder.get_token()
|
| 159 |
+
|
| 160 |
+
# Create repo if not exists
|
| 161 |
+
api.create_repo(repo_id=hf_repo, exist_ok=True)
|
| 162 |
+
|
| 163 |
+
# Clone & push
|
| 164 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 165 |
+
repo = Repository(local_dir=tmpdir, clone_from=hf_repo, use_auth_token=token)
|
| 166 |
+
shutil.copytree(output_dir, tmpdir, dirs_exist_ok=True)
|
| 167 |
+
repo.push_to_hub(commit_message="Upload fine-tuned Qwen-Gita LoRA model")
|
| 168 |
|
| 169 |
progress(1.0, desc="Complete!")
|
| 170 |
+
output_log.append("\nโ
Training complete & model uploaded successfully!")
|
| 171 |
+
|
| 172 |
except Exception as e:
|
| 173 |
output_log.append(f"\nโ Error: {e}")
|
| 174 |
|
| 175 |
return "\n".join(output_log)
|
| 176 |
|
| 177 |
|
| 178 |
+
# === Gradio Interface ===
|
| 179 |
def create_interface():
|
| 180 |
+
with gr.Blocks(title="PromptWizard โ Qwen Gita Trainer") as demo:
|
| 181 |
gr.Markdown("""
|
| 182 |
# ๐ง PromptWizard Qwen Fine-tuning โ Gita Edition
|
| 183 |
+
Fine-tune **Qwen 0.5B** on your dataset [rahul7star/Gita](https://huggingface.co/datasets/rahul7star/Gita)
|
| 184 |
+
and auto-upload to your model repo **rahul7star/Qwen0.5-3B-Gita**.
|
| 185 |
""")
|
| 186 |
|
| 187 |
with gr.Row():
|
|
|
|
| 189 |
gpu_status = gr.Textbox(
|
| 190 |
label="GPU Status",
|
| 191 |
value=check_gpu_status(),
|
| 192 |
+
interactive=False,
|
| 193 |
)
|
| 194 |
+
model_name = gr.Textbox(
|
| 195 |
+
label="Base Model",
|
|
|
|
| 196 |
value="Qwen/Qwen2.5-0.5B",
|
| 197 |
+
interactive=False,
|
| 198 |
)
|
|
|
|
| 199 |
num_epochs = gr.Slider(1, 3, value=1, step=1, label="Epochs")
|
| 200 |
batch_size = gr.Slider(1, 4, value=2, step=1, label="Batch Size")
|
| 201 |
learning_rate = gr.Number(value=5e-5, label="Learning Rate")
|
| 202 |
+
train_btn = gr.Button("๐ Start Fine-tuning", variant="primary")
|
| 203 |
|
| 204 |
with gr.Column():
|
| 205 |
output = gr.Textbox(
|
| 206 |
+
label="Training Log",
|
| 207 |
+
lines=25,
|
| 208 |
+
max_lines=40,
|
| 209 |
+
value="Click 'Start Fine-tuning' to train on the Gita dataset and upload to your model repo.",
|
| 210 |
)
|
| 211 |
|
| 212 |
train_btn.click(
|
| 213 |
fn=train_model,
|
| 214 |
inputs=[model_name, num_epochs, batch_size, learning_rate],
|
| 215 |
+
outputs=output,
|
| 216 |
)
|
| 217 |
|
| 218 |
return demo
|
|
|
|
| 220 |
|
| 221 |
if __name__ == "__main__":
|
| 222 |
demo = create_interface()
|
| 223 |
+
demo.launch()
|