Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ import threading
|
|
| 18 |
|
| 19 |
# --- Login to Hugging Face using secret ---
|
| 20 |
# Make sure HF_TOKEN is set in your Hugging Face Space > Settings > Repository secrets
|
| 21 |
-
hf_token = os.environ.get("hugface")
|
| 22 |
if not hf_token:
|
| 23 |
raise ValueError("HF_TOKEN not found. Please set it in Hugging Face Space repository secrets.")
|
| 24 |
login(token=hf_token)
|
|
@@ -26,7 +26,7 @@ print("Successfully logged into Hugging Face Hub!")
|
|
| 26 |
|
| 27 |
# --- Configuration ---
|
| 28 |
STT_MODEL_ID = "EYEDOL/SALAMA_C3"
|
| 29 |
-
LLM_MODEL_ID = "
|
| 30 |
TTS_TOKENIZER_ID = "facebook/mms-tts-swh"
|
| 31 |
TTS_ONNX_MODEL_PATH = "swahili_tts.onnx"
|
| 32 |
|
|
@@ -116,20 +116,12 @@ class WeeboAssistant:
|
|
| 116 |
return output_path
|
| 117 |
|
| 118 |
def get_llm_response(self, chat_history):
|
| 119 |
-
# <--
|
| 120 |
-
messages = []
|
| 121 |
for user_msg, assistant_msg in chat_history:
|
| 122 |
-
# Add the user's message
|
| 123 |
messages.append({"role": "user", "content": user_msg})
|
| 124 |
-
# Add the assistant's message if it exists
|
| 125 |
if assistant_msg:
|
| 126 |
messages.append({"role": "assistant", "content": assistant_msg})
|
| 127 |
-
|
| 128 |
-
# Prepend the system prompt to the content of the very first user message.
|
| 129 |
-
# This is the correct way to use a system prompt with Gemma models.
|
| 130 |
-
if messages:
|
| 131 |
-
messages[0]["content"] = f"{self.SYSTEM_PROMPT}\n\n{messages[0]['content']}"
|
| 132 |
-
# <-- END OF FIX -->
|
| 133 |
|
| 134 |
prompt = self.llm_pipeline.tokenizer.apply_chat_template(
|
| 135 |
messages, tokenize=False, add_generation_prompt=True
|
|
@@ -229,52 +221,4 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Msaidizi wa Kiswahili") as demo:
|
|
| 229 |
gr.Markdown("### Utengenezaji wa Sauti (Speech Synthesis)")
|
| 230 |
tool_t2s_text_in = gr.Textbox(label="Maandishi ya Kuingiza (Input Text)", placeholder="Andika Kiswahili hapa...")
|
| 231 |
tool_t2s_audio_out = gr.Audio(type="filepath", label="Sauti Iliyotengenezwa (Synthesized Audio)", autoplay=False)
|
| 232 |
-
tool_t2s_btn = gr.Button("Tengeneza Sauti
|
| 233 |
-
|
| 234 |
-
s2s_submit_btn.click(
|
| 235 |
-
fn=s2s_pipeline,
|
| 236 |
-
inputs=[s2s_audio_in, s2s_chatbot],
|
| 237 |
-
outputs=[s2s_chatbot, s2s_audio_out, s2s_text_out],
|
| 238 |
-
queue=True
|
| 239 |
-
).then(
|
| 240 |
-
fn=lambda: gr.Audio(value=None),
|
| 241 |
-
inputs=None,
|
| 242 |
-
outputs=s2s_audio_in
|
| 243 |
-
)
|
| 244 |
-
|
| 245 |
-
t2t_submit_btn.click(
|
| 246 |
-
fn=t2t_pipeline,
|
| 247 |
-
inputs=[t2t_text_in, t2t_chatbot],
|
| 248 |
-
outputs=[t2t_chatbot],
|
| 249 |
-
queue=True
|
| 250 |
-
).then(
|
| 251 |
-
fn=clear_textbox,
|
| 252 |
-
inputs=None,
|
| 253 |
-
outputs=t2t_text_in
|
| 254 |
-
)
|
| 255 |
-
|
| 256 |
-
t2t_text_in.submit(
|
| 257 |
-
fn=t2t_pipeline,
|
| 258 |
-
inputs=[t2t_text_in, t2t_chatbot],
|
| 259 |
-
outputs=[t2t_chatbot],
|
| 260 |
-
queue=True
|
| 261 |
-
).then(
|
| 262 |
-
fn=clear_textbox,
|
| 263 |
-
inputs=None,
|
| 264 |
-
outputs=t2t_text_in
|
| 265 |
-
)
|
| 266 |
-
|
| 267 |
-
tool_s2t_btn.click(
|
| 268 |
-
fn=assistant.transcribe_audio,
|
| 269 |
-
inputs=tool_s2t_audio_in,
|
| 270 |
-
outputs=tool_s2t_text_out,
|
| 271 |
-
queue=True
|
| 272 |
-
)
|
| 273 |
-
tool_t2s_btn.click(
|
| 274 |
-
fn=assistant.generate_speech,
|
| 275 |
-
inputs=tool_t2s_text_in,
|
| 276 |
-
outputs=tool_t2s_audio_out,
|
| 277 |
-
queue=True
|
| 278 |
-
)
|
| 279 |
-
|
| 280 |
-
demo.queue().launch(debug=True)
|
|
|
|
| 18 |
|
| 19 |
# --- Login to Hugging Face using secret ---
|
| 20 |
# Make sure HF_TOKEN is set in your Hugging Face Space > Settings > Repository secrets
|
| 21 |
+
hf_token = os.environ.get("hugface")
|
| 22 |
if not hf_token:
|
| 23 |
raise ValueError("HF_TOKEN not found. Please set it in Hugging Face Space repository secrets.")
|
| 24 |
login(token=hf_token)
|
|
|
|
| 26 |
|
| 27 |
# --- Configuration ---
|
| 28 |
STT_MODEL_ID = "EYEDOL/SALAMA_C3"
|
| 29 |
+
LLM_MODEL_ID = "meta-llama/Llama-3.2-1B-Instruct" # <-- FIX: Switched to Llama-3.2
|
| 30 |
TTS_TOKENIZER_ID = "facebook/mms-tts-swh"
|
| 31 |
TTS_ONNX_MODEL_PATH = "swahili_tts.onnx"
|
| 32 |
|
|
|
|
| 116 |
return output_path
|
| 117 |
|
| 118 |
def get_llm_response(self, chat_history):
|
| 119 |
+
# <-- FIX: Reverted to using a 'system' role, which is correct for Llama 3 -->
|
| 120 |
+
messages = [{'role': 'system', 'content': self.SYSTEM_PROMPT}]
|
| 121 |
for user_msg, assistant_msg in chat_history:
|
|
|
|
| 122 |
messages.append({"role": "user", "content": user_msg})
|
|
|
|
| 123 |
if assistant_msg:
|
| 124 |
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
prompt = self.llm_pipeline.tokenizer.apply_chat_template(
|
| 127 |
messages, tokenize=False, add_generation_prompt=True
|
|
|
|
| 221 |
gr.Markdown("### Utengenezaji wa Sauti (Speech Synthesis)")
|
| 222 |
tool_t2s_text_in = gr.Textbox(label="Maandishi ya Kuingiza (Input Text)", placeholder="Andika Kiswahili hapa...")
|
| 223 |
tool_t2s_audio_out = gr.Audio(type="filepath", label="Sauti Iliyotengenezwa (Synthesized Audio)", autoplay=False)
|
| 224 |
+
tool_t2s_btn = gr.Button("Tengeneza Sauti
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|