File size: 14,680 Bytes
a248e18
 
babb493
 
 
 
 
 
 
 
 
 
 
 
 
a248e18
 
e1a9f6f
babb493
 
e1a9f6f
a248e18
e1a9f6f
a248e18
e1a9f6f
a248e18
0a88016
e1a9f6f
0a88016
e1a9f6f
 
 
 
 
 
 
 
 
0a88016
babb493
 
 
e1a9f6f
f2cec3a
4d2bd73
 
a248e18
1452cfe
a248e18
 
 
 
babb493
e1a9f6f
 
babb493
 
 
 
 
 
 
e1a9f6f
a248e18
 
 
 
 
aeacff6
48a14e4
aeacff6
 
a248e18
 
 
 
 
 
 
 
e1a9f6f
a248e18
aeacff6
 
e1a9f6f
 
 
 
 
 
 
 
 
 
aeacff6
 
babb493
 
e1a9f6f
babb493
e1a9f6f
babb493
 
 
 
e1a9f6f
babb493
 
 
e1a9f6f
babb493
e1a9f6f
 
babb493
 
 
 
 
 
 
 
e1a9f6f
 
babb493
e1a9f6f
babb493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1a9f6f
 
 
 
 
 
 
babb493
 
e1a9f6f
 
babb493
a248e18
e1a9f6f
a248e18
e1a9f6f
 
 
 
aeacff6
 
 
a248e18
 
 
e1a9f6f
aeacff6
a248e18
 
 
 
 
 
e1a9f6f
 
 
 
 
a248e18
 
aeacff6
a248e18
e1a9f6f
a248e18
e1a9f6f
a248e18
 
 
 
 
aeacff6
a248e18
 
 
aeacff6
e1a9f6f
 
aeacff6
e1a9f6f
 
 
 
 
 
 
aeacff6
e1a9f6f
aeacff6
 
 
db07a3a
 
62b5323
 
 
 
e6292a4
e1a9f6f
 
e6292a4
e1a9f6f
 
db07a3a
e1a9f6f
 
 
 
 
 
 
 
 
 
6fefd54
e1a9f6f
 
a248e18
 
 
 
e1a9f6f
 
a248e18
e1a9f6f
 
 
 
a248e18
 
e1a9f6f
 
a248e18
 
aeacff6
e1a9f6f
 
a248e18
 
 
 
 
 
e1a9f6f
6fefd54
e6292a4
e1a9f6f
a248e18
 
 
e6292a4
a248e18
 
e1a9f6f
a248e18
 
 
aeacff6
a248e18
6fefd54
 
e1a9f6f
a248e18
 
 
e6292a4
a248e18
6fefd54
a248e18
aeacff6
 
6fefd54
aeacff6
 
e1a9f6f
a248e18
 
 
e1a9f6f
a248e18
 
 
 
 
 
 
 
 
 
e1a9f6f
a248e18
 
 
6fefd54
a248e18
 
 
 
 
 
6fefd54
a248e18
 
 
 
 
 
ab740c4
 
 
 
 
 
e1a9f6f
ab740c4
 
 
e1a9f6f
ab740c4
 
 
 
 
 
e1a9f6f
ab740c4
 
 
e1a9f6f
ab740c4
e1a9f6f
ab740c4
 
 
 
e1a9f6f
ab740c4
 
 
e1a9f6f
ab740c4
 
 
 
 
 
e1a9f6f
ab740c4
e1a9f6f
ab740c4
 
 
 
e1a9f6f
ab740c4
 
e1a9f6f
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# -*- coding: utf-8 -*-
"""
Salama Assistant โ€” fixed full app.py with PEFT adapter loading (base + adapter)

Drop this file into your Hugging Face Space (replace your existing app.py).

Requirements:
- transformers
- peft
- onnxruntime
- librosa
- huggingface_hub
- gradio

Note: install `peft` (e.g. add to requirements.txt: "peft>=0.4.0") or pip install in your environment.
"""

import os
import json
import tempfile
import threading
import numpy as np
import gradio as gr
import librosa
import torch
from scipy.io.wavfile import write as write_wav
from huggingface_hub import login
import onnxruntime

from transformers import (
    AutoProcessor,
    AutoModelForSpeechSeq2Seq,
    AutoTokenizer,
    AutoConfig,
    AutoModelForCausalLM,
    pipeline,
    TextIteratorStreamer,
)

# PEFT imports
from peft import PeftModel, PeftConfig

# -------------------- Configuration --------------------
STT_MODEL_ID = "EYEDOL/SALAMA_C3"
ADAPTER_REPO_ID = "EYEDOL/Llama-3.2-3b_ON_ALPACA5"  # adapter-only repo
BASE_MODEL_ID = "unsloth/Llama-3.2-3B-Instruct"    # full base model referenced by adapter
TTS_TOKENIZER_ID = "facebook/mms-tts-swh"
TTS_ONNX_MODEL_PATH = "swahili_tts.onnx"

TEMP_DIR = "temp"
os.makedirs(TEMP_DIR, exist_ok=True)

# Use HF token from env; Spaces normally provide HF_TOKEN
HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("hugface")
if not HF_TOKEN:
    print("Warning: HF_TOKEN not found in env. Public models may still load, but private repos require a token.")
else:
    try:
        login(token=HF_TOKEN)
        print("Successfully logged into Hugging Face Hub!")
    except Exception as e:
        print("Warning: huggingface_hub.login() failed:", e)


class WeeboAssistant:
    def __init__(self):
        self.STT_SAMPLE_RATE = 16000
        self.TTS_SAMPLE_RATE = 16000
        self.SYSTEM_PROMPT = (
            "Wewe ni msaidizi mwenye akili, jibu swali lililoulizwa KWA UFUPI na kwa usahihi kwa sauti ya mazungumzo. "
            "Jibu kwa lugha ya Kiswahili pekee. Hakuna jibu refu."
        )
        self._init_models()

    def _init_models(self):
        print("Initializing models...")
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        self.torch_dtype = torch.bfloat16 if self.device == "cuda" else torch.float32
        print(f"Using device: {self.device}")

        # ---------------- STT ----------------
        print(f"Loading STT model: {STT_MODEL_ID}")
        self.stt_processor = AutoProcessor.from_pretrained(STT_MODEL_ID)
        self.stt_model = AutoModelForSpeechSeq2Seq.from_pretrained(
            STT_MODEL_ID,
            torch_dtype=self.torch_dtype,
            low_cpu_mem_usage=True,
            use_safetensors=True,
        )
        if self.device == "cuda":
            try:
                self.stt_model = self.stt_model.to("cuda")
            except Exception:
                pass
        print("STT model loaded successfully.")

        # ---------------- LLM (base + PEFT adapter) ----------------
        print(f"Loading base LLM: {BASE_MODEL_ID} and applying adapter: {ADAPTER_REPO_ID}")

        # 1) Tokenizer: prefer base tokenizer
        try:
            self.llm_tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID, use_fast=True)
        except Exception as e:
            print("Warning: could not load base tokenizer, falling back to adapter tokenizer. Error:", e)
            self.llm_tokenizer = AutoTokenizer.from_pretrained(ADAPTER_REPO_ID, use_fast=True)

        # 2) Load base model
        device_map = "auto" if torch.cuda.is_available() else None
        try:
            self.llm_model = AutoModelForCausalLM.from_pretrained(
                BASE_MODEL_ID,
                torch_dtype=self.torch_dtype,
                low_cpu_mem_usage=True,
                device_map=device_map,
                trust_remote_code=True,
            )
        except Exception as e:
            # Helpful error info and hint
            raise RuntimeError(
                "Failed to load base model. Ensure the base model ID is correct and the HF_TOKEN has access if private. Error: "
                + str(e)
            )

        # 3) Load and apply PEFT adapter (adapter-only repo)
        try:
            # This discovers adapter config (adapter_config.json) and applies weights
            peft_config = PeftConfig.from_pretrained(ADAPTER_REPO_ID)
            self.llm_model = PeftModel.from_pretrained(
                self.llm_model,
                ADAPTER_REPO_ID,
                device_map=device_map,
                torch_dtype=self.torch_dtype,
                low_cpu_mem_usage=True,
            )
        except Exception as e:
            raise RuntimeError(
                "Failed to load/apply PEFT adapter from adapter repo. Make sure adapter files (adapter_config.json and adapter_model.safetensors) are present and HF_TOKEN has access if private. Error: "
                + str(e)
            )

        # 4) Optionally create a non-streaming pipeline for quick tests
        try:
            device_index = 0 if torch.cuda.is_available() else -1
            self.llm_pipeline = pipeline(
                "text-generation",
                model=self.llm_model,
                tokenizer=self.llm_tokenizer,
                device=device_index,
                model_kwargs={"torch_dtype": self.torch_dtype},
            )
        except Exception as e:
            print("Warning: could not create text-generation pipeline. Streaming generate will still work. Error:", e)
            self.llm_pipeline = None

        print("LLM base + adapter loaded successfully.")

        # ---------------- TTS ----------------
        print(f"Loading TTS model: {TTS_ONNX_MODEL_PATH}")
        providers = ["CPUExecutionProvider"]
        if torch.cuda.is_available():
            providers = ["CUDAExecutionProvider", "CPUExecutionProvider"]
        self.tts_session = onnxruntime.InferenceSession(TTS_ONNX_MODEL_PATH, providers=providers)
        self.tts_tokenizer = AutoTokenizer.from_pretrained(TTS_TOKENIZER_ID)
        print("TTS model and tokenizer loaded successfully.")

        print("-" * 30)
        print("All models initialized successfully! โœ…")

    # ---------------- Utility methods ----------------
    def transcribe_audio(self, audio_tuple):
        if audio_tuple is None:
            return ""
        sample_rate, audio_data = audio_tuple
        if audio_data.ndim > 1:
            audio_data = audio_data.mean(axis=1)
        if audio_data.dtype != np.float32:
            if np.issubdtype(audio_data.dtype, np.integer):
                max_val = np.iinfo(audio_data.dtype).max
                audio_data = audio_data.astype(np.float32) / float(max_val)
            else:
                audio_data = audio_data.astype(np.float32)
        if sample_rate != self.STT_SAMPLE_RATE:
            audio_data = librosa.resample(y=audio_data, orig_sr=sample_rate, target_sr=self.STT_SAMPLE_RATE)
        if len(audio_data) < 1000:
            return "(Audio too short to transcribe)"

        inputs = self.stt_processor(audio_data, sampling_rate=self.STT_SAMPLE_RATE, return_tensors="pt")
        inputs = {k: v.to(next(self.stt_model.parameters()).device) for k, v in inputs.items()}
        with torch.no_grad():
            generated_ids = self.stt_model.generate(**inputs, max_new_tokens=128)
        transcription = self.stt_processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
        return transcription.strip()

    def generate_speech(self, text):
        if not text:
            return None
        text = text.strip()
        inputs = self.tts_tokenizer(text, return_tensors="np")
        input_name = self.tts_session.get_inputs()[0].name
        ort_inputs = {input_name: inputs["input_ids"]}
        audio_waveform = self.tts_session.run(None, ort_inputs)[0].flatten()

        if np.issubdtype(audio_waveform.dtype, np.floating):
            audio_clip = np.clip(audio_waveform, -1.0, 1.0)
            audio_int16 = (audio_clip * 32767).astype(np.int16)
        else:
            audio_int16 = audio_waveform.astype(np.int16)

        output_path = os.path.join(TEMP_DIR, f"{os.urandom(8).hex()}.wav")
        write_wav(output_path, self.TTS_SAMPLE_RATE, audio_int16)
        return output_path

    def get_llm_response(self, chat_history):
        prompt_lines = [self.SYSTEM_PROMPT.strip(),
""  # Empty string or add intended string content here
]


                        
        for user_msg, assistant_msg in chat_history:
            if user_msg:
                prompt_lines.append("User: " + user_msg)
            if assistant_msg:
                prompt_lines.append("Assistant: " + assistant_msg)
        prompt_lines.append("Assistant: ")
        prompt = "".join(prompt_lines)

        inputs = self.llm_tokenizer(prompt, return_tensors="pt")
        try:
            model_device = next(self.llm_model.parameters()).device
        except StopIteration:
            model_device = torch.device("cpu")
        inputs = {k: v.to(model_device) for k, v in inputs.items()}

        streamer = TextIteratorStreamer(self.llm_tokenizer, skip_prompt=True, skip_special_tokens=True)

        generation_kwargs = dict(
            input_ids=inputs["input_ids"],
            attention_mask=inputs.get("attention_mask", None),
            max_new_tokens=512,
            do_sample=True,
            temperature=0.6,
            top_p=0.9,
            streamer=streamer,
            eos_token_id=getattr(self.llm_tokenizer, "eos_token_id", None),
        )

        gen_thread = threading.Thread(target=self.llm_model.generate, kwargs=generation_kwargs, daemon=True)
        gen_thread.start()

        return streamer


# -------------------- Create assistant instance --------------------
assistant = WeeboAssistant()


# -------------------- Gradio pipelines --------------------

def s2s_pipeline(audio_input, chat_history):
    user_text = assistant.transcribe_audio(audio_input)
    if not user_text or user_text.startswith("("):
        chat_history.append((user_text or "(No valid speech detected)", None))
        yield chat_history, None, "Please record your voice again."
        return

    chat_history.append((user_text, ""))
    yield chat_history, None, "..."

    response_stream = assistant.get_llm_response(chat_history)
    llm_response_text = ""
    for text_chunk in response_stream:
        llm_response_text += text_chunk
        chat_history[-1] = (user_text, llm_response_text)
        yield chat_history, None, llm_response_text

    final_audio_path = assistant.generate_speech(llm_response_text)
    yield chat_history, final_audio_path, llm_response_text


def t2t_pipeline(text_input, chat_history):
    chat_history.append((text_input, ""))
    yield chat_history

    response_stream = assistant.get_llm_response(chat_history)
    llm_response_text = ""
    for text_chunk in response_stream:
        llm_response_text += text_chunk
        chat_history[-1] = (text_input, llm_response_text)
        yield chat_history


def clear_textbox():
    return gr.Textbox(value="")


# -------------------- Gradio UI --------------------
with gr.Blocks(theme=gr.themes.Soft(), title="Msaidizi wa Kiswahili") as demo:
    gr.Markdown("# ๐Ÿค– Msaidizi wa Sauti wa Kiswahili (Swahili Voice Assistant)")
    gr.Markdown("Ongea na msaidizi kwa Kiswahili. Toa sauti, andika maandishi, na upate majibu kwa sauti au maandishi.")

    with gr.Tabs():
        with gr.TabItem("๐ŸŽ™๏ธ Sauti-kwa-Sauti (Speech-to-Speech)"):
            with gr.Row():
                with gr.Column(scale=2):
                    s2s_audio_in = gr.Audio(sources=["microphone"], type="numpy", label="Ongea Hapa (Speak Here)")
                    s2s_submit_btn = gr.Button("Tuma (Submit)", variant="primary")
                with gr.Column(scale=3):
                    s2s_chatbot = gr.Chatbot(label="Mazungumzo (Conversation)", bubble_full_width=False, height=400)
                    s2s_audio_out = gr.Audio(type="filepath", label="Jibu la Sauti (Audio Response)", autoplay=True)
                    s2s_text_out = gr.Textbox(label="Jibu la Maandishi (Text Response)", interactive=False)

        with gr.TabItem("โŒจ๏ธ Maandishi-kwa-Maandishi (Text-to-Text)"):
            t2t_chatbot = gr.Chatbot(label="Mazungumzo (Conversation)", bubble_full_width=False, height=500)
            with gr.Row():
                t2t_text_in = gr.Textbox(show_label=False, placeholder="Habari yako...", scale=4, container=False)
                t2t_submit_btn = gr.Button("Tuma (Submit)", variant="primary", scale=1)

        with gr.TabItem("๐Ÿ› ๏ธ Zana (Tools)"):
            with gr.Row():
                with gr.Column():
                    gr.Markdown("### Unukuzi wa Sauti (Speech Transcription)")
                    tool_s2t_audio_in = gr.Audio(sources=["microphone", "upload"], type="numpy", label="Sauti ya Kuingiza (Input Audio)")
                    tool_s2t_text_out = gr.Textbox(label="Maandishi Yaliyonukuliwa (Transcribed Text)", interactive=False)
                    tool_s2t_btn = gr.Button("Nukuu (Transcribe)")
                with gr.Column():
                    gr.Markdown("### Utengenezaji wa Sauti (Speech Synthesis)")
                    tool_t2s_text_in = gr.Textbox(label="Maandishi ya Kuingiza (Input Text)", placeholder="Andika Kiswahili hapa...")
                    tool_t2s_audio_out = gr.Audio(type="filepath", label="Sauti Iliyotengenezwa (Synthesized Audio)", autoplay=False)
                    tool_t2s_btn = gr.Button("Tengeneza Sauti (Synthesize)")

    s2s_submit_btn.click(
        fn=s2s_pipeline,
        inputs=[s2s_audio_in, s2s_chatbot],
        outputs=[s2s_chatbot, s2s_audio_out, s2s_text_out],
        queue=True,
    ).then(
        fn=lambda: gr.Audio(value=None),
        inputs=None,
        outputs=s2s_audio_in,
    )

    t2t_submit_btn.click(
        fn=t2t_pipeline,
        inputs=[t2t_text_in, t2t_chatbot],
        outputs=[t2t_chatbot],
        queue=True,
    ).then(
        fn=clear_textbox,
        inputs=None,
        outputs=t2t_text_in,
    )

    t2t_text_in.submit(
        fn=t2t_pipeline,
        inputs=[t2t_text_in, t2t_chatbot],
        outputs=[t2t_chatbot],
        queue=True,
    ).then(
        fn=clear_textbox,
        inputs=None,
        outputs=t2t_text_in,
    )

    tool_s2t_btn.click(
        fn=assistant.transcribe_audio,
        inputs=tool_s2t_audio_in,
        outputs=tool_s2t_text_out,
        queue=True,
    )

    tool_t2s_btn.click(
        fn=assistant.generate_speech,
        inputs=tool_t2s_text_in,
        outputs=tool_t2s_audio_out,
        queue=True,
    )

demo.queue().launch(debug=True)