Uotpia commited on
Commit
b3dc0ab
·
verified ·
1 Parent(s): e50b3a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -15
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,与 CTranslate2 版本一样精准
12
  MODEL_NAME = "openai/whisper-small"
13
 
14
- # 2. 全局初始化 Pipeline,默认放在 CPU 上,防止启动报错
15
- # generate_kwargs 指定中文识别
 
 
16
  pipe = pipeline(
17
  "automatic-speech-recognition",
18
  model=MODEL_NAME,
19
  chunk_length_s=30,
20
- device="cpu"
21
  )
22
 
23
  # 3. 核心计算函数
24
- @spaces.GPU(duration=60)
25
  def transcribe_core(audio_path: str):
26
- # 【核心安全操作进入 GPU 节点后,态将 Pipeline 的模型送入 CUDA 显存
27
- pipe.model.to("cuda")
28
-
29
- # 运行转录(开启 FP16 混合精度极速推理)
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 界面