Spaces:
Runtime error
Runtime error
Fix: Prevent data wipe in initialize and set correct port
Browse files21 unmodified lines2223242525262728293031323334353637383929404142434445464748495051525354313233343555363738394041425644454657585960616263646566674849505152535455565758596061686970717273747576777879808182838485868788899091929321 unmodified lines url = os.getenv("QDRANT_URL", ":memory:") api_key = os.getenv("QDRANT_API_KEY", None) print(f"Connecting to Qdrant at {url}...") self.client = QdrantClient(location=url, api_key=api_key, timeout=60) # Relaxed connection settings for HF Spaces port = 443 if url.startswith("https") else 6333 self.client = QdrantClient( location=url, port=port, api_key=api_key, timeout=60, check_compatibility=False, verify=False # Passed to httpx ) self.is_local = url == ":memory:" or not url.startswith("http") if self.is_local: print("WARNING: Running in local/memory mode. Custom Sharding is NOT supported. Simulating behavior.") print("Running in local/memory mode. Custom Sharding is NOT supported. Simulating behavior.") self.num_clusters = 1 if self.client.collection_exists(collection_name=self.collection_name): self.client.delete_collection(collection_name=self.collection_name) self.client.create_collection( collection_name=self.collection_name, vectors_config=VectorParams(size=self.vector_size, distance=Distance.COSINE) ) print(f"Created standard collection '{self.collection_name}'.") else: # Check if exists first to avoid accidental deletion if self.client.collection_exists(self.collection_name): print(f"Collection '{self.collection_name}' already exists. Skipping initialization.") return
# Check if collection exists, if so, recreate it for a clean slate (or handle gracefully) if self.client.collection_exists(self.collection_name): self.client.delete_collection(self.collection_name) # Try to create collection with full clusters # Try to create collection with full clusters try: self._create_collection_and_shards(self.num_clusters) print(f"Successfully created collection with {self.num_clusters} clusters.") except Exception as e: print(f"Failed to create {self.num_clusters} clusters: {e}") print("Attempting fallback to 8 clusters (Free Tier limit mitigation)...") # Fallback 1: 8 Clusters try: self.num_clusters = 8 if self.client.collection_exists(self.collection_name): self.client.delete_collection(self.collection_name) self._create_collection_and_shards(self.num_clusters) print(f"Successfully created collection with {self.num_clusters} clusters.") except Exception as e: print(f"Failed to create {self.num_clusters} clusters: {e}") print("Attempting fallback to 8 clusters (Free Tier limit mitigation)...") # Fallback 1: 8 Clusters try: self.num_clusters = 8 if self.client.collection_exists(self.collection_name): self.client.delete_collection(self.collection_name) self._create_collection_and_shards(self.num_clusters) print(f"Fallback successful: Created collection with {self.num_clusters} clusters.") except Exception as e2: print(f"Failed to create 8 clusters: {e2}") print("CRITICAL: Custom Sharding not supported. Falling back to Standard Collection (No Sharding).") # Fallback 2: Standard Collection self.num_clusters = 1 # Virtual clusters only if self.client.collection_exists(self.collection_name): self.client.delete_collection(self.collection_name) self.client.create_collection( collection_name=self.collection_name, vectors_config=VectorParams(size=self.vector_size, distance=Distance.COSINE) ) print("Fallback successful: Created Standard Collection.") print(f"Fallback successful: Created collection with {self.num_clusters} clusters.") except Exception as e2: print(f"Failed to create 8 clusters: {e2}") print("CRITICAL: Custom Sharding not supported. Falling back to Standard Collection (No Sharding).") # Fallback 2: Standard Collection self.num_clusters = 1 if self.client.collection_exists(self.collection_name): self.client.delete_collection(self.collection_name) self.client.create_collection( collection_name=self.collection_name, vectors_config=VectorParams(size=self.vector_size, distance=Distance.COSINE) ) print("Fallback successful: Created Standard Collection.")
def _create_collection_and_shards(self, n_clusters): print(f"Creating collection '{self.collection_name}' with custom sharding ({n_clusters} clusters)...") if self.client.collection_exists(self.collection_name): print(f"Collection '{self.collection_name}' already exists. Skipping creation.") return
if self.is_local: # Local mode doesn't support sharding_method=CUSTOM self.client.create_collection(src/vector_db.py+56-31