Fedir Zadniprovskyi 2024-10-01
chore: fix some ruff errors
@e14ef3b4d7157033c412f1e4fc28eaf93ed3bc77
pyproject.toml
--- pyproject.toml
+++ pyproject.toml
@@ -48,11 +48,9 @@
     "FIX",
     "TD", # disable todo warnings
     "ERA",  # allow commented out code
-    "PTH",
 
     "ANN003", # missing kwargs
     "ANN101", # missing self type
-    "ANN102", # missing cls
     "B006",
     "B008",
     "COM812", # trailing comma
scripts/client.py
--- scripts/client.py
+++ scripts/client.py
@@ -64,7 +64,7 @@
     print(f"Recording finished. File size: {file.stat().st_size} bytes")
 
     try:
-        with open(file, "rb") as fd:
+        with file.open("rb") as fd:
             start = time.perf_counter()
             res = client.post(
                 OPENAI_BASE_URL + TRANSCRIBE_PATH,
src/faster_whisper_server/gradio_app.py
--- src/faster_whisper_server/gradio_app.py
+++ src/faster_whisper_server/gradio_app.py
@@ -1,4 +1,5 @@
 from collections.abc import Generator
+from pathlib import Path
 
 import gradio as gr
 import httpx
@@ -33,7 +34,7 @@
             yield audio_task(file_path, endpoint, temperature, model)
 
     def audio_task(file_path: str, endpoint: str, temperature: float, model: str) -> str:
-        with open(file_path, "rb") as file:
+        with Path(file_path).open("rb") as file:
             response = http_client.post(
                 endpoint,
                 files={"file": file},
@@ -50,7 +51,7 @@
     def streaming_audio_task(
         file_path: str, endpoint: str, temperature: float, model: str
     ) -> Generator[str, None, None]:
-        with open(file_path, "rb") as file:
+        with Path(file_path).open("rb") as file:
             kwargs = {
                 "files": {"file": file},
                 "data": {
src/faster_whisper_server/routers/list_models.py
--- src/faster_whisper_server/routers/list_models.py
+++ src/faster_whisper_server/routers/list_models.py
@@ -24,7 +24,7 @@
 def get_models() -> ListModelsResponse:
     models = huggingface_hub.list_models(library="ctranslate2", tags="automatic-speech-recognition", cardData=True)
     models = list(models)
-    models.sort(key=lambda model: model.downloads, reverse=True)  # type: ignore  # noqa: PGH003
+    models.sort(key=lambda model: model.downloads or -1, reverse=True)
     transformed_models: list[Model] = []
     for model in models:
         assert model.created_at is not None
@@ -56,7 +56,7 @@
         model_name=model_name, library="ctranslate2", tags="automatic-speech-recognition", cardData=True
     )
     models = list(models)
-    models.sort(key=lambda model: model.downloads, reverse=True)  # type: ignore  # noqa: PGH003
+    models.sort(key=lambda model: model.downloads or -1, reverse=True)
     if len(models) == 0:
         raise HTTPException(status_code=404, detail="Model doesn't exists")
     exact_match: ModelInfo | None = None
src/faster_whisper_server/routers/stt.py
--- src/faster_whisper_server/routers/stt.py
+++ src/faster_whisper_server/routers/stt.py
@@ -57,26 +57,27 @@
     response_format: ResponseFormat,
 ) -> Response:
     segments = list(segments)
-    if response_format == ResponseFormat.TEXT:  # noqa: RET503
-        return Response(segments_to_text(segments), media_type="text/plain")
-    elif response_format == ResponseFormat.JSON:
-        return Response(
-            CreateTranscriptionResponseJson.from_segments(segments).model_dump_json(),
-            media_type="application/json",
-        )
-    elif response_format == ResponseFormat.VERBOSE_JSON:
-        return Response(
-            CreateTranscriptionResponseVerboseJson.from_segments(segments, transcription_info).model_dump_json(),
-            media_type="application/json",
-        )
-    elif response_format == ResponseFormat.VTT:
-        return Response(
-            "".join(segments_to_vtt(segment, i) for i, segment in enumerate(segments)), media_type="text/vtt"
-        )
-    elif response_format == ResponseFormat.SRT:
-        return Response(
-            "".join(segments_to_srt(segment, i) for i, segment in enumerate(segments)), media_type="text/plain"
-        )
+    match response_format:
+        case ResponseFormat.TEXT:
+            return Response(segments_to_text(segments), media_type="text/plain")
+        case ResponseFormat.JSON:
+            return Response(
+                CreateTranscriptionResponseJson.from_segments(segments).model_dump_json(),
+                media_type="application/json",
+            )
+        case ResponseFormat.VERBOSE_JSON:
+            return Response(
+                CreateTranscriptionResponseVerboseJson.from_segments(segments, transcription_info).model_dump_json(),
+                media_type="application/json",
+            )
+        case ResponseFormat.VTT:
+            return Response(
+                "".join(segments_to_vtt(segment, i) for i, segment in enumerate(segments)), media_type="text/vtt"
+            )
+        case ResponseFormat.SRT:
+            return Response(
+                "".join(segments_to_srt(segment, i) for i, segment in enumerate(segments)), media_type="text/plain"
+            )
 
 
 def format_as_sse(data: str) -> str:
tests/sse_test.py
--- tests/sse_test.py
+++ tests/sse_test.py
@@ -1,5 +1,5 @@
 import json
-import os
+from pathlib import Path
 
 import anyio
 from faster_whisper_server.api_models import (
@@ -26,7 +26,7 @@
 @pytest.mark.asyncio()
 @pytest.mark.parametrize(("file_path", "endpoint"), parameters)
 async def test_streaming_transcription_text(aclient: AsyncClient, file_path: str, endpoint: str) -> None:
-    extension = os.path.splitext(file_path)[1]
+    extension = Path(file_path).suffix[1:]
     async with await anyio.open_file(file_path, "rb") as f:
         data = await f.read()
     kwargs = {
@@ -42,7 +42,7 @@
 @pytest.mark.asyncio()
 @pytest.mark.parametrize(("file_path", "endpoint"), parameters)
 async def test_streaming_transcription_json(aclient: AsyncClient, file_path: str, endpoint: str) -> None:
-    extension = os.path.splitext(file_path)[1]
+    extension = Path(file_path).suffix[1:]
     async with await anyio.open_file(file_path, "rb") as f:
         data = await f.read()
     kwargs = {
@@ -57,7 +57,7 @@
 @pytest.mark.asyncio()
 @pytest.mark.parametrize(("file_path", "endpoint"), parameters)
 async def test_streaming_transcription_verbose_json(aclient: AsyncClient, file_path: str, endpoint: str) -> None:
-    extension = os.path.splitext(file_path)[1]
+    extension = Path(file_path).suffix[1:]
     async with await anyio.open_file(file_path, "rb") as f:
         data = await f.read()
     kwargs = {
Add a comment
List