Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
"""
|
| 2 |
Répétiteur Vocal — POC (Bac tchadien, Maths série D, français)
|
| 3 |
Interface : réplique WhatsApp (le produit final vivra sur WhatsApp).
|
| 4 |
-
Pipeline : Vocale élève → ASR (faster-whisper) → LLM (Qwen2.5
|
| 5 |
→ TTS (MMS-TTS français) → Vocale réponse
|
| 6 |
|
| 7 |
-
100% modèles open-source
|
| 8 |
-
|
| 9 |
"""
|
| 10 |
|
| 11 |
import os
|
|
@@ -18,14 +18,15 @@ import numpy as np
|
|
| 18 |
import scipy.io.wavfile as wavfile
|
| 19 |
import torch
|
| 20 |
from faster_whisper import WhisperModel
|
| 21 |
-
from huggingface_hub import
|
|
|
|
| 22 |
from transformers import AutoTokenizer, VitsModel
|
| 23 |
|
| 24 |
# ---------------------------------------------------------------------------
|
| 25 |
# Configuration
|
| 26 |
# ---------------------------------------------------------------------------
|
| 27 |
-
|
| 28 |
-
|
| 29 |
ASR_MODEL_SIZE = "small"
|
| 30 |
TTS_MODEL = "facebook/mms-tts-fra"
|
| 31 |
|
|
@@ -57,7 +58,9 @@ tts_model = VitsModel.from_pretrained(TTS_MODEL)
|
|
| 57 |
tts_tokenizer = AutoTokenizer.from_pretrained(TTS_MODEL)
|
| 58 |
tts_model.eval()
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
print("Modèles prêts.")
|
| 62 |
|
| 63 |
# ---------------------------------------------------------------------------
|
|
@@ -74,10 +77,10 @@ def repondre(question: str, memoire: list) -> str:
|
|
| 74 |
messages.append({"role": "user", "content": q})
|
| 75 |
messages.append({"role": "assistant", "content": r})
|
| 76 |
messages.append({"role": "user", "content": question})
|
| 77 |
-
reponse =
|
| 78 |
-
messages=messages, max_tokens=
|
| 79 |
)
|
| 80 |
-
return reponse
|
| 81 |
|
| 82 |
|
| 83 |
def nettoyer_pour_tts(texte: str) -> str:
|
|
@@ -129,7 +132,7 @@ def traiter(audio_path, question_texte, chat, memoire):
|
|
| 129 |
explication = repondre(question, memoire)
|
| 130 |
except Exception as e:
|
| 131 |
chat.append({"role": "assistant",
|
| 132 |
-
"content": f"⚠️ Erreur LLM
|
| 133 |
return chat, memoire, None, ""
|
| 134 |
|
| 135 |
# Note vocale du répétiteur, puis transcription texte (comme sur WhatsApp)
|
|
|
|
| 1 |
"""
|
| 2 |
Répétiteur Vocal — POC (Bac tchadien, Maths série D, français)
|
| 3 |
Interface : réplique WhatsApp (le produit final vivra sur WhatsApp).
|
| 4 |
+
Pipeline : Vocale élève → ASR (faster-whisper) → LLM (Qwen2.5-1.5B local, GGUF)
|
| 5 |
→ TTS (MMS-TTS français) → Vocale réponse
|
| 6 |
|
| 7 |
+
100% modèles open-source, 100% local : AUCUN token ni secret requis.
|
| 8 |
+
Conçu pour un Space HF gratuit (CPU 2 vCPU, 16 Go RAM).
|
| 9 |
"""
|
| 10 |
|
| 11 |
import os
|
|
|
|
| 18 |
import scipy.io.wavfile as wavfile
|
| 19 |
import torch
|
| 20 |
from faster_whisper import WhisperModel
|
| 21 |
+
from huggingface_hub import hf_hub_download
|
| 22 |
+
from llama_cpp import Llama
|
| 23 |
from transformers import AutoTokenizer, VitsModel
|
| 24 |
|
| 25 |
# ---------------------------------------------------------------------------
|
| 26 |
# Configuration
|
| 27 |
# ---------------------------------------------------------------------------
|
| 28 |
+
LLM_REPO = "Qwen/Qwen2.5-1.5B-Instruct-GGUF"
|
| 29 |
+
LLM_FILE = "qwen2.5-1.5b-instruct-q4_k_m.gguf"
|
| 30 |
ASR_MODEL_SIZE = "small"
|
| 31 |
TTS_MODEL = "facebook/mms-tts-fra"
|
| 32 |
|
|
|
|
| 58 |
tts_tokenizer = AutoTokenizer.from_pretrained(TTS_MODEL)
|
| 59 |
tts_model.eval()
|
| 60 |
|
| 61 |
+
print("Chargement LLM local (Qwen2.5-1.5B GGUF, Q4)...")
|
| 62 |
+
llm_path = hf_hub_download(repo_id=LLM_REPO, filename=LLM_FILE)
|
| 63 |
+
llm = Llama(model_path=llm_path, n_ctx=2048, n_threads=2, verbose=False)
|
| 64 |
print("Modèles prêts.")
|
| 65 |
|
| 66 |
# ---------------------------------------------------------------------------
|
|
|
|
| 77 |
messages.append({"role": "user", "content": q})
|
| 78 |
messages.append({"role": "assistant", "content": r})
|
| 79 |
messages.append({"role": "user", "content": question})
|
| 80 |
+
reponse = llm.create_chat_completion(
|
| 81 |
+
messages=messages, max_tokens=250, temperature=0.4
|
| 82 |
)
|
| 83 |
+
return reponse["choices"][0]["message"]["content"].strip()
|
| 84 |
|
| 85 |
|
| 86 |
def nettoyer_pour_tts(texte: str) -> str:
|
|
|
|
| 132 |
explication = repondre(question, memoire)
|
| 133 |
except Exception as e:
|
| 134 |
chat.append({"role": "assistant",
|
| 135 |
+
"content": f"⚠️ Erreur LLM : {e}"})
|
| 136 |
return chat, memoire, None, ""
|
| 137 |
|
| 138 |
# Note vocale du répétiteur, puis transcription texte (comme sur WhatsApp)
|