Spaces:
Sleeping
Sleeping
| import numpy as np | |
| from google.cloud import texttospeech as tts | |
| from .utils import setup_gcp_credentials | |
| # --- GCP Credential Setup --- | |
| setup_gcp_credentials() | |
| # --- TTS Client and Configuration --- | |
| try: | |
| client_tts = tts.TextToSpeechClient() | |
| voice_name = "Zephyr" | |
| language_code = "th-TH" | |
| streaming_config = tts.StreamingSynthesizeConfig( | |
| voice=tts.VoiceSelectionParams(language_code=language_code, name=voice_name,model_name="gemini-2.5-flash-tts"), | |
| streaming_audio_config = tts.StreamingAudioConfig(speaking_rate = 1.20) | |
| ) | |
| print("Google TTS Client initialized.") | |
| except Exception as e: | |
| client_tts = None | |
| print(f"Failed to initialize Google TTS Client: {e}") | |
| def _request_generator(text): | |
| """Generator for TTS streaming requests.""" | |
| yield tts.StreamingSynthesizeRequest(streaming_config=streaming_config) | |
| yield tts.StreamingSynthesizeRequest(input=tts.StreamingSynthesisInput(text=text)) | |
| import collections | |
| BUFFER_SIZE_IN_CHUNKS = 100 | |
| def synthesize_text(text: str, lang = 'th' , speed = 2.0): | |
| """ | |
| Synthesizes text using Google Cloud Text-to-Speech streaming synthesis. | |
| This function yields (sample_rate, audio_chunk) tuples. | |
| """ | |
| if not client_tts: | |
| print("TTS client not available. Skipping synthesis.") | |
| return | |
| # Clean and preprocess text for better pronunciation | |
| text = text.translate(str.maketrans('', '', ':*!\"\'()')) | |
| replacements = { | |
| '1577': 'หนึ่งห้าเจ็ดเจ็ด', ' 2.': 'สอง.', '/n2.': ' สอง.', ' 3.': ' สาม.', '/n3.': ' สาม.', | |
| ' 4.': ' สี่.', '/n4.': ' สี่.', ' 10.': ' สิบ.', '/n10.': ' สิบ.', | |
| 'พ.ศ.': 'พอศอ', '. ': ' ', '-19': ' 19', 'เพื่อยก': 'เพื่อ ยก', | |
| '√': 'เครื่องหมายติ๊กถูก', '=>': 'จากนั้นเลือก', 'รอกด': 'รอ กด', ' ณ ': ' นะ ', | |
| '[2ฟรี1]': 'สองฟรีหนึ่ง', "+": 'บวก', "12X": "สิบสองเอ็กซ์", "12x": "สิบสองเอ็กซ์",'[2ฟร2]': 'สองฟรีสอง', | |
| "/": "ทับ", 'leading name': '', " | ": " ","|":"" | |
| } | |
| for old, new in replacements.items(): | |
| text = text.replace(old, new) | |
| print(f"TTS input text: {text}") | |
| if text.endswith('.'): | |
| text = text[:-1] | |
| if not text.strip(): | |
| return | |
| try: | |
| responses = client_tts.streaming_synthesize(_request_generator(text)) | |
| audio_buffer = collections.deque() | |
| response_iterator = iter(responses) | |
| for _ in range(BUFFER_SIZE_IN_CHUNKS): | |
| try: | |
| response = next(response_iterator) | |
| if response.audio_content: | |
| audio_buffer.append(response.audio_content) | |
| except StopIteration: | |
| break | |
| while True: | |
| # Yield chunk แรกออกจาก buffer | |
| if audio_buffer: | |
| chunk_to_yield = audio_buffer.popleft() | |
| samples = np.frombuffer(chunk_to_yield, dtype=np.int16) | |
| yield (24000, samples) | |
| # เติม chunk ใหม่เข้าไปใน buffer | |
| try: | |
| response = next(response_iterator) | |
| if response.audio_content: | |
| audio_buffer.append(response.audio_content) | |
| except StopIteration: | |
| # ถ้า stream จบแล้ว แต่ยังมีของใน buffer ให้ yield จนหมด | |
| while audio_buffer: | |
| chunk_to_yield = audio_buffer.popleft() | |
| samples = np.frombuffer(chunk_to_yield, dtype=np.int16) | |
| yield (24000, samples) | |
| break # ออกจาก loop หลัก | |
| except Exception as e: | |
| print(f"Error during TTS synthesis for text '{text}': {e}") | |
| if __name__ == "__main__": | |
| for a,b in synthesize_text("สวัสดี"): | |
| print(b) | |