-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* do not merge - audio api init * Get audio stuff working. (#245) * Initially getting things working. * More closely match spec * Formatting fixes. * Adjust handling of different types to make linter happy. * Add type definition * Decode bytes in ternary * bump to version 1.3.14 --------- Co-authored-by: jdreamerz <[email protected]> Co-authored-by: Justin Driemeyer <[email protected]>
- Loading branch information
1 parent
c185015
commit 82dba38
Showing
9 changed files
with
325 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ build-backend = "poetry.masonry.api" | |
|
||
[tool.poetry] | ||
name = "together" | ||
version = "1.3.13" | ||
version = "1.3.14" | ||
authors = [ | ||
"Together AI <[email protected]>" | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from functools import cached_property | ||
|
||
from together.resources.audio.speech import AsyncSpeech, Speech | ||
from together.types import ( | ||
TogetherClient, | ||
) | ||
|
||
|
||
class Audio: | ||
def __init__(self, client: TogetherClient) -> None: | ||
self._client = client | ||
|
||
@cached_property | ||
def speech(self) -> Speech: | ||
return Speech(self._client) | ||
|
||
|
||
class AsyncAudio: | ||
def __init__(self, client: TogetherClient) -> None: | ||
self._client = client | ||
|
||
@cached_property | ||
def speech(self) -> AsyncSpeech: | ||
return AsyncSpeech(self._client) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any, AsyncGenerator, Dict, Iterator, List, Union | ||
|
||
from together.abstract import api_requestor | ||
from together.together_response import TogetherResponse | ||
from together.types import ( | ||
AudioSpeechRequest, | ||
AudioResponseFormat, | ||
AudioLanguage, | ||
AudioResponseEncoding, | ||
AudioSpeechStreamChunk, | ||
AudioSpeechStreamEvent, | ||
AudioSpeechStreamResponse, | ||
TogetherClient, | ||
TogetherRequest, | ||
) | ||
|
||
|
||
class Speech: | ||
def __init__(self, client: TogetherClient) -> None: | ||
self._client = client | ||
|
||
def create( | ||
self, | ||
*, | ||
model: str, | ||
input: str, | ||
voice: str | None = None, | ||
response_format: str = "wav", | ||
language: str = "en", | ||
response_encoding: str = "pcm_f32le", | ||
sample_rate: int = 44100, | ||
stream: bool = False, | ||
**kwargs: Any, | ||
) -> AudioSpeechStreamResponse: | ||
""" | ||
Method to generate audio from input text using a specified model. | ||
Args: | ||
model (str): The name of the model to query. | ||
input (str): Input text to generate the audio for. | ||
voice (str, optional): The voice to use for generating the audio. | ||
Defaults to None. | ||
response_format (str, optional): The format of audio output. | ||
Defaults to "wav". | ||
language (str, optional): Language of input text. | ||
Defaults to "en". | ||
response_encoding (str, optional): Audio encoding of response. | ||
Defaults to "pcm_f32le". | ||
sample_rate (int, optional): Sampling rate to use for the output audio. | ||
Defaults to 44100. | ||
stream (bool, optional): If true, output is streamed for several characters at a time. | ||
Defaults to False. | ||
Returns: | ||
Union[bytes, Iterator[AudioSpeechStreamChunk]]: The generated audio as bytes or an iterator over audio stream chunks. | ||
""" | ||
|
||
requestor = api_requestor.APIRequestor( | ||
client=self._client, | ||
) | ||
|
||
parameter_payload = AudioSpeechRequest( | ||
model=model, | ||
input=input, | ||
voice=voice, | ||
response_format=AudioResponseFormat(response_format), | ||
language=AudioLanguage(language), | ||
response_encoding=AudioResponseEncoding(response_encoding), | ||
sample_rate=sample_rate, | ||
stream=stream, | ||
**kwargs, | ||
).model_dump(exclude_none=True) | ||
|
||
response, streamed, _ = requestor.request( | ||
options=TogetherRequest( | ||
method="POST", | ||
url="audio/speech", | ||
params=parameter_payload, | ||
), | ||
stream=stream, | ||
) | ||
|
||
return AudioSpeechStreamResponse(response=response) | ||
|
||
|
||
class AsyncSpeech: | ||
def __init__(self, client: TogetherClient) -> None: | ||
self._client = client | ||
|
||
async def create( | ||
self, | ||
*, | ||
model: str, | ||
input: str, | ||
voice: str | None = None, | ||
response_format: str = "wav", | ||
language: str = "en", | ||
response_encoding: str = "pcm_f32le", | ||
sample_rate: int = 44100, | ||
stream: bool = False, | ||
**kwargs: Any, | ||
) -> AudioSpeechStreamResponse: | ||
""" | ||
Async method to generate audio from input text using a specified model. | ||
Args: | ||
model (str): The name of the model to query. | ||
input (str): Input text to generate the audio for. | ||
voice (str, optional): The voice to use for generating the audio. | ||
Defaults to None. | ||
response_format (str, optional): The format of audio output. | ||
Defaults to "wav". | ||
language (str, optional): Language of input text. | ||
Defaults to "en". | ||
response_encoding (str, optional): Audio encoding of response. | ||
Defaults to "pcm_f32le". | ||
sample_rate (int, optional): Sampling rate to use for the output audio. | ||
Defaults to 44100. | ||
stream (bool, optional): If true, output is streamed for several characters at a time. | ||
Defaults to False. | ||
Returns: | ||
Union[bytes, AsyncGenerator[AudioSpeechStreamChunk, None]]: The generated audio as bytes or an async generator over audio stream chunks. | ||
""" | ||
|
||
requestor = api_requestor.APIRequestor( | ||
client=self._client, | ||
) | ||
|
||
parameter_payload = AudioSpeechRequest( | ||
model=model, | ||
input=input, | ||
voice=voice, | ||
response_format=AudioResponseFormat(response_format), | ||
language=AudioLanguage(language), | ||
response_encoding=AudioResponseEncoding(response_encoding), | ||
sample_rate=sample_rate, | ||
stream=stream, | ||
**kwargs, | ||
).model_dump(exclude_none=True) | ||
|
||
response, _, _ = await requestor.arequest( | ||
options=TogetherRequest( | ||
method="POST", | ||
url="audio/speech", | ||
params=parameter_payload, | ||
), | ||
stream=stream, | ||
) | ||
|
||
return AudioSpeechStreamResponse(response=response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.