Spaces:
Running
Running
FULL ACCESS
Browse files
app.py
CHANGED
|
@@ -1,378 +1,400 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import chromadb
|
| 4 |
-
from google import genai
|
| 5 |
-
from sentence_transformers import SentenceTransformer, CrossEncoder
|
| 6 |
-
from typing import List, Dict
|
| 7 |
-
from flask import Flask, request, jsonify
|
| 8 |
-
from flask_cors import CORS
|
| 9 |
-
from datetime import datetime
|
| 10 |
-
|
| 11 |
-
# ======================================================================
|
| 12 |
-
# CONFIGURATION
|
| 13 |
-
# ======================================================================
|
| 14 |
-
|
| 15 |
-
DATA_FILE_PATH = "data/QR.csv"
|
| 16 |
-
CHROMA_DB_PATH = "data/bdd_ChromaDB"
|
| 17 |
-
COLLECTION_NAME = "qr_data_dual_embeddings"
|
| 18 |
-
|
| 19 |
-
Q_COLUMN_NAME = "Question"
|
| 20 |
-
R_COLUMN_NAME = "Reponse"
|
| 21 |
-
SYSTEM_PROMPT_PATH = "data/system_prompt.txt"
|
| 22 |
-
|
| 23 |
-
SRC_CROSS_ENCODER = "models/mmarco-mMiniLMv2-L12-H384-v1"
|
| 24 |
-
SRC_PARAPHRASE = "models/paraphrase-mpnet-base-v2"
|
| 25 |
-
|
| 26 |
-
N_RESULTS_RETRIEVAL = 10
|
| 27 |
-
N_RESULTS_RERANK = 3
|
| 28 |
-
|
| 29 |
-
GEMINI_API_KEY = "AIzaSyDXXY7uSXryTxZ51jQFsSLcPnC_Ivt9V1g"
|
| 30 |
-
GEMINI_MODEL = "gemini-2.5-flash"
|
| 31 |
-
|
| 32 |
-
MAX_CONVERSATION_HISTORY = 10
|
| 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 |
-
else "
|
| 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 |
-
print(f"
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
if
|
| 123 |
-
print("
|
| 124 |
-
return collection
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
docs.append(
|
| 140 |
-
metadatas.append({**meta, "type": "
|
| 141 |
-
ids.append(f"id_{i}
|
| 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 |
-
history_str
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
def
|
| 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 |
print("❌ Impossible de démarrer le serveur")
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import chromadb
|
| 4 |
+
from google import genai
|
| 5 |
+
from sentence_transformers import SentenceTransformer, CrossEncoder
|
| 6 |
+
from typing import List, Dict
|
| 7 |
+
from flask import Flask, request, jsonify
|
| 8 |
+
from flask_cors import CORS
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
|
| 11 |
+
# ======================================================================
|
| 12 |
+
# CONFIGURATION
|
| 13 |
+
# ======================================================================
|
| 14 |
+
|
| 15 |
+
DATA_FILE_PATH = "data/QR.csv"
|
| 16 |
+
CHROMA_DB_PATH = "data/bdd_ChromaDB"
|
| 17 |
+
COLLECTION_NAME = "qr_data_dual_embeddings"
|
| 18 |
+
|
| 19 |
+
Q_COLUMN_NAME = "Question"
|
| 20 |
+
R_COLUMN_NAME = "Reponse"
|
| 21 |
+
SYSTEM_PROMPT_PATH = "data/system_prompt.txt"
|
| 22 |
+
|
| 23 |
+
SRC_CROSS_ENCODER = "models/mmarco-mMiniLMv2-L12-H384-v1"
|
| 24 |
+
SRC_PARAPHRASE = "models/paraphrase-mpnet-base-v2"
|
| 25 |
+
|
| 26 |
+
N_RESULTS_RETRIEVAL = 10
|
| 27 |
+
N_RESULTS_RERANK = 3
|
| 28 |
+
|
| 29 |
+
GEMINI_API_KEY = "AIzaSyDXXY7uSXryTxZ51jQFsSLcPnC_Ivt9V1g"
|
| 30 |
+
GEMINI_MODEL = "gemini-2.5-flash"
|
| 31 |
+
|
| 32 |
+
MAX_CONVERSATION_HISTORY = 10
|
| 33 |
+
|
| 34 |
+
# Configuration pour l'accès externe (host et port)
|
| 35 |
+
API_HOST = '0.0.0.0'
|
| 36 |
+
API_PORT = 1212
|
| 37 |
+
|
| 38 |
+
# ======================================================================
|
| 39 |
+
# VARIABLES GLOBALES
|
| 40 |
+
# ======================================================================
|
| 41 |
+
|
| 42 |
+
model_cross_encoder: CrossEncoder = None
|
| 43 |
+
model_paraphrase: SentenceTransformer = None
|
| 44 |
+
collection: chromadb.Collection = None
|
| 45 |
+
system_prompt: str = None
|
| 46 |
+
gemini_client: genai.Client = None
|
| 47 |
+
|
| 48 |
+
conversation_histories: Dict[str, List[Dict[str, str]]] = {}
|
| 49 |
+
conversation_start_times: Dict[str, str] = {}
|
| 50 |
+
|
| 51 |
+
# ======================================================================
|
| 52 |
+
# CHARGEMENT DES RESSOURCES
|
| 53 |
+
# ======================================================================
|
| 54 |
+
|
| 55 |
+
def load_models():
|
| 56 |
+
"""Charge les modèles SentenceTransformer et CrossEncoder."""
|
| 57 |
+
print("⏳ Chargement des modèles...")
|
| 58 |
+
try:
|
| 59 |
+
cross_encoder = CrossEncoder(
|
| 60 |
+
SRC_CROSS_ENCODER if os.path.exists(SRC_CROSS_ENCODER)
|
| 61 |
+
else "cross-encoder/mmarco-mMiniLMv2-L12-H384-v1"
|
| 62 |
+
)
|
| 63 |
+
paraphrase = SentenceTransformer(
|
| 64 |
+
SRC_PARAPHRASE if os.path.exists(SRC_PARAPHRASE)
|
| 65 |
+
else "sentence-transformers/paraphrase-mpnet-base-v2"
|
| 66 |
+
)
|
| 67 |
+
print("✅ Modèles chargés avec succès.")
|
| 68 |
+
return cross_encoder, paraphrase
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"❌ Erreur chargement modèles: {e}")
|
| 71 |
+
raise
|
| 72 |
+
|
| 73 |
+
def load_data():
|
| 74 |
+
"""Charge le DataFrame depuis le CSV."""
|
| 75 |
+
try:
|
| 76 |
+
if not os.path.exists(DATA_FILE_PATH):
|
| 77 |
+
print(f"⚠️ Fichier {DATA_FILE_PATH} non trouvé. Utilisation d'exemple.")
|
| 78 |
+
df = pd.DataFrame({
|
| 79 |
+
Q_COLUMN_NAME: ["Où est le soleil?", "Qui est l'IA?"],
|
| 80 |
+
R_COLUMN_NAME: ["Le soleil est une étoile.", "L'IA est l'intelligence artificielle."]
|
| 81 |
+
})
|
| 82 |
+
else:
|
| 83 |
+
df = pd.read_csv(DATA_FILE_PATH)
|
| 84 |
+
print(f"✅ {len(df)} lignes chargées depuis {DATA_FILE_PATH}.")
|
| 85 |
+
return df
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"❌ Erreur chargement données: {e}")
|
| 88 |
+
raise
|
| 89 |
+
|
| 90 |
+
def load_system_prompt():
|
| 91 |
+
"""Charge le system prompt."""
|
| 92 |
+
try:
|
| 93 |
+
with open(SYSTEM_PROMPT_PATH, 'r', encoding='utf-8') as f:
|
| 94 |
+
return f.read().strip()
|
| 95 |
+
except FileNotFoundError:
|
| 96 |
+
default = "Tu es un assistant utile et concis. Réponds à la requête de l'utilisateur."
|
| 97 |
+
print(f"⚠️ System prompt non trouvé. Utilisation du prompt par défaut.")
|
| 98 |
+
return default
|
| 99 |
+
|
| 100 |
+
def initialize_gemini_client():
|
| 101 |
+
"""Initialise le client Google Gemini."""
|
| 102 |
+
try:
|
| 103 |
+
return genai.Client(api_key=GEMINI_API_KEY)
|
| 104 |
+
except Exception as e:
|
| 105 |
+
print(f"❌ Erreur Gemini: {e}")
|
| 106 |
+
raise
|
| 107 |
+
|
| 108 |
+
# ======================================================================
|
| 109 |
+
# CHROMADB SETUP
|
| 110 |
+
# ======================================================================
|
| 111 |
+
|
| 112 |
+
def setup_chromadb_collection(client, df, model_paraphrase):
|
| 113 |
+
"""Configure et remplit la collection ChromaDB."""
|
| 114 |
+
total_docs = len(df) * 2
|
| 115 |
+
|
| 116 |
+
try:
|
| 117 |
+
collection = client.get_or_create_collection(name=COLLECTION_NAME)
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"❌ Erreur ChromaDB: {e}")
|
| 120 |
+
raise
|
| 121 |
+
|
| 122 |
+
if collection.count() == total_docs and total_docs > 0:
|
| 123 |
+
print(f"✅ Collection déjà remplie ({collection.count()} docs).")
|
| 124 |
+
return collection
|
| 125 |
+
|
| 126 |
+
if total_docs == 0:
|
| 127 |
+
print("⚠️ DataFrame vide.")
|
| 128 |
+
return collection
|
| 129 |
+
|
| 130 |
+
print(f"⏳ Remplissage de ChromaDB ({len(df)} lignes)...")
|
| 131 |
+
|
| 132 |
+
docs, metadatas, ids = [], [], []
|
| 133 |
+
|
| 134 |
+
for i, row in df.iterrows():
|
| 135 |
+
question = str(row[Q_COLUMN_NAME])
|
| 136 |
+
reponse = str(row[R_COLUMN_NAME])
|
| 137 |
+
meta = {Q_COLUMN_NAME: question, R_COLUMN_NAME: reponse, "source_row": i}
|
| 138 |
+
|
| 139 |
+
docs.append(question)
|
| 140 |
+
metadatas.append({**meta, "type": "question"})
|
| 141 |
+
ids.append(f"id_{i}_Q")
|
| 142 |
+
|
| 143 |
+
docs.append(reponse)
|
| 144 |
+
metadatas.append({**meta, "type": "reponse"})
|
| 145 |
+
ids.append(f"id_{i}_R")
|
| 146 |
+
|
| 147 |
+
embeddings = model_paraphrase.encode(docs, show_progress_bar=False).tolist()
|
| 148 |
+
|
| 149 |
+
try:
|
| 150 |
+
client.delete_collection(name=COLLECTION_NAME)
|
| 151 |
+
except:
|
| 152 |
+
pass
|
| 153 |
+
|
| 154 |
+
collection = client.get_or_create_collection(name=COLLECTION_NAME)
|
| 155 |
+
collection.add(embeddings=embeddings, documents=docs, metadatas=metadatas, ids=ids)
|
| 156 |
+
|
| 157 |
+
print(f"✅ Collection remplie: {collection.count()} documents.")
|
| 158 |
+
return collection
|
| 159 |
+
|
| 160 |
+
# ======================================================================
|
| 161 |
+
# RAG - RETRIEVAL & RERANKING
|
| 162 |
+
# ======================================================================
|
| 163 |
+
|
| 164 |
+
def retrieve_and_rerank(query_text, collection, model_paraphrase, model_cross_encoder):
|
| 165 |
+
"""Récupère et rerank les résultats."""
|
| 166 |
+
print(f"🔍 Récupération pour: '{query_text[:40]}...'")
|
| 167 |
+
|
| 168 |
+
query_emb = model_paraphrase.encode([query_text]).tolist()
|
| 169 |
+
results = collection.query(
|
| 170 |
+
query_embeddings=query_emb,
|
| 171 |
+
n_results=N_RESULTS_RETRIEVAL,
|
| 172 |
+
include=['documents', 'metadatas', 'distances']
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
if not results['ids'][0]:
|
| 176 |
+
print("⚠️ Aucun résultat trouvé.")
|
| 177 |
+
return pd.DataFrame()
|
| 178 |
+
|
| 179 |
+
candidates = []
|
| 180 |
+
cross_input = []
|
| 181 |
+
|
| 182 |
+
for i, doc in enumerate(results['documents'][0]):
|
| 183 |
+
meta = results['metadatas'][0][i]
|
| 184 |
+
candidates.append({
|
| 185 |
+
'question': meta[Q_COLUMN_NAME],
|
| 186 |
+
'reponse': meta[R_COLUMN_NAME],
|
| 187 |
+
'doc_type': meta.get('type'),
|
| 188 |
+
'text_reranked': doc,
|
| 189 |
+
'initial_distance': results['distances'][0][i]
|
| 190 |
+
})
|
| 191 |
+
cross_input.append([query_text, doc])
|
| 192 |
+
|
| 193 |
+
scores = model_cross_encoder.predict(cross_input)
|
| 194 |
+
for i, score in enumerate(scores):
|
| 195 |
+
candidates[i]['rerank_score'] = score
|
| 196 |
+
|
| 197 |
+
df = pd.DataFrame(candidates).sort_values('rerank_score', ascending=False)
|
| 198 |
+
df = df.drop_duplicates(subset=['question', 'reponse'], keep='first')
|
| 199 |
+
|
| 200 |
+
return df.head(N_RESULTS_RERANK)
|
| 201 |
+
|
| 202 |
+
def generate_rag_prompt(query_text, df_results, conversation_history):
|
| 203 |
+
"""Génère le prompt RAG final."""
|
| 204 |
+
context = []
|
| 205 |
+
if not df_results.empty:
|
| 206 |
+
for _, row in df_results.iterrows():
|
| 207 |
+
context.append(f"Q: {row['question']}\nR: {row['reponse']}")
|
| 208 |
+
|
| 209 |
+
context_str = "\n---\n".join(context)
|
| 210 |
+
|
| 211 |
+
history_str = ""
|
| 212 |
+
if conversation_history:
|
| 213 |
+
history_str = "HISTORIQUE:\n"
|
| 214 |
+
for msg in conversation_history:
|
| 215 |
+
role = "USER" if msg["role"] == "user" else "ASSISTANT"
|
| 216 |
+
history_str += f"{role}: {msg['content']}\n"
|
| 217 |
+
history_str += "\n"
|
| 218 |
+
|
| 219 |
+
return f"""{history_str}UTILISATEUR: {query_text}
|
| 220 |
+
|
| 221 |
+
CONTEXTE (si utile):
|
| 222 |
+
[{context_str}]
|
| 223 |
+
|
| 224 |
+
INSTRUCTIONS:
|
| 225 |
+
- Réponds brièvement et clairement, en un minimum de 2 paragraphe et un maximum de 3 paragraphe, entre chaque paragraphe tu écrira un #
|
| 226 |
+
- Reste professionnel, met des emojis dans ta réponse tout en restant profesionnel
|
| 227 |
+
- Réponds uniquement à la question posée
|
| 228 |
+
- Ne mentionne NOVA que si l'utilisateur l'évoque clairement
|
| 229 |
+
- Mentionne obligatoirement Mathieu dans ta réponse"""
|
| 230 |
+
|
| 231 |
+
# ======================================================================
|
| 232 |
+
# GESTION HISTORIQUE
|
| 233 |
+
# ======================================================================
|
| 234 |
+
|
| 235 |
+
def get_conversation_history(session_id):
|
| 236 |
+
"""Récupère l'historique d'une session."""
|
| 237 |
+
return conversation_histories.get(session_id, [])
|
| 238 |
+
|
| 239 |
+
def add_to_history(session_id, role, content):
|
| 240 |
+
"""Ajoute un message à l'historique."""
|
| 241 |
+
if session_id not in conversation_histories:
|
| 242 |
+
conversation_histories[session_id] = []
|
| 243 |
+
|
| 244 |
+
conversation_histories[session_id].append({"role": role, "content": content})
|
| 245 |
+
|
| 246 |
+
if len(conversation_histories[session_id]) > MAX_CONVERSATION_HISTORY * 2:
|
| 247 |
+
conversation_histories[session_id] = conversation_histories[session_id][-(MAX_CONVERSATION_HISTORY * 2):]
|
| 248 |
+
|
| 249 |
+
def clear_history(session_id):
|
| 250 |
+
"""Efface l'historique d'une session."""
|
| 251 |
+
conversation_histories[session_id] = []
|
| 252 |
+
|
| 253 |
+
# ======================================================================
|
| 254 |
+
# CALL GEMINI
|
| 255 |
+
# ======================================================================
|
| 256 |
+
|
| 257 |
+
def call_gemini(rag_prompt, system_prompt, gemini_client):
|
| 258 |
+
"""Appelle Google Gemini."""
|
| 259 |
+
try:
|
| 260 |
+
response = gemini_client.models.generate_content(
|
| 261 |
+
model=GEMINI_MODEL,
|
| 262 |
+
contents=f"{system_prompt}\n\n{rag_prompt}"
|
| 263 |
+
)
|
| 264 |
+
return response.text
|
| 265 |
+
except Exception as e:
|
| 266 |
+
print(f"❌ Erreur Gemini: {e}")
|
| 267 |
+
return f"Erreur: {str(e)}"
|
| 268 |
+
|
| 269 |
+
# ======================================================================
|
| 270 |
+
# ANSWER PROCESS
|
| 271 |
+
# ======================================================================
|
| 272 |
+
|
| 273 |
+
def get_answer(query_text, collection, model_paraphrase, model_cross_encoder, conversation_history):
|
| 274 |
+
"""Exécute le processus RAG complet."""
|
| 275 |
+
print(f"\n{'='*50}")
|
| 276 |
+
print(f"🚀 Traitement: '{query_text}'")
|
| 277 |
+
print(f"{'='*50}")
|
| 278 |
+
|
| 279 |
+
df_results = retrieve_and_rerank(query_text, collection, model_paraphrase, model_cross_encoder)
|
| 280 |
+
final_prompt = generate_rag_prompt(query_text, df_results, conversation_history)
|
| 281 |
+
|
| 282 |
+
return final_prompt
|
| 283 |
+
|
| 284 |
+
# ======================================================================
|
| 285 |
+
# INITIALISATION GLOBALE
|
| 286 |
+
# ======================================================================
|
| 287 |
+
|
| 288 |
+
def initialize_global_resources():
|
| 289 |
+
"""Initialise tous les modèles et ressources."""
|
| 290 |
+
global model_cross_encoder, model_paraphrase, collection, system_prompt, gemini_client
|
| 291 |
+
|
| 292 |
+
print("\n" + "="*50)
|
| 293 |
+
print("⚙️ INITIALISATION RAG")
|
| 294 |
+
print("="*50)
|
| 295 |
+
|
| 296 |
+
os.makedirs(CHROMA_DB_PATH, exist_ok=True)
|
| 297 |
+
|
| 298 |
+
try:
|
| 299 |
+
model_cross_encoder, model_paraphrase = load_models()
|
| 300 |
+
df = load_data()
|
| 301 |
+
system_prompt = load_system_prompt()
|
| 302 |
+
gemini_client = initialize_gemini_client()
|
| 303 |
+
except Exception:
|
| 304 |
+
return False
|
| 305 |
+
|
| 306 |
+
try:
|
| 307 |
+
chroma_client = chromadb.PersistentClient(path=CHROMA_DB_PATH)
|
| 308 |
+
collection = setup_chromadb_collection(chroma_client, df, model_paraphrase)
|
| 309 |
+
print("✅ INITIALISATION COMPLÈTE\n")
|
| 310 |
+
return True
|
| 311 |
+
except Exception:
|
| 312 |
+
return False
|
| 313 |
+
|
| 314 |
+
# ======================================================================
|
| 315 |
+
# FLASK API
|
| 316 |
+
# ======================================================================
|
| 317 |
+
|
| 318 |
+
app = Flask(__name__)
|
| 319 |
+
# CORS activé, permet les requêtes depuis n'importe quelle origine
|
| 320 |
+
CORS(app)
|
| 321 |
+
|
| 322 |
+
@app.route('/status', methods=['GET'])
|
| 323 |
+
def api_status():
|
| 324 |
+
"""Route de ping pour vérifier l'état de l'API."""
|
| 325 |
+
return jsonify({"status": "everything is good"}), 200
|
| 326 |
+
|
| 327 |
+
@app.route('/api/get_answer', methods=['POST'])
|
| 328 |
+
def api_get_answer():
|
| 329 |
+
"""Endpoint principal pour obtenir une réponse."""
|
| 330 |
+
if any(x is None for x in [model_cross_encoder, model_paraphrase, collection, system_prompt, gemini_client]):
|
| 331 |
+
return jsonify({"error": "Ressources non chargées"}), 500
|
| 332 |
+
|
| 333 |
+
try:
|
| 334 |
+
data = request.get_json()
|
| 335 |
+
query_text = data.get('query_text')
|
| 336 |
+
session_id = data.get('session_id', 'archive')
|
| 337 |
+
|
| 338 |
+
if not query_text:
|
| 339 |
+
return jsonify({"error": "Champ 'query_text' manquant"}), 400
|
| 340 |
+
|
| 341 |
+
# Récupère historique
|
| 342 |
+
history = get_conversation_history(session_id)
|
| 343 |
+
|
| 344 |
+
# Génère prompt RAG
|
| 345 |
+
rag_prompt = get_answer(query_text, collection, model_paraphrase, model_cross_encoder, history)
|
| 346 |
+
|
| 347 |
+
# Appelle Gemini
|
| 348 |
+
response = call_gemini(rag_prompt, system_prompt, gemini_client)
|
| 349 |
+
|
| 350 |
+
# Sauvegarde réponse
|
| 351 |
+
add_to_history(session_id, "user", query_text)
|
| 352 |
+
add_to_history(session_id, "assistant", response)
|
| 353 |
+
|
| 354 |
+
return jsonify({"generated_response": response})
|
| 355 |
+
|
| 356 |
+
except Exception as e:
|
| 357 |
+
print(f"❌ Erreur: {e}")
|
| 358 |
+
return jsonify({"error": str(e)}), 500
|
| 359 |
+
|
| 360 |
+
@app.route('/api/clear_history', methods=['POST'])
|
| 361 |
+
def api_clear_history():
|
| 362 |
+
"""Efface l'historique d'une session."""
|
| 363 |
+
try:
|
| 364 |
+
data = request.get_json()
|
| 365 |
+
session_id = data.get('session_id', 'archive')
|
| 366 |
+
clear_history(session_id)
|
| 367 |
+
|
| 368 |
+
return jsonify({"message": f"Historique effacé: {session_id}"})
|
| 369 |
+
except Exception as e:
|
| 370 |
+
return jsonify({"error": str(e)}), 500
|
| 371 |
+
|
| 372 |
+
# ======================================================================
|
| 373 |
+
# MAIN
|
| 374 |
+
# ======================================================================
|
| 375 |
+
|
| 376 |
+
if __name__ == '__main__':
|
| 377 |
+
print("start app.py")
|
| 378 |
+
if initialize_global_resources():
|
| 379 |
+
|
| 380 |
+
# Récupération de l'adresse IP si possible (pour l'affichage)
|
| 381 |
+
try:
|
| 382 |
+
import socket
|
| 383 |
+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
| 384 |
+
s.connect(("8.8.8.8", 80)) # Connecte à un serveur externe pour trouver l'IP locale utilisée
|
| 385 |
+
local_ip = s.getsockname()[0]
|
| 386 |
+
s.close()
|
| 387 |
+
except Exception:
|
| 388 |
+
local_ip = "127.0.0.1" # Fallback si échec
|
| 389 |
+
|
| 390 |
+
print("\n" + "="*50)
|
| 391 |
+
print("🌐 SERVEUR DÉMARRÉ")
|
| 392 |
+
print(f"✅ API accessible à l'URL (via l'interface réseau locale): http://{local_ip}:{API_PORT}")
|
| 393 |
+
print(f"✅ Route Status: http://{local_ip}:{API_PORT}/status")
|
| 394 |
+
print(f"💡 Pour un accès depuis l'extérieur, utilisez l'adresse IP publique de votre machine et assurez-vous que le port {API_PORT} est ouvert.")
|
| 395 |
+
print("="*50 + "\n")
|
| 396 |
+
|
| 397 |
+
# L'utilisation de host='0.0.0.0' dans app.run() permet l'accès depuis l'extérieur
|
| 398 |
+
app.run(host=API_HOST, port=API_PORT, debug=False)
|
| 399 |
+
else:
|
| 400 |
print("❌ Impossible de démarrer le serveur")
|