【尝试】本地部署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

相关推荐
手揽回忆怎么睡3 天前
Java集成whisper.cpp
java·开发语言·whisper
YXWik63 天前
Linux安装Whisper(C++版)音频解析文本
linux·c++·whisper
普通网友5 天前
解决下载慢!Whisper 模型国内镜像源汇总与各版本快速获取
whisper
minhuan7 天前
大模型应用:语音转文本(ASR)实践:OpenAI Whisper精准转录解析.21
whisper·asr·多模态模型·语音转文本应用·语音大模型应用
DARLING Zero two♡13 天前
浏览器里跑 AI 语音转写?Whisper Web + cpolar让本地服务跑遍全网
前端·人工智能·whisper
johnny23315 天前
OpenAI Whisper:生态
whisper
不解不惑24 天前
OpenAI whisper 语音识别服务器搭建
服务器·whisper·语音识别
许泽宇的技术分享24 天前
用 OpenAI Whisper + pyannote.audio 打造“谁在说什么”的全栈语音理解系统
macos·whisper·xcode
这儿有一堆花1 个月前
使用 Whisper 转写语音的完整教学
人工智能·ai·whisper
数据饕餮2 个月前
Faster-Whisper命令和意图识别程序设计调优:上下文感知和领域词汇增强
whisper