Spaces:
Running
Running
Update stt_factory.py
Browse files- stt_factory.py +89 -48
stt_factory.py
CHANGED
@@ -5,59 +5,100 @@ from typing import Optional
|
|
5 |
from stt_interface import STTInterface, STTEngineType, log
|
6 |
from stt_google import GoogleCloudSTT
|
7 |
from config_provider import ConfigProvider
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
class STTFactory:
|
10 |
-
"""Factory for creating STT
|
11 |
|
12 |
@staticmethod
|
13 |
def create_provider() -> Optional[STTInterface]:
|
14 |
"""Create STT provider based on configuration"""
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
log("
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
52 |
|
53 |
@staticmethod
|
54 |
-
def
|
55 |
-
"""Get
|
56 |
-
|
57 |
-
return None
|
58 |
-
|
59 |
-
if stt_config.api_key.startswith("enc:"):
|
60 |
-
from encryption_utils import decrypt
|
61 |
-
return decrypt(stt_config.api_key)
|
62 |
-
|
63 |
-
return stt_config.api_key
|
|
|
5 |
from stt_interface import STTInterface, STTEngineType, log
|
6 |
from stt_google import GoogleCloudSTT
|
7 |
from config_provider import ConfigProvider
|
8 |
+
from stt_interface import STTInterface
|
9 |
|
10 |
+
# Import providers conditionally
|
11 |
+
stt_providers = {}
|
12 |
+
|
13 |
+
try:
|
14 |
+
from stt_google import GoogleCloudSTT
|
15 |
+
stt_providers['google'] = GoogleCloudSTT
|
16 |
+
except ImportError:
|
17 |
+
log("⚠️ Google Cloud STT not available")
|
18 |
+
|
19 |
+
try:
|
20 |
+
from stt_azure import AzureSTT
|
21 |
+
stt_providers['azure'] = AzureSTT
|
22 |
+
except ImportError:
|
23 |
+
log("⚠️ Azure STT not available")
|
24 |
+
|
25 |
+
try:
|
26 |
+
from stt_flicker import FlickerSTT
|
27 |
+
stt_providers['flicker'] = FlickerSTT
|
28 |
+
except ImportError:
|
29 |
+
log("⚠️ Flicker STT not available")
|
30 |
+
|
31 |
+
class NoSTT(STTInterface):
|
32 |
+
"""Dummy STT provider when STT is disabled"""
|
33 |
+
|
34 |
+
async def start_streaming(self, config) -> None:
|
35 |
+
pass
|
36 |
+
|
37 |
+
async def stream_audio(self, audio_chunk: bytes):
|
38 |
+
return
|
39 |
+
yield # Make it a generator
|
40 |
+
|
41 |
+
async def stop_streaming(self):
|
42 |
+
return None
|
43 |
+
|
44 |
+
def supports_realtime(self) -> bool:
|
45 |
+
return False
|
46 |
+
|
47 |
+
def get_supported_languages(self):
|
48 |
+
return []
|
49 |
+
|
50 |
+
def get_provider_name(self) -> str:
|
51 |
+
return "no_stt"
|
52 |
+
|
53 |
class STTFactory:
|
54 |
+
"""Factory for creating STT providers"""
|
55 |
|
56 |
@staticmethod
|
57 |
def create_provider() -> Optional[STTInterface]:
|
58 |
"""Create STT provider based on configuration"""
|
59 |
+
try:
|
60 |
+
cfg = ConfigProvider.get()
|
61 |
+
stt_engine = cfg.global_config.stt_engine
|
62 |
+
|
63 |
+
log(f"🎤 Creating STT provider: {stt_engine}")
|
64 |
+
|
65 |
+
if stt_engine == "no_stt":
|
66 |
+
return NoSTT()
|
67 |
+
|
68 |
+
# Get provider class
|
69 |
+
provider_class = stt_providers.get(stt_engine)
|
70 |
+
if not provider_class:
|
71 |
+
log(f"⚠️ STT provider '{stt_engine}' not available")
|
72 |
+
return NoSTT()
|
73 |
+
|
74 |
+
# Get API key or credentials
|
75 |
+
api_key = cfg.global_config.get_stt_api_key()
|
76 |
+
|
77 |
+
if not api_key:
|
78 |
+
log(f"⚠️ No API key configured for {stt_engine}")
|
79 |
+
return NoSTT()
|
80 |
+
|
81 |
+
# Create provider instance
|
82 |
+
if stt_engine == "google":
|
83 |
+
# For Google, api_key is the path to credentials JSON
|
84 |
+
return provider_class(credentials_path=api_key)
|
85 |
+
elif stt_engine == "azure":
|
86 |
+
# For Azure, parse the key format
|
87 |
+
parts = api_key.split('|')
|
88 |
+
if len(parts) != 2:
|
89 |
+
log("⚠️ Invalid Azure STT key format. Expected: subscription_key|region")
|
90 |
+
return NoSTT()
|
91 |
+
return provider_class(subscription_key=parts[0], region=parts[1])
|
92 |
+
elif stt_engine == "flicker":
|
93 |
+
return provider_class(api_key=api_key)
|
94 |
+
else:
|
95 |
+
return provider_class(api_key=api_key)
|
96 |
+
|
97 |
+
except Exception as e:
|
98 |
+
log(f"❌ Failed to create STT provider: {e}")
|
99 |
+
return NoSTT()
|
100 |
|
101 |
@staticmethod
|
102 |
+
def get_available_providers():
|
103 |
+
"""Get list of available STT providers"""
|
104 |
+
return list(stt_providers.keys()) + ["no_stt"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|