File size: 6,457 Bytes
c643b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
import json
import gradio as gr
import torch
from transformers import (
    TrainingArguments,
    Trainer,
    AutoModelForCausalLM,
    AutoTokenizer,
    DataCollatorForLanguageModeling
)
from datasets import Dataset
from peft import (
    prepare_model_for_kbit_training,
    LoraConfig,
    get_peft_model
)

# Constants
MODEL_NAME = "deepseek-ai/DeepSeek-R1"
OUTPUT_DIR = "finetuned_models"
LOGS_DIR = "training_logs"

def save_uploaded_file(file):
    """Save uploaded file and return its path"""
    os.makedirs('uploads', exist_ok=True)
    file_path = os.path.join('uploads', file.name)
    with open(file_path, 'wb') as f:
        f.write(file.read())
    return file_path

def prepare_training_components(
    data_path,
    learning_rate,
    num_epochs,
    batch_size,
    model_name=MODEL_NAME
):
    """Prepare model, tokenizer, and training arguments"""
    
    # Create output directory with timestamp
    import time
    timestamp = time.strftime("%Y%m%d_%H%M%S")
    specific_output_dir = os.path.join(OUTPUT_DIR, f"run_{timestamp}")
    os.makedirs(specific_output_dir, exist_ok=True)
    os.makedirs(LOGS_DIR, exist_ok=True)

    # Load tokenizer and model
    tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
    model = AutoModelForCausalLM.from_pretrained(
        model_name,
        trust_remote_code=True,
        torch_dtype=torch.float16,
        load_in_8bit=True
    )

    # LoRA Configuration
    lora_config = LoraConfig(
        r=16,
        lora_alpha=32,
        target_modules=[
            "q_proj", "k_proj", "v_proj", "o_proj",
            "gate_proj", "up_proj", "down_proj"
        ],
        lora_dropout=0.05,
        bias="none",
        task_type="CAUSAL_LM"
    )

    # Prepare model
    model = prepare_model_for_kbit_training(model)
    model = get_peft_model(model, lora_config)

    # Training Arguments
    training_args = TrainingArguments(
        output_dir=specific_output_dir,
        num_train_epochs=num_epochs,
        per_device_train_batch_size=batch_size,
        learning_rate=learning_rate,
        fp16=True,
        gradient_accumulation_steps=8,
        gradient_checkpointing=True,
        logging_dir=os.path.join(LOGS_DIR, f"run_{timestamp}"),
        logging_steps=10,
        save_strategy="epoch",
        evaluation_strategy="epoch",
        save_total_limit=2,
    )

    # Load and prepare dataset
    with open(data_path, 'r') as f:
        raw_data = json.load(f)
    
    # Convert to datasets format
    dataset = Dataset.from_dict({
        'text': [item['text'] for item in raw_data]
    })

    # Create data collator
    data_collator = DataCollatorForLanguageModeling(
        tokenizer=tokenizer,
        mlm=False
    )

    return {
        'model': model,
        'tokenizer': tokenizer,
        'training_args': training_args,
        'dataset': dataset,
        'data_collator': data_collator,
        'output_dir': specific_output_dir
    }

def train_model(
    file,
    learning_rate=2e-4,
    num_epochs=3,
    batch_size=4,
    progress=gr.Progress()
):
    """Training function for Gradio interface"""
    try:
        # Save uploaded file
        file_path = save_uploaded_file(file)
        
        # Prepare components
        progress(0.2, desc="Preparing training components...")
        components = prepare_training_components(
            file_path,
            learning_rate,
            num_epochs,
            batch_size
        )
        
        # Initialize trainer
        progress(0.4, desc="Initializing trainer...")
        trainer = Trainer(
            model=components['model'],
            args=components['training_args'],
            train_dataset=components['dataset'],
            data_collator=components['data_collator'],
        )
        
        # Train
        progress(0.5, desc="Training model...")
        trainer.train()
        
        # Save model and tokenizer
        progress(0.9, desc="Saving model...")
        trainer.save_model()
        components['tokenizer'].save_pretrained(components['output_dir'])
        
        progress(1.0, desc="Training complete!")
        return f"Training completed! Model saved in {components['output_dir']}"
    
    except Exception as e:
        return f"Error during training: {str(e)}"

# Create Gradio interface
def create_interface():
    with gr.Blocks() as demo:
        gr.Markdown("# DeepSeek-R1 Model Finetuning Interface")
        
        with gr.Row():
            with gr.Column():
                file_input = gr.File(
                    label="Upload Training Data (JSON)",
                    type="binary",
                    file_types=[".json"]
                )
                
                learning_rate = gr.Slider(
                    minimum=1e-5,
                    maximum=1e-3,
                    value=2e-4,
                    label="Learning Rate"
                )
                
                num_epochs = gr.Slider(
                    minimum=1,
                    maximum=10,
                    value=3,
                    step=1,
                    label="Number of Epochs"
                )
                
                batch_size = gr.Slider(
                    minimum=1,
                    maximum=8,
                    value=4,
                    step=1,
                    label="Batch Size"
                )
                
                train_button = gr.Button("Start Training")
                
            with gr.Column():
                output = gr.Textbox(label="Training Status")
                
        train_button.click(
            fn=train_model,
            inputs=[file_input, learning_rate, num_epochs, batch_size],
            outputs=output
        )
        
        gr.Markdown("""
        ## Instructions
        1. Upload your training data in JSON format:
        ```json
        [
            {"text": "User: Question\nAssistant: Answer"},
            {"text": "User: Another question\nAssistant: Another answer"}
        ]
        ```
        2. Adjust training parameters if needed
        3. Click 'Start Training'
        4. Wait for training to complete
        """)
    
    return demo

if __name__ == "__main__":
    # Create necessary directories
    os.makedirs(OUTPUT_DIR, exist_ok=True)
    os.makedirs(LOGS_DIR, exist_ok=True)
    
    # Launch Gradio interface
    demo = create_interface()
    demo.launch(share=True)