File size: 8,549 Bytes
6f5c468
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4659c
 
6f5c468
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39a2491
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
#!/usr/bin/env python3
"""
OpenFinancial Chatbot - Hugging Face Space Trainer
==================================================

This script is designed to run directly in a Hugging Face Space.
Upload this file along with your training data to a HF Space and it will:
1. Load your training data automatically
2. Train the model using available hardware (GPU/CPU)
3. Save the trained model to the space's file system
4. Provide a simple interface to monitor progress

Instructions:
1. Create a new HF Space (Gradio SDK)
2. Upload this file as app.py
3. Upload your training CSV files to the space
4. The space will automatically start training when it loads
"""

import os
import json
import time
import pandas as pd
from datasets import Dataset
from transformers import (
    AutoModelForCausalLM, 
    AutoTokenizer, 
    Trainer, 
    TrainingArguments, 
    DataCollatorForLanguageModeling
)
import torch
from huggingface_hub import login
import gradio as gr

# Configuration
BASE_MODEL = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
OUTPUT_MODEL_DIR = "./trained_model"
TRAINING_DATA_FILES = ["customer_service_conversations.csv", "financial_conversations.csv", "financial_qa_conversations.csv", "trainingData.csv"]  # Try multiple names

def find_training_data():
    """Find training data files in the space"""
    
    # Check for CSV files
    for filename in TRAINING_DATA_FILES:
        if os.path.exists(filename):
            return filename
    
    # Check all CSV files in current directory
    csv_files = [f for f in os.listdir('.') if f.endswith('.csv')]
    if csv_files:
        return csv_files[0]  # Use the first one
    
    print("No training data found. Please upload a CSV file with 'Question' and 'Answer' columns.")
    return None

def load_training_data(filename):
    """Load and prepare training data"""
    
    try:
        # Read CSV file
        df = pd.read_csv(filename)
        
        # Check for required columns (flexible naming)
        question_cols = [col for col in df.columns if 'question' in col.lower()]
        answer_cols = [col for col in df.columns if 'answer' in col.lower()]
        
        if not question_cols or not answer_cols:
            raise ValueError("Could not find Question/Answer columns")
        
        question_col = question_cols[0]
        answer_col = answer_cols[0]
        
        # Create training format
        training_data = []
        for _, row in df.iterrows():
            question = str(row[question_col]).strip()
            answer = str(row[answer_col]).strip()
            
            if question and answer and question != 'nan' and answer != 'nan':
                # Format as conversation
                text = f"### Question: {question}\n### Answer: {answer}<|endoftext|>"
                training_data.append({"text": text})
        
        print(f"Processed {len(training_data)} valid training examples")
        return training_data
        
    except Exception as e:
        return None

def train_model(training_data):
    """Train the model with the provided data"""
    print("Starting model training...")
    
    # Check hardware
    device = "cuda" if torch.cuda.is_available() else "cpu"
    print(f"Using device: {device}")
    if torch.cuda.is_available():
        print(f"GPU: {torch.cuda.get_device_name(0)}")
    
    # Create dataset
    dataset = Dataset.from_list(training_data)
    
    # Load tokenizer and model
    tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
    if tokenizer.pad_token is None:
        tokenizer.pad_token = tokenizer.eos_token
    
    model = AutoModelForCausalLM.from_pretrained(
        BASE_MODEL,
        torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
        device_map="auto" if torch.cuda.is_available() else None
    )
    
    # Tokenize dataset
    def tokenize_function(examples):
        return tokenizer(
            examples["text"], 
            truncation=True, 
            padding=False, 
            max_length=512
        )
    
    tokenized_dataset = dataset.map(
        tokenize_function, 
        batched=True, 
        remove_columns=["text"]
    )
    
    # Training arguments
    batch_size = 4 if torch.cuda.is_available() else 2
    gradient_steps = 4 if torch.cuda.is_available() else 8
    
    training_args = TrainingArguments(
        output_dir="./results",
        num_train_epochs=3,
        per_device_train_batch_size=batch_size,
        gradient_accumulation_steps=gradient_steps,
        warmup_steps=50,
        learning_rate=2e-5,
        logging_steps=10,
        save_steps=500,
        save_total_limit=2,
        remove_unused_columns=False,
        dataloader_num_workers=0,  # Avoid multiprocessing issues
        fp16=torch.cuda.is_available(),
        report_to=None,  # Disable wandb
    )
    
    # Data collator
    data_collator = DataCollatorForLanguageModeling(
        tokenizer=tokenizer,
        mlm=False,
    )
    
    # Create trainer
    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=tokenized_dataset,
        data_collator=data_collator,
        tokenizer=tokenizer,
    )
    
    # Train the model
    start_time = time.time()
    
    try:
        trainer.train()
        
        end_time = time.time()
        training_duration = (end_time - start_time) / 60
        
        # Save the model
        trainer.save_model(OUTPUT_MODEL_DIR)
        tokenizer.save_pretrained(OUTPUT_MODEL_DIR)
        
        # Create a completion marker
        with open("training_complete.txt", "w") as f:
            f.write(f"Training completed successfully!\nDuration: {training_duration:.1f} minutes\nModel saved to: {OUTPUT_MODEL_DIR}")
        
        return f"Training completed in {training_duration:.1f} minutes!\n\nModel saved to: {OUTPUT_MODEL_DIR}\n\nYou can now download the trained_model folder."
        
    except Exception as e:
        error_msg = f"Training failed: {str(e)}"
        print(error_msg)
        
        # Create error marker
        with open("training_error.txt", "w") as f:
            f.write(error_msg)
        
        return error_msg

def create_interface():
    """Create Gradio interface"""
    
    # Check for existing status
    initial_status = "Ready to start training..."
    
    if os.path.exists("training_complete.txt"):
        with open("training_complete.txt", "r") as f:
            initial_status = f.read()
    elif os.path.exists("training_error.txt"):
        with open("training_error.txt", "r") as f:
            initial_status = f.read()
    
    with gr.Blocks(title="OpenFinancial Chatbot Trainer") as demo:
        gr.Markdown("# OpenFinancial Chatbot - Cloud Trainer")
        gr.Markdown("Upload your training CSV file and click 'Start Training' to begin.")
        
        status_output = gr.Textbox(
            label="Training Status",
            value=initial_status,
            lines=10,
            max_lines=20
        )
        
        with gr.Row():
            start_btn = gr.Button("Start Training", variant="primary")
            refresh_btn = gr.Button("Refresh Status", variant="secondary")
        
        # File download section
        gr.Markdown("## Download Trained Model")
        download_info = gr.Markdown("After training completes, download the files below:")
        
        def start_training():
            # Find and load data
            data_file = find_training_data()
            if not data_file:
                return "No training data found. Please upload a CSV file with Question and Answer columns."
            
            training_data = load_training_data(data_file)
            if not training_data:
                return "Failed to load training data. Check the CSV format."
            
            # Start training
            return train_model(training_data)
        
        def refresh_status():
            if os.path.exists("training_complete.txt"):
                with open("training_complete.txt", "r") as f:
                    return f.read()
            elif os.path.exists("training_error.txt"):
                with open("training_error.txt", "r") as f:
                    return f.read()
            else:
                return "Ready to start training..."
        
        start_btn.click(start_training, outputs=status_output)
        refresh_btn.click(refresh_status, outputs=status_output)
    
    return demo

if __name__ == "__main__":
    
    # Launch interface
    interface = create_interface()
    interface.launch()