|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any, AsyncGenerator, Dict, Iterator, List, Union |
| 4 | + |
| 5 | +from together.abstract import api_requestor |
| 6 | +from together.together_response import TogetherResponse |
| 7 | +from together.types import ( |
| 8 | + AudioSpeechRequest, |
| 9 | + AudioResponseFormat, |
| 10 | + AudioLanguage, |
| 11 | + AudioResponseEncoding, |
| 12 | + AudioSpeechStreamChunk, |
| 13 | + AudioSpeechStreamEvent, |
| 14 | + AudioSpeechStreamResponse, |
| 15 | + TogetherClient, |
| 16 | + TogetherRequest, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class Speech: |
| 21 | + def __init__(self, client: TogetherClient) -> None: |
| 22 | + self._client = client |
| 23 | + |
| 24 | + def create( |
| 25 | + self, |
| 26 | + *, |
| 27 | + model: str, |
| 28 | + input: str, |
| 29 | + voice: str | None = None, |
| 30 | + response_format: str = "wav", |
| 31 | + language: str = "en", |
| 32 | + response_encoding: str = "pcm_f32le", |
| 33 | + sample_rate: int = 44100, |
| 34 | + stream: bool = False, |
| 35 | + **kwargs: Any, |
| 36 | + ) -> AudioSpeechStreamResponse: |
| 37 | + """ |
| 38 | + Method to generate audio from input text using a specified model. |
| 39 | +
|
| 40 | + Args: |
| 41 | + model (str): The name of the model to query. |
| 42 | + input (str): Input text to generate the audio for. |
| 43 | + voice (str, optional): The voice to use for generating the audio. |
| 44 | + Defaults to None. |
| 45 | + response_format (str, optional): The format of audio output. |
| 46 | + Defaults to "wav". |
| 47 | + language (str, optional): Language of input text. |
| 48 | + Defaults to "en". |
| 49 | + response_encoding (str, optional): Audio encoding of response. |
| 50 | + Defaults to "pcm_f32le". |
| 51 | + sample_rate (int, optional): Sampling rate to use for the output audio. |
| 52 | + Defaults to 44100. |
| 53 | + stream (bool, optional): If true, output is streamed for several characters at a time. |
| 54 | + Defaults to False. |
| 55 | +
|
| 56 | + Returns: |
| 57 | + Union[bytes, Iterator[AudioSpeechStreamChunk]]: The generated audio as bytes or an iterator over audio stream chunks. |
| 58 | + """ |
| 59 | + |
| 60 | + requestor = api_requestor.APIRequestor( |
| 61 | + client=self._client, |
| 62 | + ) |
| 63 | + |
| 64 | + parameter_payload = AudioSpeechRequest( |
| 65 | + model=model, |
| 66 | + input=input, |
| 67 | + voice=voice, |
| 68 | + response_format=AudioResponseFormat(response_format), |
| 69 | + language=AudioLanguage(language), |
| 70 | + response_encoding=AudioResponseEncoding(response_encoding), |
| 71 | + sample_rate=sample_rate, |
| 72 | + stream=stream, |
| 73 | + **kwargs, |
| 74 | + ).model_dump(exclude_none=True) |
| 75 | + |
| 76 | + response, streamed, _ = requestor.request( |
| 77 | + options=TogetherRequest( |
| 78 | + method="POST", |
| 79 | + url="audio/speech", |
| 80 | + params=parameter_payload, |
| 81 | + ), |
| 82 | + stream=stream, |
| 83 | + ) |
| 84 | + |
| 85 | + return AudioSpeechStreamResponse(response=response) |
| 86 | + |
| 87 | + |
| 88 | +class AsyncSpeech: |
| 89 | + def __init__(self, client: TogetherClient) -> None: |
| 90 | + self._client = client |
| 91 | + |
| 92 | + async def create( |
| 93 | + self, |
| 94 | + *, |
| 95 | + model: str, |
| 96 | + input: str, |
| 97 | + voice: str | None = None, |
| 98 | + response_format: str = "wav", |
| 99 | + language: str = "en", |
| 100 | + response_encoding: str = "pcm_f32le", |
| 101 | + sample_rate: int = 44100, |
| 102 | + stream: bool = False, |
| 103 | + **kwargs: Any, |
| 104 | + ) -> AudioSpeechStreamResponse: |
| 105 | + """ |
| 106 | + Async method to generate audio from input text using a specified model. |
| 107 | +
|
| 108 | + Args: |
| 109 | + model (str): The name of the model to query. |
| 110 | + input (str): Input text to generate the audio for. |
| 111 | + voice (str, optional): The voice to use for generating the audio. |
| 112 | + Defaults to None. |
| 113 | + response_format (str, optional): The format of audio output. |
| 114 | + Defaults to "wav". |
| 115 | + language (str, optional): Language of input text. |
| 116 | + Defaults to "en". |
| 117 | + response_encoding (str, optional): Audio encoding of response. |
| 118 | + Defaults to "pcm_f32le". |
| 119 | + sample_rate (int, optional): Sampling rate to use for the output audio. |
| 120 | + Defaults to 44100. |
| 121 | + stream (bool, optional): If true, output is streamed for several characters at a time. |
| 122 | + Defaults to False. |
| 123 | +
|
| 124 | + Returns: |
| 125 | + Union[bytes, AsyncGenerator[AudioSpeechStreamChunk, None]]: The generated audio as bytes or an async generator over audio stream chunks. |
| 126 | + """ |
| 127 | + |
| 128 | + requestor = api_requestor.APIRequestor( |
| 129 | + client=self._client, |
| 130 | + ) |
| 131 | + |
| 132 | + parameter_payload = AudioSpeechRequest( |
| 133 | + model=model, |
| 134 | + input=input, |
| 135 | + voice=voice, |
| 136 | + response_format=AudioResponseFormat(response_format), |
| 137 | + language=AudioLanguage(language), |
| 138 | + response_encoding=AudioResponseEncoding(response_encoding), |
| 139 | + sample_rate=sample_rate, |
| 140 | + stream=stream, |
| 141 | + **kwargs, |
| 142 | + ).model_dump(exclude_none=True) |
| 143 | + |
| 144 | + response, _, _ = await requestor.arequest( |
| 145 | + options=TogetherRequest( |
| 146 | + method="POST", |
| 147 | + url="audio/speech", |
| 148 | + params=parameter_payload, |
| 149 | + ), |
| 150 | + stream=stream, |
| 151 | + ) |
| 152 | + |
| 153 | + return AudioSpeechStreamResponse(response=response) |
0 commit comments