please help me complete the writing of this code: import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import json
import math
from torch.nn.utils.rnn import pad_sequence
from torch.utils.data import DataLoader, Dataset
from tqdm import tqdm
import matplotlib.pyplot as plt
from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score
from tokenizers import Tokenizer
from torch.optim.lr_scheduler import SequentialLR, StepLR, LinearLR



# ---------- Device Configuration ----------
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# -------- Performance ----------

def evaluate_model(model, data_loader, device):
    model.eval()
    all_preds, all_targets = [], []

    with torch.no_grad():
        for inputs, targets in data_loader:
            inputs, targets = inputs.to(device), targets.to(device)

            outputs = model(inputs)
            predictions = torch.argmax(outputs, dim=-1).view(-1)  # Flatten predicted indices

            all_preds.extend(predictions.cpu().numpy())
            all_targets.extend(targets.view(-1).cpu().numpy())  # Ensure targets are also flattened

    # Calculate precision, recall, and F1 score after ensuring all_preds and all_targets are correctly aligned.
    accuracy = accuracy_score(all_targets, all_preds)
    precision = precision_score(all_targets, all_preds, average='macro', zero_division=0)
    recall = recall_score(all_targets, all_preds, average='macro', zero_division=0)
    f1 = f1_score(all_targets, all_preds, average='macro', zero_division=0)

    print(f"Accuracy: {accuracy:.4f}")
    print(f"Precision: {precision:.4f}")
    print(f"Recall: {recall:.4f}")
    print(f"F1 Score: {f1:.4f}")

    return accuracy ,precision, recall, f1

# Function to plot loss over time
def plot_loss(loss_history):
    plt.figure(figsize=(10, 5))
    plt.plot(loss_history, label='Training Loss')
    plt.xlabel('Batches')
    plt.ylabel('Loss')
    plt.title('Training Loss Over Time')
    plt.legend()
    plt.show()

# ---------- Model Definitions ----------
    
def generate_square_subsequent_mask(sz: int) -> torch.Tensor:
    """Generates an upper-triangular matrix of -inf, with zeros on diag."""
    return torch.triu(torch.ones((sz, sz)) * float("-inf"), diagonal=1)

class ConfigurableTransformer(nn.Module):
    def __init__(self, vocab_size, d_model_configs, nhead, dim_feedforward_configs, output_size):
        super(ConfigurableTransformer, self).__init__()
        self.embedding = nn.Embedding(vocab_size, d_model_configs[0])
        
        transformer_layers = []
        input_dim = d_model_configs[0]
        for d_model, dim_feedforward in zip(d_model_configs, dim_feedforward_configs):
            transformer_layer = nn.TransformerEncoderLayer(d_model=d_model, nhead=nhead, dim_feedforward=dim_feedforward)
            transformer_layers.append(transformer_layer)
            
            # Optional: if you want to support changing d_model within the stack
            if d_model != input_dim:
                transformer_layers.append(nn.Linear(input_dim, d_model))
                input_dim = d_model
                
        self.transformer_encoder = nn.Sequential(*transformer_layers)
        self.output_fc = nn.Linear(input_dim, output_size)

class SimpleTransformer(nn.Module):
    def __init__(self, vocab_size, d_model, output_size, nhead, dim_feedforward, num_encoder_layers, dropout_rate=0.1):
        super(SimpleTransformer, self).__init__()
        self.embedding = nn.Embedding(vocab_size, d_model)
        self.pos_encoder = nn.Parameter(self.positional_encoding(vocab_size, d_model), requires_grad=False)
        self.encoder_layer = nn.TransformerEncoderLayer(d_model=d_model, nhead=nhead, dim_feedforward=dim_feedforward, dropout=dropout_rate)
        self.transformer_encoder = nn.TransformerEncoder(self.encoder_layer, num_layers=num_encoder_layers)
        self.output_fc = nn.Linear(d_model, output_size)
        self.dropout = nn.Dropout(dropout_rate)
    
    def positional_encoding(self, seq_len, d_model):
        position = torch.arange(seq_len, dtype=torch.float).unsqueeze(1)
        div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
        pe = torch.zeros(seq_len, d_model)
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        return pe.unsqueeze(0)
    
    def forward(self, src):
        src = self.embedding(src) * math.sqrt(self.embedding.embedding_dim)
        seq_length = src.size(1)  # Assuming src shape [batch_size, seq_len, d_model]
        attention_mask = generate_square_subsequent_mask(seq_length).to(device)
        # Ensure positional encoding is resized to match input sequence length
        pos_encoder = self.positional_encoding(seq_len, self.embedding.embedding_dim).to(src.device)
        src = src + pos_encoder[:, :seq_len]
        src = self.dropout(src)
        output = self.transformer_encoder(src, src_key_padding_mask=attention_mask)
        output = self.output_fc(output)
        return output

# ---------- Dataset Definitions ----------
class QAJsonlDataset(Dataset):
    def __init__(self, path, seq_len, tokenizer_path):
        # Load the trained tokenizer
        self.tokenizer = Tokenizer.from_file(tokenizer_path)
        
        self.seq_len = seq_len
        self.pairs = self.load_data(path)
        # Using BPE, so no need for manual vocab or idx2token.
        # Tokenization will now happen using self.tokenizer.
        self.tokenized_pairs = [(self.tokenize(q), self.tokenize(a)) for q, a in self.pairs]

    def load_data(self, path):
        pairs = []
        with open(path, "r", encoding="utf-8") as f:
            for line in f:
                data = json.loads(line.strip())
                question, answer = data.get("user", ""), data.get("content", "")
                pairs.append((question, answer))  # Store questions and answers as raw strings
        return pairs

    def tokenize(self, text):
        # Tokenizing using the BPE tokenizer
        encoded = self.tokenizer.encode(text)
        tokens = encoded.ids
        # Padding/truncation
        if len(tokens) < self.seq_len:
            # Padding
            tokens += [self.tokenizer.token_to_id("<pad>")] * (self.seq_len - len(tokens))
        else:
            # Truncation
            tokens = tokens[:self.seq_len - 1] + [self.tokenizer.token_to_id("<eos>")]
        return tokens
        
    def __len__(self):
        return len(self.tokenized_pairs)

    def __getitem__(self, idx):
        tokenized_question, tokenized_answer = self.tokenized_pairs[idx]
        return torch.tensor(tokenized_question, dtype=torch.long), torch.tensor(tokenized_answer, dtype=torch.long)

def collate_fn(batch):
    questions, answers = zip(*batch)
    questions = pad_sequence(questions, batch_first=True, padding_value=0)
    answers = pad_sequence(answers, batch_first=True, padding_value=0)
    return questions, answers

# ---------- Training and Inference Functions ----------

best_loss = float('inf')
best_model = None

def train_model(model, criterion, optimizer, num_epochs, data_loader, label_smoothing=0.1):
    criterion = nn.CrossEntropyLoss(label_smoothing=label_smoothing)
    model.train()
    loss_history = []  # Initialize a list to keep track of losses
    for epoch in range(num_epochs):
        total_loss = 0
        total_items = 0  # Keep track of total items processed
        progress_bar = tqdm(enumerate(data_loader), total=len(data_loader), desc=f"Epoch {epoch+1}", leave=False)
        for i, (inputs, targets) in progress_bar:
            inputs, targets = inputs.to(device), targets.to(device)
            optimizer.zero_grad()
            
            # Predict
            predictions = model(inputs)
            predictions = predictions.view(-1, predictions.size(-1))  # Make sure predictions are the right shape
            targets = targets.view(-1)  # Flatten targets to match prediction shape if necessary
            
            # Calculate loss
            loss = criterion(predictions, targets)
            loss.backward()
            
            # Gradient clipping for stabilization
            torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
            optimizer.step()
            #scheduler.step()
            
            # Update total loss and the number of items
            total_loss += loss.item() * inputs.size(0)  # Multiply loss by batch size
            total_items += inputs.size(0)
            loss_history.append(loss.item())
            
            progress_bar.set_postfix({"Loss": loss.item()})
        
        average_loss = total_loss / total_items  # Correctly compute average loss
        print(f"Epoch {epoch+1}, Average Loss: {average_loss:.6f}")
    
    return loss_history

class WarmupLR(torch.optim.lr_scheduler._LRScheduler):
    def __init__(self, optimizer, warmup_steps, scheduler_step_lr):
        self.warmup_steps = warmup_steps
        self.scheduler_step_lr = scheduler_step_lr  # The subsequent scheduler
        super(WarmupLR, self).__init__(optimizer)
    
    def get_lr(self):
        if self._step_count <= self.warmup_steps:
            warmup_factor = float(self._step_count) / float(max(1, self.warmup_steps))
            for base_lr in self.base_lrs:
                yield base_lr * warmup_factor
        else:
            self.scheduler_step_lr.step()  # Update the subsequent scheduler
            for param_group in self.optimizer.param_groups:
                yield param_group['lr']

class GERU(nn.Module):
    def __init__(self, in_features):
        super(GERU, self).__init__()
        self.alpha = nn.Parameter(torch.rand(in_features))
        
    def forward(self, x):
        return torch.max(x, torch.zeros_like(x)) + self.alpha * torch.min(x, torch.zeros_like(x))

def generate_text(model, tokenizer, seed_text, num_generate, temperature=1.0):
    model.eval()
    
    generated_tokens = []
    
    # Encode the seed text using the tokenizer
    encoded_input = tokenizer.encode(seed_text)
    input_ids = torch.tensor(encoded_input.ids, dtype=torch.long).unsqueeze(0).to(device)

    # Generate num_generate tokens
    with torch.no_grad():
        for _ in range(num_generate):
            output = model(input_ids)
            
            # Get the last logits and apply temperature
            logits = output[:, -1, :] / temperature
            probabilities = F.softmax(logits, dim=-1)
            next_token_id = torch.argmax(probabilities, dim=-1).item()

            # Append generated token ID and prepare the new input_ids
            generated_tokens.append(next_token_id)
            input_ids = torch.cat([input_ids, torch.tensor([[next_token_id]], dtype=torch.long).to(device)], dim=1)

    # Decode the generated token IDs back to text
    generated_text = tokenizer.decode(generated_tokens)
    return generated_text

def count_tokens_in_dataset(dataset):
    return sum([len(pair[0]) + len(pair[1]) for pair in dataset.pairs])

def count_parameters(model):
    return sum(p.numel() for p in model.parameters() if p.requires_grad)

# ---------- Hyperparameters and Model Instantiation ----------
# Transformer :
d_model = 128
nhead = 8
dim_feedforward = 512
num_encoder_layers = 7

# Training Parameters
batch_size = 64  # Adjustable batch size
optimizer_type = "AdamW"  # Could be “SGD”, “RMSprop”, etc.
learning_rate = 0.01
weight_decay = 0.01  # For L2 regularization
num_epochs = 500

# Dataset :
path_to_dataset = "C:/Users/L14/Documents/Projets/Easy-MoE/Easy-MoE/data/Real_talk.jsonl"
tokenizer_path = "tokenizer/BPE_tokenizer(Real-Talk).json"
seq_len = 64
dataset = QAJsonlDataset(path_to_dataset, seq_len, tokenizer_path)
data_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True, collate_fn=collate_fn, pin_memory=True)
    
num_tokens = count_tokens_in_dataset(dataset)
print(f"Total number of tokens in the dataset: {num_tokens}")

# Load the tokenizer
tokenizer = Tokenizer.from_file(tokenizer_path)

# Determine the vocabulary size
vocab_size = tokenizer.get_vocab_size()

moe = SimpleTransformer(
    vocab_size=vocab_size, 
    d_model=d_model, 
    output_size=vocab_size, 
    nhead=nhead, 
    dim_feedforward=dim_feedforward, 
    num_encoder_layers=num_encoder_layers, 
    dropout_rate=0.1
).to(device)

# Count of total parameters :
total_params = count_parameters(moe)
print(f"Total trainable parameters: {total_params}")

# ---------- Training ----------

# Adjusting optimizer setup to include weight decay and allow switching between types
if optimizer_type == "AdamW":
    optimizer = torch.optim.AdamW(moe.parameters(), lr=learning_rate, weight_decay=weight_decay)
elif optimizer_type == "SGD":
    optimizer = torch.optim.SGD(moe.parameters(), lr=learning_rate, momentum=0.9, weight_decay=weight_decay)
elif optimizer_type == "Adam":
    optimizer = torch.optim.Adam(moe.parameters(), lr=learning_rate, weight_decay=weight_decay)

# Setup optimizers just like before
warmup_epochs = 2
scheduler1 = LinearLR(optimizer, start_factor=0.01, total_iters=warmup_epochs)
scheduler2 = StepLR(optimizer, step_size=10, gamma=0.9)

scheduler = SequentialLR(optimizer, schedulers=[scheduler1, scheduler2], milestones=[warmup_epochs])

criterion = nn.CrossEntropyLoss(label_smoothing=0.1)

# Train the model
loss_history = train_model(moe, criterion, optimizer, num_epochs, data_loader)

# Evaluating the model
plot_loss(loss_history)
train_accuracy = evaluate_model(moe, data_loader, device)

# ---------- Inference ----------
def interactive_text_generation(model, dataset, max_length=32, temperature=1.0):
    while True:
        try:
            # Get user input
            seed_text = input("Enter seed text (type 'quit' to exit and save the model): ").strip()
            
            # Check if the user wants to quit the interaction
            if seed_text.lower() == 'quit':
                print("Exiting text generation mode.")
                break

            # Generate text based on the seed text
            if seed_text:
                generated_text = generate_text(model, dataset, seed_text, max_length, temperature)  # Modify max_length/temperature as needed
                print("Generated Text:", generated_text)
            else:
                print("Seed text cannot be empty. Please enter some text.")
                
        except Exception as e:
            # Handle potential errors gracefully
            print(f"An error occurred: {e}. Try again.")

interactive_text_generation(moe, tokenizer)

# ---------- Save Trained Model ----------

def save_model_with_config(model, config, save_dir, model_name):
    """
    Saves the model weights, configuration, and performance metrics.

    Parameters:
    - model: the PyTorch model to save.
    - config: a dictionary with the model's configuration.
    - metrics: a dictionary with the model's performance metrics.
    - save_dir: the root directory to save the model and its info.
    - model_name: the name of the model, used to create a subdirectory.
    """
    model_path = os.path.join(save_dir, model_name)
    os.makedirs(model_path, exist_ok=True)

    # Save model weigths
    torch.save(model.state_dict(), os.path.join(model_path, '.pth'))

    # Save configuration
    with open(os.path.join(model_path, 'config.json'), 'w') as config_file:
        json.dump(config, config_file, indent=4)

    # Save metrics
    #with open(os.path.join(model_path, 'metrics.json'), 'w') as metrics_file:
    #    json.dump(metrics, metrics_file, indent=4)

    print(f"Model, configuration, and metrics saved in {model_path}")

config = {
    'd_model': d_model,'nhead': nhead,'dim_feedforward': dim_feedforward,'num_encoder_layers': num_encoder_layers,
    'seq_len': seq_len,'batch_size': batch_size,'learning_rate': learning_rate,
    'weight_decay': weight_decay,'num_epochs': num_epochs,
}

save_model_with_config(moe, config, "Trained_models", "Transformer-Alpha-v04")