Skip to content

Commit 3e82a5c

Browse files
authored
Merge pull request #11 from speechpro/check_session
Добавил проверку активности сессии + аннотации типов тут и там
2 parents 2aee60a + 3a26226 commit 3e82a5c

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

speechpro/cloud/speech/common/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from speechpro.cloud.speech.common.rest.cloud_client.models.auth_status_dto import AuthStatusDto
12
from speechpro.cloud.speech.common.rest.cloud_client import SessionApi, AuthRequestDto
23

34

@@ -9,10 +10,18 @@ def __init__(self, username, domain_id, password):
910
self._session_id = None
1011

1112

13+
def _check_session_status(self) -> bool:
14+
session_api = SessionApi()
15+
status: AuthStatusDto = session_api.check(self._session_id)
16+
return status.is_active
17+
18+
1219
@property
1320
def session_id(self):
14-
if not self._session_id:
21+
if self._session_id and self._check_session_status():
22+
return self._session_id
23+
else:
1524
session_api = SessionApi()
1625
credentials = AuthRequestDto(self.username, self.domain_id, self.password)
1726
self._session_id = session_api.login(credentials).session_id
18-
return self._session_id
27+
return self._session_id

speechpro/cloud/speech/recognition/rest/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import base64
2+
import enum
3+
from typing import Any, Dict, Tuple
24

35
from speechpro.cloud.speech.recognition.rest.cloud_client.api import RecognizeApi
46
from speechpro.cloud.speech.recognition.rest.cloud_client.models import AudioFileDto, RecognitionRequestDto, AdvancedRecognitionRequestDto
@@ -12,22 +14,22 @@
1214

1315
class ShortAudioRecognitionClient():
1416

15-
model_mapping = {
17+
model_mapping: Dict[Tuple[enums.Language, enums.Model], str] = {
1618
(enums.Language.RU, enums.Model.GENERAL): 'FarField',
1719
(enums.Language.RU, enums.Model.PHONE_CALL): 'TelecomRus',
1820
(enums.Language.KZ, enums.Model.PHONE_CALL): 'TelecomKz',
1921
(enums.Language.EN, enums.Model.PHONE_CALL): 'TelecomEngUs',
2022
(enums.Language.ES, enums.Model.PHONE_CALL): 'TelecomEsp'
2123
}
2224

23-
def validate_enum_value(self, config, key, enum_type):
25+
def validate_enum_value(self, config: Dict[str, Any], key: str, enum_type: enum.EnumMeta):
2426
try:
2527
return config[key] if isinstance(config[key], enum_type) else enum_type[config[key]]
2628
except:
2729
raise ValueError(f"{enum_type.__name__} is not provided or does not exist. Available models: {', '.join([m.name for m in enum_type])}")
2830

2931

30-
def recognize(self, config, audio):
32+
def recognize(self, config: Dict[str, Any], audio: bytes) -> Any:
3133
b64encoded_audio = base64.standard_b64encode(audio)
3234
audio_str = str(b64encoded_audio, 'ascii', 'ignore')
3335

speechpro/cloud/speech/synthesis/rest/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
from typing import Union
23

34
from speechpro.cloud.speech.synthesis.rest.cloud_client import Synthesize, SynthesizeRequest, SynthesizeText
45
from speechpro.cloud.speech.synthesis import enums
@@ -24,7 +25,7 @@ def get_enum_value(self, value, enum_type):
2425
return value if isinstance(value, enum_type) else enum_type[value]
2526

2627

27-
def synthesize(self, voice, profile, text):
28+
def synthesize(self, voice: Union[enums.Voice, str], profile: Union[enums.PlaybackProfile, str], text: str):
2829
try:
2930
voice = self.get_enum_value(voice, enums.Voice)
3031
profile = self.get_enum_value(profile, enums.PlaybackProfile)

0 commit comments

Comments
 (0)