Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,20 @@ from datasets import Dataset
|
|
6 |
import time
|
7 |
from datetime import datetime
|
8 |
import plotly.graph_objects as go
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Advanced Cyberpunk Styling
|
11 |
def setup_advanced_cyberpunk_style():
|
@@ -13,7 +27,8 @@ def setup_advanced_cyberpunk_style():
|
|
13 |
<style>
|
14 |
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&display=swap');
|
15 |
@import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap');
|
16 |
-
|
|
|
17 |
</style>
|
18 |
""", unsafe_allow_html=True)
|
19 |
|
@@ -30,10 +45,7 @@ def prepare_dataset(data, tokenizer, block_size=128):
|
|
30 |
|
31 |
raw_dataset = Dataset.from_dict({'text': data})
|
32 |
tokenized_dataset = raw_dataset.map(tokenize_function, batched=True, remove_columns=['text'])
|
33 |
-
tokenized_dataset = tokenized_dataset.map(
|
34 |
-
lambda examples: {'labels': examples['input_ids']},
|
35 |
-
batched=True
|
36 |
-
)
|
37 |
tokenized_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
|
38 |
return tokenized_dataset
|
39 |
|
@@ -72,36 +84,88 @@ def display_progress(progress):
|
|
72 |
</div>
|
73 |
""", unsafe_allow_html=True)
|
74 |
|
75 |
-
#
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
with st.spinner("Training in progress..."):
|
90 |
for generation in range(1, num_generations + 1):
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
time.sleep(1) # Simulate delay for each individual training
|
99 |
|
100 |
# Main Function
|
101 |
def main():
|
102 |
setup_advanced_cyberpunk_style()
|
103 |
st.markdown('<h1 class="main-title">Neural Evolution GPT-2 Training Hub</h1>', unsafe_allow_html=True)
|
104 |
|
|
|
|
|
|
|
|
|
|
|
105 |
# Load Model and Tokenizer
|
106 |
model, tokenizer = initialize_model()
|
107 |
|
@@ -114,12 +178,16 @@ def main():
|
|
114 |
|
115 |
# Sidebar Configuration
|
116 |
st.sidebar.markdown("### Training Parameters")
|
117 |
-
num_generations = st.sidebar.slider("Generations", 1,
|
118 |
-
population_size = st.sidebar.slider("Population Size", 4, 20,
|
|
|
|
|
|
|
|
|
119 |
|
120 |
# Run Training
|
121 |
if st.button("Start Training"):
|
122 |
-
training_loop(dashboard,
|
123 |
|
124 |
if __name__ == "__main__":
|
125 |
main()
|
|
|
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():
|
|
|
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 |
|
|
|
45 |
|
46 |
raw_dataset = Dataset.from_dict({'text': data})
|
47 |
tokenized_dataset = raw_dataset.map(tokenize_function, batched=True, remove_columns=['text'])
|
48 |
+
tokenized_dataset = tokenized_dataset.map(lambda examples: {'labels': examples['input_ids']}, batched=True)
|
|
|
|
|
|
|
49 |
tokenized_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
|
50 |
return tokenized_dataset
|
51 |
|
|
|
84 |
</div>
|
85 |
""", unsafe_allow_html=True)
|
86 |
|
87 |
+
# Custom Genetic Algorithm
|
88 |
+
class GeneticAlgorithm:
|
89 |
+
def __init__(self, model, tokenizer, dataset, population_size, mutation_rate=0.1):
|
90 |
+
self.model = model
|
91 |
+
self.tokenizer = tokenizer
|
92 |
+
self.dataset = dataset
|
93 |
+
self.population_size = population_size
|
94 |
+
self.mutation_rate = mutation_rate
|
95 |
+
self.population = [self.clone_model() for _ in range(population_size)]
|
96 |
+
|
97 |
+
def clone_model(self):
|
98 |
+
# Create a clone of the model
|
99 |
+
return GPT2LMHeadModel.from_pretrained("gpt2")
|
100 |
+
|
101 |
+
def evaluate_fitness(self, model):
|
102 |
+
# Calculate the loss for a given model on the dataset
|
103 |
+
trainer = Trainer(
|
104 |
+
model=model,
|
105 |
+
args=TrainingArguments(output_dir="./results", per_device_train_batch_size=2, num_train_epochs=1),
|
106 |
+
train_dataset=self.dataset,
|
107 |
+
data_collator=DataCollatorForLanguageModeling(tokenizer=self.tokenizer, mlm=False),
|
108 |
+
)
|
109 |
+
train_result = trainer.train()
|
110 |
+
return train_result.training_loss
|
111 |
+
|
112 |
+
def select_best_models(self, num_best=2):
|
113 |
+
# Selects the top models based on fitness (loss)
|
114 |
+
fitness_scores = [(self.evaluate_fitness(model), model) for model in self.population]
|
115 |
+
fitness_scores.sort(key=lambda x: x[0]) # Sort by loss
|
116 |
+
best_models = [model for _, model in fitness_scores[:num_best]]
|
117 |
+
return best_models
|
118 |
+
|
119 |
+
def crossover(self, parent1, parent2):
|
120 |
+
# Perform crossover by combining layers from both parents
|
121 |
+
child = self.clone_model()
|
122 |
+
for (child_param, param1, param2) in zip(child.parameters(), parent1.parameters(), parent2.parameters()):
|
123 |
+
# Randomly choose parameters from each parent based on crossover probability
|
124 |
+
if np.random.rand() > 0.5:
|
125 |
+
child_param.data = param1.data.clone()
|
126 |
+
else:
|
127 |
+
child_param.data = param2.data.clone()
|
128 |
+
return child
|
129 |
+
|
130 |
+
def mutate(self, model):
|
131 |
+
# Mutate model by slightly adjusting its weights
|
132 |
+
for param in model.parameters():
|
133 |
+
if np.random.rand() < self.mutation_rate:
|
134 |
+
mutation_tensor = torch.randn_like(param) * 0.02
|
135 |
+
param.data += mutation_tensor
|
136 |
+
|
137 |
+
def generate_new_population(self):
|
138 |
+
best_models = self.select_best_models()
|
139 |
+
new_population = []
|
140 |
+
while len(new_population) < self.population_size:
|
141 |
+
parent1, parent2 = np.random.choice(best_models, 2, replace=False)
|
142 |
+
child = self.crossover(parent1, parent2)
|
143 |
+
self.mutate(child)
|
144 |
+
new_population.append(child)
|
145 |
+
self.population = new_population
|
146 |
+
|
147 |
+
# Training Loop with Genetic Algorithm and Loading Screen
|
148 |
+
def training_loop(dashboard, ga, num_generations):
|
149 |
with st.spinner("Training in progress..."):
|
150 |
for generation in range(1, num_generations + 1):
|
151 |
+
best_loss = min([ga.evaluate_fitness(model) for model in ga.population])
|
152 |
+
dashboard.update(best_loss, generation)
|
153 |
+
progress = generation / num_generations
|
154 |
+
display_progress(progress)
|
155 |
+
dashboard.display()
|
156 |
+
ga.generate_new_population()
|
157 |
+
time.sleep(0.5) # Simulate delay for each generation
|
|
|
158 |
|
159 |
# Main Function
|
160 |
def main():
|
161 |
setup_advanced_cyberpunk_style()
|
162 |
st.markdown('<h1 class="main-title">Neural Evolution GPT-2 Training Hub</h1>', unsafe_allow_html=True)
|
163 |
|
164 |
+
# Hugging Face Account Login
|
165 |
+
token = huggingface_login()
|
166 |
+
if token is None:
|
167 |
+
return
|
168 |
+
|
169 |
# Load Model and Tokenizer
|
170 |
model, tokenizer = initialize_model()
|
171 |
|
|
|
178 |
|
179 |
# Sidebar Configuration
|
180 |
st.sidebar.markdown("### Training Parameters")
|
181 |
+
num_generations = st.sidebar.slider("Generations", 1, 50, 10)
|
182 |
+
population_size = st.sidebar.slider("Population Size", 4, 20, 10)
|
183 |
+
mutation_rate = st.sidebar.slider("Mutation Rate", 0.01, 0.5, 0.1)
|
184 |
+
|
185 |
+
# Initialize Genetic Algorithm
|
186 |
+
ga = GeneticAlgorithm(model, tokenizer, train_dataset, population_size, mutation_rate)
|
187 |
|
188 |
# Run Training
|
189 |
if st.button("Start Training"):
|
190 |
+
training_loop(dashboard, ga, num_generations)
|
191 |
|
192 |
if __name__ == "__main__":
|
193 |
main()
|