|
1 | 1 | import typing |
2 | 2 |
|
| 3 | +from magic_hour.helpers.logger import get_sdk_logger |
| 4 | +from magic_hour.resources.v1.audio_projects.client import ( |
| 5 | + AsyncAudioProjectsClient, |
| 6 | + AudioProjectsClient, |
| 7 | +) |
3 | 8 | from magic_hour.types import models, params |
4 | 9 | from make_api_request import ( |
5 | 10 | AsyncBaseClient, |
|
11 | 16 | ) |
12 | 17 |
|
13 | 18 |
|
| 19 | +logger = get_sdk_logger(__name__) |
| 20 | + |
| 21 | + |
14 | 22 | class AiVoiceGeneratorClient: |
15 | 23 | def __init__(self, *, base_client: SyncBaseClient): |
16 | 24 | self._base_client = base_client |
17 | 25 |
|
| 26 | + def generate( |
| 27 | + self, |
| 28 | + *, |
| 29 | + style: params.V1AiVoiceGeneratorCreateBodyStyle, |
| 30 | + name: typing.Union[ |
| 31 | + typing.Optional[str], type_utils.NotGiven |
| 32 | + ] = type_utils.NOT_GIVEN, |
| 33 | + wait_for_completion: bool = True, |
| 34 | + download_outputs: bool = True, |
| 35 | + download_directory: typing.Optional[str] = None, |
| 36 | + request_options: typing.Optional[RequestOptions] = None, |
| 37 | + ): |
| 38 | + """ |
| 39 | + Generate AI voice (alias for create with additional functionality). |
| 40 | +
|
| 41 | + Generate speech from text. Each character costs 0.05 credits. The cost is rounded up to the nearest whole number. |
| 42 | +
|
| 43 | + Args: |
| 44 | + style: The content used to generate speech. |
| 45 | + name: The name of audio. This value is mainly used for your own identification of the audio. |
| 46 | + wait_for_completion: Whether to wait for the audio project to complete |
| 47 | + download_outputs: Whether to download the outputs |
| 48 | + download_directory: The directory to download the outputs to. If not provided, the outputs will be downloaded to the current working directory |
| 49 | + request_options: Additional options to customize the HTTP request |
| 50 | +
|
| 51 | + Returns: |
| 52 | + V1AudioProjectsGetResponseWithDownloads: The response from the AI Voice Generator API with the downloaded paths if `download_outputs` is True. |
| 53 | +
|
| 54 | + Examples: |
| 55 | + ```py |
| 56 | + response = client.v1.ai_voice_generator.generate( |
| 57 | + style={"prompt": "Hello, how are you?", "voice_name": "Elon Musk"}, |
| 58 | + name="Generated Voice", |
| 59 | + wait_for_completion=True, |
| 60 | + download_outputs=True, |
| 61 | + download_directory="outputs/", |
| 62 | + ) |
| 63 | + ``` |
| 64 | + """ |
| 65 | + |
| 66 | + create_response = self.create( |
| 67 | + style=style, |
| 68 | + name=name, |
| 69 | + request_options=request_options, |
| 70 | + ) |
| 71 | + logger.info(f"AI Voice Generator response: {create_response}") |
| 72 | + |
| 73 | + audio_projects_client = AudioProjectsClient(base_client=self._base_client) |
| 74 | + response = audio_projects_client.check_result( |
| 75 | + id=create_response.id, |
| 76 | + wait_for_completion=wait_for_completion, |
| 77 | + download_outputs=download_outputs, |
| 78 | + download_directory=download_directory, |
| 79 | + ) |
| 80 | + |
| 81 | + return response |
| 82 | + |
18 | 83 | def create( |
19 | 84 | self, |
20 | 85 | *, |
@@ -69,6 +134,63 @@ class AsyncAiVoiceGeneratorClient: |
69 | 134 | def __init__(self, *, base_client: AsyncBaseClient): |
70 | 135 | self._base_client = base_client |
71 | 136 |
|
| 137 | + async def generate( |
| 138 | + self, |
| 139 | + *, |
| 140 | + style: params.V1AiVoiceGeneratorCreateBodyStyle, |
| 141 | + name: typing.Union[ |
| 142 | + typing.Optional[str], type_utils.NotGiven |
| 143 | + ] = type_utils.NOT_GIVEN, |
| 144 | + wait_for_completion: bool = True, |
| 145 | + download_outputs: bool = True, |
| 146 | + download_directory: typing.Optional[str] = None, |
| 147 | + request_options: typing.Optional[RequestOptions] = None, |
| 148 | + ): |
| 149 | + """ |
| 150 | + Generate AI voice (alias for create with additional functionality). |
| 151 | +
|
| 152 | + Generate speech from text. Each character costs 0.05 credits. The cost is rounded up to the nearest whole number. |
| 153 | +
|
| 154 | + Args: |
| 155 | + style: The content used to generate speech. |
| 156 | + name: The name of audio. This value is mainly used for your own identification of the audio. |
| 157 | + wait_for_completion: Whether to wait for the audio project to complete |
| 158 | + download_outputs: Whether to download the outputs |
| 159 | + download_directory: The directory to download the outputs to. If not provided, the outputs will be downloaded to the current working directory |
| 160 | + request_options: Additional options to customize the HTTP request |
| 161 | +
|
| 162 | + Returns: |
| 163 | + V1AudioProjectsGetResponseWithDownloads: The response from the AI Voice Generator API with the downloaded paths if `download_outputs` is True. |
| 164 | +
|
| 165 | + Examples: |
| 166 | + ```py |
| 167 | + response = await client.v1.ai_voice_generator.generate( |
| 168 | + style={"prompt": "Hello, how are you?", "voice_name": "Elon Musk"}, |
| 169 | + name="Generated Voice", |
| 170 | + wait_for_completion=True, |
| 171 | + download_outputs=True, |
| 172 | + download_directory="outputs/", |
| 173 | + ) |
| 174 | + ``` |
| 175 | + """ |
| 176 | + |
| 177 | + create_response = await self.create( |
| 178 | + style=style, |
| 179 | + name=name, |
| 180 | + request_options=request_options, |
| 181 | + ) |
| 182 | + logger.info(f"AI Voice Generator response: {create_response}") |
| 183 | + |
| 184 | + audio_projects_client = AsyncAudioProjectsClient(base_client=self._base_client) |
| 185 | + response = await audio_projects_client.check_result( |
| 186 | + id=create_response.id, |
| 187 | + wait_for_completion=wait_for_completion, |
| 188 | + download_outputs=download_outputs, |
| 189 | + download_directory=download_directory, |
| 190 | + ) |
| 191 | + |
| 192 | + return response |
| 193 | + |
72 | 194 | async def create( |
73 | 195 | self, |
74 | 196 | *, |
|
0 commit comments