Migrate to requests library

This commit is contained in:
Aleksey Filippov 2022-01-14 17:41:27 +04:00
parent 14f3ba5171
commit 0ed8d49a72
2 changed files with 10 additions and 10 deletions

View File

@ -1 +1,2 @@
scipy~=1.7.3
scipy~=1.7.3
requests~=2.27.1

View File

@ -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: