Spaces:
Building
Building
Update stt/stt_google.py
Browse files- stt/stt_google.py +131 -0
stt/stt_google.py
CHANGED
@@ -65,6 +65,137 @@ class GoogleCloudSTT(STTInterface):
|
|
65 |
self.total_audio_bytes = 0
|
66 |
self.total_chunks = 0
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
def _reset_session_data(self):
|
69 |
"""Reset all session-specific data"""
|
70 |
# Queue'ları temizle
|
|
|
65 |
self.total_audio_bytes = 0
|
66 |
self.total_chunks = 0
|
67 |
|
68 |
+
async def stream_audio(self, audio_chunk: bytes) -> AsyncIterator[TranscriptionResult]:
|
69 |
+
"""Stream audio chunk and get transcription results"""
|
70 |
+
if not self.is_streaming:
|
71 |
+
log_error(f"❌ STT not streaming - is_streaming: {self.is_streaming}")
|
72 |
+
raise RuntimeError("Streaming not started. Call start_streaming() first.")
|
73 |
+
|
74 |
+
try:
|
75 |
+
# Put audio in queue for streaming thread
|
76 |
+
self.audio_queue.put(audio_chunk)
|
77 |
+
|
78 |
+
# Check for any results in queue
|
79 |
+
while True:
|
80 |
+
try:
|
81 |
+
# Non-blocking get from queue
|
82 |
+
result = self.responses_queue.get_nowait()
|
83 |
+
yield result
|
84 |
+
except queue.Empty:
|
85 |
+
# No more results in queue
|
86 |
+
break
|
87 |
+
|
88 |
+
except Exception as e:
|
89 |
+
log_error(f"❌ Google STT streaming error", error=str(e))
|
90 |
+
self.is_streaming = False
|
91 |
+
raise
|
92 |
+
|
93 |
+
async def stop_streaming(self) -> Optional[TranscriptionResult]:
|
94 |
+
"""Stop streaming and clean up all resources"""
|
95 |
+
if not self.is_streaming and not self.stream_thread:
|
96 |
+
log_debug("Already stopped, nothing to do")
|
97 |
+
return None
|
98 |
+
|
99 |
+
try:
|
100 |
+
log_info(f"🛑 Stopping Google STT streaming session #{self.session_id}")
|
101 |
+
|
102 |
+
# Flag'i hemen kapat
|
103 |
+
self.is_streaming = False
|
104 |
+
self.stop_event.set()
|
105 |
+
|
106 |
+
# Send poison pill to stop request generator
|
107 |
+
if self.audio_queue:
|
108 |
+
try:
|
109 |
+
self.audio_queue.put(None)
|
110 |
+
except:
|
111 |
+
pass
|
112 |
+
|
113 |
+
# Thread'i durdur
|
114 |
+
if self.stream_thread and self.stream_thread.is_alive():
|
115 |
+
log_info("⏳ Waiting for stream thread to finish...")
|
116 |
+
self.stream_thread.join(timeout=5.0)
|
117 |
+
|
118 |
+
if self.stream_thread.is_alive():
|
119 |
+
log_warning("⚠️ STT thread did not stop gracefully after 5s")
|
120 |
+
else:
|
121 |
+
log_info("✅ Stream thread finished")
|
122 |
+
|
123 |
+
# Final result'ı al
|
124 |
+
final_result = None
|
125 |
+
if self.responses_queue:
|
126 |
+
while not self.responses_queue.empty():
|
127 |
+
try:
|
128 |
+
result = self.responses_queue.get_nowait()
|
129 |
+
if result.is_final:
|
130 |
+
final_result = result
|
131 |
+
except:
|
132 |
+
pass
|
133 |
+
|
134 |
+
# Client'ı kapat
|
135 |
+
if self.client:
|
136 |
+
try:
|
137 |
+
if hasattr(self.client, 'transport') and hasattr(self.client.transport, 'close'):
|
138 |
+
self.client.transport.close()
|
139 |
+
log_debug("✅ Client transport closed")
|
140 |
+
|
141 |
+
if hasattr(self.client, '_transport') and hasattr(self.client._transport, '_grpc_channel'):
|
142 |
+
self.client._transport._grpc_channel.close()
|
143 |
+
log_debug("✅ gRPC channel closed")
|
144 |
+
except Exception as e:
|
145 |
+
log_warning(f"⚠️ Error closing Google client: {e}")
|
146 |
+
finally:
|
147 |
+
self.client = None
|
148 |
+
|
149 |
+
# Queue'ları None yap
|
150 |
+
self.audio_queue = None
|
151 |
+
self.responses_queue = None
|
152 |
+
|
153 |
+
# Diğer değişkenleri resetle
|
154 |
+
self.stream_thread = None
|
155 |
+
self.streaming_config = None
|
156 |
+
self.stop_event.clear()
|
157 |
+
|
158 |
+
log_info(f"✅ Google STT streaming session #{self.session_id} stopped and cleaned")
|
159 |
+
return final_result
|
160 |
+
|
161 |
+
except Exception as e:
|
162 |
+
log_error(f"❌ Error during stop_streaming", error=str(e))
|
163 |
+
# Force cleanup on error
|
164 |
+
self.is_streaming = False
|
165 |
+
self.stream_thread = None
|
166 |
+
self.client = None
|
167 |
+
self.streaming_config = None
|
168 |
+
self.stop_event.clear()
|
169 |
+
self.audio_queue = None
|
170 |
+
self.responses_queue = None
|
171 |
+
return None
|
172 |
+
|
173 |
+
def supports_realtime(self) -> bool:
|
174 |
+
"""Google Cloud STT supports real-time streaming"""
|
175 |
+
return True
|
176 |
+
|
177 |
+
def get_supported_languages(self) -> List[str]:
|
178 |
+
"""Get list of supported language codes"""
|
179 |
+
return [
|
180 |
+
"tr-TR", # Turkish
|
181 |
+
"en-US", # English (US)
|
182 |
+
"en-GB", # English (UK)
|
183 |
+
"de-DE", # German
|
184 |
+
"fr-FR", # French
|
185 |
+
"es-ES", # Spanish
|
186 |
+
"it-IT", # Italian
|
187 |
+
"pt-BR", # Portuguese (Brazil)
|
188 |
+
"ru-RU", # Russian
|
189 |
+
"ja-JP", # Japanese
|
190 |
+
"ko-KR", # Korean
|
191 |
+
"zh-CN", # Chinese (Simplified)
|
192 |
+
"ar-SA", # Arabic
|
193 |
+
]
|
194 |
+
|
195 |
+
def get_provider_name(self) -> str:
|
196 |
+
"""Get provider name"""
|
197 |
+
return "google"
|
198 |
+
|
199 |
def _reset_session_data(self):
|
200 |
"""Reset all session-specific data"""
|
201 |
# Queue'ları temizle
|