创建app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import tempfile
|
| 4 |
+
import spaces # 导入 Hugging Face 的 ZeroGPU 装饰器
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 7 |
+
from fastapi.responses import JSONResponse
|
| 8 |
+
from faster_whisper import WhisperModel
|
| 9 |
+
|
| 10 |
+
# 1. 声明加载的模型大小,"small" 对中文支持很好且速度快
|
| 11 |
+
MODEL_SIZE = "small"
|
| 12 |
+
|
| 13 |
+
# 2. 初始化模型(将其载入内存,优先检测 CUDA)
|
| 14 |
+
model = WhisperModel(MODEL_SIZE, device="cpu", compute_type="float32")
|
| 15 |
+
|
| 16 |
+
# 3. 核心转录函数(加上 @spaces.GPU 装饰器白嫖 A100/A10G 算力)
|
| 17 |
+
@spaces.GPU
|
| 18 |
+
def transcribe_core(audio_path: str):
|
| 19 |
+
# 动态将模型放到 GPU 上执行推理
|
| 20 |
+
gpu_model = WhisperModel(MODEL_SIZE, device="cuda", compute_type="float16")
|
| 21 |
+
segments, info = gpu_model.transcribe(audio_path, beam_size=5)
|
| 22 |
+
|
| 23 |
+
# 将分段识别的文字拼接成一整段文本
|
| 24 |
+
text = "".join([segment.text for segment in segments])
|
| 25 |
+
return text
|
| 26 |
+
|
| 27 |
+
# 4. 创建 Gradio 界面 (前端网页测试用)
|
| 28 |
+
def gradio_predict(audio_path):
|
| 29 |
+
if audio_path is None:
|
| 30 |
+
return "请先上传音频或录音!"
|
| 31 |
+
return transcribe_core(audio_path)
|
| 32 |
+
|
| 33 |
+
demo = gr.Interface(
|
| 34 |
+
fn=gradio_predict,
|
| 35 |
+
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="输入音频"),
|
| 36 |
+
outputs=gr.Textbox(label="识别出的文本"),
|
| 37 |
+
title="Whisper 语音识别 API 节点",
|
| 38 |
+
description="支持网页端直接测试,同时也支持 OpenAI 兼容的 /v1/audio/transcriptions API 接口!"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# 5. 获取 Gradio 底层的 FastAPI 实例并扩展 API 路由
|
| 42 |
+
app = demo.app # 这就是底层的 FastAPI 实例
|
| 43 |
+
|
| 44 |
+
@app.post("/v1/audio/transcriptions")
|
| 45 |
+
async def transcribe_api(
|
| 46 |
+
file: UploadFile = File(...),
|
| 47 |
+
model: str = Form("whisper-1") # 兼容 OpenAI 参数
|
| 48 |
+
):
|
| 49 |
+
# 创建临时文件保存上传的音频数据
|
| 50 |
+
suffix = os.path.splitext(file.filename)[1] or ".mp3"
|
| 51 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
| 52 |
+
shutil.copyfileobj(file.file, temp_file)
|
| 53 |
+
temp_path = temp_file.name
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
# 调用 ZeroGPU 加速的转录核心
|
| 57 |
+
transcription_text = transcribe_core(temp_path)
|
| 58 |
+
except Exception as e:
|
| 59 |
+
raise HTTPException(status_code=500, detail=f"Transcription failed: {str(e)}")
|
| 60 |
+
finally:
|
| 61 |
+
# 清理临时文件
|
| 62 |
+
if os.path.exists(temp_path):
|
| 63 |
+
os.remove(temp_path)
|
| 64 |
+
|
| 65 |
+
# 返回符合 OpenAI 规范的 JSON 格式
|
| 66 |
+
return JSONResponse(content={"text": transcription_text})
|