mirror of
https://github.com/modelscope/FunASR.git
synced 2026-10-12 02:19:18 +00:00
fix(openai-api): support n8n audio alias in example
Signed-off-by: LauraGPT <[email protected]>
This commit is contained in:
@@ -32,6 +32,8 @@ app = FastAPI(title="FunASR OpenAI-Compatible API", version="1.0.0")
|
||||
|
||||
MODEL_REGISTRY = {}
|
||||
DEVICE = "cpu"
|
||||
DEFAULT_MODEL = "sensevoice"
|
||||
N8N_OPENAI_MODEL_ALIAS = "whisper-1"
|
||||
|
||||
MODEL_CONFIGS = {
|
||||
"sensevoice": {
|
||||
@@ -95,6 +97,13 @@ def clean_text(text: str) -> str:
|
||||
return re.sub(r'<\|[^|]*\|>', '', text).strip()
|
||||
|
||||
|
||||
def resolve_openai_transcription_model(requested_model: str) -> str:
|
||||
"""Map n8n's fixed OpenAI transcription model to the started model."""
|
||||
if requested_model == N8N_OPENAI_MODEL_ALIAS:
|
||||
return DEFAULT_MODEL
|
||||
return requested_model
|
||||
|
||||
|
||||
@app.post("/v1/audio/transcriptions")
|
||||
async def transcribe(
|
||||
file: UploadFile = File(...),
|
||||
@@ -111,6 +120,8 @@ async def transcribe(
|
||||
- language: Optional language hint
|
||||
- response_format: json or verbose_json
|
||||
"""
|
||||
model = resolve_openai_transcription_model(model)
|
||||
|
||||
# Validate model
|
||||
if model not in MODEL_CONFIGS:
|
||||
raise HTTPException(
|
||||
@@ -199,8 +210,9 @@ def main():
|
||||
parser.add_argument("--model", default="sensevoice", help="Pre-load model at startup")
|
||||
args = parser.parse_args()
|
||||
|
||||
global DEVICE
|
||||
global DEFAULT_MODEL, DEVICE
|
||||
DEVICE = args.device
|
||||
DEFAULT_MODEL = args.model
|
||||
|
||||
# Pre-load default model
|
||||
load_model(args.model)
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import importlib.util
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
SERVER_PATH = REPO_ROOT / "examples" / "openai_api" / "server.py"
|
||||
|
||||
|
||||
def load_example_server():
|
||||
module_name = "funasr_example_openai_server_under_test"
|
||||
sys.modules.pop(module_name, None)
|
||||
spec = importlib.util.spec_from_file_location(module_name, SERVER_PATH)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
assert spec.loader is not None
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def test_example_server_maps_n8n_whisper_alias_to_started_model():
|
||||
module = load_example_server()
|
||||
module.DEFAULT_MODEL = "moss-transcribe-diarize"
|
||||
|
||||
assert module.resolve_openai_transcription_model("whisper-1") == "moss-transcribe-diarize"
|
||||
assert module.resolve_openai_transcription_model("sensevoice") == "sensevoice"
|
||||
Reference in New Issue
Block a user