Spaces:
Running
on
Zero
Running
on
Zero
Upload step03_chatbot.py with huggingface_hub
Browse files- step03_chatbot.py +18 -1
step03_chatbot.py
CHANGED
|
@@ -334,6 +334,11 @@ class Qwen3Reranker:
|
|
| 334 |
print(" - Reranker non disponible, scores neutres retournés")
|
| 335 |
return [0.5] * len(documents)
|
| 336 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
if instruction is None:
|
| 338 |
instruction = self._get_default_instruction()
|
| 339 |
|
|
@@ -708,6 +713,13 @@ class GenericRAGChatbot:
|
|
| 708 |
|
| 709 |
print(f"🔍 Recherche en deux étapes: {initial_k} candidats → reranking → {k} finaux")
|
| 710 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 711 |
# Étape 1: Recherche par embedding avec FAISS
|
| 712 |
if hasattr(self.embedding_model, 'prompts') and 'query' in self.embedding_model.prompts:
|
| 713 |
query_embedding = self.embedding_model.encode([query], prompt_name="query")[0]
|
|
@@ -803,6 +815,11 @@ class GenericRAGChatbot:
|
|
| 803 |
yield "❌ Modèle de génération non disponible"
|
| 804 |
return
|
| 805 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 806 |
# Construction du prompt système
|
| 807 |
system_prompt = """Tu es un assistant expert qui répond aux questions en te basant uniquement sur les documents fournis dans le contexte.
|
| 808 |
|
|
@@ -1110,7 +1127,7 @@ def _create_rag_system():
|
|
| 1110 |
'generation_model': "Qwen/Qwen3-4B-Instruct-2507", # Modèle qui fonctionne sur ZeroGPU
|
| 1111 |
'use_flash_attention': True, # ZeroGPU supporte Flash Attention
|
| 1112 |
'use_reranker': True, # GPU puissant, reranking activé
|
| 1113 |
-
'initial_k':
|
| 1114 |
'final_k': 5 # Plus de documents finaux
|
| 1115 |
}
|
| 1116 |
elif is_mac:
|
|
|
|
| 334 |
print(" - Reranker non disponible, scores neutres retournés")
|
| 335 |
return [0.5] * len(documents)
|
| 336 |
|
| 337 |
+
# Assurer que le modèle est sur GPU dans ZeroGPU
|
| 338 |
+
if torch.cuda.is_available() and not next(self.model.parameters()).is_cuda:
|
| 339 |
+
print(" - Déplacement du reranker vers GPU...")
|
| 340 |
+
self.model = self.model.cuda()
|
| 341 |
+
|
| 342 |
if instruction is None:
|
| 343 |
instruction = self._get_default_instruction()
|
| 344 |
|
|
|
|
| 713 |
|
| 714 |
print(f"🔍 Recherche en deux étapes: {initial_k} candidats → reranking → {k} finaux")
|
| 715 |
|
| 716 |
+
# Assurer que le modèle embedding est sur GPU dans ZeroGPU
|
| 717 |
+
if torch.cuda.is_available() and hasattr(self.embedding_model, 'model'):
|
| 718 |
+
device = next(self.embedding_model.model.parameters()).device
|
| 719 |
+
if not device.type == 'cuda':
|
| 720 |
+
print(" - Déplacement du modèle embedding vers GPU...")
|
| 721 |
+
self.embedding_model = self.embedding_model.cuda()
|
| 722 |
+
|
| 723 |
# Étape 1: Recherche par embedding avec FAISS
|
| 724 |
if hasattr(self.embedding_model, 'prompts') and 'query' in self.embedding_model.prompts:
|
| 725 |
query_embedding = self.embedding_model.encode([query], prompt_name="query")[0]
|
|
|
|
| 815 |
yield "❌ Modèle de génération non disponible"
|
| 816 |
return
|
| 817 |
|
| 818 |
+
# Assurer que le modèle de génération est sur GPU dans ZeroGPU
|
| 819 |
+
if torch.cuda.is_available() and not next(self.generation_model.parameters()).is_cuda:
|
| 820 |
+
print(" - Déplacement du modèle de génération vers GPU...")
|
| 821 |
+
self.generation_model = self.generation_model.cuda()
|
| 822 |
+
|
| 823 |
# Construction du prompt système
|
| 824 |
system_prompt = """Tu es un assistant expert qui répond aux questions en te basant uniquement sur les documents fournis dans le contexte.
|
| 825 |
|
|
|
|
| 1127 |
'generation_model': "Qwen/Qwen3-4B-Instruct-2507", # Modèle qui fonctionne sur ZeroGPU
|
| 1128 |
'use_flash_attention': True, # ZeroGPU supporte Flash Attention
|
| 1129 |
'use_reranker': True, # GPU puissant, reranking activé
|
| 1130 |
+
'initial_k': 20, # Même config que local
|
| 1131 |
'final_k': 5 # Plus de documents finaux
|
| 1132 |
}
|
| 1133 |
elif is_mac:
|