kemuririn
commited on
Commit
Β·
7eeb257
1
Parent(s):
09c6470
modify api implementation
Browse files
webui.py
CHANGED
|
@@ -15,10 +15,6 @@ import gradio as gr
|
|
| 15 |
from indextts.infer import IndexTTS
|
| 16 |
from tools.i18n.i18n import I18nAuto
|
| 17 |
|
| 18 |
-
from fastapi import FastAPI, UploadFile, Form
|
| 19 |
-
from fastapi.responses import FileResponse, JSONResponse
|
| 20 |
-
import uvicorn
|
| 21 |
-
|
| 22 |
i18n = I18nAuto(language="zh_CN")
|
| 23 |
MODE = 'local'
|
| 24 |
snapshot_download("IndexTeam/IndexTTS-1.5",local_dir="checkpoints",)
|
|
@@ -27,8 +23,6 @@ tts = IndexTTS(model_dir="checkpoints", cfg_path="checkpoints/config.yaml")
|
|
| 27 |
os.makedirs("outputs/tasks",exist_ok=True)
|
| 28 |
os.makedirs("prompts",exist_ok=True)
|
| 29 |
|
| 30 |
-
app = FastAPI()
|
| 31 |
-
|
| 32 |
@spaces.GPU
|
| 33 |
def infer(voice, text,output_path=None):
|
| 34 |
if not tts:
|
|
@@ -38,14 +32,14 @@ def infer(voice, text,output_path=None):
|
|
| 38 |
tts.infer(voice, text, output_path)
|
| 39 |
return output_path
|
| 40 |
|
| 41 |
-
def tts_api(voice
|
| 42 |
try:
|
| 43 |
output_path = infer(voice, text)
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
-
return
|
| 47 |
-
|
| 48 |
-
app.post("/api/tts")(tts_api)
|
| 49 |
|
| 50 |
def gen_single(prompt, text):
|
| 51 |
output_path = infer(prompt, text)
|
|
@@ -55,7 +49,6 @@ def update_prompt_audio():
|
|
| 55 |
update_button = gr.update(interactive=True)
|
| 56 |
return update_button
|
| 57 |
|
| 58 |
-
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
mutex = threading.Lock()
|
| 61 |
gr.HTML('''
|
|
@@ -85,11 +78,25 @@ with gr.Blocks() as demo:
|
|
| 85 |
inputs=[prompt_audio, input_text_single],
|
| 86 |
outputs=[output_audio])
|
| 87 |
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
def main():
|
| 91 |
tts.load_normalizer()
|
| 92 |
-
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
| 95 |
main()
|
|
|
|
| 15 |
from indextts.infer import IndexTTS
|
| 16 |
from tools.i18n.i18n import I18nAuto
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
i18n = I18nAuto(language="zh_CN")
|
| 19 |
MODE = 'local'
|
| 20 |
snapshot_download("IndexTeam/IndexTTS-1.5",local_dir="checkpoints",)
|
|
|
|
| 23 |
os.makedirs("outputs/tasks",exist_ok=True)
|
| 24 |
os.makedirs("prompts",exist_ok=True)
|
| 25 |
|
|
|
|
|
|
|
| 26 |
@spaces.GPU
|
| 27 |
def infer(voice, text,output_path=None):
|
| 28 |
if not tts:
|
|
|
|
| 32 |
tts.infer(voice, text, output_path)
|
| 33 |
return output_path
|
| 34 |
|
| 35 |
+
def tts_api(voice, text):
|
| 36 |
try:
|
| 37 |
output_path = infer(voice, text)
|
| 38 |
+
with open(output_path, "rb") as f:
|
| 39 |
+
audio_bytes = f.read()
|
| 40 |
+
return (200, {}, audio_bytes)
|
| 41 |
except Exception as e:
|
| 42 |
+
return (500, {"error": str(e)}, None)
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def gen_single(prompt, text):
|
| 45 |
output_path = infer(prompt, text)
|
|
|
|
| 49 |
update_button = gr.update(interactive=True)
|
| 50 |
return update_button
|
| 51 |
|
|
|
|
| 52 |
with gr.Blocks() as demo:
|
| 53 |
mutex = threading.Lock()
|
| 54 |
gr.HTML('''
|
|
|
|
| 78 |
inputs=[prompt_audio, input_text_single],
|
| 79 |
outputs=[output_audio])
|
| 80 |
|
| 81 |
+
# ζ°ε’APIζ₯ε£οΌδΎε€ι¨HTTPθ°η¨
|
| 82 |
+
api_iface = gr.Interface(
|
| 83 |
+
fn=lambda voice, text: infer(voice, text),
|
| 84 |
+
inputs=[gr.Audio(label="voice", type="filepath"), gr.Textbox(label="text")],
|
| 85 |
+
outputs=gr.Audio(label="result", type="filepath"),
|
| 86 |
+
allow_flagging="never",
|
| 87 |
+
description="APIζ₯ε£: δΌ ε
₯εθι³ι’εζζ¬οΌθΏεεζι³ι’γ"
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# ε°APIζ₯ε£ιθε¨UIδΈοΌεͺζ΄ι²HTTPζ₯ε£
|
| 91 |
+
with demo:
|
| 92 |
+
gr.TabItem("APIζ₯ε£", visible=False)
|
| 93 |
+
api_iface.render()
|
| 94 |
+
|
| 95 |
+
# η§»ι€ add_api_route ε mount_gradio_appοΌSpaces δΈζ―ζ
|
| 96 |
|
| 97 |
def main():
|
| 98 |
tts.load_normalizer()
|
| 99 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
main()
|