Repository navigation
feat: add FonadaLabs speech-to-text provider #322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hiyaa-atFonada
wants to merge
2
commits into
lfnovo:main
Choose a base branch
from
hiyaa-atFonada:feat/fonadalabs-stt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # FonadaLabs | ||
|
|
||
| Native Kapture V3 speech-to-text over `POST https://api.fonada.ai/v3/asr/transcribe`. Audio is processed in Fonada's cloud using customer-owned credits. | ||
|
|
||
| ```python | ||
| import os | ||
| from esperanto import AIFactory | ||
|
|
||
| model = AIFactory.create_speech_to_text( | ||
| "fonadalabs", "kapture-v3", {"api_key": os.environ["FONADALABS_API_KEY"]} | ||
| ) | ||
| result = await model.atranscribe("lecture.mp3", language="hi") | ||
| print(result.text) | ||
| ``` | ||
|
|
||
| `FONADALABS_API_KEY` is also the environment fallback. `language` maps to multipart `language_id`; config `language` and then `FONADALABS_LANGUAGE` are fallbacks, defaulting to English (`en`). The endpoint does not promise automatic language detection. Both synchronous and asynchronous methods accept paths or binary streams. Prompt guidance is unsupported and raises ValueError. | ||
|
|
||
| Kapture V3 supports `en`, `bn`, `gu`, `hi`, `kn`, `ml`, `mr`, `or`, `pa`, `ta` and `te`. Send the code for the recording language. An unsupported code is rejected by Fonada before transcription. | ||
|
|
||
| Uploads must be nonempty and at most 25,000,000 bytes. Extract video audio and split larger recordings in the calling application's media layer. HTTP 429 waits at least 60 seconds and honors a longer `Retry-After` cooldown up to one hour, then retries the same chunk once. A cooldown longer than one hour fails immediately without retrying. Other HTTP failures, including 502/503/504, and transport errors are not automatically retried: a timeout or gateway response may follow a successful billed request. Errors identify invalid keys, denied access, exhausted credits, upload limits, invalid audio/language and rate limits without echoing response bodies or secrets. | ||
|
|
||
| The response maps actual `text`, `transcript` or `transcription` fields, including the `result` envelope, to `TranscriptionResponse.text`. Timings, usage, duration and speaker labels remain absent. Model discovery returns `kapture-v3` statically and does not validate a key or consume credits. | ||
|
|
||
| An optional `base_url` accepts either `https://api.fonada.ai/v3/asr` or the complete `/v3/asr/transcribe` endpoint, with or without a trailing slash. The provider appends the transcription path only once. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,255 @@ | ||
| """FonadaLabs Kapture V3 transcription over the native REST API.""" | ||
|
|
||
| import asyncio | ||
| import logging | ||
| import os | ||
| import time | ||
| from dataclasses import dataclass | ||
| from datetime import datetime, timezone | ||
| from email.utils import parsedate_to_datetime | ||
| from pathlib import Path | ||
| from typing import BinaryIO, List, Optional, Union | ||
| from uuid import uuid4 | ||
|
|
||
| import httpx | ||
|
|
||
| from esperanto.common_types import TranscriptionResponse | ||
| from esperanto.providers.stt.base import ( | ||
| Model, | ||
| SpeechToTextModel, | ||
| _guess_audio_content_type, | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| MAX_UPLOAD_BYTES = 25_000_000 | ||
| FONADALABS_MODELS = [ | ||
| Model(id="kapture-v3", owned_by="FonadaLabs", type="speech_to_text") | ||
| ] | ||
|
|
||
|
|
||
| class FonadaLabsAPIError(RuntimeError): | ||
| """Sanitized provider failure carrying a machine-readable HTTP status.""" | ||
|
|
||
| def __init__(self, status_code: int, message: str): | ||
| self.status_code = status_code | ||
| super().__init__(message) | ||
|
|
||
|
|
||
| @dataclass | ||
| class FonadaLabsSpeechToTextModel(SpeechToTextModel): | ||
| """Plain-text ASR with one delayed retry for explicit HTTP 429 responses.""" | ||
|
|
||
| def __post_init__(self) -> None: | ||
| super().__post_init__() | ||
| self.api_key = self.api_key or os.getenv("FONADALABS_API_KEY") | ||
| if not self.api_key: | ||
| raise ValueError("Set FONADALABS_API_KEY or provide api_key in config.") | ||
| if self.get_model_name() != "kapture-v3": | ||
| raise ValueError("FonadaLabs supports the kapture-v3 model.") | ||
| self.base_url = (self.base_url or "https://api.fonada.ai/v3/asr").rstrip("/") | ||
| # Configuration forms may contain the documented full endpoint. | ||
| # Keep a single /transcribe suffix when constructing requests. | ||
| if self.base_url.endswith("/transcribe"): | ||
| self.base_url = self.base_url.removesuffix("/transcribe") | ||
| self._create_http_clients() | ||
|
|
||
| @property | ||
| def provider(self) -> str: | ||
| return "fonadalabs" | ||
|
|
||
| def _get_default_model(self) -> str: | ||
| return "kapture-v3" | ||
|
|
||
| def _get_models(self) -> List[Model]: | ||
| return list(FONADALABS_MODELS) | ||
|
|
||
| def _prepare( | ||
| self, | ||
| audio_file: Union[str, BinaryIO], | ||
| language: Optional[str], | ||
| prompt: Optional[str], | ||
| ): | ||
| if prompt: | ||
| raise ValueError("FonadaLabs does not support transcription prompts.") | ||
| language_id = ( | ||
| language | ||
| or self._config.get("language") | ||
| or os.getenv("FONADALABS_LANGUAGE") | ||
| or "en" | ||
| ) | ||
| if not isinstance(language_id, str) or not language_id.strip(): | ||
| raise ValueError("A non-empty FonadaLabs language code is required.") | ||
| if isinstance(audio_file, str): | ||
| filename = Path(audio_file).name | ||
| with open(audio_file, "rb") as stream: | ||
| data = stream.read(MAX_UPLOAD_BYTES + 1) | ||
| else: | ||
| stream_name = getattr(audio_file, "name", None) | ||
| filename = Path(stream_name).name if isinstance(stream_name, str) and stream_name else "audio.wav" | ||
| data = audio_file.read(MAX_UPLOAD_BYTES + 1) | ||
| if not data or len(data) > MAX_UPLOAD_BYTES: | ||
| raise ValueError( | ||
| "FonadaLabs requires non-empty audio up to 25 MB; split larger recordings." | ||
| ) | ||
| return {"file": (filename, data, _guess_audio_content_type(filename))}, { | ||
| "language_id": language_id.strip() | ||
| } | ||
|
|
||
| @staticmethod | ||
| def _rate_limit_delay(response: httpx.Response) -> float: | ||
| """Wait at least one minute and honor a longer server cooldown.""" | ||
| value = response.headers.get("retry-after", "").strip() | ||
| try: | ||
| seconds = float(int(value)) | ||
| except ValueError: | ||
| try: | ||
| deadline = parsedate_to_datetime(value) | ||
| if deadline.tzinfo is None: | ||
| deadline = deadline.replace(tzinfo=timezone.utc) | ||
| seconds = (deadline - datetime.now(timezone.utc)).total_seconds() | ||
| except (ValueError, TypeError, OverflowError): | ||
| seconds = 60 | ||
| # Do not occupy a worker indefinitely for a long quota suspension. | ||
| if seconds > 3600: | ||
| raise FonadaLabsAPIError( | ||
| 429, | ||
| "FonadaLabs requests a cooldown longer than one hour. Retry later after checking account limits.", | ||
| ) | ||
| return max(60, seconds) | ||
|
|
||
| def _response(self, response: httpx.Response) -> TranscriptionResponse: | ||
| if not response.is_success: | ||
| messages = { | ||
| 401: "Invalid API key", | ||
| 403: "Access denied", | ||
| 402: "Credits exhausted", | ||
| 413: "Audio exceeds 25 MB", | ||
| 422: "Unsupported audio or language", | ||
| 429: "Rate or credit limit reached", | ||
| } | ||
| message = messages.get(response.status_code, "Transcription request failed") | ||
| logger.warning( | ||
| "FonadaLabs transcription failed: HTTP %s (%s)", | ||
| response.status_code, | ||
| message, | ||
| ) | ||
| raise FonadaLabsAPIError( | ||
| response.status_code, | ||
| f"FonadaLabs HTTP {response.status_code}: {message}. Check request status before retrying.", | ||
| ) | ||
| try: | ||
| payload = response.json() | ||
| if isinstance(payload, dict) and isinstance(payload.get("result"), dict): | ||
| payload = payload["result"] | ||
| if not isinstance(payload, dict): | ||
| raise ValueError("Expected object") | ||
| text = next( | ||
| ( | ||
| payload[key] | ||
| for key in ("text", "transcript", "transcription") | ||
| if isinstance(payload.get(key), str) | ||
| ), | ||
| None, | ||
| ) | ||
| if text is None: | ||
| raise ValueError("Missing text") | ||
| except (ValueError, TypeError): | ||
| raise RuntimeError( | ||
| "FonadaLabs returned an unrecognized transcription response." | ||
| ) from None | ||
| return TranscriptionResponse( | ||
| text=text, model=self.get_model_name(), provider=self.provider | ||
| ) | ||
|
|
||
| def _post(self, files, data): | ||
| request_id = uuid4().hex | ||
| started = time.monotonic() | ||
| response = self.client.post( | ||
| f"{self.base_url}/transcribe", | ||
| headers={ | ||
| "Authorization": f"Bearer {self.api_key}", | ||
| "X-Request-ID": request_id, | ||
| }, | ||
| files=files, | ||
| data=data, | ||
| ) | ||
| logger.info( | ||
| "FonadaLabs ASR request_id=%s status=%s elapsed_seconds=%.2f", | ||
| request_id, | ||
| response.status_code, | ||
| time.monotonic() - started, | ||
| ) | ||
| return response | ||
|
|
||
| async def _apost(self, files, data): | ||
| request_id = uuid4().hex | ||
| started = time.monotonic() | ||
| response = await self.async_client.post( | ||
| f"{self.base_url}/transcribe", | ||
| headers={ | ||
| "Authorization": f"Bearer {self.api_key}", | ||
| "X-Request-ID": request_id, | ||
| }, | ||
| files=files, | ||
| data=data, | ||
| ) | ||
| logger.info( | ||
| "FonadaLabs ASR request_id=%s status=%s elapsed_seconds=%.2f", | ||
| request_id, | ||
| response.status_code, | ||
| time.monotonic() - started, | ||
| ) | ||
| return response | ||
|
|
||
| def transcribe( | ||
| self, | ||
| audio_file: Union[str, BinaryIO], | ||
| language: Optional[str] = None, | ||
| prompt: Optional[str] = None, | ||
| ) -> TranscriptionResponse: | ||
| files, data = self._prepare(audio_file, language, prompt) | ||
| try: | ||
| response = self._post(files, data) | ||
| if response.status_code == 429: | ||
| delay = self._rate_limit_delay(response) | ||
| logger.warning( | ||
| "FonadaLabs rate limit: waiting %s seconds before one retry", delay | ||
| ) | ||
| time.sleep(delay) | ||
| response = self._post(files, data) | ||
| except httpx.HTTPError as error: | ||
| logger.warning( | ||
| "FonadaLabs transcription transport failure: %s", type(error).__name__ | ||
| ) | ||
| raise RuntimeError( | ||
| "FonadaLabs connection or timeout error; request may already be billed. Check before retrying." | ||
| ) from None | ||
| return self._response(response) | ||
|
|
||
| async def atranscribe( | ||
| self, | ||
| audio_file: Union[str, BinaryIO], | ||
| language: Optional[str] = None, | ||
| prompt: Optional[str] = None, | ||
| ) -> TranscriptionResponse: | ||
| files, data = await asyncio.to_thread( | ||
| self._prepare, audio_file, language, prompt | ||
| ) | ||
| try: | ||
| response = await self._apost(files, data) | ||
| if response.status_code == 429: | ||
| delay = self._rate_limit_delay(response) | ||
| logger.warning( | ||
| "FonadaLabs rate limit: waiting %s seconds before one retry", delay | ||
| ) | ||
| await asyncio.sleep(delay) | ||
| response = await self._apost(files, data) | ||
| except httpx.HTTPError as error: | ||
| logger.warning( | ||
| "FonadaLabs transcription transport failure: %s", type(error).__name__ | ||
| ) | ||
| raise RuntimeError( | ||
| "FonadaLabs connection or timeout error; request may already be billed. Check before retrying." | ||
| ) from None | ||
| return self._response(response) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.