I want my gradio app to show both songs not just one, so give me the updated code to paste
if audio_info[0]["status"] != 'streaming':
return "Audio generation timed out."
code
audio_url = audio_info[0].get('audio_url')
if not audio_url:
return "Error fetching audio URL."
# Download the audio file from the audio_url
audio_response = requests.get(audio_url)
if audio_response.status_code == 200:
# Save the audio content to a temporary file
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
temp_file.write(audio_response.content)
temp_file.close()
# Return the path to the temporary audio file
return temp_file.name
else:
return "Error fetching audio from audio_url."
import requests
import gradio as gr
import tempfile
import time
# Generate audio by prompt
data = generate_audio_by_prompt(payload)
if not data or 'error' in data:
return "Error generating song."
ids = f"{data[0]['id']},{data[1]['id']}"
# Polling to wait for audio to be ready
for _ in range(60): # Polling for 60 times
audio_info = get_audio_information(ids)
if audio_info[0]["status"] == 'streaming':
break
time.sleep(5) # Wait for 5 seconds before the next poll
if audio_info[0]["status"] != 'streaming':
return "Audio generation timed out."
audio_url = audio_info[0].get('audio_url')
if not audio_url:
return "Error fetching audio URL."
# Download the audio file from the audio_url
audio_response = requests.get(audio_url)
if audio_response.status_code == 200:
# Save the audio content to a temporary file
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
temp_file.write(audio_response.content)
temp_file.close()
# Return the path to the temporary audio file
return temp_file.name
else:
return "Error fetching audio from audio_url."
iface = gr.Interface(
fn=generate_song,
inputs=gr.Textbox(lines=2, placeholder="Enter song prompt here..."),
outputs=gr.Audio(type='filepath'),
title="AI Song Generator",
description="Enter a prompt to generate a song using the Suno API."
)
if name == 'main':
iface.launch(share=True)
given the below integration
API Integration Code Example
Python
import time
import requests
if name == 'main':
data = generate_audio_by_prompt({
"prompt": "A popular heavy metal song about war, sung by a deep-voiced male singer, slowly and melodiously. The lyrics depict the sorrow of people after the war.",
"make_instrumental": False,
"wait_audio": False
})
code
ids = f"{data[0]['id']},{data[1]['id']}"
print(f"ids: {ids}")
for _ in range(60):
data = get_audio_information(ids)
if data[0]["status"] == 'streaming':
print(f"{data[0]['id']} ==> {data[0]['audio_url']}")
print(f"{data[1]['id']} ==> {data[1]['audio_url']}")
break
# sleep 5s
time.sleep(5)
Certainly! Here's the updated code for your Gradio app that will display both songs instead of just one. I've made changes to the generate_song function to fetch and return both audio files and updated the Gradio interface accordingly.
python
import requests
import gradio as gr
import tempfile
import time
# Replace your vercel domain
BASE_URL = 'https://suno-api-ivory-chi.vercel.app'
def generate_audio_by_prompt(payload):
url = f"{BASE_URL}/api/generate"
response = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
return response.json()
def get_audio_information(audio_ids):
url = f"{BASE_URL}/api/get?ids={audio_ids}"
response = requests.get(url)
return response.json()
def generate_song(prompt):
payload = {
"prompt": prompt,
"make_instrumental": False,
"wait_audio": False
}
# Generate audio by prompt
data = generate_audio_by_prompt(payload)
if not data or 'error' in data:
return "Error generating song."
ids = f"{data[0]['id']},{data[1]['id']}"
# Polling to wait for audio to be ready
for _ in range(60): # Polling for 60 times
audio_info = get_audio_information(ids)
# Check if both audios are streaming
if all(info["status"] == 'streaming' for info in audio_info):
break
time.sleep(5) # Wait for 5 seconds before the next poll
if not all(info["status"] == 'streaming' for info in audio_info):
return "Audio generation timed out."
audio_urls = [info.get('audio_url') for info in audio_info]
if not all(audio_urls):
return "Error fetching audio URLs."
file_paths = []
for audio_url in audio_urls:
# Download the audio file from the audio_url
audio_response = requests.get(audio_url)
if audio_response.status_code == 200:
# Save the audio content to a temporary file
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
temp_file.write(audio_response.content)
temp_file.close()
# Add the path to the list
file_paths.append(temp_file.name)
else:
return "Error fetching audio from audio_url."
# Return the list of file paths for both songs
return file_paths
iface = gr.Interface(
fn=generate_song,
inputs=gr.Textbox(lines=2, placeholder="Enter song prompt here..."),
outputs=[gr.Audio(type='filepath'), gr.Audio(type='filepath')],
title="AI Song Generator",
description="Enter a prompt to generate two versions of a song using the Suno API."
)
if __name__ == '__main__':
iface.launch(share=True)
Explanation of Changes:
Fetching Both Songs:
In the generate_song function, we modified the polling logic to check if both audio files are ready by using all(info["status"] == 'streaming' for info in audio_info).
We then retrieve both audio URLs and ensure they are valid.
We loop through each audio_url, download the audio content, and save it to temporary files, storing their paths in the file_paths list.
Finally, we return the list of file paths corresponding to both songs.
Updating Gradio Interface:
We updated the outputs parameter in the gr.Interface to be a list containing two gr.Audio components:
This tells Gradio to expect two audio outputs and display both audio players in the interface.
Usage:
When you run your Gradio app with this updated code, you'll be able to enter a song prompt, and the app will display two audio players with the generated songs.
Note:
Ensure that your BASE_URL variable is correctly set to your actual Vercel domain.
Make sure all required packages (requests, gradio, etc.) are installed in your Python environment.
Let me know if you need further assistance or have any questions!