Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import tempfile
|
|
| 4 |
import torch
|
| 5 |
import spaces
|
| 6 |
import gradio as gr
|
| 7 |
-
from fastapi import
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
from fastapi.responses import JSONResponse, PlainTextResponse
|
| 10 |
from transformers import pipeline
|
|
@@ -26,18 +26,29 @@ def run_whisper(audio_path: str, target_language: str = None, is_translate: bool
|
|
| 26 |
generate_kwargs = {}
|
| 27 |
if target_language:
|
| 28 |
generate_kwargs["language"] = target_language
|
|
|
|
| 29 |
if is_translate:
|
| 30 |
generate_kwargs["language"] = "english"
|
| 31 |
generate_kwargs["task"] = "translate"
|
|
|
|
| 32 |
result = pipe(audio_path, generate_kwargs=generate_kwargs)
|
| 33 |
return result["text"]
|
| 34 |
|
| 35 |
-
# 3.
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
app.add_middleware(
|
| 43 |
CORSMiddleware,
|
|
@@ -47,12 +58,12 @@ app.add_middleware(
|
|
| 47 |
allow_headers=["*"],
|
| 48 |
)
|
| 49 |
|
| 50 |
-
# 4. 通用音频处理函数
|
| 51 |
async def process_audio(file: UploadFile, response_format: str, language: str, is_translate: bool):
|
| 52 |
suffix = os.path.splitext(file.filename)[1] or ".wav"
|
| 53 |
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
| 54 |
shutil.copyfileobj(file.file, temp_file)
|
| 55 |
temp_path = temp_file.name
|
|
|
|
| 56 |
try:
|
| 57 |
text = run_whisper(temp_path, target_language=language, is_translate=is_translate)
|
| 58 |
except Exception as e:
|
|
@@ -60,11 +71,11 @@ async def process_audio(file: UploadFile, response_format: str, language: str, i
|
|
| 60 |
finally:
|
| 61 |
if os.path.exists(temp_path):
|
| 62 |
os.remove(temp_path)
|
|
|
|
| 63 |
if response_format in ["text", "vtt", "srt"]:
|
| 64 |
return PlainTextResponse(text)
|
| 65 |
return JSONResponse(content={"text": text})
|
| 66 |
|
| 67 |
-
# 5. API 路由(先于 Gradio 挂载)
|
| 68 |
@app.post("/v1/audio/transcriptions")
|
| 69 |
async def transcribe_api(
|
| 70 |
file: UploadFile = File(...),
|
|
@@ -82,23 +93,5 @@ async def translate_api(
|
|
| 82 |
):
|
| 83 |
return await process_audio(file, response_format, language="english", is_translate=True)
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
|
| 87 |
-
if audio_path is None:
|
| 88 |
-
return "请上传音频文件!"
|
| 89 |
-
return run_whisper(audio_path)
|
| 90 |
-
|
| 91 |
-
demo = gr.Interface(
|
| 92 |
-
fn=gradio_predict,
|
| 93 |
-
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="输入音频"),
|
| 94 |
-
outputs=gr.Textbox(label="识别结果"),
|
| 95 |
-
title="Whisper API Node"
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
# 7. 将 Gradio 挂载到主应用(作为子应用)
|
| 99 |
-
app.mount("/", demo.app)
|
| 100 |
-
#注意: 去掉 demo.launch(),HF Spaces 会自动检测 app 对象并用 uvicorn 启动。如果 Spaces 没有自动识别,在文件末尾加:
|
| 101 |
-
# 仅本地调试用,Spaces 上会自动忽略
|
| 102 |
-
if __name__ == "__main__":
|
| 103 |
-
import uvicorn
|
| 104 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 4 |
import torch
|
| 5 |
import spaces
|
| 6 |
import gradio as gr
|
| 7 |
+
from fastapi import UploadFile, File, Form, HTTPException
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
from fastapi.responses import JSONResponse, PlainTextResponse
|
| 10 |
from transformers import pipeline
|
|
|
|
| 26 |
generate_kwargs = {}
|
| 27 |
if target_language:
|
| 28 |
generate_kwargs["language"] = target_language
|
| 29 |
+
|
| 30 |
if is_translate:
|
| 31 |
generate_kwargs["language"] = "english"
|
| 32 |
generate_kwargs["task"] = "translate"
|
| 33 |
+
|
| 34 |
result = pipe(audio_path, generate_kwargs=generate_kwargs)
|
| 35 |
return result["text"]
|
| 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. 获取 FastAPI 实例并添加 CORS 跨域支持
|
| 51 |
+
app = demo.app
|
| 52 |
|
| 53 |
app.add_middleware(
|
| 54 |
CORSMiddleware,
|
|
|
|
| 58 |
allow_headers=["*"],
|
| 59 |
)
|
| 60 |
|
|
|
|
| 61 |
async def process_audio(file: UploadFile, response_format: str, language: str, is_translate: bool):
|
| 62 |
suffix = os.path.splitext(file.filename)[1] or ".wav"
|
| 63 |
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
| 64 |
shutil.copyfileobj(file.file, temp_file)
|
| 65 |
temp_path = temp_file.name
|
| 66 |
+
|
| 67 |
try:
|
| 68 |
text = run_whisper(temp_path, target_language=language, is_translate=is_translate)
|
| 69 |
except Exception as e:
|
|
|
|
| 71 |
finally:
|
| 72 |
if os.path.exists(temp_path):
|
| 73 |
os.remove(temp_path)
|
| 74 |
+
|
| 75 |
if response_format in ["text", "vtt", "srt"]:
|
| 76 |
return PlainTextResponse(text)
|
| 77 |
return JSONResponse(content={"text": text})
|
| 78 |
|
|
|
|
| 79 |
@app.post("/v1/audio/transcriptions")
|
| 80 |
async def transcribe_api(
|
| 81 |
file: UploadFile = File(...),
|
|
|
|
| 93 |
):
|
| 94 |
return await process_audio(file, response_format, language="english", is_translate=True)
|
| 95 |
|
| 96 |
+
# 5. 启动服务
|
| 97 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|