Uotpia commited on
Commit
4da74ff
·
verified ·
1 Parent(s): 6394cd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -12
app.py CHANGED
@@ -8,11 +8,10 @@ from fastapi import FastAPI, UploadFile, File, Form, HTTPException
8
  from fastapi.responses import JSONResponse, PlainTextResponse
9
  from transformers import pipeline
10
 
11
- # 1. 声明加载的模型
12
  MODEL_NAME = "openai/whisper-small"
13
-
14
- # 2. 初始化 Pipeline
15
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
16
  pipe = pipeline(
17
  "automatic-speech-recognition",
18
  model=MODEL_NAME,
@@ -20,7 +19,7 @@ pipe = pipeline(
20
  device=device
21
  )
22
 
23
- # 3. 【关键点】在最外层定义带 @spaces.GPU 的核心推理函数
24
  @spaces.GPU
25
  def run_whisper_inference(audio_path: str, target_language: str = None, is_translate: bool = False):
26
  generate_kwargs = {}
@@ -34,8 +33,7 @@ def run_whisper_inference(audio_path: str, target_language: str = None, is_trans
34
  result = pipe(audio_path, generate_kwargs=generate_kwargs)
35
  return result["text"]
36
 
37
-
38
- # --- Gradio 界面 ---
39
  def gradio_predict(audio_path):
40
  if audio_path is None:
41
  return "请先上传音频或录音!"
@@ -52,8 +50,7 @@ demo = gr.Interface(
52
  description="【完美兼容 OpenAI 规范】"
53
  )
54
 
55
-
56
- # --- FastAPI 接口配置 ---
57
  app = FastAPI()
58
 
59
  async def process_openai_audio_request(file: UploadFile, response_format: str, language: str, is_translate: bool):
@@ -63,7 +60,6 @@ async def process_openai_audio_request(file: UploadFile, response_format: str, l
63
  temp_path = temp_file.name
64
 
65
  try:
66
- # 调用最外层带 @spaces.GPU 装饰的函数
67
  text = run_whisper_inference(temp_path, target_language=language, is_translate=is_translate)
68
  except Exception as e:
69
  raise HTTPException(status_code=500, detail=f"Inference failed: {str(e)}")
@@ -76,7 +72,6 @@ async def process_openai_audio_request(file: UploadFile, response_format: str, l
76
 
77
  return JSONResponse(content={"text": text})
78
 
79
-
80
  @app.post("/v1/audio/transcriptions")
81
  async def transcribe_api(
82
  file: UploadFile = File(...),
@@ -93,7 +88,6 @@ async def transcribe_api(
93
  is_translate=False
94
  )
95
 
96
-
97
  @app.post("/v1/audio/translations")
98
  async def translate_api(
99
  file: UploadFile = File(...),
@@ -109,5 +103,5 @@ async def translate_api(
109
  is_translate=True
110
  )
111
 
112
- # 挂载 Gradio 页面到 FastAPI 根路径
113
  app = gr.mount_gradio_app(app, demo, path="/")
 
8
  from fastapi.responses import JSONResponse, PlainTextResponse
9
  from transformers import pipeline
10
 
11
+ # 1. 模型加载
12
  MODEL_NAME = "openai/whisper-small"
 
 
13
  device = "cuda" if torch.cuda.is_available() else "cpu"
14
+
15
  pipe = pipeline(
16
  "automatic-speech-recognition",
17
  model=MODEL_NAME,
 
19
  device=device
20
  )
21
 
22
+ # 2. 核心 GPU 推理函数(ZeroGPU 严格要求必须在文件顶层声明)
23
  @spaces.GPU
24
  def run_whisper_inference(audio_path: str, target_language: str = None, is_translate: bool = False):
25
  generate_kwargs = {}
 
33
  result = pipe(audio_path, generate_kwargs=generate_kwargs)
34
  return result["text"]
35
 
36
+ # 3. Gradio UI 封装
 
37
  def gradio_predict(audio_path):
38
  if audio_path is None:
39
  return "请先上传音频或录音!"
 
50
  description="【完美兼容 OpenAI 规范】"
51
  )
52
 
53
+ # 4. FastAPI 应用声明
 
54
  app = FastAPI()
55
 
56
  async def process_openai_audio_request(file: UploadFile, response_format: str, language: str, is_translate: bool):
 
60
  temp_path = temp_file.name
61
 
62
  try:
 
63
  text = run_whisper_inference(temp_path, target_language=language, is_translate=is_translate)
64
  except Exception as e:
65
  raise HTTPException(status_code=500, detail=f"Inference failed: {str(e)}")
 
72
 
73
  return JSONResponse(content={"text": text})
74
 
 
75
  @app.post("/v1/audio/transcriptions")
76
  async def transcribe_api(
77
  file: UploadFile = File(...),
 
88
  is_translate=False
89
  )
90
 
 
91
  @app.post("/v1/audio/translations")
92
  async def translate_api(
93
  file: UploadFile = File(...),
 
103
  is_translate=True
104
  )
105
 
106
+ # 5. 挂载 Gradio 到 FastAPI
107
  app = gr.mount_gradio_app(app, demo, path="/")