开源模型应用落地-语音转文本-openai-STT服务-AIGC应用探索(四)

一、前言

语音转文本技术具有重要价值。它能提高信息记录和处理的效率,使人们可以快速将语音内容转换为可编辑、可存储的文本形式,方便后续查阅和分析。在教育领域,可帮助学生更好地记录课堂重点;在办公场景中,能简化会议记录工作。同时,该技术也为残障人士提供了便利,让他们能更方便地与外界交流。此外,对于媒体行业、客服行业等都有着广泛的应用,极大地提升了工作流程和服务质量。

本文将介绍OpenAI++付费的++ 语音识别服务。这个服务可以帮助用户将++语音转换成文本++,为用户提供方便实用的语音转文本服务。


二、术语介绍

**2.1.**语音转文本

也称为语音识别或自动语音识别 (ASR)是一种将语音音频转换为文字的技术。它利用计算机程序和算法来监听语音输入,并将其转换为可读的文字输出。

**2.2.**Whisper(付费版本)

Whisper是一个通用语音识别模型。它是在大型多样化音频数据集上训练的,也是一个多任务模型,可以执行多语言语音识别、语音翻译和语言鉴别。

目前,*++开源版本++*的Whisper和通过Openai API提供的版本没有区别。但是,通过Openai API,提供了优化的推理过程,这使得通过API运行Whisper的速度要快于其他方式。

具体参见: https://platform.openai.com/docs/models/whisper


三、构建环境

3.1.基础环境

  1. 操作系统:centos7
  2. Tesla V100-SXM2-32GB CUDA Version: 12.2

3.2.安装虚拟环境

bash 复制代码
conda create -n whisper  python=3.10
conda activate whisper
pip install openai

四、技术实现

4.1. 准备测试音频文件

先搞个测试音频: 英语诗歌朗诵:Freedom 珍惜自由_Mp3免费下载-在线听力 - 听力课堂

4.2. Openai调用方式

4.2.1.Transcriptions

python 复制代码
# -*- coding: utf-8 -*-
import os
from openai import OpenAI

os.environ["OPENAI_API_KEY"] = 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'    #你的Open AI Key

if __name__ == '__main__':
    input_path = "C:\\Downloads\\freedom.mp3"
    client = OpenAI()

    audio_file = open(input_path, "rb")
    transcription = client.audio.transcriptions.create(
        model="whisper-1",
        file=audio_file
    )
    print(transcription.text)

调用结果:

结论:

Openai生成的文本和官方提供的文本有一些差异( 我也没有听过测试音频,不敢保证官方提供的文本是否百分百正确,或者模型生成的才是正解,你们来动手实践一下,看看谁更准确呢? ),对比如下:

注意:

文件上传目前限制为25 MB,支持以下输入文件类型:mp3、mp4、mpeg、mpga、m4a、wav和webm。

4.2.2.Translations

python 复制代码
# -*- coding: utf-8 -*-
import os
from openai import OpenAI

os.environ["OPENAI_API_KEY"] = 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'    #你的Open AI Key

if __name__ == '__main__':
    input_path = "C:\\Downloads\\freedom.mp3"
    client = OpenAI()

    audio_file = open(input_path, "rb")
    translation = client.audio.translations.create(
        model="whisper-1",
        file=audio_file
    )
    print(translation.text)

调用结果:

执行结果与实现方式一一致

4.3. 传统调用方式

python 复制代码
# -*- coding: utf-8 -*-
import requests

url = "https://api.openai.com/v1/audio/transcriptions"
OPENAI_API_KEY = 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'    #你的Open AI Key

if __name__ == '__main__':
    input_path = "C:\\Downloads\\freedom.mp3"

    headers = {
        "Authorization": f"Bearer {OPENAI_API_KEY}"
    }

    files = {
        'file': ('freedom.mp3', open(input_path, 'rb'), 'application/octet-stream')
    }

    params = {'model': 'whisper-1'}
    with open(input_path, 'rb') as file:
        response = requests.post(url, files={'file': file}, data=params, headers=headers)

        if response.status_code == 200:
            print("Audio transcription successful!")
            print(response.json())
        else:
            print("Audio transcription failed.")
            print(response.text)

调用结果:

执行结果与实现方式一一致

PS:

  1. 实现方式三不需要安装openai库,更轻便,通用性更好

  2. 切换url(https://api.openai.com/v1/audio/transcriptions)可以实现音频翻译的功能

4.4. CURL调用方式

Transcriptions

bash 复制代码
curl --request POST \
  --url https://api.openai.com/v1/audio/transcriptions \
  --header "Authorization: Bearer $OPENAI_API_KEY" \
  --header 'Content-Type: multipart/form-data' \
  --form file=@/path/to/file/audio.mp3 \
  --form model=whisper-1

Translations

bash 复制代码
curl --request POST \
  --url https://api.openai.com/v1/audio/translations \
  --header "Authorization: Bearer $OPENAI_API_KEY" \
  --header 'Content-Type: multipart/form-data' \
  --form file=@/path/to/file/german.mp3 \
  --form model=whisper-1

五、附带说明

5.1. 方式一的​Transcriptions​和方式二的 Translations的区别

  • Transcriptions:将音频转录为音频所使用的任何语言。
  • Translations:将音频翻译并转录成英文。与Transcriptions差异是Translations的输出不是原始输入语言,而是翻译成英文文本。

5.2. 方式三出现"Could not parse multipart form"问题

解决方法:把请求头的*"Content-Type": "multipart/form-data"* 声明去掉

相关推荐
yzx99101321 分钟前
从机器视觉到图像识别:计算机视觉的多维探索
人工智能·深度学习·机器学习
是Dream呀23 分钟前
GPT-5时代的AI工具:AiOnly一站式平台深度体验报告
人工智能·深度学习·机器学习
杨杨杨大侠2 小时前
Spring AI Alibaba Multi-Agent 架构详解
spring·aigc
bug菌4 小时前
你的Java代码还在"屎山"里挣扎?Trae能否助你重获新生?
aigc·ai编程·trae
iThinkAi4 小时前
抖音评论区才是金矿!我用Coze智能体爬了10w条评论,挖出比热点猛10倍的爆款选题库
aigc
Mintopia4 小时前
🎨 AiGC × Web Markdown:把 AI 的碎碎念渲染成人类能看懂的彩虹
前端·javascript·aigc
CoovallyAIHub5 小时前
标注成本骤降,DINOv3炸裂发布!冻结 backbone 即拿即用,性能对标SOTA
深度学习·算法·计算机视觉
聚客AI5 小时前
深度拆解AI大模型从训练框架、推理优化到市场趋势与基础设施挑战
图像处理·人工智能·pytorch·深度学习·机器学习·自然语言处理·transformer
arron88996 小时前
YOLOv8n-pose 模型使用
人工智能·深度学习·yolo
程序员X小鹿14 小时前
实测阿里开源图像编辑模型Qwen-Image-Edit,汉字也能无痕修改!1秒救废图!(附实测案例)
aigc