-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathserver.py
More file actions
130 lines (108 loc) · 5.12 KB
/
Copy pathserver.py
File metadata and controls
130 lines (108 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
FunASR Server — OpenAI-compatible speech recognition API.
Defaults: host=0.0.0.0, port=8000, device=cuda, model=auto.
Auto selection: cuda* -> fun-asr-nano; other devices -> sensevoice.
No API-key authentication is provided; keep local demos on loopback.
Usage:
funasr-server --host 127.0.0.1 --model sensevoice --device cpu
funasr-server --host 127.0.0.1 --device cpu --port 9000
funasr-server --host 127.0.0.1 --model paraformer
funasr-server --host 127.0.0.1 --model moss-transcribe-diarize --device cuda:0
funasr-server --host 127.0.0.1 --model-path /path/to/local/model
funasr-server --host 127.0.0.1 --model-path username/paraformer --hub hf
funasr-server --host 127.0.0.1 --cors-origin http://localhost:3000
"""
import argparse
import sys
from pathlib import Path
PACKAGE_VERSION = (Path(__file__).resolve().parents[1] / "version.txt").read_text().strip()
def server_version_label():
return f"FunASR Server v{PACKAGE_VERSION}"
def build_parser():
parser = argparse.ArgumentParser(
description="FunASR OpenAI-Compatible API Server",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Defaults: host=0.0.0.0, port=8000, device=cuda, model=auto.
Auto selection: cuda* -> fun-asr-nano; other devices -> sensevoice.
For a local example, use the explicit SenseVoice CPU command below.
Other model/device examples require their own prepared environment.
Examples:
funasr-server --host 127.0.0.1 --model sensevoice --device cpu
funasr-server --host 127.0.0.1 --model fun-asr-nano --device cuda # Inspect backend logs
funasr-server --host 127.0.0.1 --model paraformer
funasr-server --host 127.0.0.1 --model moss-transcribe-diarize --device cuda:0
funasr-server --host 127.0.0.1 --port 9000 # Custom port
funasr-server --host 127.0.0.1 --model-path /path/to/local/model
funasr-server --host 127.0.0.1 --model-path username/model --hub hf
funasr-server --host 127.0.0.1 --cors-origin http://localhost:3000
For the local SenseVoice example, in a second terminal:
python -m pip install openai
Then run Python (api_key is a placeholder, not authentication):
from openai import OpenAI
client = OpenAI(base_url="http://127.0.0.1:8000/v1", api_key="not-needed")
with open("a.wav", "rb") as audio:
result = client.audio.transcriptions.create(model="sensevoice", file=audio)
""",
)
parser.add_argument("--host", default="0.0.0.0", help="Bind address (default: 0.0.0.0)")
parser.add_argument("--port", type=int, default=8000, help="Port (default: 8000)")
parser.add_argument("--device", default="cuda", help="Device: cuda, cpu, mps (default: cuda)")
parser.add_argument(
"--model",
default="auto",
help=(
"Pre-load model: auto (GPU=fun-asr-nano, CPU=sensevoice), "
"sensevoice, paraformer, fun-asr-nano, moss-transcribe-diarize"
),
)
parser.add_argument("--model-path", default=None, help="Local model path or model ID (overrides --model)")
parser.add_argument("--hub", default="ms", help="Model hub: ms (ModelScope), hf (HuggingFace) (default: ms)")
parser.add_argument(
"--spk-model",
default="cam++",
help="Speaker model loaded on the first spk=true request (default: cam++)",
)
parser.add_argument(
"--cors-origin",
action="append",
default=None,
metavar="ORIGIN",
help="Trusted browser origin for CORS; repeat for multiple origins (disabled by default)",
)
return parser
def main():
args = build_parser().parse_args()
try:
import uvicorn
import fastapi
except ImportError:
print("Error: funasr-server requires additional packages.")
print("Install with: pip install vllm fastapi uvicorn python-multipart")
sys.exit(1)
# Import and configure the app
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'examples', 'openai_api'))
# Use inline app to avoid path issues
from funasr.bin._server_app import create_app
app = create_app(
device=args.device,
preload_model=args.model,
model_path=args.model_path,
hub=args.hub,
spk_model=args.spk_model,
cors_origins=args.cors_origin,
)
print(f"╔══════════════════════════════════════════════╗")
print(f"║ {server_version_label():<44}║")
print(f"║ Device: {args.device:<8} ║")
print(f"║ Model: {args.model:<12} ║")
if args.model_path:
print(f"║ Model Path: {args.model_path:<25} ║")
print(f"║ Hub: {args.hub:<8} ║")
print(f"║ URL: http://{args.host}:{args.port}/v1 ║")
print(f"║ Docs: http://{args.host}:{args.port}/docs ║")
print(f"╚══════════════════════════════════════════════╝")
uvicorn.run(app, host=args.host, port=args.port)
if __name__ == "__main__":
main()