python音频转文字调用baidu

python音频转文字调用的是百度智能云的接口,因业务需求会涉及比较多数字,所以这里做了数字的处理,可根据自己的需求修改。

python 复制代码
from flask import Flask, request, jsonify
import requests

from flask_limiter import Limiter

app = Flask(__name__)
limiter = Limiter(app)  # 初始化限流扩展

API_KEY = ""    #百度智能云上获取
SECRET_KEY = ""

import re
from cn2an import an2cn, transform

def replace_chinese_numbers(text):
    # 使用正则表达式匹配句子中的中文数字
    chinese_numbers = re.findall(r'[零一二三四五六七八九十百千万]+', text)
    
    # 遍历匹配到的中文数字,逐一替换为阿拉伯数字
    for chinese_number in chinese_numbers:
        arabic_number = transform(chinese_number, 'cn2an')
        text = text.replace(chinese_number, arabic_number)

    return text


@app.route('/transcribe', methods=['POST'])
@limiter.limit("5 per second")  # 设置限流规则为最多同时 5 个请求
def transcribe_audio():
    audio_data = request.data
    access_token = get_access_token()
    if not access_token:
        return jsonify({"error": "Error getting access token"}), 500

    url = "https://vop.baidu.com/server_api"

    headers = {
        'Content-Type': 'audio/pcm; rate=16000',  # 设置正确的 Content-Type
        'Accept': 'application/json',
    }

    params = {
        "cuid": "your_unique_id",  # 替换为你的用户唯一标识,随便写
        "token": access_token,
    }

    response = requests.post(url, headers=headers, params=params, data=audio_data)
    if response.status_code == 200:
        try:
            result = response.json()
            if "result" in result:
                transcript = result["result"][0]
                cleaned_transcript = replace_chinese_numbers(transcript)
                print(cleaned_transcript)
                return jsonify({"transcript": cleaned_transcript})
            else:
                return jsonify({"error": "No transcription found in the response"}), 500
        except UserWarning as warning:
            # 如果出现 UserWarning 异常,返回未处理的 transcript
            warnings.warn(str(warning))
            return jsonify({"transcript": transcript})
    else:
        return jsonify({"error": "Error in transcription request"}), 500

        
def get_access_token():
    url = "https://aip.baidubce.com/oauth/2.0/token"
    params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
    response = requests.post(url, params=params)

    if response.status_code == 200:
        access_token = response.json().get("access_token")
        return access_token
    else:
        print("Error getting access token:", response.text)
        return None

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=16258)
相关推荐
CodeLongBear9 分钟前
Python数据分析 -- Pandas基础入门学习笔记:从核心概念到实操代码
python·conda·pandas
u***u68514 分钟前
PHP最佳实践
开发语言·php
是店小二呀20 分钟前
使用Rust构建一个完整的DeepSeekWeb聊天应用
开发语言·后端·rust
B站_计算机毕业设计之家28 分钟前
python手写数字识别计分系统+CNN模型+YOLOv5模型 深度学习 计算机毕业设计(建议收藏)✅
python·深度学习·yolo·计算机视觉·数据分析·cnn
咖啡の猫1 小时前
二进制与字符编码
python
Tech_Lin1 小时前
手搓工具之手写签字识别提取工具
python
算法如诗1 小时前
**MATLAB R2025a** 环境下,基于 **双向时间卷积网络(BITCN)+ 双向长短期记忆网络(BiLSTM)** 的多特征分类预测完整实现
开发语言·网络·matlab
k09331 小时前
在组件外(.js文件)中使用pinia的方法2--在http.js中使用pinia
开发语言·javascript·http
Amber_372 小时前
php的数组和python的列表 -- 横向对比学习
python·学习·php
二川bro2 小时前
第44节:物理引擎进阶:Bullet.js集成与高级物理模拟
开发语言·javascript·ecmascript