import os
# import time
import requests
from openpyxl import load_workbook
from dotenv import load_dotenv
API_URL = 'https://api.edenai.run/v2/audio/text_to_speech'
HEADERS = {"Authorization": f"Bearer {os.getenv('API_KEY')}"}
DEFAULT_LANGUAGE = 'ru-RU'
DEFAULT_OPTION = 'MALE'
DEFAULT_VOICE = 'ru-RU_Alexei Syomin'
def convert_text_to_speech(row_number: int, _id: int, text: str):
payload = {
'providers': 'lovoai',
'language': DEFAULT_LANGUAGE,
'option': DEFAULT_OPTION,
'lovoai': DEFAULT_VOICE,
'text': text
}
response = requests.post(API_URL, json=payload, headers=HEADERS)
result = response.json()
audio_url = result.get('lovoai', {}).get('audio_resource_url')
if audio_url:
audio_content = requests.get(audio_url).content
with open(f'row_{row_number}_ID_{_id}.ogg', 'wb') as audio_file:
audio_file.write(audio_content)
def main(file_name: str):
book = load_workbook(file_name)
sheet = book.active
for row in range(2, sheet.max_row + 1):
_id = sheet[row][0].value
text = sheet[row][1].value
convert_text_to_speech(row, _id, text)
# Установите задержку, если имеется ограничение на кол-во запросов в секунду
# time.sleep(3)
if __name__ == '__main__':
load_dotenv()
main('name.xlsx')