Update app.py
Browse files
app.py
CHANGED
|
@@ -7,16 +7,14 @@ from sklearn.feature_extraction.text import TfidfVectorizer
|
|
| 7 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 8 |
import numpy as np
|
| 9 |
|
| 10 |
-
# === CONFIGURATION ===
|
| 11 |
MODEL_NAME = "google/gemma-2b-it"
|
| 12 |
INFO_FILE = "infos_medicaux.txt"
|
| 13 |
MAX_TOKENS = 600
|
| 14 |
-
TEMPERATURE = 0.
|
| 15 |
-
CHUNK_SIZE = 1000
|
| 16 |
-
TOP_K_CHUNKS =
|
| 17 |
|
| 18 |
-
|
| 19 |
-
print("⏳ Chargement du modèle...")
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 21 |
model = AutoModelForCausalLM.from_pretrained(
|
| 22 |
MODEL_NAME,
|
|
@@ -25,9 +23,8 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 25 |
low_cpu_mem_usage=True
|
| 26 |
)
|
| 27 |
model.eval()
|
| 28 |
-
print("
|
| 29 |
|
| 30 |
-
# === CHARGEMENT DU CONTEXTE MÉDICAL ET DÉCOUPAGE EN CHUNKS ===
|
| 31 |
medical_context_chunks = []
|
| 32 |
if os.path.exists(INFO_FILE):
|
| 33 |
with open(INFO_FILE, "r", encoding="utf-8") as f:
|
|
@@ -35,9 +32,8 @@ if os.path.exists(INFO_FILE):
|
|
| 35 |
medical_context_chunks = [medical_context[i:i+CHUNK_SIZE] for i in range(0, len(medical_context), CHUNK_SIZE)]
|
| 36 |
print(f"📘 Contexte médical chargé ({len(medical_context)} caractères, {len(medical_context_chunks)} chunks)")
|
| 37 |
else:
|
| 38 |
-
print("
|
| 39 |
|
| 40 |
-
# === TF-IDF pour recherche de chunks pertinents ===
|
| 41 |
if medical_context_chunks:
|
| 42 |
vectorizer = TfidfVectorizer().fit(medical_context_chunks)
|
| 43 |
chunk_vectors = vectorizer.transform(medical_context_chunks)
|
|
@@ -53,7 +49,6 @@ def get_relevant_chunks(question, top_k=TOP_K_CHUNKS):
|
|
| 53 |
top_indices = np.argsort(similarities)[::-1][:top_k]
|
| 54 |
return [medical_context_chunks[i] for i in top_indices]
|
| 55 |
|
| 56 |
-
# === FONCTION DE CHAT OPTIMISÉE ===
|
| 57 |
@spaces.GPU()
|
| 58 |
def chat_with_finanfa(message, history=None):
|
| 59 |
if history is None:
|
|
@@ -67,7 +62,6 @@ def chat_with_finanfa(message, history=None):
|
|
| 67 |
"Donne des réponses claires, détaillées et adaptées au Bénin."
|
| 68 |
)
|
| 69 |
|
| 70 |
-
# Récupération des chunks pertinents
|
| 71 |
relevant_chunks = get_relevant_chunks(message)
|
| 72 |
|
| 73 |
conversation = f"Système : {system_prompt}\n"
|
|
|
|
| 7 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 8 |
import numpy as np
|
| 9 |
|
|
|
|
| 10 |
MODEL_NAME = "google/gemma-2b-it"
|
| 11 |
INFO_FILE = "infos_medicaux.txt"
|
| 12 |
MAX_TOKENS = 600
|
| 13 |
+
TEMPERATURE = 0.7
|
| 14 |
+
CHUNK_SIZE = 1000
|
| 15 |
+
TOP_K_CHUNKS = 5
|
| 16 |
|
| 17 |
+
print("Chargement du modèle...")
|
|
|
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 19 |
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
MODEL_NAME,
|
|
|
|
| 23 |
low_cpu_mem_usage=True
|
| 24 |
)
|
| 25 |
model.eval()
|
| 26 |
+
print("Modèle chargé avec succès !")
|
| 27 |
|
|
|
|
| 28 |
medical_context_chunks = []
|
| 29 |
if os.path.exists(INFO_FILE):
|
| 30 |
with open(INFO_FILE, "r", encoding="utf-8") as f:
|
|
|
|
| 32 |
medical_context_chunks = [medical_context[i:i+CHUNK_SIZE] for i in range(0, len(medical_context), CHUNK_SIZE)]
|
| 33 |
print(f"📘 Contexte médical chargé ({len(medical_context)} caractères, {len(medical_context_chunks)} chunks)")
|
| 34 |
else:
|
| 35 |
+
print("Aucun fichier infos_medicaux.txt trouvé.")
|
| 36 |
|
|
|
|
| 37 |
if medical_context_chunks:
|
| 38 |
vectorizer = TfidfVectorizer().fit(medical_context_chunks)
|
| 39 |
chunk_vectors = vectorizer.transform(medical_context_chunks)
|
|
|
|
| 49 |
top_indices = np.argsort(similarities)[::-1][:top_k]
|
| 50 |
return [medical_context_chunks[i] for i in top_indices]
|
| 51 |
|
|
|
|
| 52 |
@spaces.GPU()
|
| 53 |
def chat_with_finanfa(message, history=None):
|
| 54 |
if history is None:
|
|
|
|
| 62 |
"Donne des réponses claires, détaillées et adaptées au Bénin."
|
| 63 |
)
|
| 64 |
|
|
|
|
| 65 |
relevant_chunks = get_relevant_chunks(message)
|
| 66 |
|
| 67 |
conversation = f"Système : {system_prompt}\n"
|