heerjtdev commited on
Commit
dff9996
Β·
verified Β·
1 Parent(s): 322d7a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
app.py CHANGED
@@ -1,52 +1,54 @@
1
  import gradio as gr
2
- from kokoro import TTS
3
  import torch
4
 
5
- # Load model once at startup
6
- device = "cuda" if torch.cuda.is_available() else "cpu"
7
 
8
- # You can pick a voice that exists in Kokoro (e.g. 'af_heart', 'af_calypso', 'am_adam', etc.)
9
- VOICE = "af_heart"
10
 
11
- print("πŸ”„ Loading Kokoro-TTS model...")
12
- tts = TTS(voice=VOICE, device=device)
13
- print("βœ… Model loaded successfully!")
14
 
15
 
16
- def generate_audio(text):
17
  if not text.strip():
18
  return None, "Please enter some text."
19
 
20
  try:
21
- print("🎀 Generating audio...")
22
- audio = tts.generate(text) # returns numpy array (float32)
23
- return (44100, audio), "Success!"
24
  except Exception as e:
25
  return None, f"Error: {str(e)}"
26
 
27
 
28
- ### GRADIO UI ###
29
- with gr.Blocks(title="Kokoro-TTS Text β†’ Audio") as demo:
30
- gr.Markdown("## 🎧 Kokoro-TTS: Text to Speech Demo")
 
 
 
 
 
 
31
 
32
- with gr.Row():
33
- input_text = gr.Textbox(
34
- label="Enter text",
35
- placeholder="Type something to speak...",
36
- lines=4
37
- )
38
 
39
  with gr.Row():
40
- output_audio = gr.Audio(label="Generated Audio")
41
- status = gr.Textbox(label="Status")
 
 
 
42
 
43
- generate_button = gr.Button("Generate")
44
 
45
- generate_button.click(
46
  fn=generate_audio,
47
- inputs=input_text,
48
- outputs=[output_audio, status]
49
  )
50
 
51
- if __name__ == "__main__":
52
- demo.launch()
 
1
  import gradio as gr
2
+ from kokoro import Kokoro
3
  import torch
4
 
 
 
5
 
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
 
7
 
8
+ print("πŸ”„ Loading Kokoro model...")
9
+ model = Kokoro(device=device)
10
+ print("βœ… Kokoro loaded!")
11
 
12
 
13
+ def generate_audio(text, voice):
14
  if not text.strip():
15
  return None, "Please enter some text."
16
 
17
  try:
18
+ print("🎀 Generating...")
19
+ audio = model.tts(text, voice=voice) # numpy float32 array
20
+ return (24000, audio), "Success!"
21
  except Exception as e:
22
  return None, f"Error: {str(e)}"
23
 
24
 
25
+ voices = [
26
+ "af_heart",
27
+ "af_bella",
28
+ "af_nicole",
29
+ "am_adam",
30
+ "am_michael",
31
+ "bf_emma",
32
+ "bm_george"
33
+ ]
34
 
35
+
36
+ with gr.Blocks(title="Kokoro-TTS") as demo:
37
+ gr.Markdown("## 🎧 Kokoro Text β†’ Speech")
 
 
 
38
 
39
  with gr.Row():
40
+ txt = gr.Textbox(lines=4, label="Input Text")
41
+ voice_select = gr.Dropdown(voices, value="af_heart", label="Voice")
42
+
43
+ audio_out = gr.Audio(label="Generated Audio")
44
+ status = gr.Textbox(label="Status")
45
 
46
+ btn = gr.Button("Generate Audio")
47
 
48
+ btn.click(
49
  fn=generate_audio,
50
+ inputs=[txt, voice_select],
51
+ outputs=[audio_out, status]
52
  )
53
 
54
+ demo.launch()