【尝试】本地部署openai-whisper,通过 http请求识别

安装whisper的教程,已在

https://blog.csdn.net/qq_23938507/article/details/149394418

https://blog.csdn.net/qq_23938507/article/details/149326290

中说明。

1、创建whisperDemo1.py

python 复制代码
from fastapi import FastAPI, UploadFile, File
import whisper
import os

app = FastAPI()

# 加载Whisper模型
model = whisper.load_model("medium")  # 可替换为 small/medium/large


@app.post("/transcribe/")
async def transcribe_audio(file: UploadFile = File(...)):
    try:
        # 临时保存上传的文件[7](@ref)
        temp_path = f"temp_{file.filename}"
        with open(temp_path, "wb") as f:
            f.write(await file.read())

        # 使用Whisper转录[7](@ref)
        result = model.transcribe(temp_path)

        # 删除临时文件
        os.remove(temp_path)

        return {"text": result["text"]}

    except Exception as e:
        return {"error": str(e)}

2、创建request001.py

python 复制代码
import requests


def upload_file(file_path, server_url):
    try:
        # 以二进制模式打开文件并上传[8](@ref)[9](@ref)
        with open(file_path, 'rb') as f:
            files = {'file': f}
            response = requests.post(server_url, files=files, timeout=600)

        # 检查响应状态码[9](@ref)
        if response.status_code == 200:
            try:
                # 尝试解析JSON响应[3](@ref)
                result = response.json()
                print("上传成功,转录结果:", result['text'])
            except ValueError:
                print("服务器返回非JSON响应:", response.text)
        else:
            print(f"上传失败,状态码:{response.status_code}\n响应内容:{response.text}")

    except FileNotFoundError:
        print(f"错误:文件 {file_path} 不存在")
    except requests.exceptions.RequestException as e:
        print(f"网络请求失败:{e}")


# 使用示例
if __name__ == "__main__":
    upload_file(
        file_path="001.mp3",
        server_url=" http://127.0.0.1:8000/transcribe/"
    )

3、运行whisperDemo1

在终端输入:

bash 复制代码
uvicorn whisperDemo1:app --host 0.0.0.0 --port 8000

可以通过pip安装uvicorn、fastapi

可以验证服务是否 有开启成功

启动后,在浏览器输入:

127.0.0.1:8000/docs

相关推荐
Micheal_Dad18 小时前
【尝试】基于openai-whisper进行语音转文字windows版本
whisper
andyguo4 天前
语音识别的速度革命:从 Whisper 到 Whisper-CTranslate2,我经历了什么?
人工智能·学习·ai·whisper·语音识别·xcode·ai测评
番茄老夫子4 天前
OpenAI推出的语音识别系统Whisper简析
人工智能·whisper·语音识别
A-刘晨阳7 天前
语音转文字「本地化」新解!Whisper Web+cpolar实现零服务器部署与远程操作
服务器·前端·whisper
云空24 天前
《Whisper模型版本及下载链接》
whisper
苗杨1 个月前
【Faster-Whisper】离线识别本地视频并生成字幕
python·whisper·音视频
云空1 个月前
《Whisper:OpenAI的先进语音识别模型》
人工智能·whisper·语音识别
企鹅侠客1 个月前
19|Whisper+ChatGPT:请AI代你听播客
人工智能·ai·chatgpt·whisper
落沐萧萧1 个月前
本地多语言 AI 字幕组:Whisper 实战教程
人工智能·whisper