mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2025-12-20 22:24:57 +01:00
added tts generation support
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user