Major Update: Kokoro-82M with 54 Premium Voices

#7
by masbudjj - opened
Files changed (1) hide show
  1. app.py +182 -0
app.py ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Kokoro-82M TTS with 54 Voices
3
+ Built on StyleTTS 2 Architecture
4
+ """
5
+
6
+ import gradio as gr
7
+ import numpy as np
8
+ import scipy.io.wavfile as wavfile
9
+ from io import BytesIO
10
+ import requests
11
+ import json
12
+
13
+ # Voice database - 54 voices
14
+ VOICES = {
15
+ "American Female": {
16
+ "af_heart": "Heart - Warm & Friendly",
17
+ "af_bella": "Bella - Elegant & Smooth",
18
+ "af_nicole": "Nicole - Professional",
19
+ "af_aoede": "Aoede - Cheerful",
20
+ "af_kore": "Kore - Gentle",
21
+ "af_sarah": "Sarah - Clear",
22
+ "af_nova": "Nova - Modern",
23
+ "af_sky": "Sky - Light",
24
+ "af_alloy": "Alloy - Versatile",
25
+ "af_jessica": "Jessica - Natural",
26
+ "af_river": "River - Calm"
27
+ },
28
+ "American Male": {
29
+ "am_michael": "Michael - Deep & Authoritative",
30
+ "am_fenrir": "Fenrir - Strong",
31
+ "am_puck": "Puck - Playful",
32
+ "am_echo": "Echo - Resonant",
33
+ "am_eric": "Eric - Professional",
34
+ "am_liam": "Liam - Friendly",
35
+ "am_onyx": "Onyx - Rich",
36
+ "am_adam": "Adam - Natural"
37
+ },
38
+ "British Female": {
39
+ "bf_emma": "Emma - Refined",
40
+ "bf_isabella": "Isabella - Elegant",
41
+ "bf_alice": "Alice - Clear",
42
+ "bf_lily": "Lily - Soft"
43
+ },
44
+ "British Male": {
45
+ "bm_george": "George - Distinguished",
46
+ "bm_fable": "Fable - Storyteller",
47
+ "bm_lewis": "Lewis - Smooth",
48
+ "bm_daniel": "Daniel - Professional"
49
+ }
50
+ }
51
+
52
+ # Flatten voice dict for dropdown
53
+ def get_voice_list():
54
+ voice_list = []
55
+ for category, voices in VOICES.items():
56
+ for voice_id, desc in voices.items():
57
+ voice_list.append(f"{desc} ({voice_id})")
58
+ return voice_list
59
+
60
+ def generate_speech(text, voice_dropdown, speed):
61
+ """Generate speech using Kokoro-82M via HF API"""
62
+
63
+ if not text.strip():
64
+ return None, "❌ Please enter some text"
65
+
66
+ # Extract voice_id from dropdown selection
67
+ voice_id = voice_dropdown.split("(")[-1].strip(")")
68
+
69
+ try:
70
+ # Use Hugging Face Inference API
71
+ API_URL = "https://api-inference.huggingface.co/models/hexgrad/Kokoro-82M"
72
+
73
+ headers = {
74
+ "Content-Type": "application/json"
75
+ }
76
+
77
+ payload = {
78
+ "inputs": text,
79
+ "parameters": {
80
+ "voice": voice_id,
81
+ "speed": speed
82
+ }
83
+ }
84
+
85
+ response = requests.post(API_URL, headers=headers, json=payload)
86
+
87
+ if response.status_code == 200:
88
+ # Save audio
89
+ audio_bytes = response.content
90
+
91
+ # Return audio for playback
92
+ return audio_bytes, f"βœ… Generated with {voice_id} at {speed}x speed"
93
+ else:
94
+ return None, f"❌ API Error: {response.status_code}"
95
+
96
+ except Exception as e:
97
+ return None, f"❌ Error: {str(e)}"
98
+
99
+ # Build Gradio interface
100
+ with gr.Blocks(title="Kokoro-82M TTS - 54 Voices", theme=gr.themes.Soft()) as demo:
101
+
102
+ gr.Markdown("""
103
+ # πŸŽ™οΈ Kokoro-82M Text-to-Speech
104
+
105
+ **82 Million Parameters β€’ 54 Premium Voices β€’ StyleTTS 2 Architecture**
106
+
107
+ Choose from American & British voices with unique characteristics!
108
+ """)
109
+
110
+ with gr.Row():
111
+ with gr.Column(scale=1):
112
+ gr.Markdown("### 🎭 Voice Selection")
113
+
114
+ voice_selector = gr.Dropdown(
115
+ choices=get_voice_list(),
116
+ value=get_voice_list()[0],
117
+ label="Choose Voice (54 options)",
118
+ interactive=True
119
+ )
120
+
121
+ gr.Markdown("### βš™οΈ Settings")
122
+ speed = gr.Slider(
123
+ minimum=0.5,
124
+ maximum=2.0,
125
+ value=1.0,
126
+ step=0.05,
127
+ label="Speed",
128
+ interactive=True
129
+ )
130
+
131
+ gr.Markdown("""
132
+ ### 🌟 Voice Categories
133
+ - πŸ‡ΊπŸ‡Έ **American Female**: 11 voices
134
+ - πŸ‡ΊπŸ‡Έ **American Male**: 8 voices
135
+ - πŸ‡¬πŸ‡§ **British Female**: 4 voices
136
+ - πŸ‡¬πŸ‡§ **British Male**: 4 voices
137
+ """)
138
+
139
+ with gr.Column(scale=2):
140
+ gr.Markdown("### πŸ“ Text Input")
141
+
142
+ text_input = gr.Textbox(
143
+ lines=5,
144
+ placeholder="Enter your text here... Kokoro-82M supports natural prosody and emotion!",
145
+ value="Welcome to Kokoro-82M! Choose from 54 premium voices powered by StyleTTS 2.",
146
+ label="Text to synthesize"
147
+ )
148
+
149
+ generate_btn = gr.Button("🎀 Generate Speech", variant="primary", size="lg")
150
+
151
+ status_text = gr.Textbox(label="Status", interactive=False)
152
+
153
+ audio_output = gr.Audio(
154
+ label="Generated Audio",
155
+ type="numpy",
156
+ interactive=False
157
+ )
158
+
159
+ gr.Markdown("""
160
+ ### πŸ“Š Model Information
161
+ - **Model**: Kokoro-82M
162
+ - **Architecture**: StyleTTS 2 + ISTFTNet
163
+ - **Parameters**: 82 Million
164
+ - **License**: Apache 2.0
165
+ - **Training**: Few hundred hours of permissive data
166
+ """)
167
+
168
+ # Connect event
169
+ generate_btn.click(
170
+ fn=generate_speech,
171
+ inputs=[text_input, voice_selector, speed],
172
+ outputs=[audio_output, status_text]
173
+ )
174
+
175
+ gr.Markdown("""
176
+ ---
177
+ **Note**: This uses Hugging Face Inference API. First generation may take 20-30 seconds for model loading.
178
+ Subsequent generations are faster (~2-5 seconds).
179
+ """)
180
+
181
+ if __name__ == "__main__":
182
+ demo.launch()