Spaces:
Configuration error
Configuration error
Fedir Zadniprovskyi
commited on
Commit
·
a9ee91b
1
Parent(s):
e1a6910
refactor: simplify tests
Browse files- tests/app_test.py +33 -28
tests/app_test.py
CHANGED
@@ -15,6 +15,9 @@ from speaches.main import app
|
|
15 |
from speaches.server_models import TranscriptionVerboseResponse
|
16 |
|
17 |
SIMILARITY_THRESHOLD = 0.97
|
|
|
|
|
|
|
18 |
|
19 |
|
20 |
@pytest.fixture()
|
@@ -23,12 +26,17 @@ def client() -> Generator[TestClient, None, None]:
|
|
23 |
yield client
|
24 |
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
def get_audio_file_paths():
|
27 |
file_paths = []
|
28 |
directory = "tests/data"
|
29 |
-
for filename in
|
30 |
-
|
31 |
-
file_paths.append(os.path.join(directory, filename))
|
32 |
return file_paths
|
33 |
|
34 |
|
@@ -48,7 +56,7 @@ def transcribe_audio_data(
|
|
48 |
client: TestClient, data: bytes
|
49 |
) -> TranscriptionVerboseResponse:
|
50 |
response = client.post(
|
51 |
-
|
52 |
files={"file": ("audio.raw", data, "audio/raw")},
|
53 |
)
|
54 |
data = json.loads(response.json()) # TODO: figure this out
|
@@ -56,29 +64,26 @@ def transcribe_audio_data(
|
|
56 |
|
57 |
|
58 |
@pytest.mark.parametrize("file_path", file_paths)
|
59 |
-
def test_ws_audio_transcriptions(
|
|
|
|
|
60 |
with open(file_path, "rb") as file:
|
61 |
data = file.read()
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
)
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
lambda x: x == " ", file_transcription.text, streaming_transcription.text
|
81 |
-
)
|
82 |
-
assert (
|
83 |
-
s.ratio() > SIMILARITY_THRESHOLD
|
84 |
-
), f"\nExpected: {file_transcription.text}\nReceived: {streaming_transcription.text}"
|
|
|
15 |
from speaches.server_models import TranscriptionVerboseResponse
|
16 |
|
17 |
SIMILARITY_THRESHOLD = 0.97
|
18 |
+
AUDIO_FILES_LIMIT = 5
|
19 |
+
AUDIO_FILE_DIR = "tests/data"
|
20 |
+
TRANSCRIBE_ENDPOINT = "/v1/audio/transcriptions?response_format=verbose_json"
|
21 |
|
22 |
|
23 |
@pytest.fixture()
|
|
|
26 |
yield client
|
27 |
|
28 |
|
29 |
+
@pytest.fixture()
|
30 |
+
def ws(client: TestClient) -> Generator[WebSocketTestSession, None, None]:
|
31 |
+
with client.websocket_connect(TRANSCRIBE_ENDPOINT) as ws:
|
32 |
+
yield ws
|
33 |
+
|
34 |
+
|
35 |
def get_audio_file_paths():
|
36 |
file_paths = []
|
37 |
directory = "tests/data"
|
38 |
+
for filename in sorted(os.listdir(directory)[:AUDIO_FILES_LIMIT]):
|
39 |
+
file_paths.append(os.path.join(directory, filename))
|
|
|
40 |
return file_paths
|
41 |
|
42 |
|
|
|
56 |
client: TestClient, data: bytes
|
57 |
) -> TranscriptionVerboseResponse:
|
58 |
response = client.post(
|
59 |
+
TRANSCRIBE_ENDPOINT,
|
60 |
files={"file": ("audio.raw", data, "audio/raw")},
|
61 |
)
|
62 |
data = json.loads(response.json()) # TODO: figure this out
|
|
|
64 |
|
65 |
|
66 |
@pytest.mark.parametrize("file_path", file_paths)
|
67 |
+
def test_ws_audio_transcriptions(
|
68 |
+
client: TestClient, ws: WebSocketTestSession, file_path: str
|
69 |
+
):
|
70 |
with open(file_path, "rb") as file:
|
71 |
data = file.read()
|
72 |
+
|
73 |
+
streaming_transcription: TranscriptionVerboseResponse = None # type: ignore
|
74 |
+
thread = threading.Thread(
|
75 |
+
target=stream_audio_data, args=(ws, data), kwargs={"speed": 4.0}
|
76 |
+
)
|
77 |
+
thread.start()
|
78 |
+
while True:
|
79 |
+
try:
|
80 |
+
streaming_transcription = TranscriptionVerboseResponse(**ws.receive_json())
|
81 |
+
except WebSocketDisconnect:
|
82 |
+
break
|
83 |
+
file_transcription = transcribe_audio_data(client, data)
|
84 |
+
s = SequenceMatcher(
|
85 |
+
lambda x: x == " ", file_transcription.text, streaming_transcription.text
|
86 |
+
)
|
87 |
+
assert (
|
88 |
+
s.ratio() > SIMILARITY_THRESHOLD
|
89 |
+
), f"\nExpected: {file_transcription.text}\nReceived: {streaming_transcription.text}"
|
|
|
|
|
|
|
|
|
|