File size: 1,739 Bytes
0ce63df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import torch
import gradio as gr
import soundfile as sf
from transformers import AutoProcessor, VitsModel


HF_TOKEN = os.getenv("HF_TOKEN")
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

TTS_MODELS = {
    "yoruba": "facebook/mms-tts-yor",
    "hausa": "facebook/mms-tts-hau",
}


tts_engines = {}

for lang, model_id in TTS_MODELS.items():
    print(f"Loading TTS model for {lang}...")

    processor = AutoProcessor.from_pretrained(
        model_id,
        token=HF_TOKEN
    )

    model = VitsModel.from_pretrained(
        model_id,
        token=HF_TOKEN
    ).to(DEVICE)

    model.eval()

    tts_engines[lang] = {
        "processor": processor,
        "model": model
    }

print("All TTS models loaded successfully")


def synthesize_speech(text, language):
    if not text.strip():
        return None

    language = language.lower()
    if language not in tts_engines:
        return None

    processor = tts_engines[language]["processor"]
    model = tts_engines[language]["model"]

    inputs = processor(
        text=text,
        return_tensors="pt"
    ).to(DEVICE)

    with torch.no_grad():
        output = model(**inputs)

    audio = output.waveform.squeeze().cpu().numpy()

    output_path = "tts_output.wav"
    sf.write(output_path, audio, 16000)

    return output_path


demo = gr.Interface(
    fn=synthesize_speech,
    inputs=[
        gr.Textbox(label="Text"),
        gr.Dropdown(
            choices=["yoruba", "hausa"],
            label="Language"
        )
    ],
    outputs=gr.Audio(type="filepath", label="Generated Speech"),
    title="HealthAtlas Nigerian TTS Service",
    description="Text → Speech (Yoruba & Hausa)",
)


if __name__ == "__main__":
    demo.launch()