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

相关推荐
猫头虎2 天前
DeepSeek刚刚开源了一个3B的 OCR模型:什么是DeepSeek-OCR?单张A100-40G每天可以处理20万+页文档
人工智能·开源·whisper·prompt·aigc·ocr·gpu算力
星野云联AIoT技术洞察10 天前
2025年语音识别(ASR)与语音合成(TTS)技术趋势分析对比
whisper·语音识别·模型部署·tts·asr·嵌入式ai·naturalspeech3
共绩算力14 天前
OpenAI Whisper 语音识别模型:技术与应用全面分析
人工智能·whisper·语音识别·共绩算力
人工智能技术派17 天前
Whisper推理源码解读
人工智能·语言模型·whisper·语音识别
luoyayun36121 天前
PySide6调用OpenAI的Whisper模型进行语音ASR转写
whisper·pyside6·asr
学习是生活的调味剂21 天前
PEFT实战LoRA微调OpenAI Whisper 中文语音识别
人工智能·whisper·语音识别
qq7422349841 个月前
语音识别:PyAudio、SoundDevice、Vosk、openai-whisper、Argos-Translate、FunASR(Python)
python·whisper·语音识别
AI_Gump1 个月前
WhisperLiveKit上手及主观评测
人工智能·whisper
biubiubiu07061 个月前
faster-whisper + FastAPI安装
whisper
cwll20091 个月前
使用ffmpeg8.0的whisper模块语音识别
人工智能·whisper·语音识别