File size: 2,321 Bytes
0bb4f0d
 
 
 
 
 
 
 
fbf425d
5ba53f6
fbf425d
0367980
fbf425d
0bb4f0d
 
 
 
 
 
 
5ba53f6
 
3293398
 
5ba53f6
 
 
 
af681c6
 
 
 
 
 
 
 
0bb4f0d
 
af681c6
 
 
0bb4f0d
 
 
 
 
 
 
af681c6
 
 
 
 
 
0bb4f0d
af681c6
 
5ba53f6
0bb4f0d
 
5ba53f6
 
 
0bb4f0d
 
af681c6
 
 
 
 
 
 
 
 
5ba53f6
 
0bb4f0d
 
 
 
 
 
3293398
1
2
3
4
5
6
7
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import torch
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from TTS.api import TTS
import uvicorn
import tempfile
from fastapi.responses import FileResponse


#curl -X POST -H "Content-Type: application/json" -d '{"message": "Hello ! How are you ?"}' https://micksoftware-vctk.hf.space/tts

print("Starting...")

# Charger le modèle TTS
MODEL_NAME = "tts_models/en/vctk/vits"
tts = TTS(MODEL_NAME)

# Initialiser l'application FastAPI
app = FastAPI()

print("Test du modèle TTS...")
try:
    tts.tts_to_file(text="Test audio", speaker="p339", speaker_wav=None,
                    age=22, gender="F", emotion="neutral", file_path="test.wav")
    print("Le modèle fonctionne correctement.")
except Exception as e:
    print("Erreur lors du test du modèle :", e)


SPEAKER_MAPPING = {
    "Eden": "p339",
    "voice2": "p340",
    # Ajoutez d'autres correspondances si nécessaire
}


class TextInput(BaseModel):
    text: str
    emotion: str = "neutral"
    speaker: str = "Eden"


@app.post("/tts")
def generate_tts(input: TextInput):
    """Génère un fichier audio à partir du texte donné."""
    if not input.text.strip():
        raise HTTPException(status_code=400, detail="Le texte ne peut pas être vide.")
    
    speaker_code = SPEAKER_MAPPING.get(input.speaker)
    if speaker_code is None:
        raise HTTPException(
            status_code=400, detail=f"Le speaker '{input.speaker}' n'est pas reconnu.")


    try:
        print("Generation starts:", input.text.strip(), "avec emotion:",
              input.emotion, "et speaker:", input.speaker)

        with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
            output_path = tmpfile.name

        print("tmp file ready")

        
        # Générer l'audio
        tts.tts_to_file(
            text=input.text,
            speaker=speaker_code,
            speaker_wav=None,
            age=22,
            gender="F",
            emotion=input.emotion,
            file_path=output_path
        )
        print("tmp file generated")

        return FileResponse(output_path, media_type="audio/wav", filename="output.wav")
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)