Spaces:
Running
on
Zero
Running
on
Zero
Upload step03_chatbot.py with huggingface_hub
Browse files- step03_chatbot.py +3 -16
step03_chatbot.py
CHANGED
|
@@ -334,11 +334,7 @@ class Qwen3Reranker:
|
|
| 334 |
print(" - Reranker non disponible, scores neutres retournés")
|
| 335 |
return [0.5] * len(documents)
|
| 336 |
|
| 337 |
-
#
|
| 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 |
-
self.device = torch.device("cuda")
|
| 342 |
|
| 343 |
if instruction is None:
|
| 344 |
instruction = self._get_default_instruction()
|
|
@@ -440,6 +436,7 @@ class GenericRAGChatbot:
|
|
| 440 |
if self.is_zerogpu:
|
| 441 |
print("🚀 Environnement ZeroGPU détecté - optimisations cloud")
|
| 442 |
self.use_flash_attention = False # Désactiver Flash Attention temporairement sur ZeroGPU
|
|
|
|
| 443 |
elif self.is_mps and use_flash_attention:
|
| 444 |
print("🍎 Mac avec MPS détecté - désactivation automatique de Flash Attention")
|
| 445 |
self.use_flash_attention = False
|
|
@@ -705,7 +702,6 @@ class GenericRAGChatbot:
|
|
| 705 |
except:
|
| 706 |
return 0.0
|
| 707 |
|
| 708 |
-
@spaces.GPU(duration=120) # ZeroGPU: GPU pour embedding et reranking
|
| 709 |
def search_documents(self, query: str, final_k: int = None, use_reranking: bool = None) -> List[Dict]:
|
| 710 |
"""
|
| 711 |
Recherche avancée avec reranking en deux étapes
|
|
@@ -716,16 +712,7 @@ class GenericRAGChatbot:
|
|
| 716 |
|
| 717 |
print(f"🔍 Recherche en deux étapes: {initial_k} candidats → reranking → {k} finaux")
|
| 718 |
|
| 719 |
-
#
|
| 720 |
-
if torch.cuda.is_available() and hasattr(self.embedding_model, '_modules'):
|
| 721 |
-
try:
|
| 722 |
-
# Vérifier si le modèle principal est sur GPU
|
| 723 |
-
first_param = next(self.embedding_model.parameters(), None)
|
| 724 |
-
if first_param is not None and not first_param.is_cuda:
|
| 725 |
-
print(" - Déplacement du modèle embedding vers GPU...")
|
| 726 |
-
self.embedding_model = self.embedding_model.cuda()
|
| 727 |
-
except Exception as e:
|
| 728 |
-
print(f" - Erreur lors du déplacement embedding vers GPU: {e}")
|
| 729 |
|
| 730 |
# Étape 1: Recherche par embedding avec FAISS
|
| 731 |
if hasattr(self.embedding_model, 'prompts') and 'query' in self.embedding_model.prompts:
|
|
|
|
| 334 |
print(" - Reranker non disponible, scores neutres retournés")
|
| 335 |
return [0.5] * len(documents)
|
| 336 |
|
| 337 |
+
# Le reranker fonctionne sur son device d'origine
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
|
| 339 |
if instruction is None:
|
| 340 |
instruction = self._get_default_instruction()
|
|
|
|
| 436 |
if self.is_zerogpu:
|
| 437 |
print("🚀 Environnement ZeroGPU détecté - optimisations cloud")
|
| 438 |
self.use_flash_attention = False # Désactiver Flash Attention temporairement sur ZeroGPU
|
| 439 |
+
# Sur ZeroGPU, utiliser CPU pour embedding/reranking, GPU seulement pour génération
|
| 440 |
elif self.is_mps and use_flash_attention:
|
| 441 |
print("🍎 Mac avec MPS détecté - désactivation automatique de Flash Attention")
|
| 442 |
self.use_flash_attention = False
|
|
|
|
| 702 |
except:
|
| 703 |
return 0.0
|
| 704 |
|
|
|
|
| 705 |
def search_documents(self, query: str, final_k: int = None, use_reranking: bool = None) -> List[Dict]:
|
| 706 |
"""
|
| 707 |
Recherche avancée avec reranking en deux étapes
|
|
|
|
| 712 |
|
| 713 |
print(f"🔍 Recherche en deux étapes: {initial_k} candidats → reranking → {k} finaux")
|
| 714 |
|
| 715 |
+
# Les modèles d'embedding fonctionnent bien sur CPU sur ZeroGPU
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
|
| 717 |
# Étape 1: Recherche par embedding avec FAISS
|
| 718 |
if hasattr(self.embedding_model, 'prompts') and 'query' in self.embedding_model.prompts:
|