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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
import base64
|
| 5 |
+
import pathlib
|
| 6 |
+
import threading
|
| 7 |
+
import tempfile
|
| 8 |
+
from dashscope.audio.qwen_tts_realtime import QwenTtsRealtime, QwenTtsRealtimeCallback, AudioFormat
|
| 9 |
+
import dashscope
|
| 10 |
+
import wave
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
# ======= Constants Configuration =======
|
| 14 |
+
DEFAULT_TARGET_MODEL = "qwen3-tts-vc-realtime-2025-11-27"
|
| 15 |
+
DEFAULT_PREFERRED_NAME = "custom_voice"
|
| 16 |
+
DEFAULT_AUDIO_MIME_TYPE = "audio/wav"
|
| 17 |
+
|
| 18 |
+
def init_dashscope_api_key():
|
| 19 |
+
"""Initialize the API key for dashscope SDK"""
|
| 20 |
+
api_key = os.environ['API_KEY']
|
| 21 |
+
if not api_key:
|
| 22 |
+
raise ValueError("Please set the environment variable DASHSCOPE_API_KEY")
|
| 23 |
+
dashscope.api_key = api_key
|
| 24 |
+
return api_key
|
| 25 |
+
|
| 26 |
+
def create_voice(file_path: str,
|
| 27 |
+
target_model: str = DEFAULT_TARGET_MODEL,
|
| 28 |
+
preferred_name: str = DEFAULT_PREFERRED_NAME,
|
| 29 |
+
audio_mime_type: str = DEFAULT_AUDIO_MIME_TYPE) -> str:
|
| 30 |
+
"""Create voice and return the voice parameter"""
|
| 31 |
+
api_key = os.environ['API_KEY']
|
| 32 |
+
|
| 33 |
+
file_path_obj = pathlib.Path(file_path)
|
| 34 |
+
if not file_path_obj.exists():
|
| 35 |
+
raise FileNotFoundError(f"Audio file not found: {file_path}")
|
| 36 |
+
|
| 37 |
+
base64_str = base64.b64encode(file_path_obj.read_bytes()).decode()
|
| 38 |
+
data_uri = f"data:{audio_mime_type};base64,{base64_str}"
|
| 39 |
+
|
| 40 |
+
url = "https://dashscope.aliyuncs.com/api/v1/services/audio/tts/customization"
|
| 41 |
+
payload = {
|
| 42 |
+
"model": "qwen-voice-enrollment",
|
| 43 |
+
"input": {
|
| 44 |
+
"action": "create",
|
| 45 |
+
"target_model": target_model,
|
| 46 |
+
"preferred_name": preferred_name,
|
| 47 |
+
"audio": {"data": data_uri}
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
headers = {
|
| 51 |
+
"Authorization": f"Bearer {api_key}",
|
| 52 |
+
"Content-Type": "application/json"
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
# Create session and configure retry and SSL
|
| 56 |
+
session = requests.Session()
|
| 57 |
+
# If SSL errors persist, temporarily change to False (for testing only)
|
| 58 |
+
session.verify = True # Enable SSL verification
|
| 59 |
+
|
| 60 |
+
# Configure retry strategy
|
| 61 |
+
from requests.adapters import HTTPAdapter
|
| 62 |
+
from urllib3.util.retry import Retry
|
| 63 |
+
|
| 64 |
+
retry_strategy = Retry(
|
| 65 |
+
total=3,
|
| 66 |
+
backoff_factor=1,
|
| 67 |
+
status_forcelist=[429, 500, 502, 503, 504],
|
| 68 |
+
allowed_methods=["POST"]
|
| 69 |
+
)
|
| 70 |
+
adapter = HTTPAdapter(max_retries=retry_strategy)
|
| 71 |
+
session.mount("https://", adapter)
|
| 72 |
+
session.mount("http://", adapter)
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
resp = session.post(url, json=payload, headers=headers, timeout=60)
|
| 76 |
+
if resp.status_code != 200:
|
| 77 |
+
raise RuntimeError(f"Failed to create voice: {resp.status_code}, {resp.text}")
|
| 78 |
+
|
| 79 |
+
return resp.json()["output"]["voice"]
|
| 80 |
+
except requests.exceptions.SSLError as e:
|
| 81 |
+
raise RuntimeError(f"SSL connection error: {e}. Please check network environment or try using a proxy")
|
| 82 |
+
except requests.exceptions.Timeout as e:
|
| 83 |
+
raise RuntimeError(f"Request timeout: {e}")
|
| 84 |
+
except (KeyError, ValueError) as e:
|
| 85 |
+
raise RuntimeError(f"Failed to parse voice response: {e}")
|
| 86 |
+
finally:
|
| 87 |
+
session.close()
|
| 88 |
+
|
| 89 |
+
class TTSCallback(QwenTtsRealtimeCallback):
|
| 90 |
+
"""TTS streaming callback for collecting audio data"""
|
| 91 |
+
def __init__(self):
|
| 92 |
+
self.complete_event = threading.Event()
|
| 93 |
+
self.audio_chunks = []
|
| 94 |
+
self.error_msg = None
|
| 95 |
+
|
| 96 |
+
def on_open(self) -> None:
|
| 97 |
+
print('[TTS] Connection established')
|
| 98 |
+
|
| 99 |
+
def on_close(self, close_status_code, close_msg) -> None:
|
| 100 |
+
print(f'[TTS] Connection closed code={close_status_code}, msg={close_msg}')
|
| 101 |
+
|
| 102 |
+
def on_event(self, response: dict) -> None:
|
| 103 |
+
try:
|
| 104 |
+
event_type = response.get('type', '')
|
| 105 |
+
if event_type == 'session.created':
|
| 106 |
+
print(f'[TTS] Session started: {response["session"]["id"]}')
|
| 107 |
+
elif event_type == 'response.audio.delta':
|
| 108 |
+
audio_data = base64.b64decode(response['delta'])
|
| 109 |
+
self.audio_chunks.append(audio_data)
|
| 110 |
+
elif event_type == 'response.done':
|
| 111 |
+
print('[TTS] Response completed')
|
| 112 |
+
elif event_type == 'session.finished':
|
| 113 |
+
print('[TTS] Session finished')
|
| 114 |
+
self.complete_event.set()
|
| 115 |
+
except Exception as e:
|
| 116 |
+
self.error_msg = str(e)
|
| 117 |
+
print(f'[Error] Exception while processing callback event: {e}')
|
| 118 |
+
self.complete_event.set()
|
| 119 |
+
|
| 120 |
+
def wait_for_finished(self):
|
| 121 |
+
self.complete_event.wait()
|
| 122 |
+
|
| 123 |
+
def get_audio_data(self):
|
| 124 |
+
"""Return the synthesized audio data"""
|
| 125 |
+
return b''.join(self.audio_chunks)
|
| 126 |
+
|
| 127 |
+
def synthesize_speech(audio_file, text_input):
|
| 128 |
+
"""
|
| 129 |
+
Main function for speech synthesis
|
| 130 |
+
|
| 131 |
+
Args:
|
| 132 |
+
audio_file: Path to the recorded audio file (from Gradio audio component)
|
| 133 |
+
text_input: Text to synthesize
|
| 134 |
+
|
| 135 |
+
Returns:
|
| 136 |
+
Path to the synthesized audio file
|
| 137 |
+
"""
|
| 138 |
+
try:
|
| 139 |
+
if not audio_file:
|
| 140 |
+
return None, "β Please record a voice sample first"
|
| 141 |
+
|
| 142 |
+
if not text_input or text_input.strip() == "":
|
| 143 |
+
return None, "β Please enter the text to synthesize"
|
| 144 |
+
|
| 145 |
+
# Initialize API Key
|
| 146 |
+
init_dashscope_api_key()
|
| 147 |
+
|
| 148 |
+
# Create voice clone
|
| 149 |
+
status_msg = "π€ Creating voice clone..."
|
| 150 |
+
print(status_msg)
|
| 151 |
+
voice_id = create_voice(audio_file, audio_mime_type="audio/wav")
|
| 152 |
+
|
| 153 |
+
# Initialize TTS
|
| 154 |
+
status_msg = "π Synthesizing speech..."
|
| 155 |
+
print(status_msg)
|
| 156 |
+
callback = TTSCallback()
|
| 157 |
+
qwen_tts_realtime = QwenTtsRealtime(
|
| 158 |
+
model=DEFAULT_TARGET_MODEL,
|
| 159 |
+
callback=callback,
|
| 160 |
+
url='wss://dashscope.aliyuncs.com/api-ws/v1/realtime'
|
| 161 |
+
)
|
| 162 |
+
qwen_tts_realtime.connect()
|
| 163 |
+
|
| 164 |
+
# Update session configuration
|
| 165 |
+
qwen_tts_realtime.update_session(
|
| 166 |
+
voice=voice_id,
|
| 167 |
+
response_format=AudioFormat.PCM_24000HZ_MONO_16BIT,
|
| 168 |
+
mode='server_commit'
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
# Send text
|
| 172 |
+
qwen_tts_realtime.append_text(text_input)
|
| 173 |
+
qwen_tts_realtime.finish()
|
| 174 |
+
|
| 175 |
+
# Wait for completion
|
| 176 |
+
callback.wait_for_finished()
|
| 177 |
+
|
| 178 |
+
if callback.error_msg:
|
| 179 |
+
return None, f"β Synthesis failed: {callback.error_msg}"
|
| 180 |
+
|
| 181 |
+
# Get audio data and save as WAV file
|
| 182 |
+
audio_data = callback.get_audio_data()
|
| 183 |
+
|
| 184 |
+
if not audio_data:
|
| 185 |
+
return None, "β No audio data generated"
|
| 186 |
+
|
| 187 |
+
# Create temporary file to save audio
|
| 188 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file:
|
| 189 |
+
output_path = tmp_file.name
|
| 190 |
+
|
| 191 |
+
# Write WAV file header
|
| 192 |
+
with wave.open(output_path, 'wb') as wav_file:
|
| 193 |
+
wav_file.setnchannels(1) # Mono
|
| 194 |
+
wav_file.setsampwidth(2) # 16bit
|
| 195 |
+
wav_file.setframerate(24000) # 24kHz
|
| 196 |
+
wav_file.writeframes(audio_data)
|
| 197 |
+
|
| 198 |
+
success_msg = f"β
Synthesis successful! Session ID: {qwen_tts_realtime.get_session_id()}"
|
| 199 |
+
print(success_msg)
|
| 200 |
+
return output_path, success_msg
|
| 201 |
+
|
| 202 |
+
except Exception as e:
|
| 203 |
+
error_msg = f"β An error occurred: {str(e)}"
|
| 204 |
+
print(error_msg)
|
| 205 |
+
return None, error_msg
|
| 206 |
+
|
| 207 |
+
# ======= Gradio Interface =======
|
| 208 |
+
def create_gradio_interface():
|
| 209 |
+
"""Create Gradio interface"""
|
| 210 |
+
|
| 211 |
+
with gr.Blocks(title="Qwen Voice Cloning and Synthesis", theme=gr.themes.Soft()) as demo:
|
| 212 |
+
gr.Markdown("""
|
| 213 |
+
# ποΈ Qwen Voice Cloning and Synthesis
|
| 214 |
+
|
| 215 |
+
**Usage Steps:**
|
| 216 |
+
1. Click the microphone icon to record a voice sample (recommended 10-30 seconds, clear and natural)
|
| 217 |
+
2. Enter the text content to synthesize
|
| 218 |
+
3. Click the "Start Synthesis" button
|
| 219 |
+
4. Wait for synthesis to complete, then play or download the result
|
| 220 |
+
|
| 221 |
+
**Notes:**
|
| 222 |
+
- Please ensure the environment variable `DASHSCOPE_API_KEY` is set
|
| 223 |
+
- Better recording quality leads to better synthesis results
|
| 224 |
+
""")
|
| 225 |
+
|
| 226 |
+
with gr.Row():
|
| 227 |
+
with gr.Column(scale=1):
|
| 228 |
+
gr.Markdown("### Step 1: Record Voice Sample")
|
| 229 |
+
audio_input = gr.Audio(
|
| 230 |
+
sources=["microphone"],
|
| 231 |
+
type="filepath",
|
| 232 |
+
label="Record Voice",
|
| 233 |
+
format="wav"
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
gr.Markdown("### Step 2: Enter Text to Synthesize")
|
| 237 |
+
text_input = gr.Textbox(
|
| 238 |
+
label="Text to Synthesize",
|
| 239 |
+
placeholder="Please enter the text content to synthesize...",
|
| 240 |
+
lines=5,
|
| 241 |
+
value="Hello, this is a voice synthesized using voice cloning technology."
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
submit_btn = gr.Button("π΅ Start Synthesis", variant="primary", size="lg")
|
| 245 |
+
|
| 246 |
+
with gr.Column(scale=1):
|
| 247 |
+
gr.Markdown("### Synthesis Result")
|
| 248 |
+
status_output = gr.Textbox(
|
| 249 |
+
label="Status Information",
|
| 250 |
+
interactive=False,
|
| 251 |
+
lines=2
|
| 252 |
+
)
|
| 253 |
+
audio_output = gr.Audio(
|
| 254 |
+
label="Synthesized Voice",
|
| 255 |
+
type="filepath"
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
# Bind events
|
| 259 |
+
submit_btn.click(
|
| 260 |
+
fn=synthesize_speech,
|
| 261 |
+
inputs=[audio_input, text_input],
|
| 262 |
+
outputs=[audio_output, status_output]
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
+
gr.Markdown("""
|
| 266 |
+
---
|
| 267 |
+
π‘ **Tip:** For better results, please ensure a quiet recording environment and clear, natural pronunciation.
|
| 268 |
+
""")
|
| 269 |
+
|
| 270 |
+
return demo
|
| 271 |
+
|
| 272 |
+
if __name__ == "__main__":
|
| 273 |
+
# Check API Key
|
| 274 |
+
try:
|
| 275 |
+
init_dashscope_api_key()
|
| 276 |
+
print("β
API Key verified successfully")
|
| 277 |
+
except ValueError as e:
|
| 278 |
+
print(f"β οΈ Warning: {e}")
|
| 279 |
+
print("Please set the environment variable: export DASHSCOPE_API_KEY='your-api-key'")
|
| 280 |
+
|
| 281 |
+
demo = create_gradio_interface()
|
| 282 |
+
demo.launch(
|
| 283 |
+
server_name="0.0.0.0",
|
| 284 |
+
server_port=7860,
|
| 285 |
+
share=False
|
| 286 |
+
)
|