import streamlit as st import numpy as np import torch import random from transformers import ( GPT2LMHeadModel, GPT2Tokenizer, Trainer, TrainingArguments, DataCollatorForLanguageModeling ) from datasets import Dataset from huggingface_hub import HfApi import plotly.graph_objects as go import time from datetime import datetime from typing import Dict, List, Any import pandas as pd # Added pandas import # Cyberpunk and Loading Animation Styling def setup_cyberpunk_style(): st.markdown(""" """, unsafe_allow_html=True) # Prepare Dataset Function with Padding Token Fix def prepare_dataset(data, tokenizer, block_size=128): tokenizer.pad_token = tokenizer.eos_token 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 # Define Model Initialization def initialize_model(model_name="gpt2"): model = GPT2LMHeadModel.from_pretrained(model_name) tokenizer = GPT2Tokenizer.from_pretrained(model_name) tokenizer.pad_token = tokenizer.eos_token return model, tokenizer # Load Dataset Function with Uploaded File Option def load_dataset(data_source="demo", tokenizer=None, uploaded_file=None): if data_source == "demo": data = [ "In the neon-lit streets of Neo-Tokyo, a lone hacker fights against the oppressive megacorporations.", "The rain falls in sheets, washing away the bloodstains from the alleyways.", "She plugs into the matrix, seeking answers to questions that have haunted her for years." ] elif data_source == "uploaded file" and uploaded_file is not None: if uploaded_file.name.endswith(".txt"): data = [uploaded_file.read().decode("utf-8")] elif uploaded_file.name.endswith(".csv"): df = pd.read_csv(uploaded_file) data = df[df.columns[0]].astype(str).tolist() # Ensure all data is string else: data = ["Unsupported file format."] else: data = ["No file uploaded. Please upload a dataset."] dataset = prepare_dataset(data, tokenizer) return dataset # Train Model Function def train_model(model, train_dataset, tokenizer, epochs=3, batch_size=4, use_ga=False, ga_params=None): if not use_ga: training_args = TrainingArguments( output_dir="./results", overwrite_output_dir=True, num_train_epochs=epochs, per_device_train_batch_size=batch_size, save_steps=10_000, save_total_limit=2, logging_dir="./logs", logging_steps=1, logging_strategy='steps', report_to=None, # Disable default logging to WandB or other services ) data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False) trainer = Trainer( model=model, args=training_args, data_collator=data_collator, train_dataset=train_dataset, ) trainer.train() return trainer.state.log_history else: # GA training logic param_bounds = { 'learning_rate': (1e-5, 5e-5), 'epochs': (1, ga_params['max_epochs']), 'batch_size': [2, 4, 8, 16] } population = create_ga_population(ga_params['population_size'], param_bounds) best_individual = None best_fitness = float('inf') all_losses = [] for generation in range(ga_params['num_generations']): fitnesses = [] for idx, individual in enumerate(population): model_copy = GPT2LMHeadModel.from_pretrained('gpt2') training_args = TrainingArguments( output_dir=f"./results/ga_{generation}_{idx}", num_train_epochs=individual['epochs'], per_device_train_batch_size=individual['batch_size'], learning_rate=individual['learning_rate'], logging_steps=1, logging_strategy='steps', report_to=None, # Disable default logging to WandB or other services ) trainer = Trainer( model=model_copy, args=training_args, train_dataset=train_dataset, ) # Capture the training result train_result = trainer.train() # Safely retrieve the training loss fitness = train_result.metrics.get('train_loss', None) if fitness is None: # If 'train_loss' is not available, try to compute it from log history if 'loss' in trainer.state.log_history[-1]: fitness = trainer.state.log_history[-1]['loss'] else: fitness = float('inf') # Assign a large number if loss is not available fitnesses.append(fitness) all_losses.extend(trainer.state.log_history) if fitness < best_fitness: best_fitness = fitness best_individual = individual model.load_state_dict(model_copy.state_dict()) del model_copy torch.cuda.empty_cache() # GA operations parents = select_ga_parents(population, fitnesses, ga_params['num_parents']) offspring_size = ga_params['population_size'] - ga_params['num_parents'] offspring = ga_crossover(parents, offspring_size) offspring = ga_mutation(offspring, param_bounds, ga_params['mutation_rate']) population = parents + offspring return all_losses # GA-related functions def create_ga_population(size: int, param_bounds: Dict[str, Any]) -> List[Dict[str, Any]]: """Create initial population for genetic algorithm""" population = [] for _ in range(size): individual = { 'learning_rate': random.uniform(*param_bounds['learning_rate']), 'epochs': random.randint(*param_bounds['epochs']), 'batch_size': random.choice(param_bounds['batch_size']), } population.append(individual) return population def select_ga_parents(population: List[Dict[str, Any]], fitnesses: List[float], num_parents: int) -> List[Dict[str, Any]]: """Select best performing individuals as parents""" parents = [population[i] for i in np.argsort(fitnesses)[:num_parents]] return parents def ga_crossover(parents: List[Dict[str, Any]], offspring_size: int) -> List[Dict[str, Any]]: """Create offspring through crossover of parents""" offspring = [] for _ in range(offspring_size): parent1 = random.choice(parents) parent2 = random.choice(parents) child = { 'learning_rate': random.choice([parent1['learning_rate'], parent2['learning_rate']]), 'epochs': random.choice([parent1['epochs'], parent2['epochs']]), 'batch_size': random.choice([parent1['batch_size'], parent2['batch_size']]), } offspring.append(child) return offspring def ga_mutation(offspring: List[Dict[str, Any]], param_bounds: Dict[str, Any], mutation_rate: float = 0.1) -> List[Dict[str, Any]]: """Apply random mutations to offspring""" for individual in offspring: if random.random() < mutation_rate: individual['learning_rate'] = random.uniform(*param_bounds['learning_rate']) if random.random() < mutation_rate: individual['epochs'] = random.randint(*param_bounds['epochs']) if random.random() < mutation_rate: individual['batch_size'] = random.choice(param_bounds['batch_size']) return offspring # Main App Logic def main(): setup_cyberpunk_style() st.markdown('