实用模型推荐(三)语音转文本模型:whisper

1.开原地址:https://github.com/openai/whisper

                  https://github.com/guillaumekln/faster-whisper

2.使用场景:语音转文字

3.api封装:

import os
import uvicorn
from fastapi import FastAPI, UploadFile, File
from whisper import load_model

app = FastAPI()
model = load_model("large")

def transcribe_audio(audio_file):
    if not audio_file.filename.endswith((".wav", ".mp3")):
        raise ValueError("Invalid audio file. Supported formats: .wav, .mp3")
    
    file_path = os.path.join("D:/save", audio_file.filename)
    with open(file_path, "wb") as f:
        f.write(audio_file.file.read())
    
    result = model.transcribe(file_path)
    return result["text"]

@app.post("/transcribe")
async def transcribe(upload: UploadFile = File(...)):
    try:
        transcription = transcribe_audio(upload)
        return {"transcription": transcription}
    except Exception as e:
        return {"error": str(e)}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=11120)

 

    

posted @ 2023-06-27 12:00  AmbitiousMice  阅读(334)  评论(0编辑  收藏  举报