Course ES
← back to chapter

Transcribe

Paste a public audio URL (mp3, wav, m4a) and get the transcript. Speech to text:

Showcase — Transcribe

Paste a public audio URL (mp3, wav, m4a) and get the transcript. Speech to text: the backend downloads the bytes and hands them to the transcription model. For OpenAI that's the dedicated transcription endpoint; for Gemini it's the same multimodal call as vision, with an audio part.

Run

bash bootstrap-secrets.sh              # reads ../../../../.env, writes secrets/
docker compose up --build              # default: PROVIDER=openai

Open http://localhost:3000. Gemini: PROVIDER=gemini docker compose up --build.

What's where

  • backend/ai_openai.pyaudio.transcriptions.create over the downloaded bytes.
  • backend/ai_gemini.py — a multimodal call with an audio part.
  • frontend/app/page.tsx — URL box + transcript.

Stop

docker compose down

Run locally

Download the project as a ZIP and run it with Docker. Brings up a FastAPI backend + Next.js frontend on localhost:3000.

Download transcribe.zip

unzip transcribe.zip
cd transcribe
bash bootstrap-secrets.sh   # one-time: pulls API keys into ./secrets
docker compose up --build   # default provider: openai
# or:  PROVIDER=gemini docker compose up --build

Type some input, pick a provider, and run the same code shown in Source against the live API. Sign-in required.


  

The same modules the Run button hits. The whole project (frontend, Dockerfile, compose) is in the ZIP under README.

backend/ai_openai.py

"""Showcase 1 (OpenAI): speech to text from a URL.

Paste a public audio URL (mp3, wav, m4a) and get the transcript. We download the
bytes and hand them to the transcription model with a filename so it knows the
format.
"""
import urllib.request

from openai import OpenAI

_client = OpenAI()

_MODEL = "gpt-4o-mini-transcribe"


def run(audio_url: str) -> str:
    url = audio_url.strip()
    if not url.startswith("http"):
        return "Paste a public audio URL (http/https): mp3, wav, or m4a."
    data = urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})).read()
    name = url.split("/")[-1].split("?")[0] or "audio.mp3"
    return _client.audio.transcriptions.create(model=_MODEL, file=(name, data)).text

backend/ai_gemini.py

"""Showcase 1 (Gemini): speech to text from a URL.

Transcription is the multimodal call from week 10 with an audio part instead of
an image part.
"""
import os
import urllib.request

from google import genai
from google.genai import types

_client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])

_MODEL = "gemini-3.1-flash-lite"

_MIME = {"mp3": "audio/mpeg", "wav": "audio/wav", "m4a": "audio/mp4", "ogg": "audio/ogg", "flac": "audio/flac"}


def _mime(url: str) -> str:
    ext = url.lower().split("?")[0].rsplit(".", 1)[-1]
    return _MIME.get(ext, "audio/mpeg")


def run(audio_url: str) -> str:
    url = audio_url.strip()
    if not url.startswith("http"):
        return "Paste a public audio URL (http/https): mp3, wav, or m4a."
    data = urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})).read()
    response = _client.models.generate_content(
        model=_MODEL,
        contents=[
            types.Part.from_bytes(data=data, mime_type=_mime(url)),
            "Transcribe this audio verbatim. Output only the transcript.",
        ],
    )
    return response.text or ""

Project files

  • .gitignore
  • README.es.md
  • README.md
  • backend/Dockerfile
  • backend/ai_gemini.py
  • backend/ai_openai.py
  • backend/main.py
  • backend/requirements.txt
  • bootstrap-secrets.sh
  • docker-compose.yml
  • frontend/Dockerfile
  • frontend/app/layout.tsx
  • frontend/app/page.tsx
  • frontend/next.config.ts
  • frontend/package.json
  • frontend/tsconfig.json