File size: 9,179 Bytes
e352304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dee8fe1
 
 
 
 
 
 
 
 
 
 
 
 
 
e352304
 
 
 
 
 
dee8fe1
 
e352304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5da3b02
e352304
 
 
 
 
5da3b02
 
 
 
 
 
 
 
 
 
e352304
dee8fe1
 
e352304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5da3b02
 
e352304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56ecd48
 
e352304
56ecd48
e352304
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import os
import sys
from pathlib import Path
import torch
import soundfile as sf
import tempfile
import gradio as gr

# Ensure local imports and CosyVoice path are configured
ROOT_DIR = Path(__file__).parent.resolve()
sys.path.insert(0, str(ROOT_DIR))

# Ensure CosyVoice repo is accessible
COSYVOICE_REPO = os.environ.get("COSYVOICE_REPO", "./CosyVoice")
if os.path.exists(COSYVOICE_REPO):
    sys.path.insert(0, COSYVOICE_REPO)
    matcha_path = Path(COSYVOICE_REPO) / "third_party" / "Matcha-TTS"
    if matcha_path.exists():
        sys.path.insert(0, str(matcha_path))

# Sorani text normalizer fallback logic
try:
    from sorani.frontend import normalize_sorani_text
except ImportError:
    import unicodedata
    import re

    try:
        from sorani.censor import censor_text, verify_wordlist
    except ImportError:
        from sorani.censor import CensorIntegrityError

        def censor_text(text):
            raise CensorIntegrityError(
                "Sorani censorship module (sorani/censor.py) is missing. "
                "RegaLabs-TTS refuses to synthesize without it."
            )

        def verify_wordlist():
            raise CensorIntegrityError("Sorani censorship module is missing.")

    _CHARACTER_MAP = str.maketrans({"ك": "ک", "ي": "ی", "ى": "ی", "ة": "ە"})
    def normalize_sorani_text(text: str) -> str:
        if not text:
            return ""
        text = unicodedata.normalize("NFKC", text).translate(_CHARACTER_MAP)
        text = re.sub(r"[\u064B-\u065F]", "", text)
        text = censor_text(text)
        verify_wordlist()
        return re.sub(r"\s+", " ", text).strip()

# Global model instance
COSYVOICE_MODEL = None

def get_model():
    global COSYVOICE_MODEL
    if COSYVOICE_MODEL is None:
        try:
            from cosyvoice.cli.cosyvoice import CosyVoice3
        except ImportError:
            raise RuntimeError(
                "CosyVoice runtime not found. Please ensure CosyVoice is cloned: "
                "git clone --recursive https://github.com/FunAudioLLM/CosyVoice.git"
            )
        
        base_model = "FunAudioLLM/Fun-CosyVoice3-0.5B-2512"
        flow_ckpt = ROOT_DIR / "cosyvoice3_sorani_flow_best_step2300.pt"
        adapter_ckpt = ROOT_DIR / "cosyvoice3_sorani_lora_refined_best.pt"
        
        print(f"Loading base CosyVoice model: {base_model}...")
        device_fp16 = torch.cuda.is_available()
        cosyvoice = CosyVoice3(base_model, fp16=device_fp16)
        
        if adapter_ckpt.exists():
            from cosyvoice.utils.lora import inject_lora, load_lora_state_dict
            llm = cosyvoice.model.llm
            target_count = inject_lora(llm, rank=16, alpha=32.0, dropout=0.05)
            load_lora_state_dict(
                llm, torch.load(adapter_ckpt, map_location="cpu", weights_only=False)
            )
            llm.to(cosyvoice.model.device).eval()
            print(f"Loaded RegaLabs-TTS Sorani LLM adapter ({target_count} projections).")
        
        if flow_ckpt.exists():
            from sorani.censor import verify_checkpoint
            verify_checkpoint(flow_ckpt)
            print(f"Loading RegaLabs-TTS Sorani flow weights: {flow_ckpt}...")
            flow_state = torch.load(flow_ckpt, map_location="cpu", weights_only=False)
            if isinstance(flow_state, dict):
                for key in ("model", "state_dict", "flow"):
                    nested = flow_state.get(key)
                    if isinstance(nested, dict):
                        flow_state = nested
                        break
                flow_state = {k: v for k, v in flow_state.items() if isinstance(k, str) and isinstance(v, torch.Tensor)}
            
            cosyvoice.model.flow.load_state_dict(flow_state, strict=False)
            cosyvoice.model.flow.to(cosyvoice.model.device).eval()
            print("Successfully loaded RegaLabs-TTS Sorani flow model.")
            
        COSYVOICE_MODEL = cosyvoice
        
    return COSYVOICE_MODEL

def synthesize_sorani(text, prompt_wav, prompt_text, speed, do_normalize):
    if not text or not text.strip():
        raise gr.Error("Please enter Sorani Kurdish text to synthesize.")
    if not prompt_wav:
        raise gr.Error("Please upload or select a reference prompt audio WAV.")
    if not prompt_text or not prompt_text.strip():
        raise gr.Error("Please provide the transcript text for the reference prompt audio.")

    # Text Normalization
    clean_text = normalize_sorani_text(text) if do_normalize else text.strip()
    clean_prompt_text = normalize_sorani_text(prompt_text) if do_normalize else prompt_text.strip()

    model = get_model()

    pieces = []
    with torch.inference_mode():
        for output in model.inference_zero_shot(
            clean_text,
            clean_prompt_text,
            prompt_wav,
            stream=False,
            speed=speed,
            text_frontend=False,
        ):
            pieces.append(output["tts_speech"].detach().cpu())

    if not pieces:
        raise RuntimeError("No audio was returned from the TTS model.")

    speech = torch.cat(pieces, dim=1).squeeze(0).numpy()
    
    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
        sf.write(tmp_file.name, speech, model.sample_rate, subtype="PCM_16")
        out_wav_path = tmp_file.name

    return out_wav_path, f"✅ Synthesis complete! Normalized Text: '{clean_text}'"

# Default sample files
sample_wav = str(ROOT_DIR / "samples" / "aran_en021.wav") if (ROOT_DIR / "samples" / "aran_en021.wav").exists() else None

# Custom CSS
custom_css = """
.container { max-width: 900px; margin: auto; }
.header { text-align: center; margin-bottom: 20px; }
.attribution { background-color: #1a1a24; border-radius: 8px; padding: 15px; margin-top: 25px; border-left: 4px solid #6366f1; }
"""

with gr.Blocks(title="RegaLabs-TTS: Sorani Speech Synthesis", css=custom_css) as demo:
    gr.Markdown(
        """
        # 🎙️ RegaLabs-TTS: Central Kurdish (Sorani) Speech Synthesis
        **CosyVoice 3 Zero-Shot TTS & Voice Cloning Adaptation for Sorani Kurdish (سۆرانی)**
        Developed by **[RegaLabs](https://huggingface.co/RegaLabs)**
        """,
        elem_classes=["header"]
    )
    
    with gr.Row():
        with gr.Column(scale=1):
            text_input = gr.Textbox(
                label="Sorani Text (دەقی سۆرانی)",
                placeholder="سڵاو، بەخێربێن بۆ تاقیکردنەوەی دەنگی RegaLabs-TTS...",
                value="سڵاو، بەخێربێن بۆ تاقیکردنەوەی دەنگی RegaLabs-TTS",
                lines=4,
            )
            
            prompt_audio = gr.Audio(
                label="Reference Voice Audio (دەنگی نموونە)",
                type="filepath",
                value=sample_wav,
            )
            
            prompt_text_input = gr.Textbox(
                label="Reference Voice Transcript (دەقی دەنگی نموونەکە)",
                placeholder="دەنگێکی لەسەرخۆ، هێمن و پڕ لە بڕوابەخۆبوون.",
                value="دەنگێکی لەسەرخۆ، هێمن و پڕ لە بڕوابەخۆبوون.",
                lines=2,
            )
            
            with gr.Accordion("Advanced Options (ڕێکخستنەکان)", open=False):
                speed_slider = gr.Slider(
                    minimum=0.5, maximum=1.5, value=1.0, step=0.05, label="Speech Speed (خێرایی خوێندنەوە)"
                )
                normalize_check = gr.Checkbox(
                    value=True, label="Enable Sorani Text Normalization (ڕێکخستنی دەق)"
                )
                
            generate_btn = gr.Button("🎤 Synthesize Sorani Speech (دروستکردنی دەنگ)", variant="primary", size="lg")
            
        with gr.Column(scale=1):
            audio_output = gr.Audio(label="Synthesized Sorani Speech (دەنگی دروستکراو)", type="filepath")
            status_output = gr.Textbox(label="Status & Info", interactive=False)

    generate_btn.click(
        fn=synthesize_sorani,
        inputs=[text_input, prompt_audio, prompt_text_input, speed_slider, normalize_check],
        outputs=[audio_output, status_output],
    )
    
    gr.Markdown(
        """
        ---
        ### 📜 License & Mandatory Attribution
        * **Model Checkpoint & Codebase:** **Apache 2.0** by **RegaLabs**. Commercial and non-commercial use is **fully allowed** with mandatory credit to RegaLabs.
        * **Stock Prompt Voices & Audio Samples:** **Non-Commercial Use Only**. Pre-packaged stock voice samples are strictly restricted from commercial use/cloning.
        
        **📌 Mandatory Credit:** Any public use, generated media (videos, podcasts, voiceovers, radio/TV), applications, or derivative works MUST explicitly credit **RegaLabs**:
        > *"Voice synthesized using RegaLabs-TTS by RegaLabs"* (or *"Audio powered by RegaLabs"*).
        """,
        elem_classes=["attribution"]
    )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)