Migrate to requests library
This commit is contained in:
parent
14f3ba5171
commit
0ed8d49a72
@ -1 +1,2 @@
|
|||||||
scipy~=1.7.3
|
scipy~=1.7.3
|
||||||
|
requests~=2.27.1
|
17
speech.py
17
speech.py
@ -1,6 +1,4 @@
|
|||||||
import json
|
import requests
|
||||||
from urllib.request import Request, urlopen
|
|
||||||
|
|
||||||
from scipy.io import wavfile
|
from scipy.io import wavfile
|
||||||
|
|
||||||
|
|
||||||
@ -27,13 +25,14 @@ class Speech:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def __stt(wav_file, server):
|
def __stt(wav_file, server):
|
||||||
print(f'Connecting to \'{server}\'...')
|
print(f'Connecting to \'{server}\'...')
|
||||||
request = Request(url=f'{server}/stt',
|
response = requests.post(url=f'{server}/stt',
|
||||||
data=Speech.__load_wav(wav_file),
|
data=Speech.__load_wav(wav_file),
|
||||||
headers={'Content-Type': 'audio/wav'})
|
headers={'Content-Type': 'audio/wav'})
|
||||||
result = json.loads(urlopen(request).read().decode('utf-8'))
|
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}'
|
return result['text'] if not result['code'] else f'Server error: {result}'
|
||||||
|
|
||||||
def run(self, wav_file: str, server: str) -> str:
|
def run(self, wav_file: str, server: str) -> str:
|
||||||
|
Loading…
Reference in New Issue
Block a user