import gradio as gr import torch import random from unidecode import unidecode import re from samplings import top_p_sampling, top_k_sampling, temperature_sampling from transformers import GPT2Model, GPT2LMHeadModel, PreTrainedModel device = torch.device("cuda" if torch.cuda.is_available() else "cpu") PATCH_LENGTH = 128 # Patch Length PATCH_SIZE = 32 # Patch Size PATCH_NUM_LAYERS = 9 # Number of layers in the encoder CHAR_NUM_LAYERS = 3 # Number of layers in the decoder NUM_EPOCHS = 32 # Number of epochs to train for (if early stopping doesn't intervene) LEARNING_RATE = 5e-5 # Learning rate for the optimizer PATCH_SAMPLING_BATCH_SIZE = 0 # Batch size for patch during training, 0 for full context LOAD_FROM_CHECKPOINT = False # Whether to load weights from a checkpoint SHARE_WEIGHTS = False # Whether to share weights between the encoder and decoder description = """
Duplicate Space
## ℹ️ How to use this demo? 1. Enter the control codes to set the musical form of the generated music. For details, please refer to the below "Control Codes" section. (optional) 2. Enter the prefix of the generated music. You can set the ABC header (i.e., note length, tempo, meter, and key) and the beginning of the music. (optional) 2. You can set the parameters (i.e., number of tunes, maximum length, top-p, temperature, and random seed) for the generation. (optional) 3. Click "Submit" and wait for the result. 4. The generated ABC notation can be converted to MIDI or PDF using [EasyABC](https://sourceforge.net/projects/easyabc/), you can also use this [online renderer](https://ldzhangyx.github.io/abc/) to render the ABC notation. ## 📝 Control Codes There are 3 types of control codes: section bar `[BARS_NB]`, `[SECS_NS]`, and similarity `[SIM_EDS]`. The control codes can be used to set the musical form of the generated music: - `[BARS_NB]`: controls the number of bars in a section of the melody. For example, users could specify that they want a section to contain 8 bars, and TunesFormer would generate a section that fits within that structure. It counts on the bar symbol `|`. The value of `NB` ranges from 1 to 32, where 1 means one bar and 32 means thirty-two bars. - `[SECS_NS]`: controls the number of sections in the entire melody. This can be used to create a sense of structure and coherence within the melody, as different sections can be used to create musical themes or motifs. It counts on several symbols that are commonly used in ABC notation and can be used to represent section boundaries: `[|`,`||`,`|]`,`|:`,`::`, and `:|`. The value of `NS` ranges from 1 to 8, where 1 means one section and 8 means eight sections. - `[SIM_EDS]`: controls the similarity between the generated music and the prefix. The value of `EDS` is the edit distance similarity level between the current section and a previous section in the melody. The value of EDS ranges from 0 to 10, where 0 means no similarity and 10 means the same section. To provide a clearer understanding of the control codes, we provide an example below: `[SECS_3][BARS_8][SIM_3][BARS_8][SIM_10][SIM_3][BARS_8]` The first control code `[SECS_3]` specifies there are 3 sections in the tune, and the following control code `[BARS_8]` indicates the first section has 8 bars. The next two control codes `[SIM_3]` and `[BAR_8]` indicate that the EDS between section II and section I is approximately 0.3, and section II has 8 bars. The last three control codes `[SIM_10]`, `[SIM_3]` and `[BARS_8]` specify that section III is identical to section I while dissimilar to section II, and has 8 bars. ## ❕Notice - If the prefix is given, you must also provide the control codes. - The order of the ABC header cannot be changed (i.e., note length, tempo, meter, and key), and also do not support other headers (e.g., composer, title, etc.) as they are not used in the model. For details, please refer to the official [ABC notation](https://abcnotation.com/wiki/abc:standard:v2.1#description_of_information_fields). - The demo is based on GPT2-small and trained from scratch on the [Massive ABC Notation Dataset](https://huggingface.co/datasets/sander-wood/massive_abcnotation_dataset). - The demo is still in the early stage, and the generated music is not perfect. If you have any suggestions, please feel free to contact me via [email](mailto:shangda@mail.ccom.edu.cn). """ class Patchilizer: """ A class for converting music bars to patches and vice versa. """ def __init__(self): self.delimiters = ["|:", "::", ":|", "[|", "||", "|]", "|"] self.regexPattern = '(' + '|'.join(map(re.escape, self.delimiters)) + ')' self.pad_token_id = 0 self.bos_token_id = 1 self.eos_token_id = 2 def split_bars(self, body): """ Split a body of music into individual bars. """ bars = re.split(self.regexPattern, ''.join(body)) bars = list(filter(None, bars)) # remove empty strings if bars[0] in self.delimiters: bars[1] = bars[0] + bars[1] bars = bars[1:] bars = [bars[i * 2] + bars[i * 2 + 1] for i in range(len(bars) // 2)] return bars def bar2patch(self, bar, patch_size=PATCH_SIZE): """ Convert a bar into a patch of specified length. """ patch = [self.bos_token_id] + [ord(c) for c in bar] + [self.eos_token_id] patch = patch[:patch_size] patch += [self.pad_token_id] * (patch_size - len(patch)) return patch def patch2bar(self, patch): """ Convert a patch into a bar. """ return ''.join(chr(idx) if idx > self.eos_token_id else '' for idx in patch if idx != self.eos_token_id) def encode(self, abc_code, patch_length=PATCH_LENGTH, patch_size=PATCH_SIZE, add_special_patches=False): """ Encode music into patches of specified length. """ lines = unidecode(abc_code).split('\n') lines = list(filter(None, lines)) # remove empty lines body = "" patches = [] for line in lines: if len(line) > 1 and ((line[0].isalpha() and line[1] == ':') or line.startswith('%%score')): if body: bars = self.split_bars(body) patches.extend(self.bar2patch(bar + '\n' if idx == len(bars) - 1 else bar, patch_size) for idx, bar in enumerate(bars)) body = "" patches.append(self.bar2patch(line + '\n', patch_size)) else: body += line + '\n' if body: patches.extend(self.bar2patch(bar, patch_size) for bar in self.split_bars(body)) if add_special_patches: bos_patch = [self.bos_token_id] * (patch_size-1) + [self.eos_token_id] eos_patch = [self.bos_token_id] + [self.eos_token_id] * (patch_size-1) patches = [bos_patch] + patches + [eos_patch] return patches[:patch_length] def decode(self, patches): """ Decode patches into music. """ return ''.join(self.patch2bar(patch) for patch in patches) class PatchLevelDecoder(PreTrainedModel): """ An Patch-level Decoder model for generating patch features in an auto-regressive manner. It inherits PreTrainedModel from transformers. """ def __init__(self, config): super().__init__(config) self.patch_embedding = torch.nn.Linear(PATCH_SIZE * 128, config.n_embd) torch.nn.init.normal_(self.patch_embedding.weight, std=0.02) self.base = GPT2Model(config) def forward(self, patches: torch.Tensor) -> torch.Tensor: """ The forward pass of the patch-level decoder model. :param patches: the patches to be encoded :return: the encoded patches """ patches = torch.nn.functional.one_hot(patches, num_classes=128).float() patches = patches.reshape(len(patches), -1, PATCH_SIZE * 128) patches = self.patch_embedding(patches.to(self.device)) return self.base(inputs_embeds=patches) class CharLevelDecoder(PreTrainedModel): """ A Char-level Decoder model for generating the characters within each bar patch sequentially. It inherits PreTrainedModel from transformers. """ def __init__(self, config): super().__init__(config) self.pad_token_id = 0 self.bos_token_id = 1 self.eos_token_id = 2 self.base = GPT2LMHeadModel(config) def forward(self, encoded_patches: torch.Tensor, target_patches: torch.Tensor, patch_sampling_batch_size: int): """ The forward pass of the char-level decoder model. :param encoded_patches: the encoded patches :param target_patches: the target patches :return: the decoded patches """ # preparing the labels for model training target_masks = target_patches == self.pad_token_id labels = target_patches.clone().masked_fill_(target_masks, -100) # masking the labels for model training target_masks = torch.ones_like(labels) target_masks = target_masks.masked_fill_(labels == -100, 0) # select patches if patch_sampling_batch_size!=0 and patch_sampling_batch_size= PATCH_SIZE - 1: break else: tokens = torch.cat((tokens, torch.tensor([token], device=self.device)), dim=0) return generated_patch, n_seed def generate_abc(prompt, num_tunes, max_patch, top_p, top_k, temperature, seed, show_control_code): if torch.cuda.is_available(): device = torch.device("cuda") else: device = torch.device("cpu") patchilizer = Patchilizer() patch_config = GPT2Config(num_hidden_layers=PATCH_NUM_LAYERS, max_length=PATCH_LENGTH, max_position_embeddings=PATCH_LENGTH, vocab_size=1) char_config = GPT2Config(num_hidden_layers=CHAR_NUM_LAYERS, max_length=PATCH_SIZE, max_position_embeddings=PATCH_SIZE, vocab_size=128) model = TunesFormer(patch_config, char_config, share_weights=SHARE_WEIGHTS) filename = "weights.pth" if os.path.exists(filename): print(f"Weights already exist at '{filename}'. Loading...") else: print(f"Downloading weights to '{filename}' from huggingface.co...") try: url = 'https://huggingface.co/sander-wood/tunesformer/resolve/main/weights.pth' response = requests.get(url, stream=True) total_size = int(response.headers.get('content-length', 0)) chunk_size = 1024 with open(filename, 'wb') as file, tqdm( desc=filename, total=total_size, unit='B', unit_scale=True, unit_divisor=1024, ) as bar: for data in response.iter_content(chunk_size=chunk_size): size = file.write(data) bar.update(size) except Exception as e: print(f"Error: {e}") exit() checkpoint = torch.load('weights.pth') model.load_state_dict(checkpoint['model']) model = model.to(device) model.eval() tunes = "" print("\n"+" OUTPUT TUNES ".center(60, "#")) start_time = time.time() for i in range(num_tunes): tune = "X:"+str(i+1) + "\n" + prompt lines = re.split(r'(\n)', tune) tune = "" skip = False for line in lines: if show_control_code or line[:2] not in ["S:", "B:", "E:"]: if not skip: print(line, end="") tune += line skip = False else: skip = True input_patches = torch.tensor([patchilizer.encode(prompt, add_special_patches=True)[:-1]], device=device) if tune=="": tokens = None else: prefix = patchilizer.decode(input_patches[0]) remaining_tokens = prompt[len(prefix):] tokens = torch.tensor([patchilizer.bos_token_id]+[ord(c) for c in remaining_tokens], device=device) while input_patches.shape[1]