added tts generation support

This commit is contained in:
gilcu3
2023-11-08 13:55:58 +01:00
parent 450b86d5a7
commit d637e35498
7 changed files with 159 additions and 3 deletions

View File

@@ -10,6 +10,7 @@ import openai
import requests
import json
import httpx
import tempfile
from datetime import date
from calendar import monthrange
@@ -334,6 +335,27 @@ class OpenAIHelper:
except Exception as e:
raise Exception(f"⚠️ _{localized_text('error', bot_language)}._ ⚠️\n{str(e)}") from e
async def generate_speech(self, text: str) -> tuple[any, int]:
"""
Generates an audio from the given text using TTS model.
:param prompt: The text to send to the model
:return: The audio in bytes and the text size
"""
bot_language = self.config['bot_language']
try:
response = await self.client.audio.speech.create(
model=self.config['tts_model'],
voice=self.config['tts_voice'],
input=text,
response_format='opus'
)
temp_file = tempfile.NamedTemporaryFile()
response.stream_to_file(temp_file.name)
return temp_file, len(text)
except Exception as e:
raise Exception(f"⚠️ _{localized_text('error', bot_language)}._ ⚠️\n{str(e)}") from e
async def transcribe(self, filename):
"""
Transcribes the audio file using the Whisper model.