Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
-
import tempfile, os, shutil
|
| 4 |
-
|
| 5 |
from core.cloner import KokoClone
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# Charge les modèles une seule fois au démarrage
|
| 10 |
print("Loading KokoClone models...")
|
| 11 |
cloner = KokoClone()
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
@app.post("/clone")
|
| 20 |
-
async def
|
| 21 |
text: str = Form(...),
|
| 22 |
lang: str = Form(default="en"),
|
| 23 |
reference_audio: UploadFile = File(...)
|
| 24 |
):
|
| 25 |
-
"""
|
| 26 |
-
Text + reference audio → cloned speech WAV
|
| 27 |
-
"""
|
| 28 |
-
# Sauvegarde l'audio de référence dans un fichier temporaire
|
| 29 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as ref_tmp:
|
| 30 |
shutil.copyfileobj(reference_audio.file, ref_tmp)
|
| 31 |
ref_path = ref_tmp.name
|
| 32 |
-
|
| 33 |
-
out_path = ref_path.replace(".wav", "_output.wav")
|
| 34 |
-
|
| 35 |
try:
|
| 36 |
-
cloner.generate(
|
| 37 |
-
text=text,
|
| 38 |
-
lang=lang,
|
| 39 |
-
reference_audio=ref_path,
|
| 40 |
-
output_path=out_path
|
| 41 |
-
)
|
| 42 |
return FileResponse(out_path, media_type="audio/wav", filename="cloned.wav")
|
| 43 |
except Exception as e:
|
| 44 |
raise HTTPException(status_code=500, detail=str(e))
|
| 45 |
finally:
|
| 46 |
os.unlink(ref_path)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
async def convert_voice(
|
| 51 |
source_audio: UploadFile = File(...),
|
| 52 |
reference_audio: UploadFile = File(...)
|
| 53 |
):
|
| 54 |
-
"""
|
| 55 |
-
Source audio + reference audio → re-voiced WAV
|
| 56 |
-
"""
|
| 57 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as src_tmp:
|
| 58 |
shutil.copyfileobj(source_audio.file, src_tmp)
|
| 59 |
src_path = src_tmp.name
|
| 60 |
-
|
| 61 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as ref_tmp:
|
| 62 |
shutil.copyfileobj(reference_audio.file, ref_tmp)
|
| 63 |
ref_path = ref_tmp.name
|
| 64 |
-
|
| 65 |
out_path = src_path.replace(".wav", "_converted.wav")
|
| 66 |
-
|
| 67 |
try:
|
| 68 |
-
cloner.convert(
|
| 69 |
-
source_audio=src_path,
|
| 70 |
-
reference_audio=ref_path,
|
| 71 |
-
output_path=out_path
|
| 72 |
-
)
|
| 73 |
return FileResponse(out_path, media_type="audio/wav", filename="converted.wav")
|
| 74 |
except Exception as e:
|
| 75 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import shutil
|
| 4 |
+
import tempfile
|
| 5 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 6 |
from fastapi.responses import FileResponse
|
|
|
|
|
|
|
| 7 |
from core.cloner import KokoClone
|
| 8 |
|
| 9 |
+
# --- Chargement des modèles (une seule fois) ---
|
|
|
|
|
|
|
| 10 |
print("Loading KokoClone models...")
|
| 11 |
cloner = KokoClone()
|
| 12 |
|
| 13 |
+
# --- FastAPI montée SUR l'app Gradio ---
|
| 14 |
+
def clone_voice(text, lang, ref_audio_path):
|
| 15 |
+
if not text or not text.strip():
|
| 16 |
+
raise gr.Error("Please enter some text.")
|
| 17 |
+
if not ref_audio_path:
|
| 18 |
+
raise gr.Error("Please upload or record a reference audio file.")
|
| 19 |
+
output_file = "gradio_output.wav"
|
| 20 |
+
try:
|
| 21 |
+
cloner.generate(text=text, lang=lang, reference_audio=ref_audio_path, output_path=output_file)
|
| 22 |
+
return output_file
|
| 23 |
+
except Exception as e:
|
| 24 |
+
raise gr.Error(f"Error: {str(e)}")
|
| 25 |
+
|
| 26 |
+
def convert_voice(source_audio_path, ref_audio_path):
|
| 27 |
+
if not source_audio_path:
|
| 28 |
+
raise gr.Error("Please upload a source audio file.")
|
| 29 |
+
if not ref_audio_path:
|
| 30 |
+
raise gr.Error("Please upload a reference audio file.")
|
| 31 |
+
output_file = "gradio_convert_output.wav"
|
| 32 |
+
try:
|
| 33 |
+
cloner.convert(source_audio=source_audio_path, reference_audio=ref_audio_path, output_path=output_file)
|
| 34 |
+
return output_file
|
| 35 |
+
except Exception as e:
|
| 36 |
+
raise gr.Error(f"Error: {str(e)}")
|
| 37 |
+
|
| 38 |
+
# --- UI Gradio (inchangée) ---
|
| 39 |
+
with gr.Blocks() as demo:
|
| 40 |
+
gr.Markdown("<div style='text-align:center'><h1>🎧 KokoClone</h1></div>")
|
| 41 |
+
with gr.Tabs():
|
| 42 |
+
with gr.Tab("🎤 Text → Clone"):
|
| 43 |
+
with gr.Row():
|
| 44 |
+
with gr.Column():
|
| 45 |
+
text_input = gr.Textbox(label="Text", lines=4)
|
| 46 |
+
lang_input = gr.Dropdown(
|
| 47 |
+
choices=[("English","en"),("French","fr"),("Hindi","hi"),
|
| 48 |
+
("Japanese","ja"),("Chinese","zh"),("Spanish","es")],
|
| 49 |
+
value="en", label="Language"
|
| 50 |
+
)
|
| 51 |
+
ref_audio = gr.Audio(label="Reference Voice", type="filepath")
|
| 52 |
+
btn = gr.Button("🚀 Generate", variant="primary")
|
| 53 |
+
with gr.Column():
|
| 54 |
+
out_audio = gr.Audio(label="Output", interactive=False)
|
| 55 |
+
btn.click(clone_voice, inputs=[text_input, lang_input, ref_audio], outputs=out_audio)
|
| 56 |
|
| 57 |
+
with gr.Tab("🔁 Audio → Clone"):
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column():
|
| 60 |
+
src_audio = gr.Audio(label="Source Audio", type="filepath")
|
| 61 |
+
ref_audio2 = gr.Audio(label="Reference Voice", type="filepath")
|
| 62 |
+
btn2 = gr.Button("🔁 Convert", variant="primary")
|
| 63 |
+
with gr.Column():
|
| 64 |
+
out_audio2 = gr.Audio(label="Output", interactive=False)
|
| 65 |
+
btn2.click(convert_voice, inputs=[src_audio, ref_audio2], outputs=out_audio2)
|
| 66 |
|
| 67 |
+
# --- Montage FastAPI ---
|
| 68 |
+
app = gr.mount_gradio_app(FastAPI(), demo, path="/")
|
| 69 |
|
| 70 |
+
@app.post("/api/clone")
|
| 71 |
+
async def api_clone(
|
| 72 |
text: str = Form(...),
|
| 73 |
lang: str = Form(default="en"),
|
| 74 |
reference_audio: UploadFile = File(...)
|
| 75 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as ref_tmp:
|
| 77 |
shutil.copyfileobj(reference_audio.file, ref_tmp)
|
| 78 |
ref_path = ref_tmp.name
|
| 79 |
+
out_path = ref_path.replace(".wav", "_out.wav")
|
|
|
|
|
|
|
| 80 |
try:
|
| 81 |
+
cloner.generate(text=text, lang=lang, reference_audio=ref_path, output_path=out_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
return FileResponse(out_path, media_type="audio/wav", filename="cloned.wav")
|
| 83 |
except Exception as e:
|
| 84 |
raise HTTPException(status_code=500, detail=str(e))
|
| 85 |
finally:
|
| 86 |
os.unlink(ref_path)
|
| 87 |
|
| 88 |
+
@app.post("/api/convert")
|
| 89 |
+
async def api_convert(
|
|
|
|
| 90 |
source_audio: UploadFile = File(...),
|
| 91 |
reference_audio: UploadFile = File(...)
|
| 92 |
):
|
|
|
|
|
|
|
|
|
|
| 93 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as src_tmp:
|
| 94 |
shutil.copyfileobj(source_audio.file, src_tmp)
|
| 95 |
src_path = src_tmp.name
|
|
|
|
| 96 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as ref_tmp:
|
| 97 |
shutil.copyfileobj(reference_audio.file, ref_tmp)
|
| 98 |
ref_path = ref_tmp.name
|
|
|
|
| 99 |
out_path = src_path.replace(".wav", "_converted.wav")
|
|
|
|
| 100 |
try:
|
| 101 |
+
cloner.convert(source_audio=src_path, reference_audio=ref_path, output_path=out_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
return FileResponse(out_path, media_type="audio/wav", filename="converted.wav")
|
| 103 |
except Exception as e:
|
| 104 |
raise HTTPException(status_code=500, detail=str(e))
|