π Future-Proof & Clickable β This doc is structured for quick navigation, visual clarity, and easy updates.
- π OpenAI API Key Usage
- ποΈ Whisper AI β Speech to Text
- π¦ Why Install
multipart - π Using Latest OpenAI Version
- β‘ Quick Reference Table
os.getenv()Reads Env Vars β FetchesOPEN_AI_API_KEYfrom environment variables.- Must Export It π β Before running:
export OPEN_AI_API_KEY="your_api_key" ## ποΈ 2. Whisper AI β Speech to Text
- What is Whisper? ποΈ β OpenAIβs automatic speech recognition (ASR) model for converting audio to text.
- Usage in Code π»:
import openai openai.api_key = "your_api_key" audio_file = open("your_audio.mp3", "rb") transcript = openai.Audio.transcribe("whisper-1", audio_file) print(transcript["text"])
- Purpose β Parses
multipart/form-datafor handling file uploads in FastAPI and similar frameworks. - Example:
from fastapi import FastAPI, File, UploadFile app = FastAPI() @app.post("/upload/") async def upload(file: UploadFile = File(...)): return {"filename": file.filename}
- API Key Change π β Old:
openai.apikeyβ β New:OpenAI(api_key=...)β - File Handling π β Files are temporarily saved before being sent to the API.
- Method Update π οΈ β Old:
transcribe()β β New:client.audio.transcriptions.create(...)β - FastAPI Integration β‘ β
UploadFile = File(...)ensures propermultipart/form-datahandling. - Future-Proofing π β v1+ syntax prevents deprecation issues and keeps code up-to-date.
| Section | Topic | Key Command / Code | Notes |
|---|---|---|---|
| 1 | OpenAI API Key Usage | export OPEN_AI_API_KEY="your_api_key" |
Set in terminal before running app |
| 2 | Whisper AI β Speech to Text | openai.Audio.transcribe("whisper-1", audio_file) |
Works with .mp3, .wav, .m4a, etc. |
| 3 | Install multipart |
pip install python-multipart |
Needed for file uploads in FastAPI |
| 4 | Latest OpenAI Version | OpenAI(api_key=...) & client.audio.transcriptions.create(...) |
Updated v1+ SDK syntax |
