File size: 9,381 Bytes
c643b82
 
 
 
24cf4d3
c643b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9281cb
c643b82
a9281cb
 
117a333
a9281cb
117a333
 
 
 
 
 
a9281cb
117a333
 
 
 
 
 
 
 
a9281cb
 
 
c643b82
24cf4d3
 
 
117a333
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24cf4d3
c643b82
 
 
 
 
 
 
 
117a333
 
c643b82
 
 
 
 
 
 
 
24cf4d3
117a333
 
 
 
 
 
 
 
24cf4d3
c643b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24cf4d3
c643b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9281cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c643b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2022648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import os
import json
import gradio as gr
import torch
import pandas as pd
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_obj):
    """Save uploaded file and return its path"""
    try:
        os.makedirs('uploads', exist_ok=True)
        import tempfile
        
        # Create a temporary file
        temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.csv', dir='uploads')
        
        # Write the content
        if isinstance(file_obj, (bytes, bytearray)):
            temp_file.write(file_obj)
        else:
            content = file_obj.read()
            if isinstance(content, str):
                temp_file.write(content.encode('utf-8'))
            else:
                temp_file.write(content)
        
        temp_file.close()
        return temp_file.name
        
    except Exception as e:
        raise Exception(f"Error saving file: {str(e)}")

def prepare_training_data(df):
    """Convert DataFrame into Q&A format"""
    formatted_data = []
    try:
        for _, row in df.iterrows():
            # Clean and validate the data
            chunk_id = str(row['chunk_id']).strip()
            text = str(row['text']).strip()
            
            if chunk_id and text:  # Only include non-empty pairs
                # Format each conversation in the required structure
                formatted_text = f"User: {chunk_id}\nAssistant: {text}"
                formatted_data.append({"text": formatted_text})
        
        if not formatted_data:
            raise ValueError("No valid training pairs found in the data")
            
        return formatted_data
    except Exception as e:
        raise Exception(f"Error preparing training data: {str(e)}")

def prepare_training_components(
    data_path,
    learning_rate,
    num_epochs,
    batch_size,
    model_name=MODEL_NAME
):
    """Prepare model, tokenizer, and training arguments"""
    print(f"Loading data from: {data_path}")  # Debug logging
    """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 data and convert to Q&A format
    try:
        df = pd.read_csv(data_path, encoding='utf-8')
        print(f"Loaded CSV with {len(df)} rows")  # Debug logging
        formatted_data = prepare_training_data(df)
        print(f"Prepared {len(formatted_data)} training examples")  # Debug logging
    except Exception as e:
        print(f"Error loading CSV: {str(e)}")  # Debug logging
        raise

    # 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,
    )

    # Convert to datasets format
    dataset = Dataset.from_dict({
        'text': [item['text'] for item in formatted_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"""
    if file is None:
        return "Please upload a file first."
    
    try:
        # File validation
        progress(0.1, desc="Validating 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:
        error_msg = f"Error during training: {str(e)}"
        print(error_msg)  # Log the error
        return error_msg
    """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():
    demo = gr.Interface(
        # Configure Gradio to handle larger file uploads
        upload_size_limit=100
    )
    
    with gr.Row():
        with gr.Column():
            file_input = gr.File(
                label="Upload Training Data (CSV)",
                type="binary",
                file_types=[".csv"]
            )
            
            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 CSV format with columns:
       - chunk_id (questions)
       - text (answers)
    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)