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

相关推荐
lili-felicity3 天前
CANN加速Whisper语音识别推理:流式处理与实时转录优化
人工智能·whisper·语音识别
阿里巴啦4 天前
python+yt-dlp开源项目,支持 YouTube, Bilibili, TikTok/抖音,快手 等多个平台的视频/音频/字幕下载/ai摘要等功能
python·ffmpeg·whisper·音视频·视频处理·ai摘要·音视频转录
普通网友4 天前
极致压缩:Whisper.cpp 量化版本清单与 ggml 格式模型下载
whisper
天空中最亮的一颗星4 天前
如何下载、安装whisper、faster_whisper?
whisper
runner365.git6 天前
ffmpeg8.0合入whisper,语音识别模型终于进入ffmpeg
ffmpeg·whisper·语音识别
Blossom.1187 天前
从单点工具到智能流水线:企业级多智能体AI开发工作流架构实战
人工智能·笔记·python·深度学习·神经网络·架构·whisper
Blossom.1187 天前
从“金鱼记忆“到“超级大脑“:2025年AI智能体记忆机制与MoE架构的融合革命
人工智能·python·算法·架构·自动化·whisper·哈希算法
Blossom.11813 天前
把大模型塞进蓝牙耳机:1.46MB 的 Whisper-Lite 落地全记录
人工智能·笔记·python·深度学习·神经网络·chatgpt·whisper
love530love18 天前
彻底解决 ComfyUI Mixlab 插件 Whisper.available False 的报错
人工智能·windows·python·whisper·win_comfyui