-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOmniVoice_ep.py
More file actions
173 lines (138 loc) · 5.21 KB
/
OmniVoice_ep.py
File metadata and controls
173 lines (138 loc) · 5.21 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from fastapi import FastAPI, UploadFile, File, Form
from fastapi.responses import FileResponse
import subprocess
import uuid
import os
import shutil
app = FastAPI()
MODEL_PATH = "/path/to/OmniVoice"
BASE_DIR = "/path/to/OmniVoice/api_runtime"
UPLOAD_DIR = os.path.join(BASE_DIR, "uploads")
OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
os.makedirs(UPLOAD_DIR, exist_ok=True)
os.makedirs(OUTPUT_DIR, exist_ok=True)
@app.get("/")
def root():
return {"message": "API running"}
# @app.post("/generate")
# async def generate_audio(
# text: str = Form(...),
# ref_text: str = Form(...),
# instruct: str = Form("male, young adult, moderate pitch"),
# duration: float = Form(1e-15),
# guidance_scale: float = Form(9.0),
# speed: float = Form(0.5),
# denoise: bool = Form(False),
# ref_audio: UploadFile = File(...),
# preprocess_prompt: bool = Form(True),
# postprocess_output: bool = Form(True)
# ):
# try:
# uid = str(uuid.uuid4())
# # -----------------------------
# # Save uploaded reference audio
# # -----------------------------
# ref_audio_path = os.path.join(UPLOAD_DIR, f"{uid}_{ref_audio.filename}")
# with open(ref_audio_path, "wb") as f:
# shutil.copyfileobj(ref_audio.file, f)
# # -----------------------------
# # Output path
# # -----------------------------
# output_path = os.path.join(OUTPUT_DIR, f"{uid}.wav")
# # -----------------------------
# # Build CLI command
# # -----------------------------
# cmd = [
# "omnivoice-infer",
# "--model", MODEL_PATH,
# "--text", text,
# "--ref_audio", ref_audio_path,
# "--ref_text", ref_text,
# "--instruct", instruct,
# "--output", output_path,
# "--duration", str(duration),
# "--guidance_scale", str(guidance_scale),
# "--speed", str(speed),
# "--denoise", str(denoise),
# "--preprocess_prompt", str(preprocess_prompt),
# "--postprocess_output", str(postprocess_output)
# ]
# # -----------------------------
# # Run inference
# # -----------------------------
# result = subprocess.run(cmd, capture_output=True, text=True)
# if result.returncode != 0:
# return {"error": result.stderr}
# # -----------------------------
# # Return generated audio
# # -----------------------------
# return FileResponse(output_path, media_type="audio/wav")
# except Exception as e:
# return {"error": str(e)}
@app.post("/generate")
async def generate_audio(
text: str = Form(...),
# Optional reference block
ref_audio: UploadFile | None = File(None),
ref_text: str | None = Form(None),
# Optional params
instruct: str | None = Form(None),
duration: float | None = Form(None),
guidance_scale: float | None = Form(None),
speed: float | None = Form(None),
denoise: bool | None = Form(None),
preprocess_prompt: bool | None = Form(None),
postprocess_output: bool | None = Form(None)
):
try:
uid = str(uuid.uuid4())
output_path = os.path.join(OUTPUT_DIR, f"{uid}.wav")
# -----------------------------
# Base command
# -----------------------------
cmd = [
"omnivoice-infer",
"--model", MODEL_PATH,
"--text", text,
"--output", output_path,
]
# -----------------------------
# Handle reference block
# -----------------------------
if ref_audio and ref_text:
ref_audio_path = os.path.join(UPLOAD_DIR, f"{uid}_{ref_audio.filename}")
with open(ref_audio_path, "wb") as f:
shutil.copyfileobj(ref_audio.file, f)
cmd += ["--ref_audio", ref_audio_path]
cmd += ["--ref_text", ref_text]
elif ref_audio or ref_text:
return {"error": "Both ref_audio and ref_text must be provided together"}
# -----------------------------
# Optional scalar arguments
# -----------------------------
if instruct:
cmd += ["--instruct", instruct]
if duration is not None:
cmd += ["--duration", str(duration)]
if guidance_scale is not None:
cmd += ["--guidance_scale", str(guidance_scale)]
if speed is not None:
cmd += ["--speed", str(speed)]
# -----------------------------
# Boolean flags (only if False)
# -----------------------------
if denoise is False:
cmd += ["--denoise", "False"]
if preprocess_prompt is False:
cmd += ["--preprocess_prompt", "False"]
if postprocess_output is False:
cmd += ["--postprocess_output", "False"]
# -----------------------------
# Run inference
# -----------------------------
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
return {"error": result.stderr}
return FileResponse(output_path, media_type="audio/wav")
except Exception as e:
return {"error": str(e)}