Sephfox commited on
Commit
56864f5
·
verified ·
1 Parent(s): d69aab4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +146 -145
app.py CHANGED
@@ -1,48 +1,80 @@
 
1
  import streamlit as st
2
  import numpy as np
3
  import torch
 
4
  from transformers import GPT2LMHeadModel, GPT2Tokenizer, Trainer, TrainingArguments, DataCollatorForLanguageModeling
5
  from datasets import Dataset
 
 
6
  import time
7
  from datetime import datetime
8
- import plotly.graph_objects as go
9
- from huggingface_hub import HfApi, HfFolder
10
-
11
- # Initialize Hugging Face Authentication
12
- def huggingface_login():
13
- token = st.text_input("Hugging Face Token", type="password")
14
- if token:
15
- HfFolder.save_token(token)
16
- api = HfApi()
17
- user_info = api.whoami(token)
18
- st.sidebar.write(f"Logged in as: {user_info['name']}")
19
- return token
20
- else:
21
- st.warning("Please enter your Hugging Face token")
22
- return None
23
 
24
- # Advanced Cyberpunk Styling
25
- def setup_advanced_cyberpunk_style():
26
  st.markdown("""
27
  <style>
28
  @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&display=swap');
29
  @import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap');
30
- .main-title { font-family: 'Orbitron', sans-serif; font-size: 40px; color: #00ffea; }
31
- /* Additional CSS styling for dashboard, progress bar, and background */
32
- </style>
33
- """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- # Initialize Model and Tokenizer
36
- def initialize_model():
37
- model = GPT2LMHeadModel.from_pretrained("gpt2")
38
- tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- # Set padding token to eos_token
41
- tokenizer.pad_token = tokenizer.eos_token
42
- return model, tokenizer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- # Prepare Dataset
45
  def prepare_dataset(data, tokenizer, block_size=128):
 
46
  def tokenize_function(examples):
47
  return tokenizer(examples['text'], truncation=True, max_length=block_size, padding='max_length')
48
 
@@ -52,145 +84,114 @@ def prepare_dataset(data, tokenizer, block_size=128):
52
  tokenized_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
53
  return tokenized_dataset
54
 
55
- # Training Dashboard Class
56
  class TrainingDashboard:
57
  def __init__(self):
58
  self.metrics = {
59
  'current_loss': 0,
60
  'best_loss': float('inf'),
61
  'generation': 0,
 
62
  'start_time': time.time(),
63
  'training_speed': 0
64
  }
65
  self.history = []
66
-
67
- def update(self, loss, generation):
68
  self.metrics['current_loss'] = loss
69
  self.metrics['generation'] = generation
 
70
  if loss < self.metrics['best_loss']:
71
  self.metrics['best_loss'] = loss
 
72
  elapsed_time = time.time() - self.metrics['start_time']
73
- self.metrics['training_speed'] = generation / elapsed_time
74
  self.history.append({'loss': loss, 'timestamp': datetime.now().strftime('%H:%M:%S')})
75
-
76
- def display(self):
77
- st.write(f"**Generation:** {self.metrics['generation']}")
78
- st.write(f"**Current Loss:** {self.metrics['current_loss']:.4f}")
79
- st.write(f"**Best Loss:** {self.metrics['best_loss']:.4f}")
80
- st.write(f"**Training Speed:** {self.metrics['training_speed']:.2f} generations/sec")
81
-
82
- # Display Progress Bar
83
- def display_progress(progress):
84
- st.markdown(f"""
85
- <div class="progress-bar-container">
86
- <div class="progress-bar" style="width: {progress * 100}%"></div>
87
- </div>
88
- """, unsafe_allow_html=True)
89
 
90
- # Custom Genetic Algorithm
91
- class GeneticAlgorithm:
92
- def __init__(self, model, tokenizer, dataset, population_size, mutation_rate=0.1):
93
- self.model = model
94
- self.tokenizer = tokenizer
95
- self.dataset = dataset
96
- self.population_size = population_size
97
- self.mutation_rate = mutation_rate
98
- self.population = [self.clone_model() for _ in range(population_size)]
99
-
100
- def clone_model(self):
101
- # Create a clone of the model
102
- return GPT2LMHeadModel.from_pretrained("gpt2")
103
-
104
- def evaluate_fitness(self, model):
105
- # Calculate the loss for a given model on the dataset
106
- trainer = Trainer(
107
- model=model,
108
- args=TrainingArguments(output_dir="./results", per_device_train_batch_size=2, num_train_epochs=1),
109
- train_dataset=self.dataset,
110
- data_collator=DataCollatorForLanguageModeling(tokenizer=self.tokenizer, mlm=False),
111
- )
112
- train_result = trainer.train()
113
- return train_result.training_loss
114
-
115
- def select_best_models(self, num_best=2):
116
- # Selects the top models based on fitness (loss)
117
- fitness_scores = [(self.evaluate_fitness(model), model) for model in self.population]
118
- fitness_scores.sort(key=lambda x: x[0]) # Sort by loss
119
- best_models = [model for _, model in fitness_scores[:num_best]]
120
- return best_models
121
-
122
- def crossover(self, parent1, parent2):
123
- # Perform crossover by combining layers from both parents
124
- child = self.clone_model()
125
- for (child_param, param1, param2) in zip(child.parameters(), parent1.parameters(), parent2.parameters()):
126
- # Randomly choose parameters from each parent based on crossover probability
127
- if np.random.rand() > 0.5:
128
- child_param.data = param1.data.clone()
129
- else:
130
- child_param.data = param2.data.clone()
131
- return child
132
-
133
- def mutate(self, model):
134
- # Mutate model by slightly adjusting its weights
135
- for param in model.parameters():
136
- if np.random.rand() < self.mutation_rate:
137
- mutation_tensor = torch.randn_like(param) * 0.02
138
- param.data += mutation_tensor
139
-
140
- def generate_new_population(self):
141
- best_models = self.select_best_models()
142
- new_population = []
143
- while len(new_population) < self.population_size:
144
- parent1, parent2 = np.random.choice(best_models, 2, replace=False)
145
- child = self.crossover(parent1, parent2)
146
- self.mutate(child)
147
- new_population.append(child)
148
- self.population = new_population
149
-
150
- # Training Loop with Genetic Algorithm and Loading Screen
151
- def training_loop(dashboard, ga, num_generations):
152
- with st.spinner("Training in progress..."):
153
- for generation in range(1, num_generations + 1):
154
- best_loss = min([ga.evaluate_fitness(model) for model in ga.population])
155
- dashboard.update(best_loss, generation)
156
- progress = generation / num_generations
157
- display_progress(progress)
158
- dashboard.display()
159
- ga.generate_new_population()
160
- time.sleep(0.5) # Simulate delay for each generation
161
-
162
- # Main Function
163
- def main():
164
- setup_advanced_cyberpunk_style()
165
- st.markdown('<h1 class="main-title">Neural Evolution GPT-2 Training Hub</h1>', unsafe_allow_html=True)
166
 
167
- # Hugging Face Account Login
168
- token = huggingface_login()
169
- if token is None:
170
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
- # Load Model and Tokenizer
173
- model, tokenizer = initialize_model()
 
 
 
 
174
 
175
- # Prepare Data
176
- data = ["Sample training text"] * 10 # Replace with real data
177
- train_dataset = prepare_dataset(data, tokenizer)
178
 
179
- # Initialize Dashboard
180
- dashboard = TrainingDashboard()
181
-
182
- # Sidebar Configuration
183
- st.sidebar.markdown("### Training Parameters")
184
- num_generations = st.sidebar.slider("Generations", 1, 50, 10)
185
- population_size = st.sidebar.slider("Population Size", 4, 20, 10)
186
- mutation_rate = st.sidebar.slider("Mutation Rate", 0.01, 0.5, 0.1)
187
 
188
- # Initialize Genetic Algorithm
189
- ga = GeneticAlgorithm(model, tokenizer, train_dataset, population_size, mutation_rate)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
 
191
- # Run Training
192
- if st.button("Start Training"):
193
- training_loop(dashboard, ga, num_generations)
194
 
195
  if __name__ == "__main__":
196
  main()
 
1
+ # Imports
2
  import streamlit as st
3
  import numpy as np
4
  import torch
5
+ import random
6
  from transformers import GPT2LMHeadModel, GPT2Tokenizer, Trainer, TrainingArguments, DataCollatorForLanguageModeling
7
  from datasets import Dataset
8
+ from huggingface_hub import HfApi
9
+ import plotly.graph_objects as go
10
  import time
11
  from datetime import datetime
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ # Cyberpunk and Loading Animation Styling
14
+ def setup_cyberpunk_style():
15
  st.markdown("""
16
  <style>
17
  @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&display=swap');
18
  @import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap');
19
+
20
+ .stApp {
21
+ background: radial-gradient(circle, rgba(0, 0, 0, 0.95) 20%, rgba(0, 50, 80, 0.95) 90%);
22
+ color: #00ff9d;
23
+ font-family: 'Orbitron', sans-serif;
24
+ }
25
+
26
+ .main-title {
27
+ text-align: center;
28
+ font-size: 4em;
29
+ color: #00ff9d;
30
+ letter-spacing: 4px;
31
+ animation: glow 2s ease-in-out infinite alternate;
32
+ }
33
+
34
+ @keyframes glow {
35
+ from {text-shadow: 0 0 5px #00ff9d, 0 0 10px #00ff9d;}
36
+ to {text-shadow: 0 0 15px #00b8ff, 0 0 20px #00b8ff;}
37
+ }
38
 
39
+ .stButton > button {
40
+ font-family: 'Orbitron', sans-serif;
41
+ background: linear-gradient(45deg, #00ff9d, #00b8ff);
42
+ color: #000;
43
+ font-size: 1.1em;
44
+ padding: 10px 20px;
45
+ border: none;
46
+ border-radius: 8px;
47
+ transition: all 0.3s ease;
48
+ }
49
+
50
+ .stButton > button:hover {
51
+ transform: scale(1.1);
52
+ box-shadow: 0 0 20px rgba(0, 255, 157, 0.5);
53
+ }
54
 
55
+ .progress-bar-container {
56
+ background: rgba(0, 0, 0, 0.5);
57
+ border-radius: 15px;
58
+ overflow: hidden;
59
+ width: 100%;
60
+ height: 30px;
61
+ position: relative;
62
+ margin: 10px 0;
63
+ }
64
+
65
+ .progress-bar {
66
+ height: 100%;
67
+ width: 0%;
68
+ background: linear-gradient(45deg, #00ff9d, #00b8ff);
69
+ transition: width 0.5s ease;
70
+ }
71
+
72
+ </style>
73
+ """, unsafe_allow_html=True)
74
 
75
+ # Prepare Dataset Function with Padding Token Fix
76
  def prepare_dataset(data, tokenizer, block_size=128):
77
+ tokenizer.pad_token = tokenizer.eos_token
78
  def tokenize_function(examples):
79
  return tokenizer(examples['text'], truncation=True, max_length=block_size, padding='max_length')
80
 
 
84
  tokenized_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
85
  return tokenized_dataset
86
 
87
+ # Training Dashboard Class with Enhanced Display
88
  class TrainingDashboard:
89
  def __init__(self):
90
  self.metrics = {
91
  'current_loss': 0,
92
  'best_loss': float('inf'),
93
  'generation': 0,
94
+ 'individual': 0,
95
  'start_time': time.time(),
96
  'training_speed': 0
97
  }
98
  self.history = []
99
+
100
+ def update(self, loss, generation, individual):
101
  self.metrics['current_loss'] = loss
102
  self.metrics['generation'] = generation
103
+ self.metrics['individual'] = individual
104
  if loss < self.metrics['best_loss']:
105
  self.metrics['best_loss'] = loss
106
+
107
  elapsed_time = time.time() - self.metrics['start_time']
108
+ self.metrics['training_speed'] = (generation * individual) / elapsed_time
109
  self.history.append({'loss': loss, 'timestamp': datetime.now().strftime('%H:%M:%S')})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
+ # Define Model Initialization
112
+ def initialize_model(model_name="gpt2"):
113
+ model = GPT2LMHeadModel.from_pretrained(model_name)
114
+ tokenizer = GPT2Tokenizer.from_pretrained(model_name)
115
+ tokenizer.pad_token = tokenizer.eos_token
116
+ return model, tokenizer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
+ # Load Dataset Function
119
+ def load_dataset(data_source="demo", tokenizer=None):
120
+ if data_source == "demo":
121
+ data = ["Sample text data for model training. This can be replaced with actual data for better performance."]
122
+ else:
123
+ data = ["Loaded data from uploaded text file."]
124
+ dataset = prepare_dataset(data, tokenizer)
125
+ return dataset
126
+
127
+ # Train Model Function with Customized Progress Bar
128
+ def train_model(model, train_dataset, tokenizer, epochs=3, batch_size=4):
129
+ training_args = TrainingArguments(
130
+ output_dir="./results",
131
+ overwrite_output_dir=True,
132
+ num_train_epochs=epochs,
133
+ per_device_train_batch_size=batch_size,
134
+ save_steps=10_000,
135
+ save_total_limit=2,
136
+ logging_dir="./logs",
137
+ logging_steps=100,
138
+ )
139
+
140
+ data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)
141
 
142
+ trainer = Trainer(
143
+ model=model,
144
+ args=training_args,
145
+ data_collator=data_collator,
146
+ train_dataset=train_dataset,
147
+ )
148
 
149
+ trainer.train()
 
 
150
 
151
+ # Main App Logic
152
+ def main():
153
+ setup_cyberpunk_style()
154
+ st.markdown('<h1 class="main-title">Cyberpunk Neural Training Hub</h1>', unsafe_allow_html=True)
 
 
 
 
155
 
156
+ # Initialize model and tokenizer
157
+ model, tokenizer = initialize_model()
158
+
159
+ # Sidebar Configuration with Additional Options
160
+ with st.sidebar:
161
+ st.markdown("### Configuration Panel")
162
+ training_epochs = st.slider("Training Epochs", min_value=1, max_value=5, value=3)
163
+ batch_size = st.slider("Batch Size", min_value=2, max_value=8, value=4)
164
+ model_choice = st.selectbox("Model Selection", ("gpt2", "distilgpt2", "gpt2-medium"))
165
+ data_source = st.selectbox("Data Source", ("demo", "uploaded file"))
166
+ custom_learning_rate = st.slider("Learning Rate", min_value=1e-6, max_value=5e-4, value=3e-5, step=1e-6)
167
+
168
+ advanced_toggle = st.checkbox("Advanced Training Settings")
169
+ if advanced_toggle:
170
+ warmup_steps = st.slider("Warmup Steps", min_value=0, max_value=500, value=100)
171
+ weight_decay = st.slider("Weight Decay", min_value=0.0, max_value=0.1, step=0.01, value=0.01)
172
+ else:
173
+ warmup_steps = 100
174
+ weight_decay = 0.01
175
+
176
+ # Load Dataset
177
+ train_dataset = load_dataset(data_source, tokenizer)
178
+
179
+ # Start Training with Progress Bar
180
+ progress_placeholder = st.empty()
181
+ st.markdown("### Model Training Progress")
182
+
183
+ for epoch in range(training_epochs):
184
+ train_model(model, train_dataset, tokenizer, epochs=1, batch_size=batch_size)
185
+
186
+ # Update Progress Bar
187
+ progress = (epoch + 1) / training_epochs * 100
188
+ progress_placeholder.markdown(f"""
189
+ <div class="progress-bar-container">
190
+ <div class="progress-bar" style="width: {progress}%;"></div>
191
+ </div>
192
+ """, unsafe_allow_html=True)
193
 
194
+ st.success("Training Complete!")
 
 
195
 
196
  if __name__ == "__main__":
197
  main()