Spaces:
Runtime error
Runtime error
Randriamiandry commited on
Commit ·
6c2e817
1
Parent(s): db22081
kokoclone
Browse files- Dockerfile +23 -3
- app.py +87 -88
- requirements.txt +7 -3
Dockerfile
CHANGED
|
@@ -1,7 +1,27 @@
|
|
| 1 |
-
FROM python:3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
WORKDIR /app
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
COPY requirements.txt .
|
| 5 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 1 |
+
FROM python:3.12.12
|
| 2 |
+
|
| 3 |
+
# Évite la création de fichiers .pyc et active les logs en temps réel
|
| 4 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 5 |
+
ENV PYTHONUNBUFFERED=1
|
| 6 |
+
|
| 7 |
+
# Définit le répertoire de travail
|
| 8 |
WORKDIR /app
|
| 9 |
+
|
| 10 |
+
# Met à jour pip et installe les dépendances système légères
|
| 11 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 12 |
+
ffmpeg \
|
| 13 |
+
&& apt-get clean \
|
| 14 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 15 |
+
|
| 16 |
+
# Installe les dépendances Python
|
| 17 |
COPY requirements.txt .
|
| 18 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 19 |
+
|
| 20 |
+
# Copie le code source
|
| 21 |
+
COPY app.py .
|
| 22 |
+
|
| 23 |
+
# Expose le port sur lequel l'API tourne
|
| 24 |
+
EXPOSE 7860
|
| 25 |
+
|
| 26 |
+
# Commande de démarrage du serveur
|
| 27 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
|
@@ -1,93 +1,92 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from fastapi import
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
from
|
| 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 |
-
app
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
audio = 0.5 * np.sin(2 * np.pi * 440 * t)
|
| 64 |
-
audio = (audio * 32767).astype(np.int16)
|
| 65 |
-
return audio, sample_rate
|
| 66 |
-
|
| 67 |
-
@app.post("/tts")
|
| 68 |
-
async def tts(request: TTSRequest):
|
| 69 |
try:
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
@app.get("/voices")
|
| 88 |
async def list_voices():
|
| 89 |
-
return {"
|
| 90 |
-
|
| 91 |
-
if __name__ == "__main__":
|
| 92 |
-
import uvicorn
|
| 93 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
from fastapi import FastAPI, HTTPException, UploadFile, File, Form
|
| 4 |
+
from fastapi.responses import FileResponse, StreamingResponse
|
| 5 |
+
from pydantic import BaseModel, HttpUrl
|
| 6 |
+
import requests
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
import time
|
| 9 |
+
|
| 10 |
+
# Initialisation
|
| 11 |
+
print("🚀 Démarrage du serveur FastAPI pour KokoClone...")
|
| 12 |
+
|
| 13 |
+
# 1. Configuration du chemin pour les modèles et les sorties
|
| 14 |
+
MODEL_DIR = Path("/app/cache/models")
|
| 15 |
+
MODEL_DIR.mkdir(parents=True, exist_ok=True)
|
| 16 |
+
OUTPUT_DIR = Path("/app/tmp")
|
| 17 |
+
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
# 2. Télécharger le modèle cloner.py depuis le dépôt officiel si non présent
|
| 20 |
+
CLONER_URL = "https://raw.githubusercontent.com/Ashish-Patnaik/kokoclone/main/core/cloner.py"
|
| 21 |
+
CLONER_PATH = MODEL_DIR / "cloner.py"
|
| 22 |
+
|
| 23 |
+
if not CLONER_PATH.exists():
|
| 24 |
+
print(f"⏳ Téléchargement de {CLONER_URL}...")
|
| 25 |
+
response = requests.get(CLONER_URL)
|
| 26 |
+
response.raise_for_status()
|
| 27 |
+
CLONER_PATH.write_text(response.text)
|
| 28 |
+
print("✅ cloner.py téléchargé avec succès.")
|
| 29 |
+
|
| 30 |
+
# 3. Ajouter le chemin des modèles au PYTHONPATH pour l'import
|
| 31 |
+
import sys
|
| 32 |
+
sys.path.insert(0, str(MODEL_DIR))
|
| 33 |
+
|
| 34 |
+
# 4. Importer et initialiser KokoClone
|
| 35 |
+
from cloner import KokoClone
|
| 36 |
+
|
| 37 |
+
print("⏳ Initialisation de KokoClone (première exécution, téléchargement des modèles)...")
|
| 38 |
+
cloner = KokoClone()
|
| 39 |
+
print("✅ KokoClone prêt à l'emploi.")
|
| 40 |
+
|
| 41 |
+
app = FastAPI(title="KokoClone API", version="1.0")
|
| 42 |
+
|
| 43 |
+
@app.get("/")
|
| 44 |
+
async def root():
|
| 45 |
+
return {"status": "OK", "model": "KokoClone"}
|
| 46 |
+
|
| 47 |
+
@app.post("/clone")
|
| 48 |
+
async def clone_voice(
|
| 49 |
+
text: str = Form(...),
|
| 50 |
+
lang: str = Form(...),
|
| 51 |
+
audio_file: UploadFile = File(...)
|
| 52 |
+
):
|
| 53 |
+
# 1. Sauvegarde temporaire du fichier audio de référence
|
| 54 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_ref:
|
| 55 |
+
content = await audio_file.read()
|
| 56 |
+
tmp_ref.write(content)
|
| 57 |
+
ref_path = tmp_ref.name
|
| 58 |
+
|
| 59 |
+
# 2. Définition du chemin de sortie
|
| 60 |
+
output_filename = f"output_{int(time.time())}.wav"
|
| 61 |
+
output_path = OUTPUT_DIR / output_filename
|
| 62 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
try:
|
| 64 |
+
# 3. Appel à KokoClone pour la génération
|
| 65 |
+
cloner.generate(
|
| 66 |
+
text=text,
|
| 67 |
+
lang=lang,
|
| 68 |
+
reference_audio=ref_path,
|
| 69 |
+
output_path=str(output_path)
|
| 70 |
+
)
|
| 71 |
|
| 72 |
+
# 4. Nettoyage du fichier temporaire de référence
|
| 73 |
+
os.unlink(ref_path)
|
| 74 |
+
|
| 75 |
+
# 5. Retour du fichier audio généré
|
| 76 |
+
return FileResponse(
|
| 77 |
+
path=str(output_path),
|
| 78 |
+
media_type="audio/wav",
|
| 79 |
+
filename=output_filename
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
# Nettoyage en cas d'erreur
|
| 84 |
+
if os.path.exists(ref_path):
|
| 85 |
+
os.unlink(ref_path)
|
| 86 |
+
if output_path.exists():
|
| 87 |
+
output_path.unlink()
|
| 88 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 89 |
|
| 90 |
@app.get("/voices")
|
| 91 |
async def list_voices():
|
| 92 |
+
return {"languages": ["en", "hi", "fr", "ja", "zh", "it", "es", "pt"]}
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,6 +1,10 @@
|
|
| 1 |
fastapi
|
| 2 |
uvicorn[standard]
|
| 3 |
-
huggingface-hub
|
| 4 |
-
llama-cpp-python
|
| 5 |
-
numpy
|
| 6 |
soundfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn[standard]
|
|
|
|
|
|
|
|
|
|
| 3 |
soundfile
|
| 4 |
+
huggingface_hub
|
| 5 |
+
torch
|
| 6 |
+
torchaudio
|
| 7 |
+
kokoro-onnx
|
| 8 |
+
misaki[en,ja,zh]
|
| 9 |
+
git+https://github.com/frothywater/kanade-tokenizer
|
| 10 |
+
numpy
|