惊落一身雪 发表于 2024-8-10 16:40:00

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


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

https://img-blog.csdnimg.cn/direct/30eee07e7823446c92c1d85c8b54c0d1.png#pic_center
https://img-blog.csdnimg.cn/direct/8f085e5999c14fe0a4654c8548f6ffab.png#pic_center
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
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
示例:

from faster_whisper import WhisperModel

model_size = "large-v3"

# Run on GPU with FP16
model = WhisperModel(model_size, device="cuda", compute_type="float16")

# or run on GPU with INT8
# model = WhisperModel(model_size, device="cuda", compute_type="int8_float16")
# or run on CPU with INT8
# model = WhisperModel(model_size, device="cpu", compute_type="int8")

segments, info = model.transcribe("audio.mp3", beam_size=5)

print("Detected language '%s' with probability %f" % (info.language, info.language_probability))

for segment in segments:
    print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
更换成本身的音乐,运行:
https://img-blog.csdnimg.cn/direct/6036228643a24e3cbe56ea70d40d4ccc.png#pic_center
二、服务器部署,长途调用

api.py:
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
from faster_whisper import WhisperModel
import shutil
import os
import uvicorn

app = FastAPI()

# 配置 Whisper 模型
model_size = "large-v3"
model = WhisperModel(model_size, device="cuda", compute_type="float16")

@app.post("/transcribe/")
async def transcribe_audio(file: UploadFile = File(...)):
    temp_file = f"temp_{file.filename}"
   
    # 保存上传的文件到临时文件
    with open(temp_file, 'wb') as buffer:
      shutil.copyfileobj(file.file, buffer)

    try:
      # 使用 Whisper 模型进行转录
      segments, info = model.transcribe(temp_file, beam_size=5)
      os.remove(temp_file)# 删除临时文件
      
      # 组装转录结果
      results = [{
            "start": segment.start,
            "end": segment.end,
            "text": segment.text
      } for segment in segments]

      return JSONResponse(content={
            "language": info.language,
            "language_probability": info.language_probability,
            "transcription": results
      })
    except Exception as e:
      os.remove(temp_file)# 确保即使出错也删除临时文件
      return JSONResponse(status_code=500, content={"message": str(e)})

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=1050)
长途调用:
http://192.168.110.12:1050/transcribe/
https://img-blog.csdnimg.cn/direct/31102e15669e47e589d6acce1e4f7722.png#pic_center
三、代码中调用服务器

更换成本身部署服务的ip:
import requests

with open('5.wav', 'rb') as f:
    response = requests.post(
      'http://****.***.***.***:1050/transcribe/',
      files={'file': ('output.wav', f, 'audio/wav')})
data = response.json()# Get the JSON response
text = data['transcription']['text']
print("识别结果:", text)
https://img-blog.csdnimg.cn/direct/c1be00b7e45f40459d9b34785144be4d.png#pic_center
总结:结果杠杠的,速率比whisper快太多了 ,可举行贸易应用了。






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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: python系列&deep_study系列:实战whisper第三天:fast whisper 语音识别服