Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,95 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#import spaces
|
| 2 |
+
#import torch
|
| 3 |
+
# Just a small window with a spot to put something in one side and it's about to put something in the other side, and that's about it is. Not an app, so moving on to the next one.
|
| 4 |
+
#zero = torch.Tensor([0]).cuda()
|
| 5 |
+
#print(zero.device) # <-- 'cpu' 🤔
|
| 6 |
+
#
|
| 7 |
+
#@spaces.GPU
|
| 8 |
+
#def greet(n):
|
| 9 |
+
# print(zero.device) # <-- 'cuda:0' 🤗
|
| 10 |
+
# return f"Hello {zero + n} Tensor"
|
| 11 |
+
#
|
| 12 |
+
#demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text())
|
| 13 |
+
#demo.launch()
|
| 14 |
|
| 15 |
+
# Create app.py in your folder. Copy-paste and customize this template:
|
| 16 |
+
import gradio as gr
|
| 17 |
+
import yaml
|
| 18 |
+
import subprocess
|
| 19 |
+
import os
|
| 20 |
+
import threading # For background training
|
| 21 |
+
def generate_config(base_model, dataset_path, epochs, batch_size, lr, hf_token, project_name):
|
| 22 |
+
config = {
|
| 23 |
+
'task': 'llm-sft',
|
| 24 |
+
'base_model': base_model,
|
| 25 |
+
'project_name': project_name,
|
| 26 |
+
'log': 'tensorboard',
|
| 27 |
+
'backend': 'local',
|
| 28 |
+
'data': {
|
| 29 |
+
'path': dataset_path,
|
| 30 |
+
'train_split': 'train',
|
| 31 |
+
'valid_split': None,
|
| 32 |
+
'chat_template': 'none' # Change if chat data
|
| 33 |
+
},
|
| 34 |
+
'params': {
|
| 35 |
+
'block_size': 512, # Small for CPU
|
| 36 |
+
'model_max_length': 1024,
|
| 37 |
+
'epochs': epochs,
|
| 38 |
+
'batch_size': batch_size,
|
| 39 |
+
'lr': lr,
|
| 40 |
+
'peft': True, # Enable PEFT for efficiency
|
| 41 |
+
'quantization': 'int4', # Reduce memory
|
| 42 |
+
'target_modules': 'all-linear',
|
| 43 |
+
'padding': 'right',
|
| 44 |
+
'optimizer': 'adamw_torch',
|
| 45 |
+
'scheduler': 'linear',
|
| 46 |
+
'gradient_accumulation': 4, # Helps with small batch
|
| 47 |
+
'mixed_precision': 'bf16' # Or 'fp16' if supported
|
| 48 |
+
},
|
| 49 |
+
'hub': {
|
| 50 |
+
'username': os.getenv('HF_USERNAME'), # Set env vars
|
| 51 |
+
'token': hf_token if hf_token else None,
|
| 52 |
+
'push_to_hub': bool(hf_token)
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
config_path = 'config.yaml'
|
| 56 |
+
with open(config_path, 'w') as f:
|
| 57 |
+
yaml.dump(config, f)
|
| 58 |
+
return config_path
|
| 59 |
+
def run_training(config_path, output_box):
|
| 60 |
+
def train():
|
| 61 |
+
try:
|
| 62 |
+
process = subprocess.Popen(['autotrain', '--config', config_path], stdout=subprocess.PIPE,
|
| 63 |
+
stderr=subprocess.PIPE, text=True)
|
| 64 |
+
for line in iter(process.stdout.readline, ''):
|
| 65 |
+
output_box.update(value=output_box.value + line)
|
| 66 |
+
process.stdout.close()
|
| 67 |
+
process.wait()
|
| 68 |
+
if process.returncode == 0:
|
| 69 |
+
output_box.update(value=output_box.value + "\nTraining complete!")
|
| 70 |
+
else:
|
| 71 |
+
output_box.update(value=output_box.value + "\nError: Check logs.")
|
| 72 |
+
except Exception as e:
|
| 73 |
+
output_box.update(value=output_box.value + f"\nException: {str(e)}")
|
| 74 |
+
threading.Thread(target=train).start()
|
| 75 |
+
return "Training started in background..."
|
| 76 |
+
with gr.Blocks() as demo:
|
| 77 |
+
gr.Markdown("# LLM AutoTrain App on HF Spaces")
|
| 78 |
+
base_model = gr.Dropdown(choices=["gpt2", "microsoft/phi-2", "EleutherAI/gpt-neo-125m"],
|
| 79 |
+
label="Base Model (small for CPU)")
|
| 80 |
+
dataset = gr.File(label="Upload Dataset (CSV/JSONL)")
|
| 81 |
+
epochs = gr.Number(value=1, label="Epochs (keep low: 1-3)")
|
| 82 |
+
batch_size = gr.Number(value=1, label="Batch Size (1-2 for CPU)")
|
| 83 |
+
lr = gr.Number(value=0.00005, label="Learning Rate (e.g., 5e-5)")
|
| 84 |
+
hf_token = gr.Textbox(label="HF Token (optional for pushing model)", type="password")
|
| 85 |
+
project_name = gr.Textbox(value="my-finetuned-llm", label="Project Name")
|
| 86 |
+
output = gr.Textbox(label="Training Logs", lines=10)
|
| 87 |
+
submit = gr.Button("Start Training")
|
| 88 |
+
submit.click(
|
| 89 |
+
fn=lambda bm, ds, ep, bs, lr_val, token, proj: run_training(generate_config(bm, ds.name, ep, bs, lr_val,
|
| 90 |
+
token, proj), output),
|
| 91 |
+
inputs=[base_model, dataset, epochs, batch_size, lr, hf_token, project_name],
|
| 92 |
+
outputs=gr.Textbox(value="Starting...")
|
| 93 |
+
)
|
| 94 |
+
demo.launch()
|
| 95 |
+
Te
|