You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Retrieve the generated music sample from the server <ahref="#retrieve-the-generated-video-from-the-server"id="retrieve-the-generated-video-from-the-server"></a>
30
30
@@ -33,3 +33,233 @@ After sending a request for music generation, this task is added to the queue. B
## Full Example: Generating and Retrieving the Audio From the Server <ahref="#full-example-generating-and-retrieving-the-video-from-the-server"id="full-example-generating-and-retrieving-the-video-from-the-server"></a>
38
+
39
+
The code below creates a audio generation task, then automatically polls the server every **10** seconds until it finally receives the video URL.
40
+
41
+
{% tabs %}
42
+
{% tab title="Python" %}
43
+
{% code overflow="wrap" %}
44
+
```python
45
+
import time
46
+
import requests
47
+
48
+
# Insert your AI/ML API key instead of <YOUR_AIMLAPI_KEY>:
49
+
aimlapi_key ='<YOUR_AIMLAPI_KEY>'
50
+
51
+
# Creating and sending an audio generation task to the server (returns a generation ID)
52
+
defgenerate_audio():
53
+
url ="https://api.aimlapi.com/v2/generate/audio"
54
+
payload = {
55
+
"model": "elevenlabs/eleven_music",
56
+
"prompt": "lo-fi pop hip-hop ambient music, slow intro: 10 s, then faster and with loud bass: 10 s",
# This is the main function of the program. From here, we sequentially call the audio generation and then repeatedly request the result from the server every 10 seconds:
82
+
defmain():
83
+
generation_response = generate_audio()
84
+
gen_id = generation_response.get("id")
85
+
86
+
if gen_id:
87
+
start_time = time.time()
88
+
89
+
timeout =600
90
+
while time.time() - start_time < timeout:
91
+
response_data = retrieve_audio(gen_id)
92
+
93
+
if response_data isNone:
94
+
print("Error: No response from API")
95
+
break
96
+
97
+
status = response_data.get("status")
98
+
99
+
if status in ["waiting", "queued", "generating"]:
100
+
print(f"Status: {status}. Checking again in 10 seconds.")
101
+
time.sleep(10)
102
+
else:
103
+
print("Generation complete:/n", response_data)
104
+
return response_data
105
+
106
+
print("Timeout reached. Stopping.")
107
+
returnNone
108
+
109
+
110
+
if__name__=="__main__":
111
+
main()
112
+
```
113
+
{% endcode %}
114
+
{% endtab %}
115
+
116
+
{% tab title="JavaScript" %}
117
+
{% code overflow="wrap" %}
118
+
```javascript
119
+
consthttps=require("https");
120
+
const { URL } =require("url");
121
+
122
+
// Replace <YOUR_AIMLAPI_KEY> with your actual AI/ML API key
123
+
constapiKey="<YOUR_AIMLAPI_KEY>";
124
+
constbaseUrl="https://api.aimlapi.com/v2";
125
+
126
+
// Creating and sending a audio generation task to the server
127
+
functiongenerateAudio(callback) {
128
+
constdata=JSON.stringify({
129
+
model:"elevenlabs/eleven_music",
130
+
prompt:"lo-fi pop hip-hop ambient music, slow intro: 10 s, then faster and with loud bass: 10 s",
0 commit comments