-
Notifications
You must be signed in to change notification settings - Fork 395
[Feature][TTS] Streaming Text Input for Qwen3-TTS via WebSocket #1230
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
Open
lishunyang12
wants to merge
2
commits into
vllm-project:main
Choose a base branch
from
lishunyang12:tts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,468
−93
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
234 changes: 234 additions & 0 deletions
234
examples/online_serving/qwen3_tts/streaming_speech_client.py
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,234 @@ | ||||||||
| """WebSocket client for streaming text-input TTS. | ||||||||
|
|
||||||||
| Connects to the /v1/audio/speech/stream endpoint, sends text incrementally | ||||||||
| (simulating real-time STT output), and saves per-sentence audio files. | ||||||||
|
|
||||||||
| Usage: | ||||||||
| # Send full text at once | ||||||||
| python streaming_speech_client.py --text "Hello world. How are you? I am fine." | ||||||||
|
|
||||||||
| # Simulate STT: send text word-by-word with delay | ||||||||
| python streaming_speech_client.py \ | ||||||||
| --text "Hello world. How are you? I am fine." \ | ||||||||
| --simulate-stt --stt-delay 0.1 | ||||||||
|
|
||||||||
| # VoiceDesign task | ||||||||
| python streaming_speech_client.py \ | ||||||||
| --text "Today is a great day. The weather is nice." \ | ||||||||
| --task-type VoiceDesign \ | ||||||||
| --instructions "A cheerful young female voice" | ||||||||
|
|
||||||||
| # Base task (voice cloning) | ||||||||
| python streaming_speech_client.py \ | ||||||||
| --text "Hello world. How are you?" \ | ||||||||
| --task-type Base \ | ||||||||
| --ref-audio /path/to/reference.wav \ | ||||||||
| --ref-text "Transcript of reference audio" | ||||||||
|
|
||||||||
| Requirements: | ||||||||
| pip install websockets | ||||||||
| """ | ||||||||
|
|
||||||||
| import argparse | ||||||||
| import asyncio | ||||||||
| import json | ||||||||
| import os | ||||||||
|
|
||||||||
| try: | ||||||||
| import websockets | ||||||||
| except ImportError: | ||||||||
| print("Please install websockets: pip install websockets") | ||||||||
| raise SystemExit(1) | ||||||||
|
|
||||||||
|
|
||||||||
| async def stream_tts( | ||||||||
| url: str, | ||||||||
| text: str, | ||||||||
| config: dict, | ||||||||
| output_dir: str, | ||||||||
| simulate_stt: bool = False, | ||||||||
| stt_delay: float = 0.1, | ||||||||
| ) -> None: | ||||||||
| """Connect to the streaming TTS endpoint and process audio responses.""" | ||||||||
| os.makedirs(output_dir, exist_ok=True) | ||||||||
|
|
||||||||
| async with websockets.connect(url) as ws: | ||||||||
| # 1. Send session config | ||||||||
| config_msg = {"type": "session.config", **config} | ||||||||
| await ws.send(json.dumps(config_msg)) | ||||||||
| print(f"Sent session config: {config}") | ||||||||
|
|
||||||||
| # 2. Send text (either all at once or word-by-word) | ||||||||
| async def send_text(): | ||||||||
| if simulate_stt: | ||||||||
| words = text.split(" ") | ||||||||
| for i, word in enumerate(words): | ||||||||
| chunk = word + (" " if i < len(words) - 1 else "") | ||||||||
| await ws.send( | ||||||||
| json.dumps( | ||||||||
| { | ||||||||
| "type": "input.text", | ||||||||
| "text": chunk, | ||||||||
| } | ||||||||
| ) | ||||||||
| ) | ||||||||
| print(f" Sent: {chunk!r}") | ||||||||
| await asyncio.sleep(stt_delay) | ||||||||
| else: | ||||||||
| await ws.send( | ||||||||
| json.dumps( | ||||||||
| { | ||||||||
| "type": "input.text", | ||||||||
| "text": text, | ||||||||
| } | ||||||||
| ) | ||||||||
| ) | ||||||||
| print(f"Sent full text: {text!r}") | ||||||||
|
|
||||||||
| # 3. Signal end of input | ||||||||
| await ws.send(json.dumps({"type": "input.done"})) | ||||||||
| print("Sent input.done") | ||||||||
|
|
||||||||
| # Run sender and receiver concurrently | ||||||||
| sender_task = asyncio.create_task(send_text()) | ||||||||
|
|
||||||||
| response_format = config.get("response_format", "wav") | ||||||||
| sentence_count = 0 | ||||||||
|
|
||||||||
| try: | ||||||||
| while True: | ||||||||
| message = await ws.recv() | ||||||||
|
|
||||||||
| if isinstance(message, bytes): | ||||||||
| # Binary frame: audio data | ||||||||
| filename = os.path.join( | ||||||||
| output_dir, | ||||||||
| f"sentence_{sentence_count:03d}.{response_format}", | ||||||||
| ) | ||||||||
| with open(filename, "wb") as f: | ||||||||
| f.write(message) | ||||||||
| print(f" Saved audio: {filename} ({len(message)} bytes)") | ||||||||
| sentence_count += 1 | ||||||||
| else: | ||||||||
| # JSON frame | ||||||||
| msg = json.loads(message) | ||||||||
| msg_type = msg.get("type") | ||||||||
|
|
||||||||
| if msg_type == "audio.start": | ||||||||
| print(f" [sentence {msg['sentence_index']}] Generating: {msg['sentence_text']!r}") | ||||||||
| elif msg_type == "audio.done": | ||||||||
| print(f" [sentence {msg['sentence_index']}] Done") | ||||||||
| elif msg_type == "session.done": | ||||||||
| print(f"\nSession complete: {msg['total_sentences']} sentence(s) generated") | ||||||||
| break | ||||||||
| elif msg_type == "error": | ||||||||
| print(f" ERROR: {msg['message']}") | ||||||||
| else: | ||||||||
| print(f" Unknown message: {msg}") | ||||||||
| finally: | ||||||||
| sender_task.cancel() | ||||||||
| try: | ||||||||
| await sender_task | ||||||||
| except asyncio.CancelledError: | ||||||||
|
||||||||
| except asyncio.CancelledError: | |
| except asyncio.CancelledError: | |
| # Task cancellation is expected during shutdown; safe to ignore. |
Oops, something went wrong.
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 sentence counter is incremented on receiving binary audio data, but the actual sentence index comes from the server in the audio.start message. This creates a potential mismatch if audio.start and binary frames arrive in different orders, or if generation fails for a sentence (where audio.done is still sent but no binary frame). Consider using msg['sentence_index'] from the audio.start message to name the file instead of a local counter.