[GenerateAndDownload]
Closed this issue · 0 comments
`# Fonction pour envoyer les paroles de chansons à l'API pour la génération
def send_lyrics_to_api(song):
url = 'https://api.sunoaiapi.com/api/v1/gateway/generate/music'
headers = {'Content-Type': 'application/json', 'api-key': API_KEY}
payload = {
"title": song[0],
"tags": song[1],
"prompt": song[2],
"mv": "chirp-v3" # Ajustez si nécessaire
}
print(f"Sending song to API: {payload}") # Journalisation des détails d'envoi
response = requests.post(url, json=payload, headers=headers)
if response.status_code == 200:
song_id = response.json()['data'][0]['song_id']
print(f"Successfully submitted: {song[0]}, ID: {song_id}")
return song_id
else:
print(f"Failed to submit {song[0]} with status: {response.status_code}, Response: {response.text}")
return None
# Fonction pour interroger l'état de génération d'une chanson
def query_generation_status(song_ids):
url = f"https://api.sunoaiapi.com/api/v1/gateway/query?ids={','.join(song_ids)}"
headers = {'Content-Type': 'application/json', 'api-key': API_KEY}
print(f"Querying generation status for: {song_ids}") # Journalisation des détails de requête
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
print(f"Failed to query generation status with status: {response.status_code}, Response: {response.text}")
return None
`
Thoses functions didn't manage any sucessful generation. Lyrics sent nevertheless.
IF needed, full code there :
if name == "main":
songs = get_lyrics_from_sheet(SPREADSHEET_ID, RANGE_NAME)
if songs:
for idx in range(0, len(songs), 2):
if idx + 1 >= len(songs):
continue
song_v1 = songs[idx]
song_v2 = songs[idx + 1]
if not song_v1 or not song_v2 or len(song_v1) < 3 or len(song_v2) < 3:
print(f"Skipping incomplete song entry at index {idx}")
continue
title_v1 = song_v1[0]
title_v2 = song_v2[0]
if title_v1 != title_v2:
print(f"Titles do not match for V1 and V2: {title_v1} != {title_v2}")
continue
# Envoyer les chansons V1 et V2 à l'API
v1_id = send_lyrics_to_api(song_v1)
if not v1_id:
print(f"Failed to send V1 for song: {title_v1}")
continue
v2_id = send_lyrics_to_api(song_v2)
if not v2_id:
print(f"Failed to send V2 for song: {title_v2}")
continue
# Attendre que les deux versions soient générées
v1_complete = False
v2_complete = False
while not (v1_complete and v2_complete):
if not v1_complete:
v1_result = query_generation_status([v1_id])
if v1_result and v1_result[0]['status'] == 'complete':
v1_complete = True
print(f"V1 generation complete for song: {title_v1}")
else:
print(f"V1 generation not complete for song: {title_v1}, retrying in 30 seconds...")
time.sleep(30)
if not v2_complete:
v2_result = query_generation_status([v2_id])
if v2_result and v2_result[0]['status'] == 'complete':
v2_complete = True
print(f"V2 generation complete for song: {title_v2}")
else:
print(f"V2 generation not complete for song: {title_v2}, retrying in 30 seconds...")
time.sleep(30)
# Concaténer les chansons V1 et V2
concat_id = concatenate_songs(v1_id, v2_id)
if not concat_id:
print(f"Failed to concatenate songs for: {title_v1}")
continue
# Interroger l'état de la chanson concaténée
while True:
concat_result = query_generation_status([concat_id])
if concat_result:
status = concat_result[0]['status']
if status == 'complete':
audio_url = concat_result[0]['audio_url']
output_filename = f"{title_v1}_complete.mp3"
download_audio(audio_url, output_filename)
print(f"Successfully downloaded concatenated song: {output_filename}")
# Ajouter le texte de la chanson générée à la liste pour mise à jour de la feuille
song_text = concat_result[0]['meta_data']['prompt']
update_sheet_with_text(SPREADSHEET_ID, [[song_text]])
break
elif status == 'error':
error_message = concat_result[0]['meta_data'].get('error_message', 'Unknown error')
print(f"Failed to concatenate song {title_v1}: {error_message}")
update_sheet_with_text(SPREADSHEET_ID, [["Error in concatenation: " + error_message]])
break
else:
print(f"Concatenated song {title_v1} is in status: {status}. Retrying in 30 seconds...")
time.sleep(30)
else:
print(f"Failed to get generation status for concatenated song: {concat_id}")
time.sleep(30)
else:
print("No songs found or an error occurred while fetching songs.")
Thanks in advance.
sKunZel