Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,57 @@
|
|
| 1 |
-
"""
|
| 2 |
-
app.py - Hugging Face Space for Somali Multi-Speaker TTS
|
| 3 |
-
|
| 4 |
-
This script:
|
| 5 |
-
-
|
| 6 |
-
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
import
|
| 22 |
-
|
| 23 |
-
import
|
| 24 |
-
|
| 25 |
-
from
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
def tts(text: str, speaker: str):
|
| 38 |
-
""
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
description="Select Male or Female voice to synthesize Somali text.",
|
| 59 |
-
allow_flagging="never"
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
# 7. Launch the app
|
| 63 |
-
if __name__ == "__main__":
|
| 64 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
app.py - Hugging Face Space for Somali Multi-Speaker TTS
|
| 3 |
+
|
| 4 |
+
This script:
|
| 5 |
+
- Loads a fine-tuned multi-speaker VITS model from HF Hub: "Somali-tts/somali_tts_model"
|
| 6 |
+
- Runs a Gradio interface to synthesize Somali text with Male/Female voices
|
| 7 |
+
|
| 8 |
+
Setup requirements:
|
| 9 |
+
Create a `requirements.txt` alongside this file with:
|
| 10 |
+
gradio
|
| 11 |
+
numpy
|
| 12 |
+
soundfile
|
| 13 |
+
huggingface-hub
|
| 14 |
+
torch
|
| 15 |
+
git+https://github.com/jaywalnut310/vits.git
|
| 16 |
+
|
| 17 |
+
Push both `app.py` and `requirements.txt` to your Hugging Face Space (Gradio template).
|
| 18 |
+
"""
|
| 19 |
+
import gradio as gr
|
| 20 |
+
import numpy as np
|
| 21 |
+
import os
|
| 22 |
+
from huggingface_hub import snapshot_download
|
| 23 |
+
from vits.inference import Synthesizer
|
| 24 |
+
|
| 25 |
+
# Download the multi-speaker model from HF Hub
|
| 26 |
+
MODEL_REPO = "Somali-tts/somali_tts_model"
|
| 27 |
+
local_dir = snapshot_download(repo_id=MODEL_REPO)
|
| 28 |
+
CONFIG_FILE = os.path.join(local_dir, "config.json")
|
| 29 |
+
CHECKPOINT = os.path.join(local_dir, "checkpoint.pth")
|
| 30 |
+
|
| 31 |
+
# Initialize the VITS synthesizer
|
| 32 |
+
synthesizer = Synthesizer(CHECKPOINT, CONFIG_FILE)
|
| 33 |
+
|
| 34 |
+
# Text-to-speech function
|
| 35 |
+
# text: Somali text to synthesize
|
| 36 |
+
# speaker: "Male" or "Female"
|
| 37 |
+
def tts(text: str, speaker: str):
|
| 38 |
+
spk_id = 0 if speaker.lower().startswith("m") else 1
|
| 39 |
+
wav = synthesizer.tts(text, speaker_id=spk_id)
|
| 40 |
+
return wav, synthesizer.sample_rate
|
| 41 |
+
|
| 42 |
+
# Build Gradio interface
|
| 43 |
+
demo = gr.Interface(
|
| 44 |
+
fn=tts,
|
| 45 |
+
inputs=[
|
| 46 |
+
gr.Textbox(lines=3, label="Enter Somali text to synthesize", placeholder="Qor qoraalka halkan…"),
|
| 47 |
+
gr.Radio(choices=["Male", "Female"], label="Select speaker voice")
|
| 48 |
+
],
|
| 49 |
+
outputs=gr.Audio(type="numpy", label="Generated Speech"),
|
| 50 |
+
title="Somali Multi-Speaker TTS",
|
| 51 |
+
description="Select Male or Female voice to synthesize Somali text.",
|
| 52 |
+
allow_flagging="never"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Launch the app
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|