Update app.py
Browse files
app.py
CHANGED
|
@@ -8,31 +8,27 @@ from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
|
| 8 |
from fastapi.responses import JSONResponse
|
| 9 |
from transformers import pipeline
|
| 10 |
|
| 11 |
-
# 1. 声明加载的模型(Hugging Face 官方原生 Whisper Small
|
| 12 |
MODEL_NAME = "openai/whisper-small"
|
| 13 |
|
| 14 |
-
# 2. 全局初始化 Pipeline
|
| 15 |
-
#
|
|
|
|
|
|
|
| 16 |
pipe = pipeline(
|
| 17 |
"automatic-speech-recognition",
|
| 18 |
model=MODEL_NAME,
|
| 19 |
chunk_length_s=30,
|
| 20 |
-
device="
|
| 21 |
)
|
| 22 |
|
| 23 |
# 3. 核心计算函数
|
| 24 |
-
@spaces.GPU
|
| 25 |
def transcribe_core(audio_path: str):
|
| 26 |
-
# 【
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
with torch.autocast("cuda"):
|
| 31 |
-
result = pipe(audio_path, generate_kwargs={"language": "chinese"})
|
| 32 |
-
|
| 33 |
-
# 转录完后立即将模型移回 CPU,完美符合 ZeroGPU 的释放规范
|
| 34 |
-
pipe.model.to("cpu")
|
| 35 |
-
|
| 36 |
return result["text"]
|
| 37 |
|
| 38 |
# 4. 创建 Gradio 界面
|
|
|
|
| 8 |
from fastapi.responses import JSONResponse
|
| 9 |
from transformers import pipeline
|
| 10 |
|
| 11 |
+
# 1. 声明加载的模型(Hugging Face 官方原生 Whisper Small)
|
| 12 |
MODEL_NAME = "openai/whisper-small"
|
| 13 |
|
| 14 |
+
# 2. 全局初始化 Pipeline!
|
| 15 |
+
# 注意:在 ZeroGPU 环境下,全局初始化时将 device 设置为 "cuda"。
|
| 16 |
+
# 官方的 spaces 库会在容器启动时自动拦截它,防止在 CPU 阶段报错;
|
| 17 |
+
# 同时在调用 @spaces.GPU 函数时,系统会自动把整个 Pipeline 的计算放到 A100 上。
|
| 18 |
pipe = pipeline(
|
| 19 |
"automatic-speech-recognition",
|
| 20 |
model=MODEL_NAME,
|
| 21 |
chunk_length_s=30,
|
| 22 |
+
device="cuda"
|
| 23 |
)
|
| 24 |
|
| 25 |
# 3. 核心计算函数
|
| 26 |
+
@spaces.GPU
|
| 27 |
def transcribe_core(audio_path: str):
|
| 28 |
+
# 【最关键的改变】这里完全不需要任何手动 .to("cuda")
|
| 29 |
+
# 因为我们在上面全局指定了 device="cuda",在 @spaces.GPU 装饰器内部,
|
| 30 |
+
# 框架会自动、无缝地把这个 Pipeline 调度到 A100 GPU 显存中运行!
|
| 31 |
+
result = pipe(audio_path, generate_kwargs={"language": "chinese"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
return result["text"]
|
| 33 |
|
| 34 |
# 4. 创建 Gradio 界面
|