├── LICENSE ├── README.md ├── banner.jpeg ├── requirements.txt ├── screen_cookies.jpeg ├── setup.py └── udio_wrapper └── __init__.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 flowese 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Udio Wrapper 3 | 4 | ![Udio Wrapper](banner.jpeg) 5 | 6 | Written by @Flowese 7 | 8 | Open Demo In Colab 9 | 10 | ## Description 11 | 12 | `udio_wrapper` is a Python package that allows you to generate music tracks from Udio's API using textual prompts. This package is designed to interact with Udio's API and is not officially endorsed by Udio. 13 | 14 | ## Advantages Over Other Models 15 | 16 | Unlike other music generation models, Udio offers a unique feature of extending or conditioning new tracks based on existing ones, making it ideal for iterative and creative music production processes. 17 | 18 | ## Legal Disclaimer 19 | 20 | This package is created for educational and research purposes. By using this package, you agree to do so at your own risk. This package is not affiliated, endorsed, or sponsored by Udio in any way. 21 | 22 | ## Requirements 23 | 24 | - Python 3.x 25 | - pip 26 | 27 | ## Installation 28 | 29 | ### From PyPI 30 | 31 | To install the package from PyPI, run the following command: 32 | 33 | ```bash 34 | pip install udio_wrapper 35 | ``` 36 | 37 | To upgrade the package from PyPI, run the following command: 38 | ```bash 39 | pip install --upgrade --no-cache-dir udio_wrapper 40 | ``` 41 | 42 | 43 | ### From GitHub Repository 44 | 45 | To install the package directly from the GitHub repository, run: 46 | 47 | ```bash 48 | pip install git+https://github.com/flowese/UdioWrapper.git 49 | ``` 50 | 51 | ## Configuration 52 | 53 | ### Obtaining the Authorization Token 54 | 55 | 1. Sign up at [Udio](https://www.udio.com/). 56 | 2. Once registered, open your browser's inspector: 57 | - In Chrome: `Ctrl+Shift+I` or `F12` on Windows, `Cmd+Option+I` on Mac. 58 | 3. Go to the `Application` tab. 59 | 4. On the left panel, locate and click on `Cookies`, then select the Ideogram website. 60 | 5. Find the cookie named `sb-api-auth-token`. 61 | 6. Click on `sb-api-auth-token` and copy the value in the `Value` field. 62 | 63 | ![Udio Wrapper](screen_cookies.jpeg) 64 | 65 | ### Usage 66 | 67 | To use `udio_wrapper`, import the `UdioWrapper` class and provide the necessary parameters. 68 | 69 | ## Usage Examples 70 | 71 | The following examples demonstrate various ways to use the `UdioWrapper` to generate music based on different scenarios: 72 | 73 | ```python 74 | auth_token = "your-auth-token-here" # Replace this with your actual authentication token 75 | udio_wrapper = UdioWrapper(auth_token) 76 | ``` 77 | 78 | 1. Creating a Short Song 79 | You can specify the prompt, seed, custom lyrics. 80 | ```python 81 | 82 | short_song_no_download = udio_wrapper.create_song( 83 | prompt="Relaxing jazz and soulful music", 84 | seed=-1, 85 | custom_lyrics="Short song lyrics here" 86 | ) 87 | print("Short song generated without downloading:", short_song_no_download) 88 | ``` 89 | 2. Extending a Song 90 | Extend a previously created song by providing its path and ID for conditioning. This method also allows for lyric customization. 91 | ```python 92 | 93 | extend_song_download = udio_wrapper.extend( 94 | prompt="A dynamic version of relaxing jazz and soulful music", 95 | seed=-1, 96 | audio_conditioning_path="url-generated-song", 97 | audio_conditioning_song_id="previous-song-id", 98 | custom_lyrics="Extended version lyrics" 99 | ) 100 | print("Extended song generated and downloaded:", extend_song_download) 101 | ``` 102 | 103 | 3. Adding an Outro 104 | Generate an outro for your music sequence using the last song as a base. This includes custom lyrics. 105 | ```python 106 | 107 | outro_song_download = udio_wrapper.add_outro( 108 | prompt="A smooth ending to our jazz session", 109 | seed=-1, 110 | audio_conditioning_path="url-generated-song", 111 | audio_conditioning_song_id="last-extended-song-id", 112 | custom_lyrics="Outro lyrics here" 113 | ) 114 | print("Outro song generated and downloaded:", outro_song_download) 115 | ``` 116 | 117 | 4. Creating a Complete Song Sequence 118 | Generate a full sequence of songs, including multiple extensions and an outro. This process involves defining prompts and lyrics for each part of the sequence. 119 | ```python 120 | complete_song_sequence = udio_wrapper.create_complete_song( 121 | short_prompt="On a full moon night", 122 | extend_prompts=["the soft sound of the saxophone fills the air", "creating an atmosphere of mystery and romance"], 123 | outro_prompt="Thus ends this melody, leaving an echo of emotions in the heart", 124 | num_extensions=2, 125 | custom_lyrics_short="Short song lyrics here", 126 | custom_lyrics_extend=["Lyrics for first extension", "Lyrics for second extension"], 127 | custom_lyrics_outro="Outro lyrics here" 128 | ) 129 | print("Complete song sequence generated and downloaded:", complete_song_sequence) 130 | ``` 131 | 132 | #### Parameters 133 | 134 | - **`auth_token`** *(Required)*: The authorization token you obtained from Udio, which is necessary for authenticating and making API requests. 135 | 136 | 137 | Each method in the `UdioWrapper` class can take several parameters to control song generation and processing. Below is a breakdown of the parameters and their usage: 138 | 139 | - **prompt** *(str)*: A text prompt describing the theme or emotion of the song. This is the creative input from which the song generation is based. 140 | 141 | - **seed** *(int, optional)*: A seed number to ensure the reproducibility of the song generation. Using the same seed with the same parameters will generate the same audio output. Default is `-1`, which results in random generation each time. 142 | 143 | - **custom_lyrics** *(str, optional)*: Lyrics written by the user to be included in the song. If no lyrics are provided, the generation relies solely on the musical style implied by the prompt. 144 | 145 | - **audio_conditioning_path** *(str, optional)*: The file path to an audio file that will serve as a base or influence for the song being generated. This is used primarily for extending songs or generating outros based on a previous song. 146 | 147 | - **audio_conditioning_song_id** *(str, optional)*: The identifier of a previously generated song that will be used to influence the current song generation. This is necessary when creating extended songs or outros that are meant to follow a specific musical piece. 148 | 149 | - **num_extensions** *(int, optional)*: Specifies the number of extended songs to generate in a sequence. This parameter is only used in the method that generates a complete song sequence. Default is `1`. 150 | 151 | - **extend_prompts** *(list of str, optional)*: A list of prompts for generating each extension in a sequence. Each prompt should ideally reflect a progression or variation in style or theme from the previous song. 152 | 153 | - **outro_prompt** *(str, optional)*: A prompt specifically for generating an outro. This should convey a sense of conclusion or finale relative to the musical sequence. 154 | 155 | 156 | These parameters allow full customization of the music generation process, from the initial creation through extensions to the final outro, giving users the ability to tailor both the music and lyrics to fit their specific needs or artistic vision. 157 | 158 | 159 | ## License 160 | 161 | This project is licensed under the MIT License. 162 | 163 | ## Contributing 164 | 165 | If you'd like to contribute to this project, feel free to fork the repository and send a pull request, or open an issue to discuss what you'd like to change. All contributions are welcome! 166 | 167 | ## TODO 168 | 169 | ### Pending Tasks and Features 170 | 171 | - Improve error handling and response validation. 172 | - Implement a user-friendly web interface for easier interaction with the API. 173 | 174 | ----- 175 | -------------------------------------------------------------------------------- /banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flowese/UdioWrapper/d5ef8576e61faf2d0f2f867452743bb77c568621/banner.jpeg -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests==2.31.0 -------------------------------------------------------------------------------- /screen_cookies.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flowese/UdioWrapper/d5ef8576e61faf2d0f2f867452743bb77c568621/screen_cookies.jpeg -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | with open("requirements.txt", "r") as f: 4 | requirements = f.read().splitlines() 5 | 6 | setup( 7 | name='udio_wrapper', 8 | version='0.0.1', 9 | description='Generates songs using the Udio API using textual prompts.', 10 | author='Flowese', 11 | packages=find_packages(), 12 | install_requires=requirements, 13 | ) -------------------------------------------------------------------------------- /udio_wrapper/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Udio Wrapper 3 | Author: Flowese 4 | Version: 0.0.3 5 | Date: 2024-04-15 6 | Description: Generates songs using the Udio API using textual prompts. 7 | """ 8 | 9 | import requests 10 | import os 11 | import time 12 | 13 | class UdioWrapper: 14 | API_BASE_URL = "https://www.udio.com/api" 15 | 16 | def __init__(self, auth_token): 17 | self.auth_token = auth_token 18 | self.all_track_ids = [] 19 | 20 | def make_request(self, url, method, data=None, headers=None): 21 | try: 22 | if method == 'POST': 23 | response = requests.post(url, headers=headers, json=data) 24 | else: 25 | response = requests.get(url, headers=headers) 26 | response.raise_for_status() 27 | return response 28 | except requests.exceptions.RequestException as e: 29 | print(f"Error making {method} request to {url}: {e}") 30 | return None 31 | 32 | def get_headers(self, get_request=False): 33 | headers = { 34 | "Accept": "application/json, text/plain, */*" if get_request else "application/json", 35 | "Content-Type": "application/json", 36 | "Cookie": f"; sb-api-auth-token={self.auth_token}", 37 | "Origin": "https://www.udio.com", 38 | "Referer": "https://www.udio.com/my-creations", 39 | "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36", 40 | "Sec-Fetch-Site": "same-origin", 41 | "Sec-Fetch-Mode": "cors", 42 | "Sec-Fetch-Dest": "empty" 43 | } 44 | if not get_request: 45 | headers.update({ 46 | "sec-ch-ua": '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"', 47 | "sec-ch-ua-mobile": "?0", 48 | "sec-ch-ua-platform": '"macOS"', 49 | "sec-fetch-dest": "empty" 50 | }) 51 | return headers 52 | 53 | def create_complete_song(self, short_prompt, extend_prompts, outro_prompt, seed=-1, custom_lyrics_short=None, custom_lyrics_extend=None, custom_lyrics_outro=None, num_extensions=1): 54 | print("Starting the generation of the complete song sequence...") 55 | 56 | # Generate the short song 57 | print("Generating the short song...") 58 | short_song_result = self.create_song(short_prompt, seed, custom_lyrics_short) 59 | if not short_song_result: 60 | print("Error generating the short song.") 61 | return None 62 | 63 | last_song_result = short_song_result 64 | extend_song_results = [] 65 | 66 | # Generate the extend songs 67 | for i in range(num_extensions): 68 | if i < len(extend_prompts): 69 | prompt = extend_prompts[i] 70 | lyrics = custom_lyrics_extend[i] if custom_lyrics_extend and i < len(custom_lyrics_extend) else None 71 | else: 72 | prompt = extend_prompts[-1] # Reuse the last prompt if not enough are provided 73 | lyrics = custom_lyrics_extend[-1] if custom_lyrics_extend else None 74 | 75 | print(f"Generating extend song {i + 1}...") 76 | extend_song_result = self.extend( 77 | prompt, 78 | seed, 79 | audio_conditioning_path=last_song_result[0]['song_path'], 80 | audio_conditioning_song_id=last_song_result[0]['id'], 81 | custom_lyrics=lyrics 82 | ) 83 | if not extend_song_result: 84 | print(f"Error generating extend song {i + 1}.") 85 | return None 86 | 87 | extend_song_results.append(extend_song_result) 88 | last_song_result = extend_song_result 89 | 90 | # Generate the outro 91 | print("Generating the outro...") 92 | outro_song_result = self.add_outro( 93 | outro_prompt, 94 | seed, 95 | audio_conditioning_path=last_song_result[0]['song_path'], 96 | audio_conditioning_song_id=last_song_result[0]['id'], 97 | custom_lyrics=custom_lyrics_outro 98 | ) 99 | if not outro_song_result: 100 | print("Error generating the outro.") 101 | return None 102 | 103 | print("Complete song sequence generated and processed successfully.") 104 | return { 105 | "short_song": short_song_result, 106 | "extend_songs": extend_song_results, 107 | "outro_song": outro_song_result 108 | } 109 | 110 | def create_song(self, prompt, seed=-1, custom_lyrics=None): 111 | song_result = self.generate_song(prompt, seed, custom_lyrics) 112 | if not song_result: 113 | return None 114 | track_ids = song_result.get('track_ids', []) 115 | self.all_track_ids.extend(track_ids) 116 | return self.process_songs(track_ids, "short_songs") 117 | 118 | def extend(self, prompt, seed=-1, audio_conditioning_path=None, audio_conditioning_song_id=None, custom_lyrics=None): 119 | extend_song_result = self.generate_extend_song( 120 | prompt, seed, audio_conditioning_path, audio_conditioning_song_id, custom_lyrics 121 | ) 122 | if not extend_song_result: 123 | return None 124 | extend_track_ids = extend_song_result.get('track_ids', []) 125 | self.all_track_ids.extend(extend_track_ids) 126 | return self.process_songs(extend_track_ids, "extend_songs") 127 | 128 | def add_outro(self, prompt, seed=-1, audio_conditioning_path=None, audio_conditioning_song_id=None, custom_lyrics=None): 129 | outro_result = self.generate_outro( 130 | prompt, seed, audio_conditioning_path, audio_conditioning_song_id, custom_lyrics 131 | ) 132 | if not outro_result: 133 | return None 134 | outro_track_ids = outro_result.get('track_ids', []) 135 | self.all_track_ids.extend(outro_track_ids) 136 | return self.process_songs(outro_track_ids, "outro_songs") 137 | 138 | def generate_song(self, prompt, seed, custom_lyrics=None): 139 | url = f"{self.API_BASE_URL}/generate-proxy" 140 | headers = self.get_headers() 141 | data = {"prompt": prompt, "samplerOptions": {"seed": seed}} 142 | if custom_lyrics: 143 | data["lyricInput"] = custom_lyrics 144 | response = self.make_request(url, 'POST', data, headers) 145 | return response.json() if response else None 146 | 147 | def generate_extend_song(self, prompt, seed, audio_conditioning_path, audio_conditioning_song_id, custom_lyrics=None): 148 | url = f"{self.API_BASE_URL}/generate-proxy" 149 | headers = self.get_headers() 150 | data = { 151 | "prompt": prompt, 152 | "samplerOptions": { 153 | "seed": seed, 154 | "audio_conditioning_path": audio_conditioning_path, 155 | "audio_conditioning_song_id": audio_conditioning_song_id, 156 | "audio_conditioning_type": "continuation" 157 | } 158 | } 159 | if custom_lyrics: 160 | data["lyricInput"] = custom_lyrics 161 | response = self.make_request(url, 'POST', data, headers) 162 | return response.json() if response else None 163 | 164 | def generate_outro(self, prompt, seed, audio_conditioning_path, audio_conditioning_song_id, custom_lyrics=None): 165 | url = f"{self.API_BASE_URL}/generate-proxy" 166 | headers = self.get_headers() 167 | data = { 168 | "prompt": prompt, 169 | "samplerOptions": { 170 | "seed": seed, 171 | "audio_conditioning_path": audio_conditioning_path, 172 | "audio_conditioning_song_id": audio_conditioning_song_id, 173 | "audio_conditioning_type": "continuation", 174 | "crop_start_time": 0.9 175 | } 176 | } 177 | if custom_lyrics: 178 | data["lyricInput"] = custom_lyrics 179 | response = self.make_request(url, 'POST', data, headers) 180 | return response.json() if response else None 181 | 182 | def process_songs(self, track_ids, folder): 183 | """Function to process generated songs, wait until they are ready, and download them.""" 184 | print(f"Processing songs in {folder} with track_ids {track_ids}...") 185 | while True: 186 | status_result = self.check_song_status(track_ids) 187 | if status_result is None: 188 | print(f"Error checking song status for {folder}.") 189 | return None 190 | elif status_result.get('all_finished', False): 191 | songs = [] 192 | for song in status_result['data']['songs']: 193 | self.download_song(song['song_path'], song['title'], folder=folder) 194 | songs.append(song) 195 | print(f"All songs in {folder} are ready and downloaded.") 196 | return songs 197 | else: 198 | time.sleep(5) 199 | 200 | def check_song_status(self, song_ids): 201 | url = f"{self.API_BASE_URL}/songs?songIds={','.join(song_ids)}" 202 | headers = self.get_headers(True) 203 | response = self.make_request(url, 'GET', None, headers) 204 | if response: 205 | data = response.json() 206 | all_finished = all(song['finished'] for song in data['songs']) 207 | return {'all_finished': all_finished, 'data': data} 208 | else: 209 | return None 210 | 211 | def download_song(self, song_url, song_title, folder="downloaded_songs"): 212 | os.makedirs(folder, exist_ok=True) 213 | file_path = os.path.join(folder, f"{song_title}.mp3") 214 | try: 215 | response = requests.get(song_url) 216 | response.raise_for_status() 217 | with open(file_path, 'wb') as file: 218 | file.write(response.content) 219 | print(f"Downloaded {song_title} with url {song_url} to {file_path}") 220 | except requests.exceptions.RequestException as e: 221 | print(f"Failed to download the song. Error: {e}") 222 | 223 | --------------------------------------------------------------------------------