Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import trafilatura
|
| 3 |
from trafilatura import fetch_url, extract
|
|
@@ -15,7 +17,8 @@ import nltk
|
|
| 15 |
nltk.download("punkt")
|
| 16 |
|
| 17 |
# Initialize KokoroTTS with default English
|
| 18 |
-
kokoro_tts = KPipeline(lang_code='a') # 'a' is for American English
|
|
|
|
| 19 |
|
| 20 |
# Supported TTS Languages
|
| 21 |
SUPPORTED_TTS_LANGUAGES = {
|
|
@@ -101,8 +104,11 @@ def detect_language(text):
|
|
| 101 |
return "en" # Default to English if detection fails
|
| 102 |
|
| 103 |
### 4️⃣ TTS Functionality (KokoroTTS)
|
|
|
|
| 104 |
def generate_audio_kokoro(text, lang):
|
| 105 |
"""Generate speech using KokoroTTS for supported languages."""
|
|
|
|
|
|
|
| 106 |
lang_code = SUPPORTED_TTS_LANGUAGES.get(lang, "a") # Default to English
|
| 107 |
generator = kokoro_tts(text, voice="af_bella", speed=1, split_pattern=r'\n+')
|
| 108 |
# 3. Specify Device
|
|
@@ -118,6 +124,8 @@ def generate_audio_kokoro(text, lang):
|
|
| 118 |
|
| 119 |
# Concatenate all audio data into a single array
|
| 120 |
full_audio = np.concatenate(audio_data_list)
|
|
|
|
|
|
|
| 121 |
|
| 122 |
output_file = f"audio_{lang}.wav"
|
| 123 |
sf.write(output_file, full, 24000) # Save as WAV file
|
|
|
|
| 1 |
+
import spaces # Import spaces first to avoid CUDA initialization issues
|
| 2 |
+
import os
|
| 3 |
import gradio as gr
|
| 4 |
import trafilatura
|
| 5 |
from trafilatura import fetch_url, extract
|
|
|
|
| 17 |
nltk.download("punkt")
|
| 18 |
|
| 19 |
# Initialize KokoroTTS with default English
|
| 20 |
+
#kokoro_tts = KPipeline(lang_code='a') # 'a' is for American English
|
| 21 |
+
kokoro_tts = KPipeline(lang_code='a', device="cpu") # Load initially on CPU
|
| 22 |
|
| 23 |
# Supported TTS Languages
|
| 24 |
SUPPORTED_TTS_LANGUAGES = {
|
|
|
|
| 104 |
return "en" # Default to English if detection fails
|
| 105 |
|
| 106 |
### 4️⃣ TTS Functionality (KokoroTTS)
|
| 107 |
+
@spaces.GPU # Allocate GPU dynamically
|
| 108 |
def generate_audio_kokoro(text, lang):
|
| 109 |
"""Generate speech using KokoroTTS for supported languages."""
|
| 110 |
+
global kokoro_tts # Access the preloaded model
|
| 111 |
+
kokoro_tts.device = "cuda"
|
| 112 |
lang_code = SUPPORTED_TTS_LANGUAGES.get(lang, "a") # Default to English
|
| 113 |
generator = kokoro_tts(text, voice="af_bella", speed=1, split_pattern=r'\n+')
|
| 114 |
# 3. Specify Device
|
|
|
|
| 124 |
|
| 125 |
# Concatenate all audio data into a single array
|
| 126 |
full_audio = np.concatenate(audio_data_list)
|
| 127 |
+
# Move model back to CPU after processing
|
| 128 |
+
kokoro_tts.device = "cpu"
|
| 129 |
|
| 130 |
output_file = f"audio_{lang}.wav"
|
| 131 |
sf.write(output_file, full, 24000) # Save as WAV file
|