ciyidogan commited on
Commit
ac36ecd
·
verified ·
1 Parent(s): 14d9f68

Update stt/stt_google.py

Browse files
Files changed (1) hide show
  1. stt/stt_google.py +29 -68
stt/stt_google.py CHANGED
@@ -105,83 +105,44 @@ class GoogleCloudSTT(STTInterface):
105
  raise
106
 
107
  async def stop_streaming(self) -> Optional[TranscriptionResult]:
108
- """Stop streaming and clean up all resources"""
109
- if not self.is_streaming and not self.stream_thread:
110
- log_debug("Already stopped, nothing to do")
111
  return None
112
-
113
  try:
114
- log_info(f"🛑 Stopping Google STT streaming session #{self.session_id}")
115
-
116
- # Flag'i hemen kapat
117
  self.is_streaming = False
118
  self.stop_event.set()
119
-
120
- # Send poison pill to stop request generator
121
- if self.audio_queue:
122
- try:
123
- self.audio_queue.put(None)
124
- except:
125
- pass
126
-
127
- # Thread'i durdur
128
- if self.stream_thread and self.stream_thread.is_alive():
129
- log_info("⏳ Waiting for stream thread to finish...")
130
  self.stream_thread.join(timeout=5.0)
131
-
132
- if self.stream_thread.is_alive():
133
- log_warning("⚠️ STT thread did not stop gracefully after 5s")
134
- else:
135
- log_info("✅ Stream thread finished")
136
-
137
- # Final result'ı al
 
138
  final_result = None
139
- if self.responses_queue:
140
- while not self.responses_queue.empty():
141
- try:
142
- result = self.responses_queue.get_nowait()
143
- if result.is_final:
144
- final_result = result
145
- except:
146
- pass
147
-
148
- # Client'ı kapat
149
- if self.client:
150
  try:
151
- if hasattr(self.client, 'transport') and hasattr(self.client.transport, 'close'):
152
- self.client.transport.close()
153
- log_debug("✅ Client transport closed")
154
-
155
- if hasattr(self.client, '_transport') and hasattr(self.client._transport, '_grpc_channel'):
156
- self.client._transport._grpc_channel.close()
157
- log_debug("✅ gRPC channel closed")
158
- except Exception as e:
159
- log_warning(f"⚠️ Error closing Google client: {e}")
160
- finally:
161
- self.client = None
162
-
163
- # Queue'ları None yap
164
- self.audio_queue = None
165
- self.responses_queue = None
166
-
167
- # Diğer değişkenleri resetle
168
- self.stream_thread = None
169
- self.streaming_config = None
170
- self.stop_event.clear()
171
-
172
- log_info(f"✅ Google STT streaming session #{self.session_id} stopped and cleaned")
173
  return final_result
174
-
175
  except Exception as e:
176
- log_error(f"❌ Error during stop_streaming", error=str(e))
177
- # Force cleanup on error
178
- self.is_streaming = False
179
- self.stream_thread = None
180
- self.client = None
181
- self.streaming_config = None
182
- self.stop_event.clear()
183
- self.audio_queue = None
184
- self.responses_queue = None
185
  return None
186
 
187
  def supports_realtime(self) -> bool:
 
105
  raise
106
 
107
  async def stop_streaming(self) -> Optional[TranscriptionResult]:
108
+ """Stop streaming and get final result"""
109
+ if not self.is_streaming:
 
110
  return None
111
+
112
  try:
113
+ log_info("🛑 Stopping Google STT streaming...")
114
+
 
115
  self.is_streaming = False
116
  self.stop_event.set()
117
+
118
+ # Send poison pill to queue
119
+ self.audio_queue.put(None)
120
+
121
+ # Wait for thread to finish
122
+ if self.stream_thread:
 
 
 
 
 
123
  self.stream_thread.join(timeout=5.0)
124
+
125
+ # Clear queues
126
+ while not self.audio_queue.empty():
127
+ try:
128
+ self.audio_queue.get_nowait()
129
+ except queue.Empty:
130
+ break
131
+
132
  final_result = None
133
+ while not self.responses_queue.empty():
 
 
 
 
 
 
 
 
 
 
134
  try:
135
+ result = self.responses_queue.get_nowait() # get_nowait() kullan, await değil
136
+ if result.is_final:
137
+ final_result = result
138
+ except queue.Empty:
139
+ break
140
+
141
+ log_info("✅ Google STT streaming stopped")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  return final_result
143
+
144
  except Exception as e:
145
+ log_error(f"❌ Failed to stop Google STT streaming", error=str(e))
 
 
 
 
 
 
 
 
146
  return None
147
 
148
  def supports_realtime(self) -> bool: