Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,39 @@
|
|
1 |
-
import
|
|
|
2 |
from TTS.api import TTS
|
|
|
|
|
|
|
3 |
|
4 |
# Initialize the TTS model
|
5 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2")
|
|
|
|
|
6 |
|
7 |
# Get available voices and languages
|
8 |
available_voices = tts.list_speakers()
|
9 |
available_languages = tts.list_languages()
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
return output_path
|
16 |
-
|
17 |
-
# Create Gradio interface
|
18 |
-
with gr.Blocks() as demo:
|
19 |
-
gr.Markdown("# XTTS v2 Text-to-Speech Generator")
|
20 |
|
21 |
-
|
22 |
-
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
generate_button.click(generate_speech, inputs=[text_input, voice_dropdown, language_dropdown], outputs=audio_output)
|
35 |
-
|
36 |
-
demo.launch(debug=True)
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
from TTS.api import TTS
|
4 |
+
import torch
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
|
8 |
# Initialize the TTS model
|
9 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2")
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
tts.to(device)
|
12 |
|
13 |
# Get available voices and languages
|
14 |
available_voices = tts.list_speakers()
|
15 |
available_languages = tts.list_languages()
|
16 |
|
17 |
+
class TextToSpeechRequest(BaseModel):
|
18 |
+
text: str
|
19 |
+
voice: str
|
20 |
+
language: str
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
@app.get("/voices")
|
23 |
+
def get_voices():
|
24 |
+
return {"voices": available_voices}
|
25 |
|
26 |
+
@app.get("/languages")
|
27 |
+
def get_languages():
|
28 |
+
return {"languages": available_languages}
|
29 |
|
30 |
+
@app.post("/generate")
|
31 |
+
def generate_speech(request: TextToSpeechRequest):
|
32 |
+
if request.voice not in available_voices:
|
33 |
+
raise HTTPException(status_code=400, detail="Invalid voice selected")
|
34 |
+
if request.language not in available_languages:
|
35 |
+
raise HTTPException(status_code=400, detail="Invalid language selected")
|
36 |
|
37 |
+
output_path = "output.wav"
|
38 |
+
tts.tts_to_file(text=request.text, speaker=request.voice, language=request.language, file_path=output_path)
|
39 |
+
return {"message": "Speech generated successfully", "file_path": output_path}
|
|
|
|
|
|