|
9 | 9 | $ pip install grpcio-tools |
10 | 10 | $ python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. ./vito-stt-client.proto |
11 | 11 |
|
12 | | -NOTE: This module requires the dependencies `grpcio` and `requests`. |
| 12 | +NOTE: This module requires the dependencies `grpcio`, `requests` `soundfile`. |
13 | 13 | To install using pip: |
14 | 14 | pip install grpcio |
15 | 15 | pip install requests |
| 16 | + pip install soundfile |
16 | 17 |
|
17 | 18 | Example usage: |
18 | 19 | python vitoopenapi-stt-streaming-sample.py sample/filepath |
|
35 | 36 | API_BASE = "https://openapi.vito.ai" |
36 | 37 | GRPC_SERVER_URL = "grpc-openapi.vito.ai:443" |
37 | 38 |
|
38 | | -SAMPLE_RATE = 16000 |
| 39 | +SAMPLE_RATE = 8000 |
39 | 40 | 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 |
41 | 62 |
|
42 | 63 | class RTZROpenAPIClient: |
43 | 64 | def __init__(self, client_id, client_secret): |
@@ -67,16 +88,14 @@ def transcribe_streaming_grpc(self, filepath, config): |
67 | 88 |
|
68 | 89 | def req_iterator(): |
69 | 90 | yield pb.DecoderRequest(streaming_config=config) |
70 | | - with open(filepath, "rb") as f: |
| 91 | + with FileStreamer(filepath) as f: |
71 | 92 | while True: |
72 | | - buff = f.read(DEFAULT_BUFFER_SIZE) |
| 93 | + buff = f.read(size=DEFAULT_BUFFER_SIZE) |
73 | 94 | if buff is None or len(buff) == 0: |
74 | 95 | break |
75 | 96 | yield pb.DecoderRequest(audio_content=buff) |
76 | | - |
77 | 97 | req_iter = req_iterator() |
78 | 98 | resp_iter = stub.Decode(req_iter, credentials=cred) |
79 | | - |
80 | 99 | for resp in resp_iter: |
81 | 100 | resp: pb.DecoderResponse |
82 | 101 | for res in resp.results: |
|
0 commit comments