GeminiAi commited on
Commit
2232b15
·
verified ·
1 Parent(s): 6f0abc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -21
app.py CHANGED
@@ -1,44 +1,59 @@
1
  import gradio as gr
2
- import torch
3
  import torchaudio
4
  from tortoise.api import TextToSpeech
5
  from tortoise.utils.audio import load_voice
6
 
7
- # Initialize Tortoise-TTS
8
  tts = TextToSpeech()
9
 
 
 
 
 
 
 
 
10
  # Function to generate speech
11
  def generate_speech(text, voice):
12
- # Load the selected voice
13
- voice_samples, conditioning_latents = load_voice(voice)
14
-
15
- # Generate speech
16
- with torch.no_grad():
17
- gen = tts.tts_with_preset(text, voice_samples=voice_samples, conditioning_latents=conditioning_latents, preset="fast")
18
- torchaudio.save("output.wav", gen.squeeze(0).cpu(), 24000)
19
-
20
- return "output.wav"
21
-
22
- # Gradio interface
23
- def tts_app(text, voice):
24
  if not text:
25
  return "Please enter some text."
26
- return generate_speech(text, voice)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- # Gradio UI
29
  with gr.Blocks(
30
  theme=gr.themes.Soft(primary_hue="teal", secondary_hue="pink"),
31
  css=".gradio-container {background: linear-gradient(135deg, #f5f7fa, #c3cfe2);} "
32
  "button {background: linear-gradient(135deg, #6a11cb, #2575fc); color: white; border: none; padding: 10px 20px; border-radius: 5px;} "
33
  "button:hover {background: linear-gradient(135deg, #2575fc, #6a11cb);} "
34
  ) as demo:
 
35
  gr.Markdown(
36
  """
37
- # 🎤 **Voice Cloning and Text-to-Speech**
38
- **Convert text into natural-sounding speech or clone a voice!**
39
  """
40
  )
41
 
 
42
  with gr.Row():
43
  text_input = gr.Textbox(
44
  lines=5,
@@ -46,17 +61,20 @@ with gr.Blocks(
46
  placeholder="Type your text here..."
47
  )
48
  voice_input = gr.Dropdown(
49
- choices=["random", "custom_voice"], # Add more voices as needed
50
  label="🗣️ **Select Voice**",
51
- value="random"
52
  )
53
 
 
54
  output_audio = gr.Audio(label="🎧 **Generated Speech**", type="filepath")
55
 
 
56
  submit_button = gr.Button("✨ **Generate Speech**")
57
 
 
58
  submit_button.click(
59
- tts_app,
60
  inputs=[text_input, voice_input],
61
  outputs=output_audio
62
  )
 
1
  import gradio as gr
 
2
  import torchaudio
3
  from tortoise.api import TextToSpeech
4
  from tortoise.utils.audio import load_voice
5
 
6
+ # Initialize TorToiSe
7
  tts = TextToSpeech()
8
 
9
+ # List of available voices
10
+ AVAILABLE_VOICES = [
11
+ "angie", "daniel", "deniro", "emma", "freeman", "geralt", "halle",
12
+ "jlaw", "lj", "myself", "pat", "snakes", "tom", "train_atkins",
13
+ "train_dotrice", "train_kennard", "weaver", "william"
14
+ ]
15
+
16
  # Function to generate speech
17
  def generate_speech(text, voice):
 
 
 
 
 
 
 
 
 
 
 
 
18
  if not text:
19
  return "Please enter some text."
20
+
21
+ try:
22
+ # Load the selected voice
23
+ voice_samples, conditioning_latents = load_voice(voice)
24
+
25
+ # Generate speech
26
+ gen = tts.tts_with_preset(
27
+ text,
28
+ voice_samples=voice_samples,
29
+ conditioning_latents=conditioning_latents,
30
+ preset="fast"
31
+ )
32
+
33
+ # Save the output
34
+ output_file = "output.wav"
35
+ torchaudio.save(output_file, gen.squeeze(0).cpu(), 24000)
36
+
37
+ return output_file
38
+ except Exception as e:
39
+ return f"Error generating speech: {str(e)}"
40
 
41
+ # Gradio interface
42
  with gr.Blocks(
43
  theme=gr.themes.Soft(primary_hue="teal", secondary_hue="pink"),
44
  css=".gradio-container {background: linear-gradient(135deg, #f5f7fa, #c3cfe2);} "
45
  "button {background: linear-gradient(135deg, #6a11cb, #2575fc); color: white; border: none; padding: 10px 20px; border-radius: 5px;} "
46
  "button:hover {background: linear-gradient(135deg, #2575fc, #6a11cb);} "
47
  ) as demo:
48
+ # Title and description
49
  gr.Markdown(
50
  """
51
+ # 🎤 **TorToiSe Text-to-Speech**
52
+ **Generate natural-sounding speech in multiple voices!**
53
  """
54
  )
55
 
56
+ # Inputs
57
  with gr.Row():
58
  text_input = gr.Textbox(
59
  lines=5,
 
61
  placeholder="Type your text here..."
62
  )
63
  voice_input = gr.Dropdown(
64
+ choices=AVAILABLE_VOICES,
65
  label="🗣️ **Select Voice**",
66
+ value="emma" # Default voice
67
  )
68
 
69
+ # Output
70
  output_audio = gr.Audio(label="🎧 **Generated Speech**", type="filepath")
71
 
72
+ # Submit button
73
  submit_button = gr.Button("✨ **Generate Speech**")
74
 
75
+ # Link button to function
76
  submit_button.click(
77
+ generate_speech,
78
  inputs=[text_input, voice_input],
79
  outputs=output_audio
80
  )