File size: 7,823 Bytes
b71e3bf
 
 
 
 
ba908ff
f5b3aed
 
3932506
 
 
 
 
 
 
 
 
 
 
 
 
 
177a610
ba908ff
 
177a610
 
 
ba908ff
3932506
 
177a610
 
b71e3bf
f5b3aed
 
 
 
d69aab4
 
 
f5b3aed
177a610
f5b3aed
 
 
 
b71e3bf
f5b3aed
 
3932506
f5b3aed
 
177a610
f5b3aed
ba908ff
 
 
 
 
 
 
 
 
 
 
f5b3aed
ba908ff
 
 
 
 
f5b3aed
 
ba908ff
 
f5b3aed
 
 
 
 
 
 
 
 
 
 
 
177a610
3932506
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5b3aed
 
3932506
 
 
 
 
 
 
f5b3aed
 
b71e3bf
ba908ff
177a610
 
3932506
 
 
 
 
f5b3aed
 
 
 
 
 
177a610
f5b3aed
 
 
 
 
3932506
 
 
 
 
 
f5b3aed
 
 
3932506
b71e3bf
 
f5b3aed
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
import streamlit as st
import numpy as np
import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer, Trainer, TrainingArguments, DataCollatorForLanguageModeling
from datasets import Dataset
import time
from datetime import datetime
import plotly.graph_objects as go
from huggingface_hub import HfApi, HfFolder

# Initialize Hugging Face Authentication
def huggingface_login():
    token = st.text_input("Hugging Face Token", type="password")
    if token:
        HfFolder.save_token(token)
        api = HfApi()
        user_info = api.whoami(token)
        st.sidebar.write(f"Logged in as: {user_info['name']}")
        return token
    else:
        st.warning("Please enter your Hugging Face token")
        return None

# Advanced Cyberpunk Styling
def setup_advanced_cyberpunk_style():
    st.markdown("""
        <style>
        @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&display=swap');
        @import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap');
        .main-title { font-family: 'Orbitron', sans-serif; font-size: 40px; color: #00ffea; }
        /* Additional CSS styling for dashboard, progress bar, and background */
        </style>
    """, unsafe_allow_html=True)

# Initialize Model and Tokenizer
def initialize_model():
    model = GPT2LMHeadModel.from_pretrained("gpt2")
    tokenizer = GPT2Tokenizer.from_pretrained("gpt2")

    # Set padding token to eos_token
    tokenizer.pad_token = tokenizer.eos_token
    return model, tokenizer

# Prepare Dataset
def prepare_dataset(data, tokenizer, block_size=128):
    def tokenize_function(examples):
        return tokenizer(examples['text'], truncation=True, max_length=block_size, padding='max_length')

    raw_dataset = Dataset.from_dict({'text': data})
    tokenized_dataset = raw_dataset.map(tokenize_function, batched=True, remove_columns=['text'])
    tokenized_dataset = tokenized_dataset.map(lambda examples: {'labels': examples['input_ids']}, batched=True)
    tokenized_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
    return tokenized_dataset

# Training Dashboard Class
class TrainingDashboard:
    def __init__(self):
        self.metrics = {
            'current_loss': 0,
            'best_loss': float('inf'),
            'generation': 0,
            'start_time': time.time(),
            'training_speed': 0
        }
        self.history = []
        
    def update(self, loss, generation):
        self.metrics['current_loss'] = loss
        self.metrics['generation'] = generation
        if loss < self.metrics['best_loss']:
            self.metrics['best_loss'] = loss
        elapsed_time = time.time() - self.metrics['start_time']
        self.metrics['training_speed'] = generation / elapsed_time
        self.history.append({'loss': loss, 'timestamp': datetime.now().strftime('%H:%M:%S')})
    
    def display(self):
        st.write(f"**Generation:** {self.metrics['generation']}")
        st.write(f"**Current Loss:** {self.metrics['current_loss']:.4f}")
        st.write(f"**Best Loss:** {self.metrics['best_loss']:.4f}")
        st.write(f"**Training Speed:** {self.metrics['training_speed']:.2f} generations/sec")

# Display Progress Bar
def display_progress(progress):
    st.markdown(f"""
        <div class="progress-bar-container">
            <div class="progress-bar" style="width: {progress * 100}%"></div>
        </div>
    """, unsafe_allow_html=True)

# Custom Genetic Algorithm
class GeneticAlgorithm:
    def __init__(self, model, tokenizer, dataset, population_size, mutation_rate=0.1):
        self.model = model
        self.tokenizer = tokenizer
        self.dataset = dataset
        self.population_size = population_size
        self.mutation_rate = mutation_rate
        self.population = [self.clone_model() for _ in range(population_size)]
    
    def clone_model(self):
        # Create a clone of the model
        return GPT2LMHeadModel.from_pretrained("gpt2")

    def evaluate_fitness(self, model):
        # Calculate the loss for a given model on the dataset
        trainer = Trainer(
            model=model,
            args=TrainingArguments(output_dir="./results", per_device_train_batch_size=2, num_train_epochs=1),
            train_dataset=self.dataset,
            data_collator=DataCollatorForLanguageModeling(tokenizer=self.tokenizer, mlm=False),
        )
        train_result = trainer.train()
        return train_result.training_loss

    def select_best_models(self, num_best=2):
        # Selects the top models based on fitness (loss)
        fitness_scores = [(self.evaluate_fitness(model), model) for model in self.population]
        fitness_scores.sort(key=lambda x: x[0])  # Sort by loss
        best_models = [model for _, model in fitness_scores[:num_best]]
        return best_models

    def crossover(self, parent1, parent2):
        # Perform crossover by combining layers from both parents
        child = self.clone_model()
        for (child_param, param1, param2) in zip(child.parameters(), parent1.parameters(), parent2.parameters()):
            # Randomly choose parameters from each parent based on crossover probability
            if np.random.rand() > 0.5:
                child_param.data = param1.data.clone()
            else:
                child_param.data = param2.data.clone()
        return child

    def mutate(self, model):
        # Mutate model by slightly adjusting its weights
        for param in model.parameters():
            if np.random.rand() < self.mutation_rate:
                mutation_tensor = torch.randn_like(param) * 0.02
                param.data += mutation_tensor

    def generate_new_population(self):
        best_models = self.select_best_models()
        new_population = []
        while len(new_population) < self.population_size:
            parent1, parent2 = np.random.choice(best_models, 2, replace=False)
            child = self.crossover(parent1, parent2)
            self.mutate(child)
            new_population.append(child)
        self.population = new_population

# Training Loop with Genetic Algorithm and Loading Screen
def training_loop(dashboard, ga, num_generations):
    with st.spinner("Training in progress..."):
        for generation in range(1, num_generations + 1):
            best_loss = min([ga.evaluate_fitness(model) for model in ga.population])
            dashboard.update(best_loss, generation)
            progress = generation / num_generations
            display_progress(progress)
            dashboard.display()
            ga.generate_new_population()
            time.sleep(0.5)  # Simulate delay for each generation

# Main Function
def main():
    setup_advanced_cyberpunk_style()
    st.markdown('<h1 class="main-title">Neural Evolution GPT-2 Training Hub</h1>', unsafe_allow_html=True)

    # Hugging Face Account Login
    token = huggingface_login()
    if token is None:
        return

    # Load Model and Tokenizer
    model, tokenizer = initialize_model()

    # Prepare Data
    data = ["Sample training text"] * 10  # Replace with real data
    train_dataset = prepare_dataset(data, tokenizer)

    # Initialize Dashboard
    dashboard = TrainingDashboard()
    
    # Sidebar Configuration
    st.sidebar.markdown("### Training Parameters")
    num_generations = st.sidebar.slider("Generations", 1, 50, 10)
    population_size = st.sidebar.slider("Population Size", 4, 20, 10)
    mutation_rate = st.sidebar.slider("Mutation Rate", 0.01, 0.5, 0.1)
    
    # Initialize Genetic Algorithm
    ga = GeneticAlgorithm(model, tokenizer, train_dataset, population_size, mutation_rate)
    
    # Run Training
    if st.button("Start Training"):
        training_loop(dashboard, ga, num_generations)

if __name__ == "__main__":
    main()