Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from ttsmms import download, TTS
|
3 |
+
from langdetect import detect
|
4 |
+
|
5 |
+
# Download and load TTS models for Swahili and English
|
6 |
+
swahili_dir = download("swh", "./data/swahili")
|
7 |
+
english_dir = download("eng", "./data/english") # Ensure an English TTS model is available
|
8 |
+
|
9 |
+
swahili_tts = TTS(swahili_dir)
|
10 |
+
english_tts = TTS(english_dir)
|
11 |
+
|
12 |
+
# Function to detect language and generate speech
|
13 |
+
def text_to_speech(text):
|
14 |
+
lang = detect(text) # Detect language
|
15 |
+
wav_path = "./output.wav"
|
16 |
+
|
17 |
+
if lang == "sw": # Swahili
|
18 |
+
swahili_tts.synthesis(text, wav_path=wav_path)
|
19 |
+
else: # Default to English if not Swahili
|
20 |
+
english_tts.synthesis(text, wav_path=wav_path)
|
21 |
+
|
22 |
+
return wav_path
|
23 |
+
|
24 |
+
# Gradio UI
|
25 |
+
gr.Interface(
|
26 |
+
fn=text_to_speech,
|
27 |
+
inputs=gr.Textbox(label="Enter Text"),
|
28 |
+
outputs=gr.Audio(label="Generated Speech"),
|
29 |
+
title="Swahili & English Text-to-Speech",
|
30 |
+
description="Type text and listen to the generated speech in Swahili or English.",
|
31 |
+
).launch()
|