python系列&deep_study系列:实战whisper第三天:fast whisper 语音识别服 ...

打印 上一主题 下一主题

主题 537|帖子 537|积分 1611



实战whisper第三天:fast whisper 语音识别服务器部署,可长途访问,可贸易化部署(全部代码和详细部署步骤)



Fast Whisper 是对 OpenAI 的 Whisper 模子的一个优化版本,它旨在进步音频转录和语音识别任务的速率和效率。Whisper 是一种强大的多语言和多任务语音模子,可以用于语音识别、语音翻译和语音分类等任务。
Fast Whisper 的原理

Fast Whisper 是在原始 Whisper 模子的基础上举行的优化,这些优化重要会合在进步推理速率和降低资源斲丧上。它通过以下方式实现这些目标:
模子剪枝和量化:

剪枝:通过淘汰模子中的参数数量(通常是去除那些对模子性能影响较小的参数),从而淘汰模子的复杂性和运行时内存需求。
量化:将模子的浮点运算转换为整数运算,这通常会显著淘汰模子大小和提升推理速率,特别是在支持整数运算的硬件上。
硬件优化:

Fast Whisper 针对特定的硬件架构(如 GPU 和 TPU)举行了优化,以更高效地利用这些硬件的并行处理能力。
更高效的解码器:

使用更高效的解码策略,好比更小的 beam size 或更快的贪默算法,来淘汰在生成预测时所需的计算量。
Fast Whisper 重要在以下方面举行了改进:

速率:通过上述技术,Fast Whisper 在举行语音到文本转录时的速率更快,这使得它更适合实时或靠近实时的应用场景。
资源斲丧:淘汰了模子的计算需求和内存占用,使得它可以在资源受限的设备上运行,如移动设备和嵌入式系统。
结果:在保持相似的识别正确率的同时,Fast Whisper 的重要上风是速率和效率。在某些环境下,模子优化可能会轻微捐躯正确性,但通常这种捐躯是微小的。
一、部署

下载fast-whisper

GitHub - SYSTRAN/faster-whisper: Faster Whisper transcription with CTranslate2
  1. pip install faster-whisper
复制代码
模子下载:

large-v3模子:https://huggingface.co/Systran/faster-whisper-large-v3/tree/main
large-v2模子:https://huggingface.co/guillaumekln/faster-whisper-large-v2/tree/main
large-v2模子:https://huggingface.co/guillaumekln/faster-whisper-large-v1/tree/main
medium模子:https://huggingface.co/guillaumekln/faster-whisper-medium/tree/main
small模子:https://huggingface.co/guillaumekln/faster-whisper-small/tree/main
base模子:https://huggingface.co/guillaumekln/faster-whisper-base/tree/main
tiny模子:https://huggingface.co/guillaumekln/faster-whisper-tiny/tree/main
示例:

  1. from faster_whisper import WhisperModel
  2. model_size = "large-v3"
  3. # Run on GPU with FP16
  4. model = WhisperModel(model_size, device="cuda", compute_type="float16")
  5. # or run on GPU with INT8
  6. # model = WhisperModel(model_size, device="cuda", compute_type="int8_float16")
  7. # or run on CPU with INT8
  8. # model = WhisperModel(model_size, device="cpu", compute_type="int8")
  9. segments, info = model.transcribe("audio.mp3", beam_size=5)
  10. print("Detected language '%s' with probability %f" % (info.language, info.language_probability))
  11. for segment in segments:
  12.     print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
复制代码
更换成本身的音乐,运行:

二、服务器部署,长途调用

api.py:
  1. from fastapi import FastAPI, File, UploadFile
  2. from fastapi.responses import JSONResponse
  3. from faster_whisper import WhisperModel
  4. import shutil
  5. import os
  6. import uvicorn
  7. app = FastAPI()
  8. # 配置 Whisper 模型
  9. model_size = "large-v3"
  10. model = WhisperModel(model_size, device="cuda", compute_type="float16")
  11. @app.post("/transcribe/")
  12. async def transcribe_audio(file: UploadFile = File(...)):
  13.     temp_file = f"temp_{file.filename}"
  14.    
  15.     # 保存上传的文件到临时文件
  16.     with open(temp_file, 'wb') as buffer:
  17.         shutil.copyfileobj(file.file, buffer)
  18.     try:
  19.         # 使用 Whisper 模型进行转录
  20.         segments, info = model.transcribe(temp_file, beam_size=5)
  21.         os.remove(temp_file)  # 删除临时文件
  22.         
  23.         # 组装转录结果
  24.         results = [{
  25.             "start": segment.start,
  26.             "end": segment.end,
  27.             "text": segment.text
  28.         } for segment in segments]
  29.         return JSONResponse(content={
  30.             "language": info.language,
  31.             "language_probability": info.language_probability,
  32.             "transcription": results
  33.         })
  34.     except Exception as e:
  35.         os.remove(temp_file)  # 确保即使出错也删除临时文件
  36.         return JSONResponse(status_code=500, content={"message": str(e)})
  37. if __name__ == "__main__":
  38.     uvicorn.run(app, host="0.0.0.0", port=1050)
复制代码
长途调用:
http://192.168.110.12:1050/transcribe/

三、代码中调用服务器

更换成本身部署服务的ip:
  1. import requests
  2. with open('5.wav', 'rb') as f:
  3.     response = requests.post(
  4.         'http://****.***.***.***:1050/transcribe/',
  5.         files={'file': ('output.wav', f, 'audio/wav')})
  6. data = response.json()  # Get the JSON response
  7. text = data['transcription'][0]['text']
  8. print("识别结果:", text)
复制代码

总结:结果杠杠的,速率比whisper快太多了 ,可举行贸易应用了。







学术菜鸟小晨
实战whisper第三天:fast whisper 语音识别服务器部署,可长途访问,可贸易化部署(全部代码和详细部署步骤)

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

惊落一身雪

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表