azils3 commited on
Commit
c5d2b90
·
verified ·
1 Parent(s): 304fd29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -23
app.py CHANGED
@@ -1,36 +1,39 @@
1
- import gradio as gr
 
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
- def generate_speech(text, voice, language):
12
- # Generate speech
13
- output_path = "output.wav"
14
- tts.tts_to_file(text=text, speaker_wav=voice, language=language, file_path=output_path)
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
- with gr.Row():
22
- text_input = gr.Textbox(label="Input Text", placeholder="Enter text to be synthesized")
 
23
 
24
- with gr.Row():
25
- voice_dropdown = gr.Dropdown(label="Select Voice", choices=available_voices)
26
- language_dropdown = gr.Dropdown(label="Select Language", choices=available_languages)
27
 
28
- with gr.Row():
29
- generate_button = gr.Button("Generate Speech")
 
 
 
 
30
 
31
- with gr.Row():
32
- audio_output = gr.Audio(label="Generated Speech")
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}