Spaces:
Runtime error
Runtime error
Fix: Prevent data wipe in initialize and set correct port
Browse files21 unmodified lines222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676821 unmodified linesurl = 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)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.")# 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 clusterstry: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 Clusterstry:self.num_clusters = 8if 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 Collectionself.num_clusters = 1 # Virtual clusters onlyif 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.is_local:# Local mode doesn't support sharding_method=CUSTOMself.client.create_collection(21 unmodified lines22232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929321 unmodified linesurl = os.getenv("QDRANT_URL", ":memory:")api_key = os.getenv("QDRANT_API_KEY", None)print(f"Connecting to Qdrant at {url}...")# Relaxed connection settings for HF Spacesport = 443 if url.startswith("https") else 6333self.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("Running in local/memory mode. Custom Sharding is NOT supported. Simulating behavior.")self.num_clusters = 1if 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 deletionif self.client.collection_exists(self.collection_name):print(f"Collection '{self.collection_name}' already exists. Skipping initialization.")return# Try to create collection with full clusterstry: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 Clusterstry:self.num_clusters = 8if 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 Collectionself.num_clusters = 1if 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.")returnif self.is_local:# Local mode doesn't support sharding_method=CUSTOMself.client.create_collection(
src/vector_db.py+56-31