-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.py
180 lines (159 loc) · 5.33 KB
/
command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import os
import re
import sys
from abc import ABC, abstractmethod
import grpc
import pyaudio
from dotenv import load_dotenv
from auth import authorization_metadata
from tinkoff.cloud.stt.v1 import stt_pb2, stt_pb2_grpc
from tinkoff.cloud.tts.v1 import tts_pb2, tts_pb2_grpc
sys.path.append("..")
load_dotenv()
ENDPOINT = os.environ.get("ENDPOINT") or "stt.tinkoff.ru:443"
API_KEY = os.getenv("API_KEY")
SECRET_KEY = os.getenv("SECRET_KEY")
SAMPLE_RATE = 48000
class SpeechCommand(ABC):
@abstractmethod
def execute(self) -> None:
pass
class TTS(SpeechCommand):
"""
Text To Speech
"""
def __init__(self, phrase):
phrase = phrase.replace(' ', '. ')
self._ssml = '<speak><p>' + phrase + '</p></speak>'
self._text = re.sub(r'\<[^>]*\>', '', phrase)
stub = tts_pb2_grpc.TextToSpeechStub(
grpc.secure_channel(
ENDPOINT,
grpc.ssl_channel_credentials()
)
)
metadata = authorization_metadata(
API_KEY,
SECRET_KEY,
"tinkoff.cloud.tts"
)
request = tts_pb2.SynthesizeSpeechRequest(
input=tts_pb2.SynthesisInput(
text=self._text,
ssml=self._ssml
),
audio_config=tts_pb2.AudioConfig(
audio_encoding=tts_pb2.LINEAR16,
speaking_rate=1,
sample_rate_hertz=SAMPLE_RATE
)
)
self._responses = stub.StreamingSynthesize(
request,
metadata=metadata
)
def execute(self) -> None:
pyaudio_lib = pyaudio.PyAudio()
f = pyaudio_lib.open(
output=True,
channels=1,
format=pyaudio.paInt16,
rate=SAMPLE_RATE
)
for key, value in self._responses.initial_metadata():
if key == "x-audio-num-samples":
print(self._text)
print("Estimated audio duration is "
+ str(int(value) / SAMPLE_RATE)
+ " seconds"
)
break
for stream_response in self._responses:
f.write(stream_response.audio_chunk)
class STT(SpeechCommand):
"""
Speech To Text
"""
def __init__(self, recognize):
self._recognize = recognize
r = stt_pb2.StreamingRecognizeRequest()
r.streaming_config.config.encoding = stt_pb2.AudioEncoding.LINEAR16
r.streaming_config.config.sample_rate_hertz = 16000
r.streaming_config.config.num_channels = 1
r.streaming_config.config.enable_denormalization = True
r.streaming_config.config.enable_automatic_punctuation = True
r.streaming_config.config.vad_config.silence_duration_threshold = 1.20
r.streaming_config.single_utterance = True
r.streaming_config.config.speech_contexts.append(
stt_pb2.SpeechContext(
phrases = [
stt_pb2.SpeechContextPhrase(
text=text,
score=10.0
) for text in self._recognize.context_words
]
)
)
metadata = authorization_metadata(
API_KEY,
SECRET_KEY,
"tinkoff.cloud.stt"
)
stub = stt_pb2_grpc.SpeechToTextStub(
grpc.secure_channel(
ENDPOINT,
grpc.ssl_channel_credentials()
)
)
self._responses = stub.StreamingRecognize(
self.requests(r),
metadata=metadata
)
def execute(self) -> None:
self._recognize.recognition_utterance(self._responses)
self._recognize.search_intents()
self._recognize.search_entities()
self._recognize.search_products()
self._recognize.search_name()
self._recognize.search_city()
self._recognize.search_digit()
@staticmethod
def requests(request):
try:
yield request
pyaudio_lib = pyaudio.PyAudio()
f = pyaudio_lib.open(
input=True,
channels=1,
format=pyaudio.paInt16,
rate=16000
)
for data in iter(lambda: f.read(800), b''):
request = stt_pb2.StreamingRecognizeRequest()
request.audio_content = data
yield request
except Exception as e:
print("Got exception in generate_requests", e)
raise
class Invoker:
_on_speak = None
_on_listen = None
def set_on_speak(self, ssml: SpeechCommand):
self._on_speak = TTS(ssml)
def set_on_listen(self, recognize: SpeechCommand):
self._on_listen = STT(recognize)
def do_speak(self):
if isinstance(self._on_speak, SpeechCommand):
print("\nRobot:", end=" ")
self._on_speak.execute()
def do_listen(self):
if isinstance(self._on_listen, SpeechCommand):
print("\nCustomer: ", end=" ")
self._on_listen.execute()
def do_conversation(self) -> None:
if isinstance(self._on_speak, SpeechCommand):
print("\nRobot:", end=" ")
self._on_speak.execute()
if isinstance(self._on_listen, SpeechCommand):
print("\nCustomer: ", end=" ")
self._on_listen.execute()