Update app.py
Browse files
app.py
CHANGED
|
@@ -19,7 +19,7 @@ pipe = pipeline(
|
|
| 19 |
device=device
|
| 20 |
)
|
| 21 |
|
| 22 |
-
# 2.
|
| 23 |
@spaces.GPU
|
| 24 |
def run_whisper(audio_path: str, target_language: str = None, is_translate: bool = False):
|
| 25 |
generate_kwargs = {}
|
|
@@ -34,7 +34,7 @@ def run_whisper(audio_path: str, target_language: str = None, is_translate: bool
|
|
| 34 |
return result["text"]
|
| 35 |
|
| 36 |
|
| 37 |
-
# 3.
|
| 38 |
def gradio_predict(audio_path):
|
| 39 |
if audio_path is None:
|
| 40 |
return "请上传音频文件!"
|
|
@@ -47,7 +47,7 @@ demo = gr.Interface(
|
|
| 47 |
title="Whisper API Node"
|
| 48 |
)
|
| 49 |
|
| 50 |
-
# 4.
|
| 51 |
app = demo.app
|
| 52 |
|
| 53 |
async def process_audio(file: UploadFile, response_format: str, language: str, is_translate: bool):
|
|
@@ -68,7 +68,6 @@ async def process_audio(file: UploadFile, response_format: str, language: str, i
|
|
| 68 |
return PlainTextResponse(text)
|
| 69 |
return JSONResponse(content={"text": text})
|
| 70 |
|
| 71 |
-
# 5. 添加 OpenAI 兼容接口
|
| 72 |
@app.post("/v1/audio/transcriptions")
|
| 73 |
async def transcribe_api(
|
| 74 |
file: UploadFile = File(...),
|
|
@@ -84,4 +83,7 @@ async def translate_api(
|
|
| 84 |
model: str = Form("whisper-1"),
|
| 85 |
response_format: str = Form("json")
|
| 86 |
):
|
| 87 |
-
return await process_audio(file, response_format, language="english", is_translate=True)
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
device=device
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# 2. 顶层 ZeroGPU 函数
|
| 23 |
@spaces.GPU
|
| 24 |
def run_whisper(audio_path: str, target_language: str = None, is_translate: bool = False):
|
| 25 |
generate_kwargs = {}
|
|
|
|
| 34 |
return result["text"]
|
| 35 |
|
| 36 |
|
| 37 |
+
# 3. Gradio 界面定义
|
| 38 |
def gradio_predict(audio_path):
|
| 39 |
if audio_path is None:
|
| 40 |
return "请上传音频文件!"
|
|
|
|
| 47 |
title="Whisper API Node"
|
| 48 |
)
|
| 49 |
|
| 50 |
+
# 4. 获取 Gradio 内置的 FastAPI 实例并添加 OpenAI 接口
|
| 51 |
app = demo.app
|
| 52 |
|
| 53 |
async def process_audio(file: UploadFile, response_format: str, language: str, is_translate: bool):
|
|
|
|
| 68 |
return PlainTextResponse(text)
|
| 69 |
return JSONResponse(content={"text": text})
|
| 70 |
|
|
|
|
| 71 |
@app.post("/v1/audio/transcriptions")
|
| 72 |
async def transcribe_api(
|
| 73 |
file: UploadFile = File(...),
|
|
|
|
| 83 |
model: str = Form("whisper-1"),
|
| 84 |
response_format: str = Form("json")
|
| 85 |
):
|
| 86 |
+
return await process_audio(file, response_format, language="english", is_translate=True)
|
| 87 |
+
|
| 88 |
+
# 5. 【关键修复】显式启动 Gradio 服务,防止容器直接退出
|
| 89 |
+
demo.launch()
|