Spaces:
Building
Building
Update stt/stt_google.py
Browse files- stt/stt_google.py +69 -58
stt/stt_google.py
CHANGED
@@ -27,44 +27,47 @@ from .stt_interface import STTInterface, STTConfig, TranscriptionResult
|
|
27 |
class GoogleCloudSTT(STTInterface):
|
28 |
"""Google Cloud Speech-to-Text implementation"""
|
29 |
|
30 |
-
def __init__(self
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_path
|
36 |
-
log_info(f"✅ Google credentials set from: {credentials_path}")
|
37 |
-
|
38 |
-
# Test credential'ları
|
39 |
-
try:
|
40 |
-
# Client'ı burada oluşturma, her seferinde yeni instance oluştur
|
41 |
-
test_client = speech.SpeechClient()
|
42 |
-
log_info("🔐 Testing Google credentials...")
|
43 |
-
log_info("✅ Google credentials valid")
|
44 |
-
# Test client'ı kapat
|
45 |
-
if hasattr(test_client, 'transport') and hasattr(test_client.transport, 'close'):
|
46 |
-
test_client.transport.close()
|
47 |
-
except Exception as e:
|
48 |
-
log_error(f"❌ Google credentials error", error=str(e))
|
49 |
-
raise
|
50 |
-
else:
|
51 |
-
log_error(f"❌ Google credentials path not found: {credentials_path}")
|
52 |
-
raise FileNotFoundError(f"Credentials file not found: {credentials_path}")
|
53 |
-
|
54 |
-
# Client'ı burada oluşturma, start_streaming'de oluştur
|
55 |
self.client = None
|
56 |
self.streaming_config = None
|
57 |
-
self.is_streaming = False
|
58 |
-
self.audio_queue = None # Queue'ları None olarak başlat
|
59 |
-
self.responses_queue = None
|
60 |
self.stream_thread = None
|
61 |
-
self.
|
62 |
-
self.
|
63 |
-
|
64 |
-
|
|
|
65 |
self.session_id = 0
|
66 |
-
self.
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
def _get_encoding(self, encoding_str: str):
|
70 |
"""Convert encoding string to Google Speech enum"""
|
@@ -248,31 +251,37 @@ class GoogleCloudSTT(STTInterface):
|
|
248 |
"""Get provider name"""
|
249 |
return "google"
|
250 |
|
251 |
-
def
|
252 |
-
"""Reset
|
253 |
-
#
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
self.total_audio_bytes = 0
|
270 |
-
self.total_chunks = 0
|
271 |
-
|
272 |
-
# Yeni session ID
|
273 |
self.session_id += 1
|
274 |
-
|
|
|
|
|
|
|
|
|
|
|
275 |
log_info(f"🔄 Google STT session data reset. New session ID: {self.session_id}")
|
|
|
|
|
|
|
|
|
|
|
276 |
|
277 |
def _create_fresh_queues(self):
|
278 |
"""Create fresh queue instances"""
|
@@ -308,6 +317,8 @@ class GoogleCloudSTT(STTInterface):
|
|
308 |
|
309 |
# Session verilerini resetle ve ID'yi artır
|
310 |
self._reset_session_data()
|
|
|
|
|
311 |
|
312 |
log_info(f"🎤 Starting Google STT streaming session #{self.session_id} with config: {config}")
|
313 |
|
@@ -329,7 +340,7 @@ class GoogleCloudSTT(STTInterface):
|
|
329 |
encoding=config.get("encoding", "WEBM_OPUS"),
|
330 |
enable_punctuation=config.get("enable_punctuation", True),
|
331 |
interim_results=config.get("interim_results", False),
|
332 |
-
single_utterance=
|
333 |
)
|
334 |
else:
|
335 |
stt_config = config
|
|
|
27 |
class GoogleCloudSTT(STTInterface):
|
28 |
"""Google Cloud Speech-to-Text implementation"""
|
29 |
|
30 |
+
def __init__(self):
|
31 |
+
"""Initialize Google Cloud STT"""
|
32 |
+
log_info("🎤 Creating STT provider: google")
|
33 |
+
|
34 |
+
# Initialize all required attributes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
self.client = None
|
36 |
self.streaming_config = None
|
|
|
|
|
|
|
37 |
self.stream_thread = None
|
38 |
+
self.audio_queue = queue.Queue()
|
39 |
+
self.responses_queue = queue.Queue()
|
40 |
+
self.is_streaming = False
|
41 |
+
self.should_stop = False
|
42 |
+
self.error_message = None
|
43 |
self.session_id = 0
|
44 |
+
self.stream_start_time = None
|
45 |
+
|
46 |
+
# ✅ Eksik attribute'ları ekleyelim
|
47 |
+
self.lock = threading.Lock() # Thread lock
|
48 |
+
self.single_utterance = False # Default value
|
49 |
+
self.chunk_count = 0 # Audio chunk counter
|
50 |
+
self.total_bytes = 0 # Total bytes received
|
51 |
+
|
52 |
+
# Check Google credentials
|
53 |
+
creds_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
|
54 |
+
if not creds_path:
|
55 |
+
# Try default location
|
56 |
+
creds_path = "./credentials/google-service-account.json"
|
57 |
+
if os.path.exists(creds_path):
|
58 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = creds_path
|
59 |
+
log_info(f"✅ Google credentials set from: {creds_path}")
|
60 |
+
else:
|
61 |
+
raise ValueError("Google credentials not found. Please set GOOGLE_APPLICATION_CREDENTIALS")
|
62 |
+
|
63 |
+
# Test credentials
|
64 |
+
try:
|
65 |
+
log_info("🔐 Testing Google credentials...")
|
66 |
+
test_client = speech.SpeechClient()
|
67 |
+
log_info("✅ Google credentials valid")
|
68 |
+
except Exception as e:
|
69 |
+
log_error(f"❌ Invalid Google credentials: {e}")
|
70 |
+
raise
|
71 |
|
72 |
def _get_encoding(self, encoding_str: str):
|
73 |
"""Convert encoding string to Google Speech enum"""
|
|
|
251 |
"""Get provider name"""
|
252 |
return "google"
|
253 |
|
254 |
+
def _reset_session(self):
|
255 |
+
"""Reset session data"""
|
256 |
+
# Clear queues
|
257 |
+
while not self.audio_queue.empty():
|
258 |
+
try:
|
259 |
+
self.audio_queue.get_nowait()
|
260 |
+
except queue.Empty:
|
261 |
+
break
|
262 |
+
|
263 |
+
while not self.responses_queue.empty():
|
264 |
+
try:
|
265 |
+
self.responses_queue.get_nowait()
|
266 |
+
except queue.Empty:
|
267 |
+
break
|
268 |
+
|
269 |
+
# Reset state
|
270 |
+
self.should_stop = False
|
271 |
+
self.error_message = None
|
|
|
|
|
|
|
|
|
272 |
self.session_id += 1
|
273 |
+
self.stream_start_time = time.time()
|
274 |
+
|
275 |
+
# ✅ Counter'ları sıfırla
|
276 |
+
self.chunk_count = 0
|
277 |
+
self.total_bytes = 0
|
278 |
+
|
279 |
log_info(f"🔄 Google STT session data reset. New session ID: {self.session_id}")
|
280 |
+
|
281 |
+
# Create fresh queues to be extra safe
|
282 |
+
self.audio_queue = queue.Queue()
|
283 |
+
self.responses_queue = queue.Queue()
|
284 |
+
log_debug("✅ Created fresh queues")
|
285 |
|
286 |
def _create_fresh_queues(self):
|
287 |
"""Create fresh queue instances"""
|
|
|
317 |
|
318 |
# Session verilerini resetle ve ID'yi artır
|
319 |
self._reset_session_data()
|
320 |
+
|
321 |
+
self.single_utterance = config.get("single_utterance", True)
|
322 |
|
323 |
log_info(f"🎤 Starting Google STT streaming session #{self.session_id} with config: {config}")
|
324 |
|
|
|
340 |
encoding=config.get("encoding", "WEBM_OPUS"),
|
341 |
enable_punctuation=config.get("enable_punctuation", True),
|
342 |
interim_results=config.get("interim_results", False),
|
343 |
+
single_utterance=self.single_utterance
|
344 |
)
|
345 |
else:
|
346 |
stt_config = config
|