azils3 commited on
Commit
5b910d3
1 Parent(s): c5d2b90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -25
app.py CHANGED
@@ -1,39 +1,40 @@
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}
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
  from TTS.api import TTS
 
4
 
5
+ # Set the environment variable to agree to the terms of service
6
+ os.environ["COQUI_TOS_AGREED"] = "1"
7
 
8
  # Initialize the TTS model
9
  tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2")
 
 
10
 
11
  # Get available voices and languages
12
  available_voices = tts.list_speakers()
13
  available_languages = tts.list_languages()
14
 
15
+ def generate_speech(text, voice, language):
16
+ # Generate speech
17
+ output_path = "output.wav"
18
+ tts.tts_to_file(text=text, speaker_wav=voice, language=language, file_path=output_path)
19
+ return output_path
20
 
21
+ # Create Gradio interface
22
+ with gr.Blocks() as demo:
23
+ gr.Markdown("# XTTS v2 Text-to-Speech Generator")
24
 
25
+ with gr.Row():
26
+ text_input = gr.Textbox(label="Input Text", placeholder="Enter text to be synthesized")
 
27
 
28
+ with gr.Row():
29
+ voice_dropdown = gr.Dropdown(label="Select Voice", choices=available_voices)
30
+ language_dropdown = gr.Dropdown(label="Select Language", choices=available_languages)
 
 
 
31
 
32
+ with gr.Row():
33
+ generate_button = gr.Button("Generate Speech")
34
+
35
+ with gr.Row():
36
+ audio_output = gr.Audio(label="Generated Speech")
37
+
38
+ generate_button.click(generate_speech, inputs=[text_input, voice_dropdown, language_dropdown], outputs=audio_output)
39
+
40
+ demo.launch(debug=True)