Ajaykanth Maddi commited on
Commit ·
f39aedc
1
Parent(s): 85a6900
Code Refactoring
Browse files- ragbench.py +47 -0
ragbench.py
CHANGED
|
@@ -150,6 +150,48 @@ class RAGSystem:
|
|
| 150 |
except Exception as e:
|
| 151 |
print(f"Error downloading {filename}: {e}")
|
| 152 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
def _download_FAISS_files(self, folder_path: str):
|
| 155 |
"""Download all required FAISS files from Hugging Face hub."""
|
|
@@ -170,6 +212,8 @@ class RAGSystem:
|
|
| 170 |
if file_path:
|
| 171 |
downloaded_files.append(file_path)
|
| 172 |
print(f"Downloaded: {filename} → {file_path}")
|
|
|
|
|
|
|
| 173 |
|
| 174 |
# Get the common directory
|
| 175 |
index_dir = os.path.dirname(downloaded_files[0])
|
|
@@ -221,7 +265,10 @@ class RAGSystem:
|
|
| 221 |
else:
|
| 222 |
print(f"Failed to load FAISS index from {faissFolderPath}, so load from documents")
|
| 223 |
if bChunkFileAvailable:
|
|
|
|
| 224 |
self.vector_store = FAISS.from_documents(langchain_docs, self.embedder)
|
|
|
|
|
|
|
| 225 |
|
| 226 |
|
| 227 |
# # 1. Define all required files
|
|
|
|
| 150 |
except Exception as e:
|
| 151 |
print(f"Error downloading {filename}: {e}")
|
| 152 |
return None
|
| 153 |
+
|
| 154 |
+
def _upload_file(self, filename: str, folder_path: str) -> str:
|
| 155 |
+
"""Upload a file to Hugging Face hub from the specified folder."""
|
| 156 |
+
try:
|
| 157 |
+
file_path = os.path.join(folder_path, filename)
|
| 158 |
+
if not os.path.exists(file_path):
|
| 159 |
+
raise FileNotFoundError(f"File {file_path} does not exist.")
|
| 160 |
+
|
| 161 |
+
api = HfApi()
|
| 162 |
+
api.upload_file(
|
| 163 |
+
path_or_fileobj=file_path,
|
| 164 |
+
path_in_repo=filename,
|
| 165 |
+
repo_id=HF_DATASET_REPO_NAME,
|
| 166 |
+
repo_type=HF_REPO_TYPE,
|
| 167 |
+
token=os.getenv("HF_TOKEN")
|
| 168 |
+
)
|
| 169 |
+
print(f"Uploaded {filename} to {HF_DATASET_REPO_NAME}")
|
| 170 |
+
return file_path
|
| 171 |
+
except Exception as e:
|
| 172 |
+
print(f"Error uploading {filename}: {e}")
|
| 173 |
+
return None
|
| 174 |
+
|
| 175 |
+
def _store_faiss_files(self, folder_path: str):
|
| 176 |
+
"""Store FAISS index files to Hugging Face hub."""
|
| 177 |
+
try:
|
| 178 |
+
# Ensure the folder exists
|
| 179 |
+
if not os.path.exists(folder_path):
|
| 180 |
+
os.makedirs(folder_path)
|
| 181 |
+
|
| 182 |
+
# Save the FAISS index locally
|
| 183 |
+
self.vector_store.save_local(folder_path)
|
| 184 |
+
|
| 185 |
+
# Upload required files to Hugging Face hub
|
| 186 |
+
for filename in ["index.faiss", "index.pkl"]:
|
| 187 |
+
file_path = os.path.join(folder_path, filename)
|
| 188 |
+
if os.path.exists(file_path):
|
| 189 |
+
self._upload_file(filename, folder_path)
|
| 190 |
+
else:
|
| 191 |
+
print(f"File {file_path} does not exist, skipping upload.")
|
| 192 |
+
|
| 193 |
+
except Exception as e:
|
| 194 |
+
print(f"Error storing FAISS files: {e}")
|
| 195 |
|
| 196 |
def _download_FAISS_files(self, folder_path: str):
|
| 197 |
"""Download all required FAISS files from Hugging Face hub."""
|
|
|
|
| 212 |
if file_path:
|
| 213 |
downloaded_files.append(file_path)
|
| 214 |
print(f"Downloaded: {filename} → {file_path}")
|
| 215 |
+
else:
|
| 216 |
+
return False # If any file fails to download, return False
|
| 217 |
|
| 218 |
# Get the common directory
|
| 219 |
index_dir = os.path.dirname(downloaded_files[0])
|
|
|
|
| 265 |
else:
|
| 266 |
print(f"Failed to load FAISS index from {faissFolderPath}, so load from documents")
|
| 267 |
if bChunkFileAvailable:
|
| 268 |
+
print(f"Building FAISS index from downloaded chunks")
|
| 269 |
self.vector_store = FAISS.from_documents(langchain_docs, self.embedder)
|
| 270 |
+
print(f"FAISS index built successfully from chunks, saving to {faissFolderPath}")
|
| 271 |
+
self._store_faiss_files(faissFolderPath)
|
| 272 |
|
| 273 |
|
| 274 |
# # 1. Define all required files
|