Spaces:
Running
Running
Use models/model.gguf and configure GGUF model via env vars
Browse files- app.py +17 -8
- src/rag_core.py +2 -3
app.py
CHANGED
|
@@ -9,7 +9,7 @@ import sys
|
|
| 9 |
import traceback
|
| 10 |
import gradio as gr
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
-
import
|
| 13 |
from pathlib import Path
|
| 14 |
|
| 15 |
def ensure_faiss_index_present():
|
|
@@ -44,22 +44,31 @@ ensure_faiss_index_present()
|
|
| 44 |
|
| 45 |
def ensure_model_present():
|
| 46 |
os.makedirs("models", exist_ok=True)
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
if os.path.exists(local_path):
|
| 49 |
return
|
| 50 |
|
|
|
|
| 51 |
repo_id = os.environ.get("MODEL_REPO_ID")
|
| 52 |
-
filename
|
|
|
|
| 53 |
|
| 54 |
if not repo_id:
|
| 55 |
raise RuntimeError(
|
| 56 |
-
"Modèle GGUF absent (models/
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
)
|
| 58 |
|
| 59 |
-
downloaded = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 60 |
-
|
| 61 |
shutil.copyfile(downloaded, local_path)
|
| 62 |
|
|
|
|
| 63 |
ensure_model_present()
|
| 64 |
|
| 65 |
|
|
@@ -201,9 +210,9 @@ Conseil : pour une citation exacte, demande “Donne l’intégralité de l’ar
|
|
| 201 |
gr.Markdown(
|
| 202 |
"""
|
| 203 |
> **Information importante**
|
| 204 |
-
> Lors du premier lancement, l’application peut nécessiter
|
| 205 |
> Ensuite, l’utilisation est immédiate.
|
| 206 |
-
> En cas d’utilisation simultanée, les demandes sont traitées
|
| 207 |
""".strip()
|
| 208 |
)
|
| 209 |
|
|
|
|
| 9 |
import traceback
|
| 10 |
import gradio as gr
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
+
import shutil
|
| 13 |
from pathlib import Path
|
| 14 |
|
| 15 |
def ensure_faiss_index_present():
|
|
|
|
| 44 |
|
| 45 |
def ensure_model_present():
|
| 46 |
os.makedirs("models", exist_ok=True)
|
| 47 |
+
|
| 48 |
+
# Nouveau nom stable, cohérent avec rag_core.py
|
| 49 |
+
local_path = os.path.join("models", "model.gguf")
|
| 50 |
if os.path.exists(local_path):
|
| 51 |
return
|
| 52 |
|
| 53 |
+
# Repo HF contenant le GGUF
|
| 54 |
repo_id = os.environ.get("MODEL_REPO_ID")
|
| 55 |
+
# IMPORTANT: mets ici le VRAI filename du GGUF dans le repo HF
|
| 56 |
+
filename = os.environ.get("MODEL_FILENAME")
|
| 57 |
|
| 58 |
if not repo_id:
|
| 59 |
raise RuntimeError(
|
| 60 |
+
"Modèle GGUF absent (models/model.gguf) et variable MODEL_REPO_ID non définie."
|
| 61 |
+
)
|
| 62 |
+
if not filename:
|
| 63 |
+
raise RuntimeError(
|
| 64 |
+
"Variable MODEL_FILENAME non définie (ex: Qwen2.5-1.5B-Instruct-Q4_K_M.gguf)."
|
| 65 |
)
|
| 66 |
|
| 67 |
+
downloaded = hf_hub_download(repo_id=repo_id, filename=filename, repo_type="model")
|
| 68 |
+
|
| 69 |
shutil.copyfile(downloaded, local_path)
|
| 70 |
|
| 71 |
+
|
| 72 |
ensure_model_present()
|
| 73 |
|
| 74 |
|
|
|
|
| 210 |
gr.Markdown(
|
| 211 |
"""
|
| 212 |
> **Information importante**
|
| 213 |
+
> Lors du premier lancement, l’application peut nécessiter 1 à 2 minutes d’initialisation.
|
| 214 |
> Ensuite, l’utilisation est immédiate.
|
| 215 |
+
> En cas d’utilisation simultanée, les demandes sont traitées successivement afin de garantir la fiabilité des réponses.
|
| 216 |
""".strip()
|
| 217 |
)
|
| 218 |
|
src/rag_core.py
CHANGED
|
@@ -16,7 +16,7 @@ ROUTAGE AUTO :
|
|
| 16 |
Prérequis :
|
| 17 |
- data/chunks_articles.jsonl (article-level)
|
| 18 |
- db/faiss_code_edu_by_article (FAISS)
|
| 19 |
-
- models/
|
| 20 |
"""
|
| 21 |
|
| 22 |
import json
|
|
@@ -34,7 +34,6 @@ CHUNKS_PATH = Path("data/chunks_articles.jsonl")
|
|
| 34 |
DB_DIR = Path("db/faiss_code_edu_by_article")
|
| 35 |
|
| 36 |
EMBED_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
| 37 |
-
MODEL_NAME = "mistral:latest"
|
| 38 |
|
| 39 |
TOP_K_FETCH = 30 # nb de docs candidats récupérés
|
| 40 |
TOP_K_FINAL = 4 # nb max envoyés au LLM
|
|
@@ -70,7 +69,7 @@ ARTICLES_CITES_RE = re.compile(r"Articles cités\s*:\s*(.*)$", flags=re.IGNORECA
|
|
| 70 |
|
| 71 |
# -------------------- LLM INIT (FIDÈLE) --------------------
|
| 72 |
llm = Llama(
|
| 73 |
-
model_path="models/
|
| 74 |
n_ctx=2048,
|
| 75 |
n_threads=10,
|
| 76 |
n_batch=128,
|
|
|
|
| 16 |
Prérequis :
|
| 17 |
- data/chunks_articles.jsonl (article-level)
|
| 18 |
- db/faiss_code_edu_by_article (FAISS)
|
| 19 |
+
- models/model.gguf (GGUF)
|
| 20 |
"""
|
| 21 |
|
| 22 |
import json
|
|
|
|
| 34 |
DB_DIR = Path("db/faiss_code_edu_by_article")
|
| 35 |
|
| 36 |
EMBED_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
|
|
|
| 37 |
|
| 38 |
TOP_K_FETCH = 30 # nb de docs candidats récupérés
|
| 39 |
TOP_K_FINAL = 4 # nb max envoyés au LLM
|
|
|
|
| 69 |
|
| 70 |
# -------------------- LLM INIT (FIDÈLE) --------------------
|
| 71 |
llm = Llama(
|
| 72 |
+
model_path="models/model.gguf",
|
| 73 |
n_ctx=2048,
|
| 74 |
n_threads=10,
|
| 75 |
n_batch=128,
|