Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,103 @@
|
|
1 |
import torch
|
2 |
-
|
3 |
-
from
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
bnb_4bit_quant_type="nf4",
|
|
|
15 |
bnb_4bit_use_double_quant=True
|
16 |
)
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
quantization_config=
|
21 |
device_map=device
|
22 |
)
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
)
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
|
|
|
|
1 |
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer
|
4 |
+
from peft import get_peft_model, LoraConfig, TaskType
|
5 |
+
from datasets import load_dataset
|
6 |
+
from bitsandbytes import BitsAndBytesConfig
|
7 |
+
|
8 |
+
# β
Check if a GPU is available, otherwise use CPU
|
9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
|
11 |
+
# β
Function to start training
|
12 |
+
def train_model(dataset_url, model_url, epochs):
|
13 |
+
try:
|
14 |
+
# Load the tokenizer
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_url)
|
16 |
+
|
17 |
+
# β
Load model with 4-bit quantization for CPU efficiency
|
18 |
+
bnb_config = BitsAndBytesConfig(
|
19 |
+
load_in_4bit=True if device == "cuda" else False,
|
20 |
bnb_4bit_quant_type="nf4",
|
21 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
22 |
bnb_4bit_use_double_quant=True
|
23 |
)
|
24 |
|
25 |
+
model = AutoModelForCausalLM.from_pretrained(
|
26 |
+
model_url,
|
27 |
+
quantization_config=bnb_config if device == "cuda" else None,
|
28 |
device_map=device
|
29 |
)
|
30 |
|
31 |
+
# β
Apply LoRA for efficient training
|
32 |
+
lora_config = LoraConfig(
|
33 |
+
task_type=TaskType.CAUSAL_LM,
|
34 |
+
r=8,
|
35 |
+
lora_alpha=32,
|
36 |
+
lora_dropout=0.1,
|
37 |
+
target_modules=["q_proj", "v_proj"]
|
38 |
+
)
|
39 |
+
|
40 |
+
model = get_peft_model(model, lora_config)
|
41 |
+
model.to(device)
|
42 |
+
|
43 |
+
# β
Load dataset
|
44 |
+
dataset = load_dataset(dataset_url)
|
45 |
+
|
46 |
+
# β
Tokenization function
|
47 |
+
def tokenize_function(examples):
|
48 |
+
return tokenizer(examples["text"], truncation=True, padding="max_length", max_length=256)
|
49 |
+
|
50 |
+
tokenized_datasets = dataset.map(tokenize_function, batched=True)
|
51 |
+
train_dataset = tokenized_datasets["train"]
|
52 |
+
|
53 |
+
# β
Training Arguments
|
54 |
+
training_args = TrainingArguments(
|
55 |
+
output_dir="./deepseek_lora_cpu",
|
56 |
+
evaluation_strategy="epoch",
|
57 |
+
learning_rate=5e-4,
|
58 |
+
per_device_train_batch_size=1,
|
59 |
+
per_device_eval_batch_size=1,
|
60 |
+
num_train_epochs=int(epochs),
|
61 |
+
save_strategy="epoch",
|
62 |
+
save_total_limit=2,
|
63 |
+
logging_dir="./logs",
|
64 |
+
logging_steps=10,
|
65 |
+
fp16=False,
|
66 |
+
gradient_checkpointing=True,
|
67 |
+
optim="adamw_torch",
|
68 |
+
report_to="none"
|
69 |
+
)
|
70 |
+
|
71 |
+
trainer = Trainer(
|
72 |
+
model=model,
|
73 |
+
args=training_args,
|
74 |
+
train_dataset=train_dataset
|
75 |
+
)
|
76 |
+
|
77 |
+
# β
Start Training
|
78 |
+
trainer.train()
|
79 |
+
|
80 |
+
# β
Save the Fine-Tuned Model
|
81 |
+
model.save_pretrained("./deepseek_lora_finetuned")
|
82 |
+
tokenizer.save_pretrained("./deepseek_lora_finetuned")
|
83 |
+
|
84 |
+
return "β
Training Completed! Model saved successfully."
|
85 |
|
86 |
+
except Exception as e:
|
87 |
+
return f"β Error: {str(e)}"
|
88 |
+
|
89 |
+
# β
Gradio UI
|
90 |
+
with gr.Blocks() as app:
|
91 |
+
gr.Markdown("# π AutoTrain DeepSeek R1 (CPU)")
|
92 |
+
|
93 |
+
dataset_url = gr.Textbox(label="Dataset URL (Hugging Face)", placeholder="e.g. samsum")
|
94 |
+
model_url = gr.Textbox(label="Model URL (Hugging Face)", placeholder="e.g. deepseek-ai/deepseek-r1")
|
95 |
+
epochs = gr.Slider(minimum=1, maximum=10, step=1, value=3, label="Number of Training Epochs")
|
96 |
+
|
97 |
+
train_button = gr.Button("Start Training")
|
98 |
+
output_text = gr.Textbox(label="Training Output")
|
99 |
+
|
100 |
+
train_button.click(train_model, inputs=[dataset_url, model_url, epochs], outputs=output_text)
|
101 |
|
102 |
+
# β
Launch the app
|
103 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|