当前位置: 首页 > news >正文

python音频转文字调用baidu

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

from flask import Flask, request, jsonify
import requestsfrom flask_limiter import Limiterapp = Flask(__name__)
limiter = Limiter(app)  # 初始化限流扩展API_KEY = ""    #百度智能云上获取
SECRET_KEY = ""import re
from cn2an import an2cn, transformdef 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.dataaccess_token = get_access_token()if not access_token:return jsonify({"error": "Error getting access token"}), 500url = "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"}), 500except UserWarning as warning:# 如果出现 UserWarning 异常,返回未处理的 transcriptwarnings.warn(str(warning))return jsonify({"transcript": transcript})else:return jsonify({"error": "Error in transcription request"}), 500def 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_tokenelse:print("Error getting access token:", response.text)return Noneif __name__ == '__main__':app.run(host='0.0.0.0', port=16258)

http://www.lryc.cn/news/159135.html

相关文章:

  • 靶场溯源第二题
  • mysql 的增删改查以及模糊查询、字符集语句的使用
  • Python Django框架中文教程:学习简单、灵活、高效的Web应用程序框架
  • Docker认识即安装
  • chrome 谷歌浏览器 导出插件拓展和导入插件拓展
  • fastjson漏洞批量检测工具
  • Vue进阶(六十七)页面刷新路由传参丢失问题分析及解决
  • 阿里云ubuntu服务器搭建ftp服务器
  • 03 卷积操作图片
  • 软考:中级软件设计师:程序语言基础:表达式,标准分类,法律法规,程序语言特点,函数传值传址
  • Java“牵手”1688商品详情数据,1688商品详情API接口,1688API接口申请指南
  • stable diffusion实践操作-批次出图
  • LeetCode热题100 【cpp】题解(一)哈希表和双指针
  • Python爬虫常见代理池实现和优化
  • 前端面试的话术集锦第 3 篇:进阶篇上
  • 【文字到语音的论文总结】
  • E. Data Structures Fan(思维 + 异或前缀和)
  • 初学python爬虫学习笔记——爬取网页中小说标题
  • The WebSocket session [x] has been closed and no method (apart from close())
  • 前端实现展开收起的效果 (react)
  • ABY2.0:更低的通信开销
  • vue项目预览图片
  • Tomcat 安装
  • 计算机网络的故事——HTTP报文内的HTTP信息
  • CF1120 D. Power Tree 巧妙的图论转化
  • 【算法训练-字符串 三】最长公共子串、最长公共子序列
  • lintcode 1446 · 01矩阵走路问题 【两次BFS, VIP 中等 1也计算距离,但是不入队列】
  • 第一个实例:QT实现汽车电子仪表盘
  • 【MySQL系列】MySQL的事务管理的学习(一)_ 事务概念 | 事务操作方式 | 事务隔离级别
  • 扫地机器人还能创新吗?云鲸给了个Yes