Skip to content

whisper使用示例

whisper 模型接口说明


该接口基于 Whisper 模型实现语音转文本功能,支持常见音频格式。

基础概念


  • Whisper模型: OpenAI 开源的语音识别模型,支持多语言转写

  • 音频格式: 支持 mp3、wav、m4a 等常见格式

接口地址


POST https://api.changhuai.ai/v1/audio/transcriptions

请求参数


参数名

类型

必填

说明

model

string

固定值 “whisper-1”

file

file

要转写的音频文件

请求头


Authorization: Bearer sk- **** **** **** **** **** * # 替换为你的 API 令牌

Python 调用示例


python
import json
import requests

def voice_to_text(file_path):
    """
    语音转文本功能
    
    参数:
        file_path: 音频文件路径
        
    返回:
        识别出的文本内容
    """
    url = "https://api.changhuai.ai/v1/audio/transcriptions"
    
    # 构造请求参数
    payload = {"model": "whisper-1"}
    files = {"file": ("audio.mp3", open(file_path, "rb"))}
    
    # 设置请求头(请替换为你的API密钥)
    headers = {"Authorization": "Bearer sk-***************************"} # 替换为你的 API 令牌
    
    # 发送POST请求
    response = requests.post(url, headers=headers, data=payload, files=files)
    
    # 解析响应数据
    data = json.loads(response.text)
    
    # 返回识别结果
    return data.get("text", "")

# 使用示例
print(voice_to_text("audio.mp3"))  # 替换为你的音频文件路径

响应示例


成功响应:

json
{
    "text": "这是识别出的文本内容"
}

注意事项


  1. 音频文件大小建议不超过25MB

  2. 支持中文、英文等多种语言

  3. 请妥善保管API密钥,不要泄露