67 lines
3.7 KiB
Python
67 lines
3.7 KiB
Python
import io,unittest,wave
|
|
from types import SimpleNamespace
|
|
from stt import validate_wav,read_upload,supported,REPO,REVISION,MODEL,STT
|
|
|
|
def audio():
|
|
out=io.BytesIO()
|
|
with wave.open(out,'wb') as w:w.setnchannels(1);w.setsampwidth(2);w.setframerate(16000);w.writeframes(b'\0\0'*1600)
|
|
return out.getvalue()
|
|
class STTTests(unittest.TestCase):
|
|
def test_wav(self):
|
|
validate_wav(audio())
|
|
for b in [b'',b'bad',audio()[:-2]]:
|
|
with self.assertRaises(ValueError):validate_wav(b)
|
|
def test_recipe(self):
|
|
self.assertTrue(supported(dict(repo=REPO,revision=REVISION,file=MODEL)))
|
|
self.assertFalse(supported(dict(repo=REPO,revision='other',file=MODEL)))
|
|
def test_multipart(self):
|
|
body=b'--x\r\nContent-Disposition: form-data; name="file"; filename="audio.wav"\r\n\r\n'+audio()+b'\r\n--x\r\nContent-Disposition: form-data; name="model"\r\n\r\nASR\r\n--x--\r\n'
|
|
h=SimpleNamespace(headers={'Content-Type':'multipart/form-data; boundary=x','Content-Length':str(len(body))},rfile=io.BytesIO(body),connection=SimpleNamespace(settimeout=lambda n:None))
|
|
fields,data=read_upload(h);self.assertEqual(fields['model'],'ASR');validate_wav(data)
|
|
h.headers['Transfer-Encoding']='chunked'
|
|
with self.assertRaises(ValueError):read_upload(h)
|
|
def test_missing_runtime(self):
|
|
s=STT(None,None,SimpleNamespace(status=lambda:{'active':None}))
|
|
with self.assertRaises(ValueError):s.build()
|
|
|
|
class ReferenceTranscriptionTests(unittest.TestCase):
|
|
def test_reference_normalized_and_transcribed_without_download(self):
|
|
import threading
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock,patch
|
|
from stt import STT,validate_wav
|
|
stt=STT.__new__(STT);stt.lock=threading.RLock();stt.profiles=Mock()
|
|
stt.profiles.status.return_value={'profiles':[dict(id='cpu-asr',kind='stt',runnable=True)]}
|
|
stt.start=Mock(return_value={'id':'ours'});stt.job=dict(id='ours',state='complete',text=' Referenztext ')
|
|
with patch('stt.subprocess.run',return_value=SimpleNamespace(returncode=0,stdout=b'\0\0'*16000)):
|
|
self.assertEqual(stt.transcribe_reference(b'reference',threading.Event()),'Referenztext')
|
|
args=stt.start.call_args.args;self.assertEqual(args[0],'cpu-asr');validate_wav(args[1]);self.assertEqual(args[2],'auto')
|
|
def test_reference_never_uses_unrelated_stt_result(self):
|
|
import threading
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock,patch
|
|
from stt import STT
|
|
stt=STT.__new__(STT);stt.lock=threading.RLock();stt.profiles=Mock()
|
|
stt.profiles.status.return_value={'profiles':[dict(id='cpu-asr',kind='stt',runnable=True)]}
|
|
stt.start=Mock(return_value={'id':'ours'});stt.job=dict(id='someone-else',state='complete',text='Other audio')
|
|
with patch('stt.subprocess.run',return_value=SimpleNamespace(returncode=0,stdout=b'\0\0'*16000)):
|
|
with self.assertRaisesRegex(ValueError,'ersetzt'):stt.transcribe_reference(b'reference',threading.Event())
|
|
|
|
class LongReferenceTests(unittest.TestCase):
|
|
def test_long_reference_is_transcribed_in_chunks_without_truncation(self):
|
|
import threading
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock,patch
|
|
from stt import STT,validate_wav
|
|
stt=STT.__new__(STT);stt.profiles=Mock()
|
|
stt.profiles.status.return_value={'profiles':[dict(id='cpu-asr',kind='stt',runnable=True)]}
|
|
stt._transcribe_reference_chunk=Mock(side_effect=['First','Second','Third'])
|
|
pcm=b'\0\0'*(241*16000)
|
|
with patch('stt.subprocess.run',return_value=SimpleNamespace(returncode=0,stdout=pcm)):
|
|
self.assertEqual(stt.transcribe_reference(b'long-reference',threading.Event()),'First Second Third')
|
|
lengths=[]
|
|
for call in stt._transcribe_reference_chunk.call_args_list:
|
|
validate_wav(call.args[1])
|
|
with wave.open(io.BytesIO(call.args[1])) as w:lengths.append(w.getnframes()/w.getframerate())
|
|
self.assertEqual(lengths,[120,120,1])
|