.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/musiclang_api/03_generate_async.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_musiclang_api_03_generate_async.py: Create a song in async mode ====================================================== In this example : - We do the same example that 03_generate_from_scratch.py but in async mode - We poll the API until the prediction is done every 3 seconds .. GENERATED FROM PYTHON SOURCE LINES 10-53 .. code-block:: Python import os import time from maidi import MidiScore from maidi import instrument from maidi.integrations.api import MusicLangAPI # Assuming API_URL and MUSICLANG_API_KEY are set in the environment MUSICLANG_API_KEY = os.getenv("MUSICLANG_API_KEY") # Your choice of params for generation here instruments = [ instrument.DRUMS, instrument.ELECTRIC_BASS_FINGER, ] # Create a 4 bar template with the given instruments score = MidiScore.from_empty( instruments=instruments, nb_bars=4, ts=(4, 4), tempo=120 ) # Get the controls (the prompt) for this score mask, tags, chords = score.get_empty_controls(prevent_silence=True) mask[:, :] = 1 # Regenerate everything in the score # Call the musiclang API to predict the score api = MusicLangAPI(api_key=MUSICLANG_API_KEY, verbose=True) task_id = api.predict(score, mask, tags=tags, chords=chords, async_mode=True, # Set to True to use async mode polling_interval=3 ) # Poll the API until the prediction is done while True: score = api.poll_api(task_id) if score is not None: score.write('predicted_score.mid') break time.sleep(3) .. _sphx_glr_download_auto_examples_musiclang_api_03_generate_async.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 03_generate_async.ipynb <03_generate_async.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 03_generate_async.py <03_generate_async.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 03_generate_async.zip <03_generate_async.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_