import requests
import time
# Call Lovo API to generate voiceover
url = "https://api.genny.lovo.ai/api/v1/tts"
headers = {
"accept": "application/json",
"content-type": "application/json",
"X-API-KEY": os.environ.get("LOVO_API_KEY")
}
outputs = []
# Initiate TTS Job for each Chunk
for chunk in chunks:
payload = {
"text": chunk,
"speaker": speaker_id
}
lovo_res = requests.request("POST", url, json=payload, headers=headers)
print(lover_res.json())
job_id = lovo_res.json()["id"]
outputs.append({"job_id": job_id})
# Keep Polling the API to check outputs
poll_time = 1
for output in outputs:
completed = False
while not completed:
lovo_res = requests.request("GET", f"{url}/{output['job_id']}", headers=headers)
lovo_res = lovo_res.json()['data'][0]
completed = lovo_res["status"] == "succeeded"
if completed:
output["audio_url"] = lovo_res["urls"][0]
completed = True
break
else:
time.sleep(poll_time)