Skip to content

Commit 1bb5211

Browse files
authored
[S2-446]파일을 실시간 처럼 보내도록 수정 (#1)
* 파일을 실시간 처럼 보내도록 수정 * file streamer logger 제거 * try-catch -> with 문으로 수정 * 주석 추가, 하드코딩된 값 변수로 치환
1 parent 11d89d2 commit 1bb5211

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

python-stt-sample/src/sample_stt.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
$ pip install grpcio-tools
1010
$ python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. ./vito-stt-client.proto
1111
12-
NOTE: This module requires the dependencies `grpcio` and `requests`.
12+
NOTE: This module requires the dependencies `grpcio`, `requests` `soundfile`.
1313
To install using pip:
1414
pip install grpcio
1515
pip install requests
16+
pip install soundfile
1617
1718
Example usage:
1819
python vitoopenapi-stt-streaming-sample.py sample/filepath
@@ -35,9 +36,29 @@
3536
API_BASE = "https://openapi.vito.ai"
3637
GRPC_SERVER_URL = "grpc-openapi.vito.ai:443"
3738

38-
SAMPLE_RATE = 16000
39+
SAMPLE_RATE = 8000
3940
ENCODING = pb.DecoderConfig.AudioEncoding.LINEAR16
40-
41+
BYTES_PER_SAMPLE= 2
42+
43+
# 본 예제에서는 스트리밍 입력을 음성파일을 읽어서 시뮬레이션 합니다.
44+
# 실제사용시에는 마이크 입력 등의 실시간 음성 스트림이 들어와야합니다.
45+
class FileStreamer:
46+
def __init__(self,filepath):
47+
self.filepath = filepath
48+
self.file = None
49+
def __enter__(self):
50+
self.file = open(self.filepath,"rb")
51+
return self
52+
def __exit__(self, exc_type, exc_val, exc_tb):
53+
self.file.close()
54+
os.remove(self.filepath)
55+
56+
def read(self, size):
57+
if size > 1024 * 1024:
58+
size = 1024*1024
59+
time.sleep(size / (SAMPLE_RATE*BYTES_PER_SAMPLE))
60+
content = self.file.read(size)
61+
return content
4162

4263
class RTZROpenAPIClient:
4364
def __init__(self, client_id, client_secret):
@@ -67,16 +88,14 @@ def transcribe_streaming_grpc(self, filepath, config):
6788

6889
def req_iterator():
6990
yield pb.DecoderRequest(streaming_config=config)
70-
with open(filepath, "rb") as f:
91+
with FileStreamer(filepath) as f:
7192
while True:
72-
buff = f.read(DEFAULT_BUFFER_SIZE)
93+
buff = f.read(size=DEFAULT_BUFFER_SIZE)
7394
if buff is None or len(buff) == 0:
7495
break
7596
yield pb.DecoderRequest(audio_content=buff)
76-
7797
req_iter = req_iterator()
7898
resp_iter = stub.Decode(req_iter, credentials=cred)
79-
8099
for resp in resp_iter:
81100
resp: pb.DecoderResponse
82101
for res in resp.results:

0 commit comments

Comments
 (0)