### curl: STT Examples Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt This section provides command-line examples using curl to interact with the Speech-to-Text API for various transcription scenarios, including different engines, formats, and options. ```APIDOC ## API Endpoints Examples (curl) ### Compact STT (English, fast) **Method:** POST **Endpoint:** `https://apim-ai-apis.azure-api.net/v1/stt/transcribe/base64` **Description:** Transcribes audio using the Compact STT engine, suitable for faster processing and English language. **Request Example:** ```bash API_KEY="YOUR_KEY" curl -X POST "https://apim-ai-apis.azure-api.net/v1/stt/transcribe/base64" \ -H "Content-Type: application/json" \ -H "Ocp-Apim-Subscription-Key: $API_KEY" \ -d "{\"audio\": \"$(base64 -i audio.wav)\", \"include_timestamps\": true}" \ | python3 -m json.tool ``` ### Whisper Pro (multilingual) **Method:** POST **Endpoint:** `https://apim-ai-apis.azure-api.net/v1/whisper/transcribe/base64` **Description:** Transcribes audio using the Whisper model, supporting multiple languages and speaker diarization. **Request Example:** ```bash API_KEY="YOUR_KEY" curl -X POST "https://apim-ai-apis.azure-api.net/v1/whisper/transcribe/base64" \ -H "Content-Type: application/json" \ -H "Ocp-Apim-Subscription-Key: $API_KEY" \ -d "{\"audio\": \"$(base64 -i meeting.wav)\", \"language\": \"en\", \"diarize\": true}" \ | python3 -m json.tool ``` ### Whisper multipart upload **Method:** POST **Endpoint:** `https://apim-ai-apis.azure-api.net/v1/whisper/transcribe` **Description:** Uploads an audio file directly using multipart/form-data for transcription with the Whisper model. **Request Example:** ```bash API_KEY="YOUR_KEY" curl -X POST "https://apim-ai-apis.azure-api.net/v1/whisper/transcribe" \ -H "Ocp-Apim-Subscription-Key: $API_KEY" \ -F "audio=@podcast.mp3" \ -F "language=en" \ -F "diarize=true" \ | python3 -m json.tool ``` ### Health check **Method:** GET **Endpoint:** `https://apim-ai-apis.azure-api.net/v1/whisper/health` **Description:** Checks the health status of the Whisper transcription service. **Request Example:** ```bash API_KEY="YOUR_KEY" curl -s "https://apim-ai-apis.azure-api.net/v1/whisper/health" \ -H "Ocp-Apim-Subscription-Key: $API_KEY" | python3 -m json.tool ``` ``` -------------------------------- ### STT API Examples (curl) Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt This section provides command-line examples using `curl` to interact with Speech-to-Text APIs. It demonstrates transcription using different engines (Compact, Whisper), including options for language and speaker diarization, and shows how to perform a health check. ```bash API_KEY="YOUR_KEY" # Compact STT (English, fast) curl -X POST "https://apim-ai-apis.azure-api.net/v1/stt/transcribe/base64" \ -H "Content-Type: application/json" \ -H "Ocp-Apim-Subscription-Key: $API_KEY" \ -d "{\"audio\": \"$(base64 -i audio.wav)\", \"include_timestamps\": true}" \ | python3 -m json.tool # Whisper Pro (multilingual) curl -X POST "https://apim-ai-apis.azure-api.net/v1/whisper/transcribe/base64" \ -H "Content-Type: application/json" \ -H "Ocp-Apim-Subscription-Key: $API_KEY" \ -d "{\"audio\": \"$(base64 -i meeting.wav)\", \"language\": \"en\", \"diarize\": true}" \ | python3 -m json.tool # Whisper multipart upload curl -X POST "https://apim-ai-apis.azure-api.net/v1/whisper/transcribe" \ -H "Ocp-Apim-Subscription-Key: $API_KEY" \ -F "audio=@podcast.mp3" \ -F "language=en" \ -F "diarize=true" \ | python3 -m json.tool # Health check curl -s "https://apim-ai-apis.azure-api.net/v1/whisper/health" \ -H "Ocp-Apim-Subscription-Key: $API_KEY" | python3 -m json.tool ``` -------------------------------- ### Python Whisper Multilingual Transcription with Diarization Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt Demonstrates how to perform multilingual transcription using the Whisper model via a REST API. This example shows how to send audio data in base64 format, enable speaker diarization, and automatically detect the language of the audio. It then processes the results to display the transcript with speaker labels. ```python import requests import base64 API_KEY = "YOUR_KEY" HEADERS = {"Ocp-Apim-Subscription-Key": API_KEY, "Content-Type": "application/json"} with open("meeting.wav", "rb") as f: audio_b64 = base64.b64encode(f.read()).decode() # Auto-detect language, enable speaker diarization response = requests.post( "https://apim-ai-apis.azure-api.net/v1/whisper/transcribe/base64", headers=HEADERS, json={ "audio": audio_b64, "diarize": True, "format": "wav" } ) result = response.json() print(f"Detected language: {result['metadata']['language']}") print(f"Processing time: {result['metadata']['processingTimeMs']}ms") print(f"\nTranscript:") current_speaker = None for word in result["words"]: if word.get("speaker") != current_speaker: current_speaker = word.get("speaker") print(f"\n[{current_speaker}]: ", end="") print(f"{word['word']} ", end="") ``` -------------------------------- ### Whisper Multilingual Transcription Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt This example demonstrates how to use the Whisper API for multilingual transcription with speaker diarization enabled. It shows how to read an audio file, encode it in base64, and send a POST request to the transcription endpoint. The response includes detected language, processing time, and a detailed transcript with speaker information. ```APIDOC ## POST /v1/whisper/transcribe/base64 (Multilingual with Diarization) ### Description This endpoint is used to transcribe audio files using the Whisper model, with support for multiple languages and speaker diarization. It takes base64 encoded audio and returns a structured JSON response containing the transcribed text, detected language, processing time, and word-level details including speaker identification. ### Method POST ### Endpoint https://apim-ai-apis.azure-api.net/v1/whisper/transcribe/base64 ### Parameters #### Request Body - **audio** (string) - Required - Base64 encoded audio data. - **diarize** (boolean) - Optional - Set to `true` to enable speaker diarization. Defaults to `false`. - **format** (string) - Optional - The format of the audio file (e.g., 'wav', 'mp3'). ### Request Example ```json { "audio": "BASE64_ENCODED_AUDIO_DATA", "diarize": true, "format": "wav" } ``` ### Response #### Success Response (200) - **text** (string) - The complete transcribed text. - **words** (array) - An array of word objects, each containing: - **word** (string) - The transcribed word. - **start** (number) - The start time of the word in seconds. - **end** (number) - The end time of the word in seconds. - **speaker** (string) - The identified speaker (e.g., 'SPEAKER_00', 'SPEAKER_01') if `diarize` is true. - **metadata** (object) - Contains metadata about the transcription: - **language** (string) - The detected language of the audio. - **processingTimeMs** (number) - The time taken for processing in milliseconds. #### Response Example ```json { "text": "Hello everyone, welcome to our meeting.", "words": [ {"word": "Hello", "start": 0.4, "end": 0.8, "speaker": "SPEAKER_00"}, {"word": "everyone,", "start": 0.9, "end": 1.5, "speaker": "SPEAKER_00"}, {"word": "welcome", "start": 1.6, "end": 2.1, "speaker": "SPEAKER_00"}, {"word": "to", "start": 2.2, "end": 2.3, "speaker": "SPEAKER_00"}, {"word": "our", "start": 2.4, "end": 2.6, "speaker": "SPEAKER_00"}, {"word": "meeting.", "start": 2.7, "end": 3.2, "speaker": "SPEAKER_00"} ], "metadata": { "language": "en", "processingTimeMs": 2150 } } ``` ``` -------------------------------- ### JavaScript: Express.js Transcription Service Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt This section provides an example of an Express.js service that acts as a transcription API endpoint, accepting audio files and using either the Whisper or Compact STT engine. ```APIDOC ## POST /api/transcribe ### Description An Express.js endpoint that accepts audio file uploads and transcribes them using either the Whisper or Compact STT engine. ### Method POST ### Endpoint /api/transcribe ### Parameters #### Query Parameters - **engine** (string) - Optional - The STT engine to use ('whisper' or 'compact'). Defaults to 'whisper'. - **diarize** (boolean) - Optional - Whether to perform speaker diarization (only applicable for Whisper engine). Accepts 'true' or 'false'. - **language** (string) - Optional - The language of the audio (only applicable for Whisper engine). #### Request Body - **audio** (file) - Required - The audio file to transcribe (multipart/form-data). ### Request Example (Using curl for demonstration) ```bash curl -X POST http://localhost:3001/api/transcribe \ -F "audio=@audio.wav" \ -F "engine=whisper" \ -F "language=en" \ -F "diarize=true" ``` ### Response #### Success Response (200) - **engine** (string) - The STT engine used. - **filename** (string) - The original name of the uploaded audio file. - **text** (string) - The transcribed text. - **metadata** (object) - Metadata about the transcription. - **words** (array) - An array of word objects (if applicable). #### Response Example ```json { "engine": "whisper", "filename": "audio.wav", "text": "This is the transcribed text.", "metadata": { "language": "en" } } ``` #### Error Response (400, 500) - **error** (string) - Description of the error. ``` -------------------------------- ### GET /v1/stt/health Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt Checks the health status of the compact STT engine. ```APIDOC ## GET /v1/stt/health ### Description Checks the health status of the compact STT engine. ### Method GET ### Endpoint `https://apim-ai-apis.azure-api.net/v1/stt/health` ### Parameters None ### Response #### Success Response (200) Typically returns a status indicating the engine is healthy (e.g., `{"status": "ok"}`). ``` -------------------------------- ### Migrate AssemblyAI to Brainiall Whisper (Python) Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt Compares transcription code and features for AssemblyAI versus Brainiall Whisper. Shows how to transcribe audio with speaker labels using both services, illustrating the migration to Brainiall Whisper for potential cost savings. ```python # BEFORE: AssemblyAI ($0.0025/min) import assemblyai as aai aai.settings.api_key = "YOUR_ASSEMBLYAI_KEY" transcriber = aai.Transcriber() config = aai.TranscriptionConfig(speaker_labels=True, language_code="en") transcript = transcriber.transcribe("audio.wav", config=config) text = transcript.text # AFTER: Brainiall Whisper ($0.006/min, or Compact $0.002/min for English-only) import requests, base64 with open("audio.wav", "rb") as f: audio_b64 = base64.b64encode(f.read()).decode() response = requests.post( "https://apim-ai-apis.azure-api.net/v1/whisper/transcribe/base64", headers={"Ocp-Apim-Subscription-Key": "YOUR_KEY", "Content-Type": "application/json"}, json={"audio": audio_b64, "diarize": True}, ) text = response.json()["text"] ``` -------------------------------- ### GET /v1/whisper/health Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt Checks the health status of the Whisper Pro service. This endpoint is useful for verifying that the service is operational. ```APIDOC ## GET /v1/whisper/health ### Description Whisper Pro health check. ### Method GET ### Endpoint /v1/whisper/health ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) - **status** (string) - Indicates the health status of the service (e.g., "ok"). #### Response Example ```json { "status": "ok" } ``` ``` -------------------------------- ### Migrate Google Cloud STT to Brainiall Whisper (Python) Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt Compares transcription costs and code for Google Cloud Speech-to-Text versus Brainiall Whisper. Demonstrates how to transcribe an audio file using both services, highlighting the simpler and more cost-effective approach with Brainiall Whisper. ```python # BEFORE: Google Cloud STT ($0.016/min) from google.cloud import speech_v1 client = speech_v1.SpeechClient() with open("audio.wav", "rb") as f: audio = speech_v1.RecognitionAudio(content=f.read()) config = speech_v1.RecognitionConfig( encoding=speech_v1.RecognitionConfig.AudioEncoding.LINEAR16, sample_rate_hertz=16000, language_code="en-US", enable_word_time_offsets=True, enable_automatic_punctuation=True, ) response = client.recognize(config=config, audio=audio) text = " ".join(r.alternatives[0].transcript for r in response.results) # AFTER: Brainiall Whisper ($0.006/min) — 2.7x cheaper import requests, base64 with open("audio.wav", "rb") as f: audio_b64 = base64.b64encode(f.read()).decode() response = requests.post( "https://apim-ai-apis.azure-api.net/v1/whisper/transcribe/base64", headers={"Ocp-Apim-Subscription-Key": "YOUR_KEY", "Content-Type": "application/json"}, json={"audio": audio_b64, "language": "en"}, ) text = response.json()["text"] ``` -------------------------------- ### Compact STT Health Check Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt This endpoint performs a health check for the compact STT engine. It is a simple GET request used to verify the availability and status of the service. ```http GET /v1/stt/health HTTP/1.1 Host: apim-ai-apis.azure-api.net ``` -------------------------------- ### Process Podcast Audio (Python) Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt Processes podcast audio files by transcribing them, extracting speaker information, and calculating word count and processing time. It utilizes the 'requests' library and requires an API key. ```python import requests import base64 import json API_KEY = "YOUR_KEY" def process_podcast(audio_path: str) -> dict: """Process a podcast: transcribe, extract chapters, generate summary.""" with open(audio_path, "rb") as f: audio_b64 = base64.b64encode(f.read()).decode() # Transcribe with speakers result = requests.post( "https://apim-ai-apis.azure-api.net/v1/whisper/transcribe/base64", headers={"Ocp-Apim-Subscription-Key": API_KEY, "Content-Type": "application/json"}, json={"audio": audio_b64, "diarize": True}, ).json() transcript = result.get("text", "") processing_time = result.get("metadata", {}).get("processingTimeMs", 0) return { "transcript": transcript, "word_count": len(transcript.split()), "processing_time_ms": processing_time, "speakers": list(set(w.get("speaker", "") for w in result.get("words", []) if w.get("speaker"))), } info = process_podcast("episode_42.mp3") print(f"Words: {info['word_count']}, Speakers: {info['speakers']}") ``` -------------------------------- ### Python STT Client with Retry and Error Handling Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt Implements a production-ready Speech-to-Text (STT) client in Python. It includes automatic engine selection ('compact' or 'whisper' based on file size and diarization needs), retry logic for network requests, and error handling. The client supports transcribing audio files and checking service health. ```python import requests import base64 import time from pathlib import Path class BrainiallSTT: """Production STT client with retry logic and automatic engine selection.""" def __init__(self, api_key: str, max_retries: int = 3, timeout: float = 60.0): self.api_key = api_key self.headers = {"Ocp-Apim-Subscription-Key": api_key, "Content-Type": "application/json"} self.max_retries = max_retries self.timeout = timeout def transcribe( self, audio_path: str, engine: str = "auto", language: str | None = None, diarize: bool = False, ) -> dict: """Transcribe audio file. Engine: 'compact', 'whisper', or 'auto' (auto-selects based on file size).""" audio_bytes = Path(audio_path).read_bytes() if engine == "auto": # Use compact for small files (<500KB, likely short English), whisper for everything else engine = "compact" if len(audio_bytes) < 500_000 and not diarize else "whisper" audio_b64 = base64.b64encode(audio_bytes).decode() if engine == "whisper": url = "https://apim-ai-apis.azure-api.net/v1/whisper/transcribe/base64" payload = {"audio": audio_b64, "diarize": diarize} if language: payload["language"] = language else: url = "https://apim-ai-apis.azure-api.net/v1/stt/transcribe/base64" payload = {"audio": audio_b64, "include_timestamps": True} for attempt in range(self.max_retries): try: response = requests.post( url, headers=self.headers, json=payload, timeout=self.timeout ) response.raise_for_status() result = response.json() result["engine"] = engine return result except requests.exceptions.RequestException as e: if attempt < self.max_retries - 1: wait = 2 ** attempt print(f"Retry {attempt + 1}/{self.max_retries} after {wait}s: {e}") time.sleep(wait) else: raise def transcribe_meeting(self, audio_path: str, language: str = "en") -> dict: """Convenience method: transcribe meeting with speaker diarization.""" return self.transcribe(audio_path, engine="whisper", language=language, diarize=True) def is_healthy(self, engine: str = "whisper") -> bool: """Check if the STT service is healthy.""" url = f"https://apim-ai-apis.azure-api.net/v1/{'whisper' if engine == 'whisper' else 'stt'}/health" try: r = requests.get(url, headers=self.headers, timeout=5.0) return r.status_code == 200 except Exception: return False # Usage stt = BrainiallSTT(api_key="YOUR_KEY") # Auto-select engine result = stt.transcribe("short_command.wav") # Uses compact (small file) print(f"Engine: {result['engine']}, Text: {result['text']}") result = stt.transcribe("long_podcast.mp3") # Uses whisper (large file) print(f"Engine: {result['engine']}, Text: {result['text'][:100]}...") # Meeting transcription with speakers meeting = stt.transcribe_meeting("standup.wav") current_speaker = None for word in meeting.get("words", []): if word.get("speaker") != current_speaker: current_speaker = word.get("speaker") print(f"\n[{current_speaker}]: ", end="") print(word["word"], end=" ") ``` -------------------------------- ### MCP Server Configuration for Brainiall Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt This JSON configuration defines the 'brainiall-speech' MCP server. It includes the API endpoint URL and necessary headers, such as the subscription key and accepted content types, for interacting with Brainiall's speech services. ```json { "mcpServers": { "brainiall-speech": { "url": "https://apim-ai-apis.azure-api.net/mcp/pronunciation/mcp", "headers": { "Ocp-Apim-Subscription-Key": "YOUR_KEY", "Accept": "application/json, text/event-stream" } } } } ``` -------------------------------- ### Python: Async STT Transcription with httpx Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt Enables asynchronous speech-to-text transcription using the 'httpx' library, supporting both compact and Whisper engines. It allows for optional language specification and diarization. The function can transcribe single files or batches concurrently. Requires 'httpx' and 'asyncio'. ```python import httpx import asyncio import base64 from pathlib import Path API_KEY = "YOUR_KEY" async def transcribe_async( audio_path: str, engine: str = "whisper", language: str | None = None, diarize: bool = False, client: httpx.AsyncClient | None = None, ) -> dict: """Async transcription supporting both compact and whisper engines.""" audio_b64 = base64.b64encode(Path(audio_path).read_bytes()).decode() if engine == "whisper": url = "https://apim-ai-apis.azure-api.net/v1/whisper/transcribe/base64" payload = {"audio": audio_b64, "diarize": diarize} if language: payload["language"] = language else: url = "https://apim-ai-apis.azure-api.net/v1/stt/transcribe/base64" payload = {"audio": audio_b64, "include_timestamps": True} _client = client or httpx.AsyncClient(timeout=60.0) try: response = await _client.post( url, headers={ "Ocp-Apim-Subscription-Key": API_KEY, "Content-Type": "application/json", }, json=payload, ) response.raise_for_status() return response.json() finally: if client is None: await _client.aclose() async def batch_transcribe(audio_files: list[str], engine: str = "whisper") -> list[dict]: """Transcribe multiple audio files concurrently.""" async with httpx.AsyncClient(timeout=120.0) as client: tasks = [ transcribe_async(path, engine=engine, client=client) for path in audio_files ] results = await asyncio.gather(*tasks, return_exceptions=True) output = [] for path, result in zip(audio_files, results): if isinstance(result, Exception): output.append({"file": path, "error": str(result)}) else: output.append({"file": path, **result}) return output # Usage async def main(): files = ["audio1.wav", "audio2.wav", "audio3.wav"] results = await batch_transcribe(files, engine="whisper") for r in results: if "error" in r: print(f" ERROR {r['file']}: {r['error']}") else: print(f" {r['file']}: {r['text'][:80]}...") asyncio.run(main()) ``` -------------------------------- ### Transcribe Audio using Base64 (Compact STT) Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt This endpoint utilizes the compact STT engine for transcribing base64-encoded audio. It supports inclusion of word-level timestamps and accepts common audio formats. This is ideal for short English utterances and real-time transcription. ```json { "audio": "", "include_timestamps": true, "format": "wav" } ``` -------------------------------- ### Transcribe Audio using Multipart Form-Data (Compact STT) Source: https://raw.githubusercontent.com/fasuizu-br/speech-ai-examples/main/stt-llms-full.txt This endpoint provides a multipart form-data alternative for the compact STT engine. It allows uploading audio files directly and optionally includes word-level timestamps. ```http POST /v1/stt/transcribe HTTP/1.1 Host: apim-ai-apis.azure-api.net Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW ------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="audio"