- 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
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.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.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.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.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).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 canawait the function directly.
Troubleshooting common issues
- File not found: ensure the
file_pathis 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_KEYis set and loaded viaload_dotenv()or environment variables. - Agent/Runner differences: the
agentspackage usage (Agent, Runner) is retained from the original content — adaptRunner.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)
Links and References
- OpenAI API docs
- Whisper model: https://platform.openai.com/docs/models/whisper-1
- GPT models: https://platform.openai.com/docs/models
- python-dotenv
- Jupyter: https://jupyter.org