askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
object_stt.py · 4.3 KB · 112 lines
| 1 | """Speech-to-text: OpenAI's dedicated transcription API. |
| 2 | |
| 3 | Unlike object_tts.py, there is no local/on-device path here to fall back |
| 4 | to. Voice input in this codebase is otherwise 100% browser-native |
| 5 | (SpeechRecognition / webkitSpeechRecognition) -- it never touches the |
| 6 | server, so it has no cost, no telemetry, and it silently does not work at |
| 7 | all on any browser lacking the API (Firefox, most non-Chrome/Safari |
| 8 | browsers). This module is the "not on device" alternative: the client |
| 9 | uploads real recorded audio, the server transcribes it here, with a real |
| 10 | measurable cost and latency like every other provider call in this |
| 11 | codebase. |
| 12 | """ |
| 13 | |
| 14 | from __future__ import annotations |
| 15 | |
| 16 | import json |
| 17 | from typing import Callable, Mapping |
| 18 | |
| 19 | OPENAI_TRANSCRIBE_URL = "https://api.openai.com/v1/audio/transcriptions" |
| 20 | DEFAULT_MODEL = "gpt-4o-mini-transcribe" |
| 21 | |
| 22 | # Recognized upload content-types, mapped to a filename extension -- |
| 23 | # OpenAI's endpoint infers format from the filename in the multipart |
| 24 | # part, not from an explicit format field. MediaRecorder's real-world |
| 25 | # output is audio/webm on Chrome/Firefox; Safari's support is spottier |
| 26 | # (audio/mp4 is the closest it reliably offers) -- both are covered, and |
| 27 | # anything unrecognized still gets tried rather than rejected outright, |
| 28 | # since the provider's own error is more informative than a guess here. |
| 29 | _EXTENSION_BY_CONTENT_TYPE = { |
| 30 | "audio/webm": "webm", |
| 31 | "audio/ogg": "ogg", |
| 32 | "audio/wav": "wav", |
| 33 | "audio/wave": "wav", |
| 34 | "audio/mpeg": "mp3", |
| 35 | "audio/mp4": "mp4", |
| 36 | "audio/m4a": "m4a", |
| 37 | "audio/x-m4a": "m4a", |
| 38 | } |
| 39 | _DEFAULT_EXTENSION = "webm" |
| 40 | |
| 41 | # send_http(url, headers, body_bytes) -> (status, response_bytes) -- same |
| 42 | # shape as object_ai.run_chat and object_tts.synthesize_cloud, so the |
| 43 | # server owns the timeout/thread and tests run without a network. |
| 44 | SendHttp = Callable[[str, Mapping[str, str], bytes], tuple[int, bytes]] |
| 45 | |
| 46 | |
| 47 | class STTProviderError(RuntimeError): |
| 48 | """Raised when the provider call fails (bad key, bad audio, rate limit).""" |
| 49 | |
| 50 | |
| 51 | def transcribe_cloud( |
| 52 | audio_bytes: bytes, |
| 53 | content_type: str, |
| 54 | api_key: str, |
| 55 | *, |
| 56 | send_http: SendHttp, |
| 57 | model: str = DEFAULT_MODEL, |
| 58 | ) -> str: |
| 59 | """Return transcribed text for one audio clip via OpenAI's API. |
| 60 | |
| 61 | Builds the multipart/form-data body by hand -- the stdlib has no |
| 62 | one-line helper for it -- so the caller-injects-HTTP shape used |
| 63 | everywhere else in this codebase still applies: this stays a pure |
| 64 | function of its inputs, no network access of its own. |
| 65 | """ |
| 66 | boundary = "dbbasic-stt-boundary-7f3c9e1a4b6d" |
| 67 | body = _multipart_body(boundary, audio_bytes, content_type, model) |
| 68 | headers = { |
| 69 | "authorization": f"Bearer {api_key}", |
| 70 | "content-type": f"multipart/form-data; boundary={boundary}", |
| 71 | } |
| 72 | status, response_body = send_http(OPENAI_TRANSCRIBE_URL, headers, body) |
| 73 | if status >= 400: |
| 74 | raise STTProviderError(_provider_error_detail(status, response_body)) |
| 75 | |
| 76 | try: |
| 77 | payload = json.loads(response_body.decode("utf-8")) |
| 78 | except (UnicodeDecodeError, json.JSONDecodeError) as exc: |
| 79 | raise STTProviderError("Provider returned an unreadable response") from exc |
| 80 | text = payload.get("text") |
| 81 | if not isinstance(text, str): |
| 82 | raise STTProviderError("Provider response had no transcription text") |
| 83 | return text |
| 84 | |
| 85 | |
| 86 | def _multipart_body(boundary: str, audio_bytes: bytes, content_type: str, model: str) -> bytes: |
| 87 | extension = _EXTENSION_BY_CONTENT_TYPE.get( |
| 88 | content_type.split(";")[0].strip().lower(), _DEFAULT_EXTENSION |
| 89 | ) |
| 90 | parts = [ |
| 91 | f"--{boundary}\r\n" |
| 92 | f'Content-Disposition: form-data; name="model"\r\n\r\n{model}\r\n'.encode("utf-8"), |
| 93 | ( |
| 94 | f"--{boundary}\r\n" |
| 95 | f'Content-Disposition: form-data; name="file"; filename="audio.{extension}"\r\n' |
| 96 | "Content-Type: application/octet-stream\r\n\r\n" |
| 97 | ).encode("utf-8"), |
| 98 | audio_bytes, |
| 99 | f"\r\n--{boundary}--\r\n".encode("utf-8"), |
| 100 | ] |
| 101 | return b"".join(parts) |
| 102 | |
| 103 | |
| 104 | def _provider_error_detail(status: int, body: bytes) -> str: |
| 105 | try: |
| 106 | payload = json.loads(body.decode("utf-8")) |
| 107 | except (UnicodeDecodeError, json.JSONDecodeError): |
| 108 | return f"Provider error (HTTP {status})" |
| 109 | detail = payload.get("error") |
| 110 | if isinstance(detail, dict): |
| 111 | detail = detail.get("message") or json.dumps(detail) |
| 112 | return f"Provider error (HTTP {status}): {str(detail)[:300]}" |