commit 27f2fc6f5b98a2b9a2820cb606a9503a4aaff96e Author: ModelHub XC Date: Sat Aug 8 00:05:18 2026 +0800 初始化项目,由ModelHub XC社区提供模型 Model: TristanBehrens/js-fakes-4bars Source: Original Platform diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6d34772 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,27 @@ +*.7z filter=lfs diff=lfs merge=lfs -text +*.arrow filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.bin.* filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.ftz filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.h5 filter=lfs diff=lfs merge=lfs -text +*.joblib filter=lfs diff=lfs merge=lfs -text +*.lfs.* filter=lfs diff=lfs merge=lfs -text +*.model filter=lfs diff=lfs merge=lfs -text +*.msgpack filter=lfs diff=lfs merge=lfs -text +*.onnx filter=lfs diff=lfs merge=lfs -text +*.ot filter=lfs diff=lfs merge=lfs -text +*.parquet filter=lfs diff=lfs merge=lfs -text +*.pb filter=lfs diff=lfs merge=lfs -text +*.pt filter=lfs diff=lfs merge=lfs -text +*.pth filter=lfs diff=lfs merge=lfs -text +*.rar filter=lfs diff=lfs merge=lfs -text +saved_model/**/* filter=lfs diff=lfs merge=lfs -text +*.tar.* filter=lfs diff=lfs merge=lfs -text +*.tflite filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zstandard filter=lfs diff=lfs merge=lfs -text +*tfevents* filter=lfs diff=lfs merge=lfs -text diff --git a/README.md b/README.md new file mode 100644 index 0000000..13e1321 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +--- + +tags: +- gpt2 +- text-generation +- music-modeling +- music-generation + +widget: + - text: "PIECE_START" + - text: "PIECE_START STYLE=JSFAKES GENRE=JSFAKES TRACK_START INST=48 BAR_START NOTE_ON=60" + - text: "PIECE_START STYLE=JSFAKES GENRE=JSFAKES TRACK_START INST=48 BAR_START NOTE_ON=58" + +--- + + +# GPT-2 for Music + +Language Models such as GPT-2 can be used for Music Generation. The idea is to represent pieces of music as texts, effectively reducing the task to Language Generation. + +This model is a rather small instance of GPT-2 trained on [TristanBehrens/js-fakes-4bars](https://huggingface.co/datasets/TristanBehrens/js-fakes-4bars). The model generates 4 bars at a time of Bach-like chorales with four voices (soprano, alto, tenor, bass). + +If you are contribute, if you want to say hello, if you want to know more, find me on [LinkedIn](https://www.linkedin.com/in/dr-tristan-behrens-734967a2/) + +## Model description + +The model is GPT-2 with 6 decoders and 8 attention-heads each. The context length is 512. The embedding dimensions are 512 as well. The vocabulary size is 119. + +## Intended uses & limitations + +This model is just a proof of concept. It shows that HuggingFace can be used to compose music. + +### How to use + +There is a notebook in the repo that you can run on Google Colab. + +### Limitations and bias + +Since this model has been trained on a very small corpus of music, it is overfitting heavily. \ No newline at end of file diff --git a/colab_jsfakes_generation.ipynb b/colab_jsfakes_generation.ipynb new file mode 100644 index 0000000..49e7732 --- /dev/null +++ b/colab_jsfakes_generation.ipynb @@ -0,0 +1,258 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "DWLOSBkp0A2U" + }, + "source": [ + "# GPT-2 for music.\n", + "\n", + "This notebook shows you how to generate music with GPT-2\n", + "\n", + "---\n", + "\n", + "## Install depencencies." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "6J_AnhV8D5p6" + }, + "outputs": [], + "source": [ + "!pip install transformers\n", + "!pip install note_seq" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RzhHhFll0JVl" + }, + "source": [ + "## Load the tokenizer and the model from 🤗 Hub." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "g3ih12FMD7bs" + }, + "outputs": [], + "source": [ + "from transformers import AutoTokenizer, AutoModelForCausalLM\n", + "\n", + "tokenizer = AutoTokenizer.from_pretrained(\"TristanBehrens/js-fakes-4bars\")\n", + "model = AutoModelForCausalLM.from_pretrained(\"TristanBehrens/js-fakes-4bars\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GxRBk--Q0P1q" + }, + "source": [ + "## How to generate." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "ZZSLX96ID7t8" + }, + "outputs": [], + "source": [ + "# Encode the conditioning tokens.\n", + "input_ids = tokenizer.encode(\"PIECE_START STYLE=JSFAKES GENRE=JSFAKES TRACK_START INST=48 BAR_START NOTE_ON=60\", return_tensors=\"pt\")\n", + "print(input_ids)\n", + "\n", + "# Generate more tokens.\n", + "generated_ids = model.generate(input_ids, max_length=500)\n", + "generated_sequence = tokenizer.decode(generated_ids[0])\n", + "print(generated_sequence)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YfHXFugA0WdI" + }, + "source": [ + "## Convert the generated tokens to music that you can listen to." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "L3QMj8NyEBqs" + }, + "outputs": [], + "source": [ + "import note_seq\n", + "\n", + "NOTE_LENGTH_16TH_120BPM = 0.25 * 60 / 120\n", + "BAR_LENGTH_120BPM = 4.0 * 60 / 120\n", + "\n", + "def token_sequence_to_note_sequence(token_sequence, use_program=True, use_drums=True, instrument_mapper=None, only_piano=False):\n", + "\n", + " if isinstance(token_sequence, str):\n", + " token_sequence = token_sequence.split()\n", + "\n", + " note_sequence = empty_note_sequence()\n", + "\n", + " # Render all notes.\n", + " current_program = 1\n", + " current_is_drum = False\n", + " current_instrument = 0\n", + " track_count = 0\n", + " for token_index, token in enumerate(token_sequence):\n", + "\n", + " if token == \"PIECE_START\":\n", + " pass\n", + " elif token == \"PIECE_END\":\n", + " print(\"The end.\")\n", + " break\n", + " elif token == \"TRACK_START\":\n", + " current_bar_index = 0\n", + " track_count += 1\n", + " pass\n", + " elif token == \"TRACK_END\":\n", + " pass\n", + " elif token == \"KEYS_START\":\n", + " pass\n", + " elif token == \"KEYS_END\":\n", + " pass\n", + " elif token.startswith(\"KEY=\"):\n", + " pass\n", + " elif token.startswith(\"INST\"):\n", + " instrument = token.split(\"=\")[-1]\n", + " if instrument != \"DRUMS\" and use_program:\n", + " if instrument_mapper is not None:\n", + " if instrument in instrument_mapper:\n", + " instrument = instrument_mapper[instrument]\n", + " current_program = int(instrument)\n", + " current_instrument = track_count\n", + " current_is_drum = False\n", + " if instrument == \"DRUMS\" and use_drums:\n", + " current_instrument = 0\n", + " current_program = 0\n", + " current_is_drum = True\n", + " elif token == \"BAR_START\":\n", + " current_time = current_bar_index * BAR_LENGTH_120BPM\n", + " current_notes = {}\n", + " elif token == \"BAR_END\":\n", + " current_bar_index += 1\n", + " pass\n", + " elif token.startswith(\"NOTE_ON\"):\n", + " pitch = int(token.split(\"=\")[-1])\n", + " note = note_sequence.notes.add()\n", + " note.start_time = current_time\n", + " note.end_time = current_time + 4 * NOTE_LENGTH_16TH_120BPM\n", + " note.pitch = pitch\n", + " note.instrument = current_instrument\n", + " note.program = current_program\n", + " note.velocity = 80\n", + " note.is_drum = current_is_drum\n", + " current_notes[pitch] = note\n", + " elif token.startswith(\"NOTE_OFF\"):\n", + " pitch = int(token.split(\"=\")[-1])\n", + " if pitch in current_notes:\n", + " note = current_notes[pitch]\n", + " note.end_time = current_time\n", + " elif token.startswith(\"TIME_DELTA\"):\n", + " delta = float(token.split(\"=\")[-1]) * NOTE_LENGTH_16TH_120BPM\n", + " current_time += delta\n", + " elif token.startswith(\"DENSITY=\"):\n", + " pass\n", + " elif token == \"[PAD]\":\n", + " pass\n", + " else:\n", + " #print(f\"Ignored token {token}.\")\n", + " pass\n", + "\n", + " # Make the instruments right.\n", + " instruments_drums = []\n", + " for note in note_sequence.notes:\n", + " pair = [note.program, note.is_drum]\n", + " if pair not in instruments_drums:\n", + " instruments_drums += [pair]\n", + " note.instrument = instruments_drums.index(pair)\n", + "\n", + " if only_piano:\n", + " for note in note_sequence.notes:\n", + " if not note.is_drum:\n", + " note.instrument = 0\n", + " note.program = 0\n", + "\n", + " return note_sequence\n", + "\n", + "def empty_note_sequence(qpm=120.0, total_time=0.0):\n", + " note_sequence = note_seq.protobuf.music_pb2.NoteSequence()\n", + " note_sequence.tempos.add().qpm = qpm\n", + " note_sequence.ticks_per_quarter = note_seq.constants.STANDARD_PPQ\n", + " note_sequence.total_time = total_time\n", + " return note_sequence" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "ZYpukydNESDF" + }, + "outputs": [], + "source": [ + "input_ids = tokenizer.encode(\"PIECE_START STYLE=JSFAKES GENRE=JSFAKES TRACK_START INST=48 BAR_START NOTE_ON=61\", return_tensors=\"pt\")\n", + "generated_ids = model.generate(input_ids, max_length=500, temperature=1.0)\n", + "generated_sequence = tokenizer.decode(generated_ids[0])\n", + "\n", + "note_sequence = token_sequence_to_note_sequence(generated_sequence)\n", + "\n", + "synth = note_seq.midi_synth.synthesize\n", + "note_seq.plot_sequence(note_sequence)\n", + "note_seq.play_sequence(note_sequence, synth)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "d1x6HeF90kkO" + }, + "source": [ + "# Thank you!" + ] + } + ], + "metadata": { + "colab": { + "collapsed_sections": [], + "name": "colab_jsfakes_generation.ipynb", + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/config.json b/config.json new file mode 100644 index 0000000..0c25af3 --- /dev/null +++ b/config.json @@ -0,0 +1,33 @@ +{ + "activation_function": "gelu_new", + "architectures": [ + "GPT2LMHeadModel" + ], + "attn_pdrop": 0.1, + "bos_token_id": 50256, + "embd_pdrop": 0.1, + "eos_token_id": 50256, + "initializer_range": 0.02, + "layer_norm_epsilon": 1e-05, + "model_type": "gpt2", + "n_ctx": 512, + "n_embd": 512, + "n_head": 8, + "n_inner": null, + "n_layer": 6, + "n_positions": 512, + "pad_token_id": 3, + "reorder_and_upcast_attn": false, + "resid_pdrop": 0.1, + "scale_attn_by_inverse_layer_idx": false, + "scale_attn_weights": true, + "summary_activation": null, + "summary_first_dropout": 0.1, + "summary_proj_to_labels": true, + "summary_type": "cls_index", + "summary_use_proj": true, + "torch_dtype": "float32", + "transformers_version": "4.15.0", + "use_cache": true, + "vocab_size": 119 +} diff --git a/pytorch_model.bin b/pytorch_model.bin new file mode 100644 index 0000000..1424687 --- /dev/null +++ b/pytorch_model.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:45ee67cbca1e3005006503fbd2068873b8b1a064ece8880edcc7300a3e221ae1 +size 78557661 diff --git a/special_tokens_map.json b/special_tokens_map.json new file mode 100644 index 0000000..01e526c --- /dev/null +++ b/special_tokens_map.json @@ -0,0 +1 @@ +{"pad_token": "[PAD]"} \ No newline at end of file diff --git a/tokenizer.json b/tokenizer.json new file mode 100644 index 0000000..2bfbcdc --- /dev/null +++ b/tokenizer.json @@ -0,0 +1 @@ +{"version":"1.0","truncation":{"max_length":512,"strategy":"LongestFirst","stride":0},"padding":null,"added_tokens":[{"id":0,"special":true,"content":"[UNK]","single_word":false,"lstrip":false,"rstrip":false,"normalized":false},{"id":1,"special":true,"content":"[CLS]","single_word":false,"lstrip":false,"rstrip":false,"normalized":false},{"id":2,"special":true,"content":"[SEP]","single_word":false,"lstrip":false,"rstrip":false,"normalized":false},{"id":3,"special":true,"content":"[PAD]","single_word":false,"lstrip":false,"rstrip":false,"normalized":false},{"id":4,"special":true,"content":"[MASK]","single_word":false,"lstrip":false,"rstrip":false,"normalized":false}],"normalizer":null,"pre_tokenizer":{"type":"WhitespaceSplit"},"post_processor":null,"decoder":null,"model":{"type":"WordLevel","vocab":{"[UNK]":0,"[CLS]":1,"[SEP]":2,"[PAD]":3,"[MASK]":4,"TIME_DELTA=4":5,"TIME_DELTA=2":6,"BAR_END":7,"BAR_START":8,"TRACK_START":9,"TRACK_END":10,"NOTE_OFF=67":11,"NOTE_ON=67":12,"NOTE_ON=65":13,"NOTE_OFF=65":14,"NOTE_ON=62":15,"NOTE_OFF=62":16,"NOTE_ON=60":17,"NOTE_OFF=60":18,"TIME_DELTA=8":19,"NOTE_OFF=64":20,"NOTE_ON=64":21,"NOTE_ON=63":22,"NOTE_OFF=63":23,"NOTE_OFF=66":24,"NOTE_ON=66":25,"NOTE_OFF=69":26,"NOTE_ON=69":27,"NOTE_OFF=68":28,"NOTE_ON=68":29,"NOTE_ON=61":30,"NOTE_OFF=61":31,"NOTE_OFF=58":32,"NOTE_ON=58":33,"NOTE_ON=55":34,"NOTE_OFF=55":35,"NOTE_OFF=70":36,"NOTE_ON=70":37,"NOTE_OFF=59":38,"NOTE_ON=59":39,"NOTE_OFF=57":40,"NOTE_ON=57":41,"NOTE_ON=56":42,"NOTE_OFF=56":43,"NOTE_OFF=53":44,"NOTE_ON=53":45,"NOTE_OFF=71":46,"NOTE_ON=71":47,"NOTE_ON=72":48,"NOTE_OFF=72":49,"NOTE_ON=54":50,"NOTE_OFF=54":51,"NOTE_ON=52":52,"NOTE_OFF=52":53,"NOTE_OFF=50":54,"NOTE_ON=50":55,"NOTE_ON=51":56,"NOTE_OFF=51":57,"NOTE_ON=48":58,"NOTE_OFF=48":59,"NOTE_OFF=73":60,"NOTE_ON=73":61,"NOTE_OFF=74":62,"NOTE_ON=74":63,"NOTE_OFF=49":64,"NOTE_ON=49":65,"NOTE_ON=47":66,"NOTE_OFF=47":67,"NOTE_ON=75":68,"NOTE_OFF=75":69,"NOTE_OFF=46":70,"NOTE_ON=46":71,"INST=24":72,"PIECE_END":73,"INST=48":74,"GENRE=JSFAKES":75,"INST=0":76,"PIECE_START":77,"STYLE=JSFAKES":78,"INST=32":79,"TIME_DELTA=1":80,"NOTE_OFF=45":81,"NOTE_ON=45":82,"NOTE_ON=76":83,"NOTE_OFF=76":84,"TIME_DELTA=6":85,"NOTE_OFF=44":86,"NOTE_ON=44":87,"TIME_DELTA=12":88,"NOTE_ON=43":89,"NOTE_OFF=43":90,"NOTE_OFF=77":91,"NOTE_ON=77":92,"NOTE_OFF=78":93,"NOTE_ON=78":94,"NOTE_OFF=41":95,"NOTE_ON=41":96,"NOTE_ON=42":97,"NOTE_OFF=42":98,"NOTE_ON=79":99,"NOTE_OFF=79":100,"NOTE_OFF=40":101,"NOTE_ON=40":102,"TIME_DELTA=16":103,"NOTE_ON=39":104,"NOTE_OFF=39":105,"NOTE_OFF=80":106,"NOTE_ON=80":107,"TIME_DELTA=3":108,"NOTE_OFF=38":109,"NOTE_ON=38":110,"NOTE_OFF=37":111,"NOTE_ON=37":112,"NOTE_ON=36":113,"NOTE_OFF=36":114,"NOTE_OFF=81":115,"NOTE_ON=81":116,"TIME_DELTA=7":117,"TIME_DELTA=10":118},"unk_token":"[UNK]"}} \ No newline at end of file diff --git a/tokenizer_config.json b/tokenizer_config.json new file mode 100644 index 0000000..943dd03 --- /dev/null +++ b/tokenizer_config.json @@ -0,0 +1 @@ +{"tokenizer_class": "PreTrainedTokenizerFast"} \ No newline at end of file