Update app.py
Browse files
app.py
CHANGED
|
@@ -10,9 +10,7 @@ from transformers import pipeline
|
|
| 10 |
# 1. 声明加载的模型
|
| 11 |
MODEL_NAME = "openai/whisper-small"
|
| 12 |
|
| 13 |
-
# 2. 全局
|
| 14 |
-
# 官方的 spaces 拦截层会在容器启动时自动拦截它防止在 CPU 阶段报错;
|
| 15 |
-
# 并在调用带 @spaces.GPU 装饰器的函数时,自动、无损地把它调度到 A100 GPU 显存中运行。
|
| 16 |
pipe = pipeline(
|
| 17 |
"automatic-speech-recognition",
|
| 18 |
model=MODEL_NAME,
|
|
@@ -20,20 +18,17 @@ pipe = pipeline(
|
|
| 20 |
device="cuda"
|
| 21 |
)
|
| 22 |
|
| 23 |
-
# 3. 核心计算函数
|
| 24 |
@spaces.GPU
|
| 25 |
def transcribe_core(audio_path: str, target_language: str = None, is_translate: bool = False):
|
| 26 |
-
# 【最关键的安全操作】绝不手动编写 pipe.model.to("cuda") 或 torch.autocast
|
| 27 |
-
# 彻底杜绝由于显卡热插拔带来的 RuntimeError!
|
| 28 |
generate_kwargs = {}
|
| 29 |
if target_language:
|
| 30 |
generate_kwargs["language"] = target_language
|
| 31 |
-
|
| 32 |
if is_translate:
|
| 33 |
generate_kwargs["language"] = "english"
|
| 34 |
generate_kwargs["task"] = "translate"
|
| 35 |
-
|
| 36 |
-
# 直接运行推理,ZeroGPU 机制会完美托管这一步
|
| 37 |
result = pipe(audio_path, generate_kwargs=generate_kwargs)
|
| 38 |
return result["text"]
|
| 39 |
|
|
@@ -44,19 +39,22 @@ def gradio_predict(audio_path):
|
|
| 44 |
try:
|
| 45 |
return transcribe_core(audio_path)
|
| 46 |
except Exception as e:
|
| 47 |
-
return f"
|
| 48 |
|
| 49 |
demo = gr.Interface(
|
| 50 |
fn=gradio_predict,
|
| 51 |
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="输入音频"),
|
| 52 |
outputs=gr.Textbox(label="识别出的文本"),
|
| 53 |
title="Whisper 语音识别 API 节点",
|
| 54 |
-
description="【完美兼容 OpenAI 规范
|
| 55 |
)
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
#
|
| 60 |
async def process_openai_audio_request(file, response_format, language, is_translate):
|
| 61 |
suffix = os.path.splitext(file.filename)[1] or ".mp3"
|
| 62 |
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
|
@@ -73,7 +71,7 @@ async def process_openai_audio_request(file, response_format, language, is_trans
|
|
| 73 |
|
| 74 |
if response_format in ["text", "vtt", "srt"]:
|
| 75 |
return PlainTextResponse(text)
|
| 76 |
-
|
| 77 |
return JSONResponse(content={"text": text})
|
| 78 |
|
| 79 |
|
|
@@ -81,11 +79,11 @@ async def process_openai_audio_request(file, response_format, language, is_trans
|
|
| 81 |
@app.post("/v1/audio/transcriptions")
|
| 82 |
async def transcribe_api(
|
| 83 |
file: UploadFile = File(...),
|
| 84 |
-
model: str = Form("whisper-1"),
|
| 85 |
-
language: str = Form(None),
|
| 86 |
-
prompt: str = Form(None),
|
| 87 |
-
response_format: str = Form("json"),
|
| 88 |
-
temperature: float = Form(0.0)
|
| 89 |
):
|
| 90 |
return await process_openai_audio_request(
|
| 91 |
file=file,
|
|
@@ -111,5 +109,5 @@ async def translate_api(
|
|
| 111 |
is_translate=True
|
| 112 |
)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
|
|
|
| 10 |
# 1. 声明加载的模型
|
| 11 |
MODEL_NAME = "openai/whisper-small"
|
| 12 |
|
| 13 |
+
# 2. 全局初始化 Pipeline
|
|
|
|
|
|
|
| 14 |
pipe = pipeline(
|
| 15 |
"automatic-speech-recognition",
|
| 16 |
model=MODEL_NAME,
|
|
|
|
| 18 |
device="cuda"
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# 3. 核心计算函数
|
| 22 |
@spaces.GPU
|
| 23 |
def transcribe_core(audio_path: str, target_language: str = None, is_translate: bool = False):
|
|
|
|
|
|
|
| 24 |
generate_kwargs = {}
|
| 25 |
if target_language:
|
| 26 |
generate_kwargs["language"] = target_language
|
| 27 |
+
|
| 28 |
if is_translate:
|
| 29 |
generate_kwargs["language"] = "english"
|
| 30 |
generate_kwargs["task"] = "translate"
|
| 31 |
+
|
|
|
|
| 32 |
result = pipe(audio_path, generate_kwargs=generate_kwargs)
|
| 33 |
return result["text"]
|
| 34 |
|
|
|
|
| 39 |
try:
|
| 40 |
return transcribe_core(audio_path)
|
| 41 |
except Exception as e:
|
| 42 |
+
return f"错误: {str(e)}"
|
| 43 |
|
| 44 |
demo = gr.Interface(
|
| 45 |
fn=gradio_predict,
|
| 46 |
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="输入音频"),
|
| 47 |
outputs=gr.Textbox(label="识别出的文本"),
|
| 48 |
title="Whisper 语音识别 API 节点",
|
| 49 |
+
description="【完美兼容 OpenAI 规范】"
|
| 50 |
)
|
| 51 |
|
| 52 |
+
# ===========================================================================
|
| 53 |
+
# 关键修改部分:正确初始化 FastAPI 应用
|
| 54 |
+
# ===========================================================================
|
| 55 |
+
app = FastAPI()
|
| 56 |
|
| 57 |
+
# 辅助函数:处理 OpenAI 请求
|
| 58 |
async def process_openai_audio_request(file, response_format, language, is_translate):
|
| 59 |
suffix = os.path.splitext(file.filename)[1] or ".mp3"
|
| 60 |
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
|
|
|
| 71 |
|
| 72 |
if response_format in ["text", "vtt", "srt"]:
|
| 73 |
return PlainTextResponse(text)
|
| 74 |
+
|
| 75 |
return JSONResponse(content={"text": text})
|
| 76 |
|
| 77 |
|
|
|
|
| 79 |
@app.post("/v1/audio/transcriptions")
|
| 80 |
async def transcribe_api(
|
| 81 |
file: UploadFile = File(...),
|
| 82 |
+
model: str = Form("whisper-1"),
|
| 83 |
+
language: str = Form(None),
|
| 84 |
+
prompt: str = Form(None),
|
| 85 |
+
response_format: str = Form("json"),
|
| 86 |
+
temperature: float = Form(0.0)
|
| 87 |
):
|
| 88 |
return await process_openai_audio_request(
|
| 89 |
file=file,
|
|
|
|
| 109 |
is_translate=True
|
| 110 |
)
|
| 111 |
|
| 112 |
+
# 6. 将 Gradio 挂载到 FastAPI 应用的根路径(必须放在所有 FastAPI 路由之后!)
|
| 113 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|