Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 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
|
|
@@ -10,17 +11,18 @@ from transformers import pipeline
|
|
| 10 |
# 1. 声明加载的模型
|
| 11 |
MODEL_NAME = "openai/whisper-small"
|
| 12 |
|
| 13 |
-
# 2.
|
|
|
|
| 14 |
pipe = pipeline(
|
| 15 |
"automatic-speech-recognition",
|
| 16 |
model=MODEL_NAME,
|
| 17 |
chunk_length_s=30,
|
| 18 |
-
device=
|
| 19 |
)
|
| 20 |
|
| 21 |
-
# 3. 核心
|
| 22 |
@spaces.GPU
|
| 23 |
-
def
|
| 24 |
generate_kwargs = {}
|
| 25 |
if target_language:
|
| 26 |
generate_kwargs["language"] = target_language
|
|
@@ -32,12 +34,13 @@ def transcribe_core(audio_path: str, target_language: str = None, is_translate:
|
|
| 32 |
result = pipe(audio_path, generate_kwargs=generate_kwargs)
|
| 33 |
return result["text"]
|
| 34 |
|
|
|
|
| 35 |
# --- Gradio 界面 ---
|
| 36 |
def gradio_predict(audio_path):
|
| 37 |
if audio_path is None:
|
| 38 |
return "请先上传音频或录音!"
|
| 39 |
try:
|
| 40 |
-
return
|
| 41 |
except Exception as e:
|
| 42 |
return f"错误: {str(e)}"
|
| 43 |
|
|
@@ -49,22 +52,21 @@ demo = gr.Interface(
|
|
| 49 |
description="【完美兼容 OpenAI 规范】"
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
#
|
| 54 |
-
# ===========================================================================
|
| 55 |
app = FastAPI()
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
suffix = os.path.splitext(file.filename)[1] or ".mp3"
|
| 60 |
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
| 61 |
shutil.copyfileobj(file.file, temp_file)
|
| 62 |
temp_path = temp_file.name
|
| 63 |
|
| 64 |
try:
|
| 65 |
-
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
-
raise HTTPException(status_code=500, detail=f"
|
| 68 |
finally:
|
| 69 |
if os.path.exists(temp_path):
|
| 70 |
os.remove(temp_path)
|
|
@@ -75,7 +77,6 @@ async def process_openai_audio_request(file, response_format, language, is_trans
|
|
| 75 |
return JSONResponse(content={"text": text})
|
| 76 |
|
| 77 |
|
| 78 |
-
# 4. 完美兼容接口一:语音转录 (Transcriptions)
|
| 79 |
@app.post("/v1/audio/transcriptions")
|
| 80 |
async def transcribe_api(
|
| 81 |
file: UploadFile = File(...),
|
|
@@ -93,7 +94,6 @@ async def transcribe_api(
|
|
| 93 |
)
|
| 94 |
|
| 95 |
|
| 96 |
-
# 5. 完美兼容接口二:语音翻译 (Translations)
|
| 97 |
@app.post("/v1/audio/translations")
|
| 98 |
async def translate_api(
|
| 99 |
file: UploadFile = File(...),
|
|
@@ -109,5 +109,5 @@ async def translate_api(
|
|
| 109 |
is_translate=True
|
| 110 |
)
|
| 111 |
|
| 112 |
-
#
|
| 113 |
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 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
|
|
|
|
| 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,
|
| 19 |
chunk_length_s=30,
|
| 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 = {}
|
| 27 |
if target_language:
|
| 28 |
generate_kwargs["language"] = target_language
|
|
|
|
| 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 "请先上传音频或录音!"
|
| 42 |
try:
|
| 43 |
+
return run_whisper_inference(audio_path)
|
| 44 |
except Exception as e:
|
| 45 |
return f"错误: {str(e)}"
|
| 46 |
|
|
|
|
| 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):
|
| 60 |
+
suffix = os.path.splitext(file.filename)[1] or ".wav"
|
|
|
|
| 61 |
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
| 62 |
shutil.copyfileobj(file.file, temp_file)
|
| 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)}")
|
| 70 |
finally:
|
| 71 |
if os.path.exists(temp_path):
|
| 72 |
os.remove(temp_path)
|
|
|
|
| 77 |
return JSONResponse(content={"text": text})
|
| 78 |
|
| 79 |
|
|
|
|
| 80 |
@app.post("/v1/audio/transcriptions")
|
| 81 |
async def transcribe_api(
|
| 82 |
file: UploadFile = File(...),
|
|
|
|
| 94 |
)
|
| 95 |
|
| 96 |
|
|
|
|
| 97 |
@app.post("/v1/audio/translations")
|
| 98 |
async def translate_api(
|
| 99 |
file: UploadFile = File(...),
|
|
|
|
| 109 |
is_translate=True
|
| 110 |
)
|
| 111 |
|
| 112 |
+
# 挂载 Gradio 页面到 FastAPI 根路径
|
| 113 |
app = gr.mount_gradio_app(app, demo, path="/")
|