Penality commited on
Commit
a5f4249
·
verified ·
1 Parent(s): 053606e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -86,10 +86,19 @@ def retrieve_document(query):
86
  print(f"Error fetching metadata: {metadata_.status_code} - {metadata_.text}")
87
  return None
88
 
 
 
 
 
 
 
89
  try:
90
- embeddings_file = embeddings_.json()
91
- except requests.exceptions.JSONDecodeError as e:
92
- print(f"Error decoding embeddings JSON: {e}")
 
 
 
93
  return None
94
 
95
  try:
@@ -98,13 +107,12 @@ def retrieve_document(query):
98
  print(f"Error decoding metadata JSON: {e}")
99
  return None
100
 
101
- print(embeddings_file, metadata_file)
102
 
103
  # Generate query embedding
104
  query_embedding = embedding_model.encode([query]).astype(np.float32)
105
 
106
  # Search for the closest document in FAISS index
107
- index = faiss.read_index(embeddings_file)
108
  _, closest_idx = index.search(query_embedding, 1)
109
 
110
  with open(metadata_file, "r") as f:
 
86
  print(f"Error fetching metadata: {metadata_.status_code} - {metadata_.text}")
87
  return None
88
 
89
+ # Save the FAISS file temporarily
90
+ with tempfile.NamedTemporaryFile(delete=False) as temp_file:
91
+ for chunk in embeddings_.iter_content(chunk_size=8192):
92
+ temp_file.write(chunk)
93
+ temp_path = temp_file.name # Store file path
94
+
95
  try:
96
+ # Load FAISS index from file
97
+ index = faiss.read_index(embeddings_file)
98
+ print(f"Successfully loaded FAISS index with {index.ntotal} vectors.")
99
+
100
+ except Exception as e:
101
+ print(f"Error loading FAISS index: {e}")
102
  return None
103
 
104
  try:
 
107
  print(f"Error decoding metadata JSON: {e}")
108
  return None
109
 
110
+ print(index, metadata_file)
111
 
112
  # Generate query embedding
113
  query_embedding = embedding_model.encode([query]).astype(np.float32)
114
 
115
  # Search for the closest document in FAISS index
 
116
  _, closest_idx = index.search(query_embedding, 1)
117
 
118
  with open(metadata_file, "r") as f: