语音转文本

使用 OpenAI API 的多模态系统

James Chapman

Curriculum Manager, DataCamp

即将学习...

$$

课程目标
  • OpenAI 的音频模型
  • 文本审核
  • 案例研究:客服聊天机器人

展示音频模型、文本审核和案例研究的图片

使用 OpenAI API 的多模态系统

回顾...

from openai import OpenAI


# 创建 OpenAI 客户端 client = OpenAI(api_key="<OPENAI_API_TOKEN>")
# 向 Chat Completions 端点发起请求 response = client.chat.completions.create(
model="gpt-4o-mini", messages=[{"role": "user", "content": "What is the OpenAI API?"}]
)
  • 无需 API 密钥——已为你配置好 🎉
使用 OpenAI API 的多模态系统

回顾...

# 从响应中提取内容
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 的多模态系统

¡Vamos a practicar!

使用 OpenAI API 的多模态系统

Preparing Video For Download...