File size: 10,120 Bytes
2f9e7b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# MuseBot \u2014 MusicGen + RVC Automatic Colab\n",
        "\n",
        "This Colab notebook is prepared to run **MusicGen (instrument)** + **RVC (voice conversion)** pipeline with automatic style selection.\n",
        "\n",
        "Instructions:\n",
        "1. Open this notebook in Google Colab (use the provided download link).  \n",
        "2. Select **Runtime > Change runtime type > GPU** and Runtime > Run all.  \n",
        "3. Upload a 10\u201320s clean WAV voice sample when prompted (optional).  \n",
        "4. Wait for the notebook to finish; download `final_<style>.wav` files from the Files panel.\n",
        "\n",
        "Notes:\n",
        "- This notebook uses `facebook/musicgen-small` by default to reduce GPU memory usage.  \n",
        "- RVC model weights are downloaded automatically (placeholders included).  \n",
        "- If you want higher-quality results or a custom RVC checkpoint, upload the `.pth` file when requested.\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Cell 1: Environment setup\n",
        "!nvidia-smi || true\n",
        "!pip install --upgrade pip -q\n",
        "# Compatible versions to avoid safetensors/transformers conflicts\n",
        "!pip install 'transformers==4.39.3' 'safetensors==0.4.2' torch --quiet\n",
        "!pip install librosa soundfile pyloudnorm audiocraft --quiet\n",
        "!git clone https://github.com/RVC-Project/Retrieval-based-Voice-Conversion.git rvc || true\n",
        "!pip install -r rvc/requirements.txt --quiet || true\n",
        "print('Environment setup complete')\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Cell 2: Upload voice sample (optional but recommended)\n",
        "from google.colab import files\n",
        "print('\\n--- Upload 10-20s clean voice sample (WAV recommended) ---')\n",
        "uploaded = files.upload()\n",
        "voice_sample_path = None\n",
        "for fn in uploaded.keys():\n",
        "    voice_sample_path = fn\n",
        "print('Uploaded sample:', voice_sample_path)\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Cell 3: Automatic lyrics generation (English) \u2014 GPT-2 fallback (no OpenAI key required)\n",
        "import torch\n",
        "from transformers import pipeline\n",
        "device = 0 if torch.cuda.is_available() else -1\n",
        "text_gen = pipeline('text-generation', model='gpt2', device=device)\n",
        "prompt_templates = {\n",
        "    'pop': 'Write a catchy radio-friendly English pop chorus and short verse about joy and freedom:\\n',\n",
        "    'rock': 'Write an energetic rock-style English chorus and short verse about hope and defiance:\\n',\n",
        "    'dance': 'Write an upbeat dance/EDM English chorus and short verse about nightlife and celebration:\\n',\n",
        "    'country': 'Write a warm American country-style English chorus and short verse about small-town life and love:\\n',\n",
        "    'gospel': 'Write a heartfelt gospel/Christian English chorus and short verse praising faith and hope:\\n'\n",
        "}\n",
        "lyrics_by_style = {}\n",
        "for style, prompt in prompt_templates.items():\n",
        "    out = text_gen(prompt, max_length=120, do_sample=True, temperature=0.9)[0]['generated_text']\n",
        "    lyrics_by_style[style] = out[len(prompt):].strip()\n",
        "print('\\n--- Generated lyrics (samples) ---')\n",
        "for k,v in lyrics_by_style.items():\n",
        "    print(f'[{k.upper()}]\\n{v}\\n')\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Cell 4: MusicGen instrumentation generation\n",
        "from audiocraft.models import MusicGen\n",
        "MODEL_SIZE = 'small'  # change to 'medium' or 'large' if you have a powerful GPU\n",
        "model_name = 'facebook/musicgen-small' if MODEL_SIZE=='small' else ('facebook/musicgen-medium' if MODEL_SIZE=='medium' else 'facebook/musicgen-large')\n",
        "print('Loading MusicGen model:', model_name)\n",
        "model = MusicGen.get_pretrained(model_name)\n",
        "model.set_generation_params(duration=30)\n",
        "style_prompts = {\n",
        "    'pop':'energetic pop with bright guitar and punchy drums, 120 bpm',\n",
        "    'rock':'driving rock with electric guitar riffs and strong drums, 120 bpm',\n",
        "    'dance':'upbeat dance with strong synth leads, 128 bpm',\n",
        "    'country':'acoustic country with warm guitar and steady drums, 100 bpm',\n",
        "    'gospel':'uplifting gospel with organ and choir, 90 bpm'\n",
        "}\n",
        "generated_instruments = {}\n",
        "for style, sprompt in style_prompts.items():\n",
        "    print('Generating:', style)\n",
        "    wavs = model.generate([sprompt])\n",
        "    outname = f'musicgen_{style}.wav'\n",
        "    model.save_audio(outname, wavs[0], 32000)\n",
        "    generated_instruments[style] = outname\n",
        "print('Instrument generation done. Files:', generated_instruments)\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Cell 5: Download RVC model weights (automatic selection by style)\n",
        "import os\n",
        "os.makedirs('rvc/weights', exist_ok=True)\n",
        "def download_if_missing(url, path):\n",
        "    if os.path.exists(path):\n",
        "        print('Already exists:', path)\n",
        "        return\n",
        "    print('Downloading', url)\n",
        "    !wget -q -O {path} {url}\n",
        "\n",
        "rvc_models = {\n",
        "    'female': 'https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/weights/Shiroko_Singing.pth',\n",
        "    'male': 'https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/weights/Jun_Male_Singing.pth'\n",
        "}\n",
        "style_to_vocal = {'pop':'female','dance':'female','gospel':'female','rock':'male','country':'male'}\n",
        "for k,v in rvc_models.items():\n",
        "    download_if_missing(v, f'rvc/weights/{k}.pth')\n",
        "print('RVC model files ready in rvc/weights (if download links are available)')\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Cell 6: RVC inference (placeholder and guidance)\n",
        "print('RVC inference varies across forks. This cell provides guidance and tries a simple demo if inference script exists.')\n",
        "import os\n",
        "if os.path.exists('rvc/weights/female.pth') or os.path.exists('rvc/weights/male.pth'):\n",
        "    print('RVC checkpoints are present. You should follow the rvc repo README to run the exact inference command for that fork.')\n",
        "else:\n",
        "    print('No RVC checkpoint found. You can upload one via Files panel or re-run the weights download cell.')\n",
        "\n",
        "print('\\nNote: Different RVC forks expect different CLI parameters. If you want, share which RVC fork you prefer and I will add the exact inference command.')\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Cell 7: Simple placeholder vocal creation (if no RVC checkpoint) and mixing\n",
        "import soundfile as sf\n",
        "import numpy as np\n",
        "from librosa.effects import pitch_shift\n",
        "\n",
        "for style, inst_file in generated_instruments.items():\n",
        "    if not os.path.exists(inst_file):\n",
        "        continue\n",
        "    print('Processing style:', style)\n",
        "    y_inst, sr = sf.read(inst_file)\n",
        "    # create a naive vocal placeholder by pitch-shifting the instrument\n",
        "    try:\n",
        "        y_voc = pitch_shift(y_inst.astype(float), sr, n_steps=3.0)\n",
        "    except Exception:\n",
        "        # mono fallback\n",
        "        y_inst_mono = np.mean(y_inst, axis=1) if y_inst.ndim>1 else y_inst\n",
        "        y_voc = pitch_shift(y_inst_mono.astype(float), sr, n_steps=3.0)\n",
        "    min_len = min(len(y_inst), len(y_voc))\n",
        "    mix = 0.7*y_inst[:min_len] + 0.9*y_voc[:min_len]\n",
        "    mix = mix / (np.max(np.abs(mix))+1e-6) * 0.95\n",
        "    outname = f'final_{style}.wav'\n",
        "    sf.write(outname, mix, sr)\n",
        "    print('Wrote', outname)\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Cell 8: List and download final files\n",
        "!ls -1 final_*.wav || true\n",
        "from google.colab import files as gcfiles\n",
        "for fn in ['final_pop.wav','final_rock.wav','final_dance.wav','final_country.wav','final_gospel.wav']:\n",
        "    if os.path.exists(fn):\n",
        "        print('Ready to download:', fn)\n",
        "        # Uncomment next line to auto-download (commented to avoid auto popups)\n",
        "        # gcfiles.download(fn)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Next steps / Tips\n",
        "- If you want true RVC-generated vocals (not placeholders), provide a compatible RVC `.pth` checkpoint or tell me which RVC fork you prefer and I'll add the exact inference cell.  \n",
        "- After downloading `final_*.wav`, bring files into your local MuseBot for the mixing/mastering profile transfer.  \n",
        "- When you finish, revoke any temporary HF token you created for security.\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.10"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}