Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
|
3 |
+
from datasets import load_dataset
|
4 |
+
|
5 |
+
# Step 1: Load pre-trained model and tokenizer
|
6 |
+
MODEL_NAME = "deepseek-ai/DeepSeek-V3-0324" # Pre-trained model from Hugging Face
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
9 |
+
|
10 |
+
# Step 2: Load your custom dataset from Hugging Face
|
11 |
+
dataset = load_dataset("epicDev123/deepseek") # Replace with your dataset name if different
|
12 |
+
|
13 |
+
# Step 3: Tokenization function (tokenize the text data for model input)
|
14 |
+
def tokenize_function(examples):
|
15 |
+
return tokenizer(examples["text"], padding="max_length", truncation=True)
|
16 |
+
|
17 |
+
# Tokenize the dataset
|
18 |
+
tokenized_datasets = dataset.map(tokenize_function, batched=True)
|
19 |
+
|
20 |
+
# Step 4: Set up training arguments
|
21 |
+
training_args = TrainingArguments(
|
22 |
+
output_dir="./results", # Output directory to save the fine-tuned model
|
23 |
+
num_train_epochs=3, # Number of training epochs
|
24 |
+
per_device_train_batch_size=8, # Batch size for training
|
25 |
+
per_device_eval_batch_size=8, # Batch size for evaluation
|
26 |
+
warmup_steps=500, # Number of warmup steps
|
27 |
+
weight_decay=0.01, # Weight decay for regularization
|
28 |
+
logging_dir="./logs", # Directory for logs
|
29 |
+
logging_steps=10, # Log training every 10 steps
|
30 |
+
save_steps=500, # Save model checkpoints every 500 steps
|
31 |
+
evaluation_strategy="epoch", # Evaluate model after each epoch
|
32 |
+
save_total_limit=2, # Limit the number of saved checkpoints
|
33 |
+
)
|
34 |
+
|
35 |
+
# Step 5: Initialize the Trainer
|
36 |
+
trainer = Trainer(
|
37 |
+
model=model, # Model to train
|
38 |
+
args=training_args, # Training arguments
|
39 |
+
train_dataset=tokenized_datasets["train"], # Training dataset
|
40 |
+
eval_dataset=tokenized_datasets["validation"], # Validation dataset (optional)
|
41 |
+
)
|
42 |
+
|
43 |
+
# Step 6: Fine-tune the model
|
44 |
+
trainer.train()
|
45 |
+
|
46 |
+
# Step 7: Save the fine-tuned model
|
47 |
+
model.save_pretrained("./fine_tuned_deepseek") # Save the model to a directory
|
48 |
+
tokenizer.save_pretrained("./fine_tuned_deepseek") # Save the tokenizer
|
49 |
+
|
50 |
+
# Step 8: Optionally, you can push the model to Hugging Face Model Hub (after logging into Hugging Face)
|
51 |
+
model.push_to_hub("your-username/fine-tuned-deepseek") # Replace with your username and desired model name
|
52 |
+
tokenizer.push_to_hub("your-username/fine-tuned-deepseek")
|
53 |
+
|
54 |
+
print("Fine-tuning complete! Your model has been saved.")
|