Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ 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 |
|
|
@@ -33,7 +34,6 @@ def run_whisper(audio_path: str, target_language: str = None, is_translate: bool
|
|
| 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:
|
|
@@ -47,9 +47,17 @@ 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):
|
| 54 |
suffix = os.path.splitext(file.filename)[1] or ".wav"
|
| 55 |
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
|
@@ -85,5 +93,5 @@ async def translate_api(
|
|
| 85 |
):
|
| 86 |
return await process_audio(file, response_format, language="english", is_translate=True)
|
| 87 |
|
| 88 |
-
# 5.
|
| 89 |
demo.launch()
|
|
|
|
| 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
|
| 11 |
|
|
|
|
| 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:
|
|
|
|
| 47 |
title="Whisper API Node"
|
| 48 |
)
|
| 49 |
|
| 50 |
+
# 4. 获取 FastAPI 实例并添加 CORS 跨域支持
|
| 51 |
app = demo.app
|
| 52 |
|
| 53 |
+
app.add_middleware(
|
| 54 |
+
CORSMiddleware,
|
| 55 |
+
allow_origins=["*"],
|
| 56 |
+
allow_credentials=True,
|
| 57 |
+
allow_methods=["*"],
|
| 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:
|
|
|
|
| 93 |
):
|
| 94 |
return await process_audio(file, response_format, language="english", is_translate=True)
|
| 95 |
|
| 96 |
+
# 5. 启动服务
|
| 97 |
demo.launch()
|