|
1 | 1 | """ |
2 | | -This module contains the OpenAITextToSpeech class, which uses OpenAI's API |
3 | | -to convert text into speech. |
| 2 | +OpenAITextToSpeech Module |
4 | 3 | """ |
5 | 4 |
|
6 | 5 | from openai import OpenAI |
7 | 6 |
|
8 | 7 |
|
9 | 8 | class OpenAITextToSpeech: |
10 | 9 | """ |
11 | | - A class that uses OpenAI's API to convert text to speech. |
12 | | -
|
13 | | - Args: |
14 | | - llm_config (dict): The configuration for the language model. |
| 10 | + Implements a text-to-speech model using the OpenAI API. |
15 | 11 |
|
16 | 12 | Attributes: |
| 13 | + client (OpenAI): The OpenAI client used to interact with the API. |
17 | 14 | model (str): The model to use for text-to-speech conversion. |
18 | 15 | voice (str): The voice model to use for generating speech. |
19 | 16 |
|
20 | | - Methods: |
21 | | - run(text): Converts the provided text to speech and returns the |
22 | | - bytes of the generated speech. |
| 17 | + Args: |
| 18 | + tts_config (dict): Configuration parameters for the text-to-speech model. |
23 | 19 | """ |
24 | 20 |
|
25 | 21 | def __init__(self, tts_config: dict): |
26 | | - """ |
27 | | - Initializes an instance of the OpenAITextToSpeech class. |
28 | | -
|
29 | | - Args: |
30 | | - llm_config (dict): The configuration for the language model. |
31 | | - model (str, optional): The model to use for text-to-speech conversion. |
32 | | - Defaults to "tts-1". |
33 | | - voice (str, optional): The voice model to use for generating speech. |
34 | | - Defaults to "alloy". |
35 | | - """ |
36 | 22 |
|
37 | 23 | # convert model_name to model |
38 | 24 | self.client = OpenAI(api_key=tts_config.get("api_key")) |
39 | 25 | self.model = tts_config.get("model", "tts-1") |
40 | 26 | self.voice = tts_config.get("voice", "alloy") |
41 | 27 |
|
42 | | - def run(self, text): |
| 28 | + def run(self, text: str) -> bytes: |
43 | 29 | """ |
44 | 30 | Converts the provided text to speech and returns the bytes of the generated speech. |
45 | 31 |
|
46 | 32 | Args: |
47 | 33 | text (str): The text to convert to speech. |
48 | 34 |
|
| 35 | + Returns: |
| 36 | + bytes: The bytes of the generated speech audio. |
49 | 37 | """ |
50 | 38 | response = self.client.audio.speech.create( |
51 | 39 | model=self.model, |
|
0 commit comments