-
Notifications
You must be signed in to change notification settings - Fork 25
feat: add WebSocketOptions for configurable WebSocket connections #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5c1e168
feat: add WebSocketOptions for configurable WebSocket connections
twangodev 2bf07e8
style: improve formatting of WebSocketOptions kwargs conversion
twangodev c316875
test: add unit tests for WebSocketOptions and its integration in stre…
twangodev 14fc516
feat: add WebSocket options for configurable timeouts and message lim…
twangodev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,58 @@ | ||
| """WebSocket-level options for WebSocket connections.""" | ||
|
|
||
| from typing import Any, Dict, Optional | ||
|
|
||
|
|
||
| class WebSocketOptions: | ||
| """ | ||
| Options for configuring WebSocket connections. | ||
|
|
||
| These options are passed directly to httpx_ws's connect_ws/aconnect_ws functions. | ||
| For complete documentation, see https://frankie567.github.io/httpx-ws/reference/httpx_ws/ | ||
|
|
||
| Attributes: | ||
| keepalive_ping_timeout_seconds: Maximum delay the client will wait for an answer | ||
| to its Ping event. If the delay is exceeded, WebSocketNetworkError will be | ||
| raised and the connection closed. Default: 20 seconds. | ||
| keepalive_ping_interval_seconds: Interval at which the client will automatically | ||
| send a Ping event to keep the connection alive. Set to None to disable this | ||
| mechanism. Default: 20 seconds. | ||
| max_message_size_bytes: Message size in bytes to receive from the server. | ||
| Default: 65536 bytes (64 KiB). | ||
| queue_size: Size of the queue where received messages will be held until they | ||
| are consumed. If the queue is full, the client will stop receiving messages | ||
| from the server until the queue has room available. Default: 512. | ||
|
|
||
| Note: | ||
| Parameter descriptions adapted from httpx_ws documentation. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| keepalive_ping_timeout_seconds: Optional[float] = None, | ||
| keepalive_ping_interval_seconds: Optional[float] = None, | ||
| max_message_size_bytes: Optional[int] = None, | ||
| queue_size: Optional[int] = None, | ||
| ): | ||
| self.keepalive_ping_timeout_seconds = keepalive_ping_timeout_seconds | ||
| self.keepalive_ping_interval_seconds = keepalive_ping_interval_seconds | ||
| self.max_message_size_bytes = max_message_size_bytes | ||
| self.queue_size = queue_size | ||
|
|
||
| def to_httpx_ws_kwargs(self) -> Dict[str, Any]: | ||
| """Convert to kwargs dict for httpx_ws aconnect_ws/connect_ws.""" | ||
| kwargs = {} | ||
| if self.keepalive_ping_timeout_seconds is not None: | ||
| kwargs["keepalive_ping_timeout_seconds"] = ( | ||
| self.keepalive_ping_timeout_seconds | ||
| ) | ||
| if self.keepalive_ping_interval_seconds is not None: | ||
| kwargs["keepalive_ping_interval_seconds"] = ( | ||
| self.keepalive_ping_interval_seconds | ||
| ) | ||
| if self.max_message_size_bytes is not None: | ||
| kwargs["max_message_size_bytes"] = self.max_message_size_bytes | ||
| if self.queue_size is not None: | ||
| kwargs["queue_size"] = self.queue_size | ||
| return kwargs |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |
| from httpx_ws import AsyncWebSocketSession, WebSocketSession, aconnect_ws, connect_ws | ||
|
|
||
| from .realtime import aiter_websocket_audio, iter_websocket_audio | ||
| from ..core import AsyncClientWrapper, ClientWrapper, RequestOptions | ||
| from ..core import AsyncClientWrapper, ClientWrapper, RequestOptions, WebSocketOptions | ||
| from ..core.iterators import AsyncAudioStream, AudioStream | ||
| from ..types import ( | ||
| AudioFormat, | ||
|
|
@@ -215,6 +215,7 @@ def stream_websocket( | |
| config: TTSConfig = TTSConfig(), | ||
| model: Model = "s1", | ||
| max_workers: int = 10, | ||
| ws_options: Optional[WebSocketOptions] = None, | ||
| ) -> Iterator[bytes]: | ||
| """ | ||
| Stream text and receive audio in real-time via WebSocket. | ||
|
|
@@ -231,13 +232,16 @@ def stream_websocket( | |
| config: TTS configuration (audio settings, voice, model parameters) | ||
| model: TTS model to use | ||
| max_workers: ThreadPoolExecutor workers for concurrent sender | ||
| ws_options: WebSocket connection options for configuring timeouts, message size limits, etc. | ||
| Useful for long-running generations that may exceed default timeout values. | ||
| See WebSocketOptions class for available parameters. | ||
|
|
||
| Returns: | ||
| Iterator of audio bytes | ||
|
|
||
| Example: | ||
| ```python | ||
| from fishaudio import FishAudio, TTSConfig, ReferenceAudio | ||
| from fishaudio import FishAudio, TTSConfig, ReferenceAudio, WebSocketOptions | ||
|
|
||
| client = FishAudio(api_key="...") | ||
|
|
||
|
|
@@ -273,6 +277,16 @@ def text_generator(): | |
| ): | ||
| f.write(audio_chunk) | ||
|
|
||
| # With WebSocket options for long-running generations | ||
| # Useful if you're generating very long responses that may take >20 seconds | ||
| ws_options = WebSocketOptions(keepalive_ping_timeout_seconds=60.0) | ||
| with open("output.mp3", "wb") as f: | ||
| for audio_chunk in client.tts.stream_websocket( | ||
| text_generator(), | ||
| ws_options=ws_options | ||
| ): | ||
| f.write(audio_chunk) | ||
|
|
||
| # Parameters override config values | ||
| config = TTSConfig(format="mp3", latency="balanced") | ||
| with open("output.wav", "wb") as f: | ||
|
|
@@ -305,6 +319,9 @@ def text_generator(): | |
| speed, base=config.prosody | ||
| ) | ||
|
|
||
| # Prepare WebSocket connection kwargs | ||
| ws_kwargs = ws_options.to_httpx_ws_kwargs() if ws_options else {} | ||
|
|
||
| executor = ThreadPoolExecutor(max_workers=max_workers) | ||
|
|
||
| try: | ||
|
|
@@ -316,6 +333,7 @@ def text_generator(): | |
| "model": model, | ||
| "Authorization": f"Bearer {self._client.api_key}", | ||
| }, | ||
| **ws_kwargs, | ||
| ) as ws: | ||
|
|
||
| def sender(): | ||
|
|
@@ -502,6 +520,7 @@ async def stream_websocket( | |
| speed: Optional[float] = None, | ||
| config: TTSConfig = TTSConfig(), | ||
| model: Model = "s1", | ||
| ws_options: Optional[WebSocketOptions] = None, | ||
|
||
| ): | ||
| """ | ||
| Stream text and receive audio in real-time via WebSocket (async). | ||
|
|
@@ -517,13 +536,16 @@ async def stream_websocket( | |
| speed: Speech speed multiplier, e.g. 1.5 for 1.5x speed (overrides config.prosody.speed if provided) | ||
| config: TTS configuration (audio settings, voice, model parameters) | ||
| model: TTS model to use | ||
| ws_options: WebSocket connection options for configuring timeouts, message size limits, etc. | ||
| Useful for long-running generations that may exceed default timeout values. | ||
| See WebSocketOptions class for available parameters. | ||
|
|
||
| Returns: | ||
| Async iterator of audio bytes | ||
|
|
||
| Example: | ||
| ```python | ||
| from fishaudio import AsyncFishAudio, TTSConfig, ReferenceAudio | ||
| from fishaudio import AsyncFishAudio, TTSConfig, ReferenceAudio, WebSocketOptions | ||
|
|
||
| client = AsyncFishAudio(api_key="...") | ||
|
|
||
|
|
@@ -559,6 +581,16 @@ async def text_generator(): | |
| ): | ||
| await f.write(audio_chunk) | ||
|
|
||
| # With WebSocket options for long-running generations | ||
| # Useful if you're generating very long responses that may take >20 seconds | ||
| ws_options = WebSocketOptions(keepalive_ping_timeout_seconds=60.0) | ||
| async with aiofiles.open("output.mp3", "wb") as f: | ||
| async for audio_chunk in client.tts.stream_websocket( | ||
| text_generator(), | ||
| ws_options=ws_options | ||
| ): | ||
| await f.write(audio_chunk) | ||
|
|
||
| # Parameters override config values | ||
| config = TTSConfig(format="mp3", latency="balanced") | ||
| async with aiofiles.open("output.wav", "wb") as f: | ||
|
|
@@ -591,11 +623,15 @@ async def text_generator(): | |
| speed, base=config.prosody | ||
| ) | ||
|
|
||
| # Prepare WebSocket connection kwargs | ||
| ws_kwargs = ws_options.to_httpx_ws_kwargs() if ws_options else {} | ||
|
|
||
| ws: AsyncWebSocketSession | ||
| async with aconnect_ws( | ||
| "/v1/tts/live", | ||
| client=self._client.client, | ||
| headers={"model": model, "Authorization": f"Bearer {self._client.api_key}"}, | ||
| **ws_kwargs, | ||
| ) as ws: | ||
|
|
||
| async def sender(): | ||
|
|
||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
ws_optionsparameter is missing from the method's docstring Args section. Please add documentation for this parameter to help users understand how to configure WebSocket connection options.