Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,11 @@ import tempfile
|
|
| 4 |
import torch
|
| 5 |
import spaces
|
| 6 |
import gradio as gr
|
| 7 |
-
from fastapi import
|
| 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 |
|
|
@@ -19,9 +19,9 @@ pipe = pipeline(
|
|
| 19 |
device=device
|
| 20 |
)
|
| 21 |
|
| 22 |
-
# 2. 核心 GPU
|
| 23 |
@spaces.GPU
|
| 24 |
-
def
|
| 25 |
generate_kwargs = {}
|
| 26 |
if target_language:
|
| 27 |
generate_kwargs["language"] = target_language
|
|
@@ -33,75 +33,55 @@ def run_whisper_inference(audio_path: str, target_language: str = None, is_trans
|
|
| 33 |
result = pipe(audio_path, generate_kwargs=generate_kwargs)
|
| 34 |
return result["text"]
|
| 35 |
|
| 36 |
-
|
|
|
|
| 37 |
def gradio_predict(audio_path):
|
| 38 |
if audio_path is None:
|
| 39 |
-
return "请
|
| 40 |
-
|
| 41 |
-
return run_whisper_inference(audio_path)
|
| 42 |
-
except Exception as e:
|
| 43 |
-
return f"错误: {str(e)}"
|
| 44 |
|
| 45 |
demo = gr.Interface(
|
| 46 |
fn=gradio_predict,
|
| 47 |
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="输入音频"),
|
| 48 |
-
outputs=gr.Textbox(label="识别
|
| 49 |
-
title="Whisper
|
| 50 |
-
description="【完美兼容 OpenAI 规范】"
|
| 51 |
)
|
| 52 |
|
| 53 |
-
# 4.
|
| 54 |
-
app =
|
| 55 |
|
| 56 |
-
async def
|
| 57 |
suffix = os.path.splitext(file.filename)[1] or ".wav"
|
| 58 |
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
| 59 |
shutil.copyfileobj(file.file, temp_file)
|
| 60 |
temp_path = temp_file.name
|
| 61 |
|
| 62 |
try:
|
| 63 |
-
text =
|
| 64 |
except Exception as e:
|
| 65 |
-
raise HTTPException(status_code=500, detail=
|
| 66 |
finally:
|
| 67 |
if os.path.exists(temp_path):
|
| 68 |
os.remove(temp_path)
|
| 69 |
|
| 70 |
if response_format in ["text", "vtt", "srt"]:
|
| 71 |
return PlainTextResponse(text)
|
| 72 |
-
|
| 73 |
return JSONResponse(content={"text": text})
|
| 74 |
|
|
|
|
| 75 |
@app.post("/v1/audio/transcriptions")
|
| 76 |
async def transcribe_api(
|
| 77 |
file: UploadFile = File(...),
|
| 78 |
model: str = Form("whisper-1"),
|
| 79 |
language: str = Form(None),
|
| 80 |
-
|
| 81 |
-
response_format: str = Form("json"),
|
| 82 |
-
temperature: float = Form(0.0)
|
| 83 |
):
|
| 84 |
-
return await
|
| 85 |
-
file=file,
|
| 86 |
-
response_format=response_format,
|
| 87 |
-
language=language,
|
| 88 |
-
is_translate=False
|
| 89 |
-
)
|
| 90 |
|
| 91 |
@app.post("/v1/audio/translations")
|
| 92 |
async def translate_api(
|
| 93 |
file: UploadFile = File(...),
|
| 94 |
model: str = Form("whisper-1"),
|
| 95 |
-
|
| 96 |
-
response_format: str = Form("json"),
|
| 97 |
-
temperature: float = Form(0.0)
|
| 98 |
):
|
| 99 |
-
return await
|
| 100 |
-
file=file,
|
| 101 |
-
response_format=response_format,
|
| 102 |
-
language="english",
|
| 103 |
-
is_translate=True
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
# 5. 挂载 Gradio 到 FastAPI
|
| 107 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 4 |
import torch
|
| 5 |
import spaces
|
| 6 |
import gradio as gr
|
| 7 |
+
from fastapi import 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 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
|
|
|
|
| 19 |
device=device
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# 2. 【核心】顶层单独定义的 @spaces.GPU 函数(绝对不能被任何类或内部函数包裹)
|
| 23 |
@spaces.GPU
|
| 24 |
+
def run_whisper(audio_path: str, target_language: str = None, is_translate: bool = False):
|
| 25 |
generate_kwargs = {}
|
| 26 |
if target_language:
|
| 27 |
generate_kwargs["language"] = target_language
|
|
|
|
| 33 |
result = pipe(audio_path, generate_kwargs=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 "请上传音频文件!"
|
| 41 |
+
return run_whisper(audio_path)
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
demo = gr.Interface(
|
| 44 |
fn=gradio_predict,
|
| 45 |
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="输入音频"),
|
| 46 |
+
outputs=gr.Textbox(label="识别结果"),
|
| 47 |
+
title="Whisper API Node"
|
|
|
|
| 48 |
)
|
| 49 |
|
| 50 |
+
# 4. 直接使用 Gradio 自带的 demo.app(避免零 GPU 扫描机制找不到路由)
|
| 51 |
+
app = demo.app
|
| 52 |
|
| 53 |
+
async def process_audio(file: UploadFile, response_format: str, language: str, is_translate: bool):
|
| 54 |
suffix = os.path.splitext(file.filename)[1] or ".wav"
|
| 55 |
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
| 56 |
shutil.copyfileobj(file.file, temp_file)
|
| 57 |
temp_path = temp_file.name
|
| 58 |
|
| 59 |
try:
|
| 60 |
+
text = run_whisper(temp_path, target_language=language, is_translate=is_translate)
|
| 61 |
except Exception as e:
|
| 62 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 63 |
finally:
|
| 64 |
if os.path.exists(temp_path):
|
| 65 |
os.remove(temp_path)
|
| 66 |
|
| 67 |
if response_format in ["text", "vtt", "srt"]:
|
| 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(...),
|
| 75 |
model: str = Form("whisper-1"),
|
| 76 |
language: str = Form(None),
|
| 77 |
+
response_format: str = Form("json")
|
|
|
|
|
|
|
| 78 |
):
|
| 79 |
+
return await process_audio(file, response_format, language, is_translate=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
@app.post("/v1/audio/translations")
|
| 82 |
async def translate_api(
|
| 83 |
file: UploadFile = File(...),
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|