Spaces:
Runtime error
Runtime error
| import tempfile | |
| import os | |
| from fastapi import FastAPI, HTTPException, UploadFile, File, Form | |
| from fastapi.responses import FileResponse | |
| from pathlib import Path | |
| import time | |
| # Importer KokoClone depuis le package installé | |
| from kokoclone import KokoClone | |
| print("🚀 Initialisation de KokoClone...") | |
| cloner = KokoClone() | |
| print("✅ KokoClone prêt") | |
| app = FastAPI(title="KokoClone API") | |
| async def root(): | |
| return {"status": "ok", "model": "KokoClone"} | |
| async def clone_voice( | |
| text: str = Form(...), | |
| lang: str = Form(...), | |
| audio_file: UploadFile = File(...) | |
| ): | |
| try: | |
| # Sauvegarde du fichier audio de référence | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_ref: | |
| content = await audio_file.read() | |
| tmp_ref.write(content) | |
| ref_path = tmp_ref.name | |
| # Chemin de sortie | |
| output_filename = f"output_{int(time.time())}.wav" | |
| output_path = Path("/app/tmp") / output_filename | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| # Génération | |
| cloner.generate( | |
| text=text, | |
| lang=lang, | |
| reference_audio=ref_path, | |
| output_path=str(output_path) | |
| ) | |
| # Nettoyage | |
| os.unlink(ref_path) | |
| return FileResponse( | |
| path=str(output_path), | |
| media_type="audio/wav", | |
| filename=output_filename | |
| ) | |
| except Exception as e: | |
| if 'ref_path' in locals() and os.path.exists(ref_path): | |
| os.unlink(ref_path) | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def list_voices(): | |
| return {"languages": ["fr", "en", "es", "it", "ja", "zh", "hi", "pt"]} | |