Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -397,43 +397,6 @@ def create_documents(data_source: str, data: List[dict]) -> List[Document]:
|
|
397 |
|
398 |
import faiss
|
399 |
|
400 |
-
import os
|
401 |
-
import numpy as np
|
402 |
-
import faiss
|
403 |
-
|
404 |
-
# Step 1: Define the path where the index will be saved
|
405 |
-
index_dir = "/home/wendy/Downloads/faiss_index"
|
406 |
-
os.makedirs(index_dir, exist_ok=True) # Create the directory if it doesn't exist
|
407 |
-
index_file_path = os.path.join(index_dir, "index.faiss")
|
408 |
-
|
409 |
-
# Step 2: Generate random data (e.g., 1000 vectors of dimension 128)
|
410 |
-
d = 128 # Vector dimensionality
|
411 |
-
n = 1000 # Number of vectors
|
412 |
-
np.random.seed(42) # For reproducibility
|
413 |
-
|
414 |
-
# Generate random vectors (uniform distribution)
|
415 |
-
xb = np.random.random((n, d)).astype('float32')
|
416 |
-
|
417 |
-
# Step 3: Create the FAISS index
|
418 |
-
# Using the L2 distance metric (Euclidean distance)
|
419 |
-
index = faiss.IndexFlatL2(d)
|
420 |
-
|
421 |
-
# Step 4: Add the vectors to the index
|
422 |
-
index.add(xb)
|
423 |
-
|
424 |
-
# Step 5: Save the index to the specified path
|
425 |
-
faiss.write_index(index, index_file_path)
|
426 |
-
|
427 |
-
print(f"FAISS index saved to: {index_file_path}")
|
428 |
-
|
429 |
-
# Step 6: Load the FAISS index from the file
|
430 |
-
loaded_index = faiss.read_index(index_file_path)
|
431 |
-
|
432 |
-
print("FAISS index loaded successfully from:", index_file_path)
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
# Custom FAISS wrapper (optional, if you still want it)
|
438 |
class MyVector_Store:
|
439 |
def __init__(self, index: faiss.Index):
|
@@ -468,7 +431,7 @@ embedding_model = HuggingFaceEmbeddings(
|
|
468 |
# -----------------------------
|
469 |
try:
|
470 |
vector_store = FAISS.load_local(
|
471 |
-
"/home/wendy/
|
472 |
embedding_model,
|
473 |
allow_dangerous_deserialization=True
|
474 |
)
|
@@ -481,7 +444,7 @@ except Exception as e:
|
|
481 |
# -----------------------------
|
482 |
# Load FAISS index properly
|
483 |
# -----------------------------
|
484 |
-
loaded_store = FAISS.load_local("/home/wendy/
|
485 |
|
486 |
|
487 |
# -----------------------------
|
|
|
397 |
|
398 |
import faiss
|
399 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
400 |
# Custom FAISS wrapper (optional, if you still want it)
|
401 |
class MyVector_Store:
|
402 |
def __init__(self, index: faiss.Index):
|
|
|
431 |
# -----------------------------
|
432 |
try:
|
433 |
vector_store = FAISS.load_local(
|
434 |
+
"/home/wendy/my_hf_agent_course_projects/faiss_index/index.faiss",
|
435 |
embedding_model,
|
436 |
allow_dangerous_deserialization=True
|
437 |
)
|
|
|
444 |
# -----------------------------
|
445 |
# Load FAISS index properly
|
446 |
# -----------------------------
|
447 |
+
loaded_store = FAISS.load_local("/home/wendy/my_hf_agent_course_projects/faiss_index", embedding_model, allow_dangerous_deserialization=True)
|
448 |
|
449 |
|
450 |
# -----------------------------
|