diff --git a/requirements.txt b/requirements.txt index 882bafa..8998e3f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -scipy~=1.7.3 \ No newline at end of file +scipy~=1.7.3 +requests~=2.27.1 \ No newline at end of file diff --git a/speech.py b/speech.py index 418ce0a..885e495 100644 --- a/speech.py +++ b/speech.py @@ -1,6 +1,4 @@ -import json -from urllib.request import Request, urlopen - +import requests from scipy.io import wavfile @@ -27,13 +25,14 @@ class Speech: @staticmethod def __stt(wav_file, server): print(f'Connecting to \'{server}\'...') - request = Request(url=f'{server}/stt', - data=Speech.__load_wav(wav_file), - headers={'Content-Type': 'audio/wav'}) - result = json.loads(urlopen(request).read().decode('utf-8')) + response = requests.post(url=f'{server}/stt', + data=Speech.__load_wav(wav_file), + headers={'Content-Type': 'audio/wav'}) + result = response.json() + + if response.status_code != requests.codes.ok: + response.raise_for_status() - if not ('code' in result and 'text' in result): - raise RuntimeError(f'Wrong reply from server: {result}') return result['text'] if not result['code'] else f'Server error: {result}' def run(self, wav_file: str, server: str) -> str: