{"id":16434558,"url":"https://github.com/halfrost/bachgpt","last_synced_at":"2025-07-22T19:03:52.726Z","repository":{"id":207536652,"uuid":"719476154","full_name":"halfrost/BachGPT","owner":"halfrost","description":null,"archived":false,"fork":false,"pushed_at":"2024-06-25T13:36:45.000Z","size":77687,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-08T04:05:30.441Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/halfrost.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-16T08:49:40.000Z","updated_at":"2025-02-03T07:43:21.000Z","dependencies_parsed_at":"2024-06-25T15:25:57.123Z","dependency_job_id":null,"html_url":"https://github.com/halfrost/BachGPT","commit_stats":null,"previous_names":["halfrost/bachgpt"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/halfrost/BachGPT","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halfrost%2FBachGPT","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halfrost%2FBachGPT/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halfrost%2FBachGPT/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halfrost%2FBachGPT/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/halfrost","download_url":"https://codeload.github.com/halfrost/BachGPT/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halfrost%2FBachGPT/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266553993,"owners_count":23947249,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T08:49:32.455Z","updated_at":"2025-07-22T19:03:52.709Z","avatar_url":"https://github.com/halfrost.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BachGPT\n\n![DALL·E 2023-12-03 23 39 02 - An artistic representation of Johann Sebastian Bach as 'BachGPT', an AI musician  He is wearing stylish sunglasses, symbolizing the blend of classical](https://github.com/halfrost/BachGPT/assets/10825609/a2de2c57-9080-4550-a617-57f1581aead3)\n\n\n\n# Background\n\nThe BachGPT aims to explore the intersection of artificial intelligence and classical music composition. This project utilizes LSTM (Long Short-Term Memory) + attention neural networks, which are particularly suited for sequential data like music. The focus is on generating compositions in the style of Johann Sebastian Bach, a renowned composer known for his complex harmonies and counterpoint techniques.\n\nThis endeavor follows in the footsteps of previous works in AI-driven music generation, aiming to capture the essence of Bach's style while exploring the creative capacities of machine learning models in arts.\n\n# Dataset \n\nFor this project, the dataset consists of MIDI files of various compositions by Bach. These files were processed to extract patterns and sequences characteristic of Bach's musical style. The pipeline involves converting MIDI files into a format suitable for training the LSTM model, ensuring the network learns to mimic the intricacies and nuances of Bach's compositions.\n\nI have 4 sets of dataset\n1. Raw MIDI music\n2. Contrapunctus XIV\n3. [JSB Chorales Dataset](https://github.com/czhuang/JSB-Chorales-dataset) - A dataset with 4 voice midi files from bach\n4. [JS Fake Chorales](https://github.com/omarperacha/js-fakes) - A similar dataset to JSB Chorales, but fake, generated by AI and annotated by humans\n\nI have used all 4 of these data sources, but when I finally trained the model with dataset 2. Finally, 3 pieces of music were output. The first and third pieces of music came from dataset 2, and the second piece of music came from dataset 3.\n\n\n# Train methods\n\nLSTM with Attention\nOur approach employs a Long Short-Term Memory (LSTM) network enhanced with an attention mechanism. LSTM networks are adept at handling sequences with long-range dependencies, making them ideal for music generation tasks.\n\n- LSTM Overview: LSTMs are capable of learning order dependence in sequence prediction problems.\n- Attention Mechanism: This feature allows the network to focus on specific parts of the input sequence, enhancing the quality and coherence of the generated music.\n\n\n# Evaluations\n\nThe model's performance was evaluated based on its ability to authentically replicate Bach's musical style. We conducted several experiments to optimize the model's architecture and parameters.\n\n- Experiments and Results: The results of these experiments were quantified and are presented in tables and figures, demonstrating the model's accuracy in generating music that aligns with Bach's style.\n- Comparison with Baselines: The model's performance was also compared against established baselines to ascertain its efficacy and uniqueness.\n\n![](https://github.com/halfrost/BachGPT/blob/main/evaluations/my_plot1.png)\n![](https://github.com/halfrost/BachGPT/blob/main/evaluations/my_plot2.png)\n![](https://github.com/halfrost/BachGPT/blob/main/evaluations/my_plot3.png)\n![](https://github.com/halfrost/BachGPT/blob/main/evaluations/my_plot4.png)\n![](https://github.com/halfrost/BachGPT/blob/main/evaluations/my_plot5.png)\n![](https://github.com/halfrost/BachGPT/blob/main/evaluations/my_plot6.png)\n![](https://github.com/halfrost/BachGPT/blob/main/evaluations/my_plot7.png)\n![](https://github.com/halfrost/BachGPT/blob/main/evaluations/my_plot8.png)\n![](https://github.com/halfrost/BachGPT/blob/main/evaluations/my_plot9.png)\n![](https://github.com/halfrost/BachGPT/blob/main/evaluations/my_plot10.png)\n\n\n# How to run\n\nI highly recommend running the notebook in [Colaboratory](https://colab.research.google.com/) so you can view the results and change parameters in real-time. The jupyter notebook code is [here](https://github.com/halfrost/BachGPT/blob/main/bachGPT.ipynb). However, you can run Python code on your local machine.\n\n## 1. Train model\n\n```python\npython3 bachGPT.py \n```\n\n## 2. Use the model to generate predicted output\n\n```python\npython3 bach_predict.py ./models/{your_model_name}.pth \n```\n\n## 3. Create midi music file\n\n```python\npython3 create_midi.py {your_model_name}.pth/output.txt\n```\n\n## 4. Generate mp3 file\n\n```python\ntimidity output/output.midi -Ow -o - | ffmpeg -i - -acodec libmp3lame -ab 320k output/output.mp3\ntimidity output/complete.midi -Ow -o - | ffmpeg -i - -acodec libmp3lame -ab 320k output/complete.mp3\n```\n\nThe final generated Bach-style music is in the output folder.\n\n# Generated music\n\n\n\n\n![image](https://github.com/halfrost/BachGPT/assets/10825609/c4d98cfc-504f-446a-a92f-55487ec98add)\n\n\n\nhttps://github.com/halfrost/BachGPT/assets/10825609/ba1f8a7b-5317-4cce-898a-98ca69de66de\n\n\n\n![image](https://github.com/halfrost/BachGPT/assets/10825609/665c63b4-e59b-41fe-8add-1f6e27bb92e1)\n\n\n\n\nhttps://github.com/halfrost/BachGPT/assets/10825609/cb740315-36c5-4722-bac3-a619888c18b9\n\n\n\n\n![image](https://github.com/halfrost/BachGPT/assets/10825609/6870cf4c-4cf3-4ef8-a4b6-cf684b1db998)\n\n\n\n\nhttps://github.com/halfrost/BachGPT/assets/10825609/069e8bf7-a2c9-4624-820a-deccf4ef2aac\n\n\n\n\n# Reference\n1. [Generating Original Classical Music with an LSTM Neural Network and Attention](https://medium.com/@alexissa122/generating-original-classical-music-with-an-lstm-neural-network-and-attention-abf03f9ddcb4)\n2. [Generating Long-Term Structure in Songs and Stories](https://magenta.tensorflow.org/2016/07/15/lookback-rnn-attention-rnn)\n3. [Neural Machine Translation by Jointly Learning to Align and Translate](https://arxiv.org/abs/1409.0473)\n4. [Baroque Music Generator](https://github.com/Pudkip/Bach-Bot)\n5. [Generating Music using LSTM](https://www.researchgate.net/publication/351502178_Generating_Music_using_LSTM)\n6. [Music Generation using Deep Learning](https://medium.com/@sabadejuyee21/music-generation-using-deep-learning-7d3dbb2254af)\n7. [How to Generate Music using a LSTM Neural Network in Keras](https://towardsdatascience.com/how-to-generate-music-using-a-lstm-neural-network-in-keras-68786834d4c5)\n8. [Classical-Piano-Composer](https://github.com/Skuldur/Classical-Piano-Composer)\n9. [Classic_Music_Generation](https://github.com/thebeyonder001/Classic_Music_Generation)\n10. [conversion-tool](https://www.conversion-tool.com/audiotomidi/)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhalfrost%2Fbachgpt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhalfrost%2Fbachgpt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhalfrost%2Fbachgpt/lists"}