| from fastapi import FastAPI, Response, HTTPException, Request |
| from fastapi.middleware.cors import CORSMiddleware |
| from transformers import VitsModel, AutoTokenizer |
| import torch |
| import scipy.io.wavfile |
| import io |
| import numpy as np |
| import os |
|
|
| app = FastAPI() |
|
|
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["*"], |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
|
|
| |
| |
| MODEL_ID = "facebook/mms-tts-mlg" |
| model = None |
| tokenizer = None |
|
|
| print("⏳ Démarrage du serveur Malagasy TTS...") |
|
|
| def load_model(): |
| global model, tokenizer |
| try: |
| if model is not None: return True |
| |
| print(f"📥 Chargement du modèle {MODEL_ID}...") |
| |
| model = VitsModel.from_pretrained(MODEL_ID) |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) |
| |
| print("✅ Modèle Malgache chargé !") |
| return True |
| except Exception as e: |
| print(f"❌ Erreur critique chargement : {e}") |
| return False |
|
|
| |
| load_model() |
|
|
| @app.post("/tts") |
| async def generate_speech(request: Request, data: dict): |
| |
| |
| |
| |
| |
|
|
| |
| if model is None: |
| if not load_model(): |
| raise HTTPException(status_code=500, detail="Modèle indisponible") |
| |
| text = data.get("text", "") |
| if not text: |
| raise HTTPException(status_code=400, detail="Texte vide") |
| |
| print(f"🇲🇬 Génération pour : {text[:30]}...") |
| |
| try: |
| |
| inputs = tokenizer(text, return_tensors="pt") |
| |
| |
| with torch.no_grad(): |
| output = model(**inputs).waveform |
| |
| |
| |
| audio_array = output.float().numpy().squeeze() |
| sample_rate = model.config.sampling_rate |
| |
| |
| |
| max_amp = np.max(np.abs(audio_array)) |
| if max_amp > 0: |
| audio_array = audio_array / max_amp |
| |
| |
| audio_int16 = (audio_array * 32767.0).astype(np.int16) |
| |
|
|
| |
| buffer = io.BytesIO() |
| scipy.io.wavfile.write(buffer, rate=sample_rate, data=audio_int16) |
| buffer.seek(0) |
| |
| return Response(content=buffer.read(), media_type="audio/wav") |
| except Exception as e: |
| print(f"❌ Erreur génération : {e}") |
| return Response(content=str(e), status_code=500) |
|
|
| @app.get("/") |
| def home(): |
| status = "Ready ✅" if model else "Error ❌" |
| return {"status": status, "lang": "mlg (Malagasy)", "optimized": "Int16 Compression Active"} |