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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -16
app.py CHANGED
@@ -1,28 +1,41 @@
1
  import os
2
  import shutil
3
  import tempfile
 
4
  import spaces
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. 声明加载的模型大小
11
- MODEL_SIZE = "small"
12
 
13
- # 2. 核心计算函数:加上 @spaces.GPU 装饰器
14
- # 每次触发时,这个函数都会运行在 A100 显卡节点上
15
- @spaces.GPU(duration=60) # 显式声明单次最大运行时间为 60 秒,避免超时
 
 
 
 
 
 
 
 
16
  def transcribe_core(audio_path: str):
17
- # GPU 节点内部动态创建模型实例,此时 CUDA 驱动是 100% 在且可用的
18
- # 使用 float16 推理速度会极快
19
- gpu_model = WhisperModel(MODEL_SIZE, device="cuda", compute_type="float16")
 
 
 
 
 
 
20
 
21
- segments, info = gpu_model.transcribe(audio_path, beam_size=5)
22
- text = "".join([segment.text for segment in segments])
23
- return text
24
 
25
- # 3. 创建 Gradio 界面
26
  def gradio_predict(audio_path):
27
  if audio_path is None:
28
  return "请先上传音频或录音!"
@@ -36,10 +49,10 @@ demo = gr.Interface(
36
  inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="输入音频"),
37
  outputs=gr.Textbox(label="识别出的文本"),
38
  title="Whisper 语音识别 API 节点",
39
- description="【A100 GPU 动态加速版】支持网页端测试,同时也支持 OpenAI 兼容的 /v1/audio/transcriptions 接口!"
40
  )
41
 
42
- # 4. 获取 FastAPI 实例并扩展 API 路由
43
  app = demo.app
44
 
45
  @app.post("/v1/audio/transcriptions")
@@ -62,6 +75,6 @@ async def transcribe_api(
62
 
63
  return JSONResponse(content={"text": transcription_text})
64
 
65
- # 5. 启动服务
66
  if __name__ == "__main__":
67
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import os
2
  import shutil
3
  import tempfile
4
+ import torch
5
  import spaces
6
  import gradio as gr
7
  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 界面
39
  def gradio_predict(audio_path):
40
  if audio_path is None:
41
  return "请先上传音频或录音!"
 
49
  inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="输入音频"),
50
  outputs=gr.Textbox(label="识别出的文本"),
51
  title="Whisper 语音识别 API 节点",
52
+ description="【A100 GPU 动态加速版 - Transformers 官方兼容版】支持网页端测试,同时也支持 OpenAI 兼容的 /v1/audio/transcriptions 接口!"
53
  )
54
 
55
+ # 5. 获取 FastAPI 实例并扩展 API 路由
56
  app = demo.app
57
 
58
  @app.post("/v1/audio/transcriptions")
 
75
 
76
  return JSONResponse(content={"text": transcription_text})
77
 
78
+ # 6. 启动服务
79
  if __name__ == "__main__":
80
  demo.launch(server_name="0.0.0.0", server_port=7860)