語音轉文字

使用 OpenAI API 的多模態系統

James Chapman

Curriculum Manager, DataCamp

接下來你會學到…

$$

課程目標
  • OpenAI 音訊模型
  • 文字審核
  • 案例研究:客服聊天機器人

一張展示音訊模型、文字審核與案例研究的圖片

使用 OpenAI API 的多模態系統

重點回顧…

from openai import OpenAI


# Create the OpenAI client client = OpenAI(api_key="<OPENAI_API_TOKEN>")
# Create a request to the Chat Completions endpoint response = client.chat.completions.create(
model="gpt-4o-mini", messages=[{"role": "user", "content": "What is the OpenAI API?"}]
)
  • 不需要 API 金鑰——已為你設定好 🎉
使用 OpenAI API 的多模態系統

重點回顧…

# Extract the content from the response
print(response.choices[0].message.content)
The OpenAI API is a cloud-based service provided by OpenAI that allows developers
to integrate advanced AI models into their applications.

$$

  • OpenAI API 不只支援文字 🚀
使用 OpenAI API 的多模態系統

OpenAI 的音訊模型

語音轉文字 功能:

  • 語音轉錄
  • 非英文音訊翻譯
  • 支援 mp3mp4mpegmpgam4awavwebm(上限 25 MB)

 

應用情境

  • 會議逐字稿
  • 影片字幕

一個顯示音訊錄製與文字區塊的圖示。

  • 客服來電處理
使用 OpenAI API 的多模態系統

載入音訊檔

 

範例:轉錄 meeting_recording.mp3

audio_file = open("meeting_recording.mp3", "rb")

$$

如果檔案在「不同目錄」

audio_file = open("path/to/file/meeting_recording.mp3", "rb")
使用 OpenAI API 的多模態系統

建立轉錄

  • 音訊端點
audio_file= open("meeting_recording.mp3", "rb")

response = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file
)
print(response)
Transcription(text="Welcome everyone to the June product monthly. We'll get started in...)
1 https://platform.openai.com/docs/guides/speech-to-text
使用 OpenAI API 的多模態系統

逐字稿內容

print(response.text)
Welcome everyone to the June product monthly. We'll get started in just a minute.
Alright, let's get started. Today's agenda will start with a spotlight from Chris
on the new mobile user onboarding flow, then we'll review how we're tracking on
our quarterly targets, and finally, we'll finish with another spotlight from Katie
who will discuss the upcoming branding updates...
使用 OpenAI API 的多模態系統

轉錄非英文音訊

一個顯示音訊錄製與文字區塊的圖示。

轉錄流程

  1. 使用 open() 開啟音訊檔
  2. 送出轉錄請求
  3. 擷取文字
使用 OpenAI API 的多模態系統

建立翻譯

audio_file = open("non_english_audio.m4a", "rb")


response = client.audio.translations.create(
model="whisper-1",
file=audio_file
)
print(response.text)
The search volume for keywords like A I has increased rapidly since the launch of
Cha GTP.
使用 OpenAI API 的多模態系統

轉錄效能

 

  • 效能差異可能很大,取決於:
    • 音訊品質
    • 音訊語言
    • 模型對主題的掌握程度

世界各地的不同語言。

使用 OpenAI API 的多模態系統

一起來練習吧!

使用 OpenAI API 的多模態系統

Preparing Video For Download...