|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Deploying AudioCraft to your own server" |
| 4 | +date: 2023-08-05 06:20:32 -0700 |
| 5 | +categories: ai |
| 6 | +post_photo: assets/posts/deploying-audiocraft/post_photo.jpg |
| 7 | +permalink: /blog/:slug |
| 8 | +--- |
| 9 | + |
| 10 | +A few days ago, [AudioCraft](https://ai.meta.com/blog/audiocraft-musicgen-audiogen-encodec-generative-ai-audio/) was released to the public, this post assumes that you know what it is but you may not have played with it yet. |
| 11 | + |
| 12 | +AudioCraft idea sounds pretty cool, when I shared the news with my family, they were keen to give it a try, unfortunately, there isn't any live version to play with. |
| 13 | + |
| 14 | +Given that I just delivered a project, what could be a better prize than playing with new tech? I decided to launch AudioCraft in a server so that my family can play with it. |
| 15 | + |
| 16 | +This post explains how I deployed AudioCraft to Ubuntu 22.04, you can try the live version at [audiocraft.wiringbits.net](https://audiocraft.wiringbits.net), I don't know how much load the server will handle but it should be enough to let a few visitors play. |
| 17 | + |
| 18 | +**NOTE** The live server will be public for a few days only. |
| 19 | + |
| 20 | +> Ad: Do you need a reliable dev to help with your project or infrastructure? I'll be talking to potential customers next week to choose my next project. |
| 21 | +
|
| 22 | + |
| 23 | +## Summary |
| 24 | + |
| 25 | +A powerful server is required to run AudioCraft, from my tests, the app seems to require at least 12G of memory + a lot of CPU power, it consumes most of my computer resources, specially when processing a request. |
| 26 | + |
| 27 | +I'll use an old-school approach that involves nginx as the reverse proxy which handles TLS + a systemd service to make sure that AudioCraft runs when the server starts, as well restarting it when it crashes. |
| 28 | + |
| 29 | +[Hetzner](https://www.hetzner.com/) is the provider where I'll host my dedicated server for a reasonable price, given the high CPU requirement, I doubt that a shared cpu would behave nicely but I haven't tried, I selected the `CCX32` version which has 8 vCPUs + 32G memory + 240G disk, costing `77 €`/month. |
| 30 | + |
| 31 | +## Details |
| 32 | + |
| 33 | +The following details include the exact steps I took to launch AudioCraft in my own Ubuntu 22.04 server until I got to the current version that's live at [audiocraft.wiringbits.net](https://audiocraft.wiringbits.net) |
| 34 | + |
| 35 | + |
| 36 | +### 1. Create a server |
| 37 | + |
| 38 | +I have selected the `CCX32` version from Hetzner (a dedicated server) running Ubuntu 22.04: |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +I have included an ipv4 + a firewall adequate for a webapp (allowing traffic only on ports 22/80/443): |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +There is no need to enable backups or increase the disk size, from my tests, we are limited by the CPU. |
| 47 | + |
| 48 | +**NOTE** There is a high chance that using a server with a dedicated GPU can improve the performance considerably but I couldn't find any on Hetzner which can be created immediately. |
| 49 | + |
| 50 | +You should be able to log into the server with ssh `ssh root@[ipv4]` (replace `[ipv4]` with your server ip). |
| 51 | + |
| 52 | + |
| 53 | +### 2. Initial setup |
| 54 | + |
| 55 | +This brief section includes steps I follow in all my servers. |
| 56 | + |
| 57 | +Apply standard updates and useful packages: |
| 58 | + |
| 59 | +```shell |
| 60 | +apt update |
| 61 | +apt upgrade |
| 62 | +apt install ntp fail2ban |
| 63 | +``` |
| 64 | + |
| 65 | +**NOTE** A system restart may be suggested. |
| 66 | + |
| 67 | +Install AudioCraft dependencies (see [action.yml](https://github.com/facebookresearch/audiocraft/blob/main/.github/actions/audiocraft_build/action.yml)): |
| 68 | + |
| 69 | +```shell |
| 70 | +apt install libsndfile1-dev ffmpeg python3-pip python3.10-venv |
| 71 | +``` |
| 72 | + |
| 73 | +Create a system user, an unprivileged user who will run AudioCraft: |
| 74 | + |
| 75 | +```shell |
| 76 | +useradd audiocraft --create-home --system |
| 77 | +``` |
| 78 | + |
| 79 | +**NOTE**: If you plan to leave this server running for a while, it is a good idea to do a few extra steps like disabling root login and adding another user to handle that, as well as disabling login by password, changing the default ssh port can be a good idea too. |
| 80 | + |
| 81 | + |
| 82 | +### 3. First AudioCraft execution |
| 83 | + |
| 84 | +Let's start by switching to the `audiocraft` user, the one who will execute AudioCraft: |
| 85 | + |
| 86 | +```shell |
| 87 | +sudo su -s /bin/bash - audiocraft |
| 88 | +``` |
| 89 | + |
| 90 | +Clone the AudioCraft repository and move to such a directory: |
| 91 | + |
| 92 | +```shell |
| 93 | +git clone https://github.com/facebookresearch/audiocraft.git |
| 94 | +cd audiocraft/ |
| 95 | +``` |
| 96 | + |
| 97 | +Create a virtual environment for executing AudioCraft and switch to it: |
| 98 | + |
| 99 | +```shell |
| 100 | +python3 -m venv .venv |
| 101 | +. .venv/bin/activate |
| 102 | +``` |
| 103 | + |
| 104 | +Install AudioCraft Python dependencies: |
| 105 | + |
| 106 | +```shell |
| 107 | +python -m pip install --upgrade pip |
| 108 | +pip install -r requirements.txt |
| 109 | + |
| 110 | +``` |
| 111 | + |
| 112 | +Install AudioCraft packages: |
| 113 | + |
| 114 | +```shell |
| 115 | +pip install -e '.[dev]' |
| 116 | +``` |
| 117 | + |
| 118 | +Finally, execute AudioCraft: |
| 119 | + |
| 120 | +```shell |
| 121 | +python demos/musicgen_app.py |
| 122 | +``` |
| 123 | + |
| 124 | +AudioCraft should be running now, use `ctrl+C` to stop it, while this has a few drawbacks: |
| 125 | + |
| 126 | +1. AudioCraft execution will stop as soon as your ssh session is terminated. |
| 127 | +2. AudioCraft won't run if the server is restarted or if AudioCraft crashes. |
| 128 | + |
| 129 | +Still, it is good to do this for verifying that the execution will work, you can test this in two ways: |
| 130 | + |
| 131 | +1. Launch a second ssh-session and run `curl localhost:7860`, the result must show some kind of html. |
| 132 | +2. Expose AudioCraft to your computer by opening an ssh tunnel like `ssh -nNT -L 7860:localhost:7860 root@[ipv4]`, then, load `localhost:7860` in your favorite browser. |
| 133 | + |
| 134 | +Once you verify that AudioCraft worked, stop the service (`ctrl+C`) and exit (run `exit`) the session, you should be back to the root shell. |
| 135 | + |
| 136 | + |
| 137 | +### 4. Add systemd service |
| 138 | + |
| 139 | +This is the time to solve the drawbacks from step 3, let's create a systemd service by filling `/etc/systemd/system/audiocraft.service` with this content (use your preferred editor): |
| 140 | + |
| 141 | +```shell |
| 142 | +[Unit] |
| 143 | +Description=audiocraft musicgen_app |
| 144 | + |
| 145 | +[Service] |
| 146 | +Type=simple |
| 147 | +WorkingDirectory=/home/audiocraft/audiocraft |
| 148 | +StandardError=journal |
| 149 | +StandardOutput=journal |
| 150 | +StandardInput=null |
| 151 | +LimitNOFILE=65535 |
| 152 | +User=audiocraft |
| 153 | +ExecStart=/bin/sh -c ". .venv/bin/activate && python demos/musicgen_app.py --username=iMustConsiderHiringAlexis --password=toMaintainMyProjectOrCreateAnMVP" |
| 154 | +Restart=on-failure |
| 155 | + |
| 156 | +[Install] |
| 157 | +WantedBy=multi-user.target |
| 158 | +``` |
| 159 | + |
| 160 | +**NOTE**: Edit the values from the `--username`/`--password` settings, this protects your instance from unauthorized usage, removing those will leave the service public like mine (for now). |
| 161 | + |
| 162 | +Tell systemd to load the new service `systemctl daemon-reload` and start the service with `service audiocraft start`. |
| 163 | + |
| 164 | +These commands will be helpful: |
| 165 | + |
| 166 | +1. Check the service status: `service audiocraft status` |
| 167 | +2. Stop the service: `service audiocraft stop` |
| 168 | +3. Restart the service: `service audiocraft stop` |
| 169 | +4. Check the logs `journalctl -u audiocraft -f` |
| 170 | + |
| 171 | +Verify that AudioCraft is working (see step 3 for alternatives), then, run `systemctl enable audiocraft` to start AudioCraft when the server starts. |
| 172 | + |
| 173 | +**NOTE**: Remember we set a firewall preventing AudioCraft from being accessed from anywhere outside of its server. |
| 174 | + |
| 175 | + |
| 176 | +## 5. Expose AudioCraft to the internet |
| 177 | + |
| 178 | +In my case, I'll create a subdomain to access AudioCraft, you will need to update your DNS to add an `Alias`/`A` record, like this: |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | +A way to verify that the record has been propagated is by running `dig A audiocraft.wiringbits.net` (use your own domain), it must print the ip address from your server, like: |
| 183 | + |
| 184 | +``` |
| 185 | +... |
| 186 | +;; ANSWER SECTION: |
| 187 | +audiocraft.wiringbits.net. 1561 IN A 65.21.255.90 |
| 188 | +... |
| 189 | +``` |
| 190 | + |
| 191 | +Once this is done, let's add the nginx server settings, I created `/etc/nginx/sites-enabled/audiocraft.wiringbits.net` with this content: |
| 192 | + |
| 193 | +``` |
| 194 | +server { |
| 195 | + listen 80; |
| 196 | +
|
| 197 | + server_name audiocraft.wiringbits.net; |
| 198 | +
|
| 199 | + # websocket |
| 200 | + location /queue/join { |
| 201 | + proxy_pass http://localhost:7860; |
| 202 | + proxy_http_version 1.1; |
| 203 | + proxy_set_header Upgrade $http_upgrade; |
| 204 | + proxy_set_header Connection "upgrade"; |
| 205 | + proxy_read_timeout 86400; |
| 206 | + } |
| 207 | +
|
| 208 | + # normal requests |
| 209 | + location / { |
| 210 | + proxy_pass http://127.0.0.1:7860; |
| 211 | + } |
| 212 | +} |
| 213 | +``` |
| 214 | + |
| 215 | +**NOTE**: Update the `server_name` to match your domain. |
| 216 | + |
| 217 | +Then, `service nginx reload` will pick the new settings, this should allow you to use AudioCraft from your own domain, for example, visiting `http://audiocraft.wiringbits.net` would work (https version next). |
| 218 | + |
| 219 | + |
| 220 | +### 6. Get TLS certificate |
| 221 | + |
| 222 | +We'll use [Certbot](ttps://certbot.eff.org/instructions?ws=nginx&os=ubuntufocal) for this task, which generates TLS certificates from LetsEncrypt. |
| 223 | + |
| 224 | +The steps are simple: |
| 225 | + |
| 226 | +```shell |
| 227 | +apt install snapd |
| 228 | +snap install --classic certbot |
| 229 | +certbot --nginx |
| 230 | +``` |
| 231 | + |
| 232 | +Just follow the instructions and your TLS certificate must be deployed. |
| 233 | + |
| 234 | + |
| 235 | +## Some screenshots |
| 236 | + |
| 237 | +The UI from MusicGen, the AudioCraft demo app deployed by this guide: |
| 238 | + |
| 239 | + |
| 240 | + |
| 241 | +CPU/Memory usage from `htop` when AudioCraft is processing a request: |
| 242 | + |
| 243 | + |
| 244 | + |
| 245 | +My laptop crazy usage when AudioCraft is processing a request: |
| 246 | + |
| 247 | + |
| 248 | + |
| 249 | + |
| 250 | +### Conclusion |
| 251 | + |
| 252 | +In this post we have covered a detailed guide for running your own AudioCraft server in Ubuntu 22.04, I'm still playing with this interesting piece of technology, deploying your own server can allow you to share AudioCraft with your loved ones. |
0 commit comments