Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -100,6 +100,10 @@ def load_system_prompt():
|
|
| 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}")
|
|
@@ -116,7 +120,7 @@ def setup_chromadb_collection(client, df, model_paraphrase):
|
|
| 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:
|
|
@@ -144,15 +148,26 @@ def setup_chromadb_collection(client, df, model_paraphrase):
|
|
| 144 |
metadatas.append({**meta, "type": "reponse"})
|
| 145 |
ids.append(f"id_{i}_R")
|
| 146 |
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
try:
|
|
|
|
| 150 |
client.delete_collection(name=COLLECTION_NAME)
|
| 151 |
-
except:
|
|
|
|
| 152 |
pass
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
print(f"✅ Collection remplie: {collection.count()} documents.")
|
| 158 |
return collection
|
|
@@ -296,19 +311,29 @@ def initialize_global_resources():
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
# ======================================================================
|
|
|
|
| 100 |
def initialize_gemini_client():
|
| 101 |
"""Initialise le client Google Gemini."""
|
| 102 |
try:
|
| 103 |
+
# NOTE: Using a placeholder key in the code. A real API key
|
| 104 |
+
# should be loaded from an environment variable or a secret.
|
| 105 |
+
if GEMINI_API_KEY == "AIzaSyDXXY7uSXryTxZ51jQFsSLcPnC_Ivt9V1g":
|
| 106 |
+
print("⚠️ Clé Gemini par défaut. Assurez-vous d'utiliser une clé valide ou un secret.")
|
| 107 |
return genai.Client(api_key=GEMINI_API_KEY)
|
| 108 |
except Exception as e:
|
| 109 |
print(f"❌ Erreur Gemini: {e}")
|
|
|
|
| 120 |
try:
|
| 121 |
collection = client.get_or_create_collection(name=COLLECTION_NAME)
|
| 122 |
except Exception as e:
|
| 123 |
+
print(f"❌ Erreur get/create collection ChromaDB: {e}")
|
| 124 |
raise
|
| 125 |
|
| 126 |
if collection.count() == total_docs and total_docs > 0:
|
|
|
|
| 148 |
metadatas.append({**meta, "type": "reponse"})
|
| 149 |
ids.append(f"id_{i}_R")
|
| 150 |
|
| 151 |
+
# --- Potentielle source d'erreur: Encodage ---
|
| 152 |
+
try:
|
| 153 |
+
embeddings = model_paraphrase.encode(docs, show_progress_bar=False).tolist()
|
| 154 |
+
except Exception as e:
|
| 155 |
+
print(f"❌ Erreur d'encodage des documents pour ChromaDB: {e}")
|
| 156 |
+
raise
|
| 157 |
|
| 158 |
try:
|
| 159 |
+
# Tentative de suppression et recréation pour forcer la mise à jour
|
| 160 |
client.delete_collection(name=COLLECTION_NAME)
|
| 161 |
+
except Exception:
|
| 162 |
+
# Ignorer si la collection n'existe pas ou si la suppression échoue
|
| 163 |
pass
|
| 164 |
|
| 165 |
+
try:
|
| 166 |
+
collection = client.get_or_create_collection(name=COLLECTION_NAME)
|
| 167 |
+
collection.add(embeddings=embeddings, documents=docs, metadatas=metadatas, ids=ids)
|
| 168 |
+
except Exception as e:
|
| 169 |
+
print(f"❌ Erreur d'ajout des données à la collection ChromaDB: {e}")
|
| 170 |
+
raise
|
| 171 |
|
| 172 |
print(f"✅ Collection remplie: {collection.count()} documents.")
|
| 173 |
return collection
|
|
|
|
| 311 |
os.makedirs(CHROMA_DB_PATH, exist_ok=True)
|
| 312 |
|
| 313 |
try:
|
| 314 |
+
# 1. Chargement des modèles, données et prompt
|
| 315 |
model_cross_encoder, model_paraphrase = load_models()
|
| 316 |
df = load_data()
|
| 317 |
system_prompt = load_system_prompt()
|
| 318 |
gemini_client = initialize_gemini_client()
|
| 319 |
except Exception:
|
| 320 |
+
# Si une erreur se produit ici (modèles/données), on ne continue pas.
|
| 321 |
return False
|
| 322 |
|
| 323 |
try:
|
| 324 |
+
# 2. Initialisation et configuration de ChromaDB
|
| 325 |
+
print(f"⏳ Initialisation de ChromaDB à l'emplacement: {CHROMA_DB_PATH}")
|
| 326 |
+
# Note : Le chemin doit être accessible en écriture
|
| 327 |
+
chroma_client = chromadb.PersistentClient(path=CHROMA_DB_PATH)
|
| 328 |
collection = setup_chromadb_collection(chroma_client, df, model_paraphrase)
|
| 329 |
+
|
| 330 |
print("✅ INITIALISATION COMPLÈTE\n")
|
| 331 |
return True
|
| 332 |
+
except Exception as e:
|
| 333 |
+
# 3. Gérer spécifiquement les erreurs ChromaDB
|
| 334 |
+
print(f"❌ Erreur lors de l'initialisation de ChromaDB ou du remplissage: {e}")
|
| 335 |
+
# Pour les déploiements sur Hugging Face Spaces, vérifiez que 'data/bdd_ChromaDB'
|
| 336 |
+
# est accessible en écriture, ou essayez de le placer dans '/tmp/bdd_ChromaDB'.
|
| 337 |
return False
|
| 338 |
|
| 339 |
# ======================================================================
|