Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature: Download generated files to local session file storage #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 83 additions & 62 deletions Mubert_Text_to_Music.ipynb
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"collapsed_sections": [],
"toc_visible": true,
"authorship_tag": "ABX9TyNRcUVF3ZzTw+oK4ortpcH+",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
"colab_type": "text",
"id": "view-in-github"
},
"source": [
"<a href=\"https://colab.research.google.com/github/ferluht/Mubert-Text-to-Music/blob/main/Mubert_Text_to_Music.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gHJPhnu7Lg2v"
},
"source": [
"# **Mubert Text to Music ✍ ➡ 🎹🎵🔊**\n",
"\n",
"A simple notebook demonstrating prompt-based music generation via [Mubert](https://mubert.com) [API](https://mubert2.docs.apiary.io/)"
],
"metadata": {
"id": "gHJPhnu7Lg2v"
}
]
},
{
"cell_type": "code",
Expand Down Expand Up @@ -67,12 +49,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "yW-3aTNYvKM_"
},
"outputs": [],
"source": [
"#@title **Define Mubert methods and pre-compute things**\n",
"\n",
"import numpy as np\n",
"from sentence_transformers import SentenceTransformer\n",
"minilm = SentenceTransformer('all-MiniLM-L6-v2')\n",
"import urllib.request\n",
"\n",
"mubert_tags_string = 'tribal,action,kids,neo-classic,run 130,pumped,jazz / funk,ethnic,dubtechno,reggae,acid jazz,liquidfunk,funk,witch house,tech house,underground,artists,mystical,disco,sensorium,r&b,agender,psychedelic trance / psytrance,peaceful,run 140,piano,run 160,setting,meditation,christmas,ambient,horror,cinematic,electro house,idm,bass,minimal,underscore,drums,glitchy,beautiful,technology,tribal house,country pop,jazz & funk,documentary,space,classical,valentines,chillstep,experimental,trap,new jack swing,drama,post-rock,tense,corporate,neutral,happy,analog,funky,spiritual,sberzvuk special,chill hop,dramatic,catchy,holidays,fitness 90,optimistic,orchestra,acid techno,energizing,romantic,minimal house,breaks,hyper pop,warm up,dreamy,dark,urban,microfunk,dub,nu disco,vogue,keys,hardcore,aggressive,indie,electro funk,beauty,relaxing,trance,pop,hiphop,soft,acoustic,chillrave / ethno-house,deep techno,angry,dance,fun,dubstep,tropical,latin pop,heroic,world music,inspirational,uplifting,atmosphere,art,epic,advertising,chillout,scary,spooky,slow ballad,saxophone,summer,erotic,jazzy,energy 100,kara mar,xmas,atmospheric,indie pop,hip-hop,yoga,reggaeton,lounge,travel,running,folk,chillrave & ethno-house,detective,darkambient,chill,fantasy,minimal techno,special,night,tropical house,downtempo,lullaby,meditative,upbeat,glitch hop,fitness,neurofunk,sexual,indie rock,future pop,jazz,cyberpunk,melancholic,happy hardcore,family / kids,synths,electric guitar,comedy,psychedelic trance & psytrance,edm,psychedelic rock,calm,zen,bells,podcast,melodic house,ethnic percussion,nature,heavy,bassline,indie dance,techno,drumnbass,synth pop,vaporwave,sad,8-bit,chillgressive,deep,orchestral,futuristic,hardtechno,nostalgic,big room,sci-fi,tutorial,joyful,pads,minimal 170,drill,ethnic 108,amusing,sleepy ambient,psychill,italo disco,lofi,house,acoustic guitar,bassline house,rock,k-pop,synthwave,deep house,electronica,gabber,nightlife,sport & fitness,road trip,celebration,electro,disco house,electronic'\n",
"mubert_tags = np.array(mubert_tags_string.split(','))\n",
Expand All @@ -82,7 +71,7 @@
"import httpx\n",
"import json\n",
"\n",
"def get_track_by_tags(tags, pat, duration, maxit=20, autoplay=False, loop=False):\n",
"def get_track_by_tags(tags, pat, duration, prompt, maxit=20, autoplay=False, loop=False):\n",
" if loop:\n",
" mode = \"loop\"\n",
" else:\n",
Expand All @@ -107,6 +96,9 @@
" r = httpx.get(trackurl)\n",
" if r.status_code == 200:\n",
" display(Audio(trackurl, autoplay=autoplay))\n",
" urllib.request.urlretrieve(trackurl, str(prompt)+\".mp3\")\n",
" print('All generated files will save in this sessions \"Files\" on the left, please download if you wish to keep them. \\n (May take 30sec-2min to appear)')\n",
" print('\\n')\n",
" break\n",
" time.sleep(1)\n",
" print('.', end='')\n",
Expand All @@ -131,16 +123,16 @@
" print(f\"Prompt: {prompts[i]}\\nTags: {', '.join(top_tags)}\\nScores: {top_prob}\\n\\n\\n\")\n",
" ret.append((prompts[i], list(top_tags)))\n",
" return ret"
],
"metadata": {
"cellView": "form",
"id": "yW-3aTNYvKM_"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "a4ACdvWLRJ5U"
},
"outputs": [],
"source": [
"#@markdown **Get personal access token in Mubert and define API methods**\n",
"email = \"your e-mail here\" #@param {type:\"string\"}\n",
Expand All @@ -160,16 +152,16 @@
"assert rdata['status'] == 1, \"probably incorrect e-mail\"\n",
"pat = rdata['data']['pat']\n",
"print(f'Got token: {pat}')"
],
"metadata": {
"cellView": "form",
"id": "a4ACdvWLRJ5U"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "hTf7sZcfbI0K"
},
"outputs": [],
"source": [
"#@title **Generate some music 🎵**\n",
"\n",
Expand All @@ -180,31 +172,32 @@
"def generate_track_by_prompt(prompt, duration, loop=False):\n",
" _, tags = get_tags_for_prompts([prompt,])[0]\n",
" try:\n",
" get_track_by_tags(tags, pat, duration, autoplay=True, loop=loop)\n",
" get_track_by_tags(tags, pat, duration, prompt, autoplay=True, loop=loop)\n",
" except Exception as e:\n",
" print(str(e))\n",
" print('Prompt:', prompt)\n",
" print(tags)\n",
" print('\\n')\n",
"\n",
"generate_track_by_prompt(prompt, duration, loop)\n"
],
"metadata": {
"cellView": "form",
"id": "hTf7sZcfbI0K"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
"source": [
"### **Batch generation 🎶**"
],
"metadata": {
"id": "wSKTfub-bitp"
}
},
"source": [
"### **Batch generation 🎶**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "BzrhcwIHXlg0"
},
"outputs": [],
"source": [
"duration = 60\n",
"\n",
Expand All @@ -229,21 +222,49 @@
" except Exception as e:\n",
" print(str(e))\n",
" print('\\n')"
],
"metadata": {
"id": "BzrhcwIHXlg0"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"source": [],
"execution_count": null,
"metadata": {
"id": "JieLk6kjZFai"
},
"execution_count": null,
"outputs": []
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"authorship_tag": "ABX9TyNRcUVF3ZzTw+oK4ortpcH+",
"collapsed_sections": [],
"include_colab_link": true,
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3.9.7 64-bit",
"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"
},
"vscode": {
"interpreter": {
"hash": "81794d4967e6c3204c66dcd87b604927b115b27c00565d3d43f05ba2f3a2cb0d"
}
}
]
}
},
"nbformat": 4,
"nbformat_minor": 0
}