File size: 988 Bytes
1b81f3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import gradio as gr
from ttsmms import download, TTS
from langdetect import detect

# Download and load TTS models for Swahili and English
swahili_dir = download("swh", "./data/swahili")
english_dir = download("eng", "./data/english")  # Ensure an English TTS model is available

swahili_tts = TTS(swahili_dir)
english_tts = TTS(english_dir)

# Function to detect language and generate speech
def text_to_speech(text):
    lang = detect(text)  # Detect language
    wav_path = "./output.wav"

    if lang == "sw":  # Swahili
        swahili_tts.synthesis(text, wav_path=wav_path)
    else:  # Default to English if not Swahili
        english_tts.synthesis(text, wav_path=wav_path)

    return wav_path

# Gradio UI
gr.Interface(
    fn=text_to_speech,
    inputs=gr.Textbox(label="Enter Text"),
    outputs=gr.Audio(label="Generated Speech"),
    title="Swahili & English Text-to-Speech",
    description="Type text and listen to the generated speech in Swahili or English.",
).launch()