Skip to main content
Welcome back. In this lesson we’ll build an asynchronous audio-to-insight pipeline that:
  • Accepts a WAV audio file
  • Transcribes the audio
  • Detects the spoken language and a prevailing emotion/tone
  • Translates the transcription into English
  • Generates a concise summary and a suggested title
This design separates each step into small, composable async functions so you can reuse or replace individual components (for example swapping models or custom agents).
Store your OpenAI API key in a .env file (for example OPENAI_API_KEY=<your_key>). This lesson will load environment variables via python-dotenv.
Quick overview — Pipeline steps and the corresponding functions:

Setup and imports

Load environment variables, initialize the OpenAI client, and import utilities. This example uses the modern OpenAI Python client (OpenAI()), plus an assumed agents package providing Agent and Runner as used in the original material.
Be mindful of API usage and costs when using large models like gpt-4 and uploading audio files. Use lower-cost models for development and testing if desired.

Transcription (Whisper)

We create an async helper to upload a WAV file to the Whisper transcription model and return the transcription text. The function validates the path and handles common return shapes from the client.
Reference: Whisper docs — https://platform.openai.com/docs/models/whisper-1

Language and Emotion Analysis

Use a chat model to detect the language and provide a one-word emotional descriptor. The function uses a deterministic temperature (0.3) and extracts values using tolerant regular expressions to handle slightly varied replies.
Note: temperature is set to 0.3 to favor more deterministic outputs, which helps reliable parsing of the model response.

Translator Agent and translate_text

This example uses a simple Agent to translate text into English and a Runner to execute it. The Agent/Runner implementation is assumed from the original content; if your agents package returns different shapes, adapt the result extraction accordingly.
If your agents/runner implementation differs, adapt the return extraction accordingly.

Title and Summary Generation

Ask a chat model to provide a concise summary and a suggested title. Temperature is slightly higher for creativity (0.5).
Tip: If you prefer structured outputs (e.g., JSON with title and summary), ask the model to respond in JSON and parse the result. For simple display, the free-text response above often suffices.

Full pipeline: process_audio_translation

This orchestrator composes the previous functions into a complete asynchronous flow. Each step’s output is printed; you can replace prints with logging, storage, or event emissions for production usage.

Run the pipeline

Pass in the full path to your WAV file. In Jupyter or other async-capable REPLs you can await the function directly.
If running from a standard Python script, wrap the call in asyncio:

Troubleshooting common issues

  • File not found: ensure the file_path is correct and accessible by your process.
  • UnboundLocalError or NameError: double-check variable names and that you return the expected attributes (for example result.final_output).
  • API key errors: confirm OPENAI_API_KEY is set and loaded via load_dotenv() or environment variables.
  • Agent/Runner differences: the agents package usage (Agent, Runner) is retained from the original content — adapt Runner.run() and result access if your agents library returns different shapes.
  • Unexpected model output format: prefer instructing the model to respond in a strict format (for example Language: <language>\nEmotion: <emotion> or JSON), then validate with regex or a JSON parser.

Example output (expected)

After running on a French sample, the pipeline prints something like:
  • Transcript: “Apprendre à programmer, c’est comme avoir un super-pouvoir…”
  • Detected language: French
  • Detected emotion: Encouraging
  • Translation: “Learning to program is like having a superpower…”
  • Title and Summary: (a short summary and a suggested title)
You now have a working asynchronous pipeline that transcribes audio, detects language and emotion, translates into English, and generates a title plus a short summary. If you want to extend this pipeline: consider adding speaker diarization, punctuation normalization, or persisting outputs to a database for downstream search and analytics.

Watch Video

Practice Lab