Skip to content

Commit 0bf8de2

Browse files
📝 Add docstrings to modernize
Docstrings generation was requested by @JarbasAl. * #45 (comment) The following files were modified: * `ovos_stt_http_server/__init__.py` * `ovos_stt_http_server/gradio_app.py`
1 parent d1adb73 commit 0bf8de2

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

ovos_stt_http_server/__init__.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,38 @@ def unload_engine(self, lang: str):
9292
self.engines.pop(lang)
9393

9494
def process_audio(self, audio: AudioData, lang: str):
95+
"""
96+
Transcribes the provided audio using the engine for the specified language.
97+
98+
Parameters:
99+
audio (AudioData): Audio content to transcribe.
100+
lang (str): Language code identifying which engine to use.
101+
102+
Returns:
103+
str: Transcribed text for the audio, or an empty string if no transcription is produced.
104+
"""
95105
engine = self.get_engine(lang)
96106
return engine.execute(audio, language=lang) or ""
97107

98108

99109
def create_app(stt_plugin, lang_plugin=None, multi=False, has_gradio=False):
110+
"""
111+
Create and configure a FastAPI app that exposes STT and language-detection endpoints and returns the app with its model container.
112+
113+
Configures CORS origins from the CORS_ORIGINS environment variable, initializes either a single-model or multi-model container using the provided plugins, and registers three endpoints:
114+
- GET /status: returns service and plugin metadata.
115+
- POST /stt: accepts raw audio bytes in the request body (query params: `lang`, `sample_rate`, `sample_width`), optionally performs language detection when `lang=auto`, and returns transcribed text.
116+
- POST /lang_detect: accepts raw audio bytes and returns detected language and confidence (supports `valid_langs` query param).
117+
118+
Parameters:
119+
stt_plugin (str): Name or identifier of the STT plugin to load.
120+
lang_plugin (str, optional): Name or identifier of an optional language-detection plugin. Defaults to None.
121+
multi (bool, optional): If True, use a MultiModelContainer (one engine per language); otherwise use a single ModelContainer. Defaults to False.
122+
has_gradio (bool, optional): Flag included in the /status response indicating whether a Gradio UI is available. Defaults to False.
123+
124+
Returns:
125+
tuple: (app, model) where `app` is the configured FastAPI application and `model` is the initialized ModelContainer or MultiModelContainer instance.
126+
"""
100127
app = FastAPI()
101128
cors_origins = os.environ.get("CORS_ORIGINS", "*")
102129
origins = [origin.strip() for origin in cors_origins.split(",")] if cors_origins != "*" else ["*"]
@@ -121,6 +148,18 @@ def stats(request: Request):
121148

122149
@app.post("/stt", response_class=PlainTextResponse)
123150
async def get_stt(request: Request):
151+
"""
152+
Handle an STT request: read audio from the request body, determine language if requested, and return the transcription.
153+
154+
Parameters:
155+
request (Request): HTTP request whose body contains raw audio bytes. Query parameters:
156+
- lang: language code or "auto" (default from Configuration().get("lang", "auto")).
157+
- sample_rate: sample rate in Hz for the audio (default 16000).
158+
- sample_width: sample width in bytes (default 2).
159+
160+
Returns:
161+
str: Transcribed text from the provided audio, or an empty string if no transcription is produced.
162+
"""
124163
lang = str(request.query_params.get("lang", Configuration().get("lang", "auto"))).lower()
125164
sr = int(request.query_params.get("sample_rate", 16000))
126165
sw = int(request.query_params.get("sample_width", 2))
@@ -147,4 +186,4 @@ def start_stt_server(engine: str,
147186
multi: bool = False,
148187
has_gradio: bool = False) -> (FastAPI, ModelContainer):
149188
app, engine = create_app(engine, lang_engine, multi, has_gradio)
150-
return app, engine
189+
return app, engine

ovos_stt_http_server/gradio_app.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import gradio as gr
32

43
from os.path import join, dirname, basename, splitext, isfile
@@ -10,6 +9,18 @@
109

1110

1211
def transcribe(audio_file, language: str, sample_rate: int = 16000, sample_width: int = 2):
12+
"""
13+
Transcribe an audio file into text using the configured STT engine.
14+
15+
Parameters:
16+
audio_file (str): Path to the audio file to transcribe.
17+
language (str): Language code to use for transcription.
18+
sample_rate (int): Sample rate in Hz for the provided audio (default 16000).
19+
sample_width (int): Sample width in bytes for the provided audio (default 2).
20+
21+
Returns:
22+
transcription (str): The transcribed text, or `None` if the file is missing or invalid.
23+
"""
1324
try:
1425
with open(audio_file, 'rb') as f:
1526
audio = f.read()
@@ -22,7 +33,22 @@ def transcribe(audio_file, language: str, sample_rate: int = 16000, sample_width
2233
def bind_gradio_service(app, stt_engine: ModelContainer,
2334
title, description, info, badge,
2435
default_lang="en", cache=True):
25-
global STT
36+
"""
37+
Create and mount a Gradio-based transcription UI at /gradio using the provided STT engine.
38+
39+
Initializes the module STT with the given ModelContainer, prepares available language choices and example audio files, constructs a Gradio Interface configured to call the transcribe function, and mounts that interface to the supplied app at path "/gradio". This function logs a deprecation warning for the Gradio interface.
40+
41+
Parameters:
42+
app: The web application or framework instance to which the Gradio interface will be mounted.
43+
stt_engine (ModelContainer): Speech-to-text engine container used to perform transcriptions and to obtain available languages.
44+
title (str): Title to display in the Gradio UI.
45+
description (str): Short description shown in the Gradio UI.
46+
info (str): Additional informational HTML or text displayed in the Gradio UI article section.
47+
badge: UI badge metadata (present for API compatibility; not used by this function).
48+
default_lang (str): Preferred default language code; if not available it will be adjusted or replaced with the first available language.
49+
cache (bool): Whether to cache example executions to speed up runtime after initial initialization.
50+
"""
51+
global STT
2652
LOG.warning("gradio interface is deprecated and will be removed in a follow up release")
2753
STT = stt_engine
2854
languages = list(stt_engine.engine.available_languages or [default_lang])
@@ -61,4 +87,4 @@ def bind_gradio_service(app, stt_engine: ModelContainer,
6187
analytics_enabled=False)
6288

6389
LOG.info(f"Mounting app to /gradio")
64-
gr.mount_gradio_app(app, iface, path="/gradio")
90+
gr.mount_gradio_app(app, iface, path="/gradio")

0 commit comments

Comments
 (0)