Spaces:
Sleeping
Sleeping
Update stock_vector_db.py
Browse files- stock_vector_db.py +61 -91
stock_vector_db.py
CHANGED
|
@@ -1,106 +1,76 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
from langchain.vectorstores import FAISS
|
| 6 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
-
from langchain.schema import Document
|
| 8 |
import streamlit as st
|
| 9 |
|
| 10 |
-
class
|
| 11 |
-
def __init__(self,
|
| 12 |
-
self.
|
| 13 |
-
self.
|
| 14 |
-
|
| 15 |
-
self.embedding_model =
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
st.write(f"π Loading existing FAISS index from '{index_path}'")
|
| 20 |
-
self.index = FAISS.load_local(index_path, self.embedding_model)
|
| 21 |
-
else:
|
| 22 |
-
st.write(f"π No FAISS index found. Will create when documents are added.")
|
| 23 |
-
self.index = None # Delay creation
|
| 24 |
-
|
| 25 |
-
# Load or initialize log
|
| 26 |
-
if os.path.exists(self.log_path):
|
| 27 |
-
with open(self.log_path, "r") as f:
|
| 28 |
-
try:
|
| 29 |
-
self.log_data = json.load(f)
|
| 30 |
-
except json.JSONDecodeError:
|
| 31 |
-
self.log_data = []
|
| 32 |
-
else:
|
| 33 |
-
self.log_data = []
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
docs = []
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
}
|
| 50 |
-
docs.append(Document(page_content=content, metadata=metadata))
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
})
|
| 60 |
-
except KeyError as e:
|
| 61 |
-
st.write(f"β Skipping malformed stock data: {e}")
|
| 62 |
-
continue
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
self.save_index()
|
| 71 |
-
self.save_log()
|
| 72 |
-
st.write(f"β
Stored {len(docs)} documents for {formatted_date}")
|
| 73 |
else:
|
| 74 |
-
st.write("β οΈ No
|
| 75 |
|
| 76 |
-
def
|
| 77 |
-
|
| 78 |
-
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
with open(self.log_path, "w") as f:
|
| 82 |
-
json.dump(self.log_data, f, indent=2)
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
st.write("β οΈ FAISS index is empty.")
|
| 87 |
-
return []
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
st.write(res.page_content)
|
| 94 |
-
st.write("-" * 80)
|
| 95 |
-
return results
|
| 96 |
|
| 97 |
-
def
|
| 98 |
-
if
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
self.index
|
| 106 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import shutil
|
| 3 |
+
from huggingface_hub import hf_hub_download, Repository
|
| 4 |
+
from langchain_community.vectorstores.faiss import FAISS
|
|
|
|
|
|
|
|
|
|
| 5 |
import streamlit as st
|
| 6 |
|
| 7 |
+
class HFVectorDB:
|
| 8 |
+
def __init__(self, hf_repo_id, hf_token, local_index_dir="/tmp/vector_index", embedding_model=None):
|
| 9 |
+
self.hf_repo_id = hf_repo_id
|
| 10 |
+
self.hf_token = hf_token
|
| 11 |
+
self.local_index_dir = local_index_dir
|
| 12 |
+
self.embedding_model = embedding_model
|
| 13 |
|
| 14 |
+
os.makedirs(self.local_index_dir, exist_ok=True)
|
| 15 |
+
self.index = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Download index files from HF repo (if exist)
|
| 18 |
+
self._download_index_files()
|
| 19 |
+
self._load_index()
|
|
|
|
| 20 |
|
| 21 |
+
def _download_index_files(self):
|
| 22 |
+
try:
|
| 23 |
+
faiss_path = hf_hub_download(repo_id=self.hf_repo_id, filename="index.faiss", use_auth_token=self.hf_token)
|
| 24 |
+
pkl_path = hf_hub_download(repo_id=self.hf_repo_id, filename="index.pkl", use_auth_token=self.hf_token)
|
| 25 |
+
shutil.copy(faiss_path, os.path.join(self.local_index_dir, "index.faiss"))
|
| 26 |
+
shutil.copy(pkl_path, os.path.join(self.local_index_dir, "index.pkl"))
|
| 27 |
+
st.write("β
Downloaded FAISS index files from HF repo")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
st.write(f"β οΈ Could not download FAISS index files: {e}")
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
def _load_index(self):
|
| 32 |
+
try:
|
| 33 |
+
self.index = FAISS.load_local(self.local_index_dir, self.embedding_model)
|
| 34 |
+
st.write("β
Loaded FAISS index from local")
|
| 35 |
+
except Exception:
|
| 36 |
+
st.write("βΉοΈ No local FAISS index found, starting with empty index")
|
| 37 |
+
self.index = None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
def save_index(self):
|
| 40 |
+
if self.index is not None:
|
| 41 |
+
self.index.save_local(self.local_index_dir)
|
| 42 |
+
st.write("β
Saved FAISS index locally")
|
| 43 |
+
self._upload_index_files()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
else:
|
| 45 |
+
st.write("β οΈ No FAISS index to save")
|
| 46 |
|
| 47 |
+
def _upload_index_files(self):
|
| 48 |
+
repo_local_path = "/tmp/hf_dataset_repo_clone"
|
| 49 |
+
if os.path.exists(repo_local_path):
|
| 50 |
+
shutil.rmtree(repo_local_path)
|
| 51 |
|
| 52 |
+
repo = Repository(local_dir=repo_local_path, clone_from=self.hf_repo_id, use_auth_token=self.hf_token)
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
shutil.copy(os.path.join(self.local_index_dir, "index.faiss"), os.path.join(repo_local_path, "index.faiss"))
|
| 55 |
+
shutil.copy(os.path.join(self.local_index_dir, "index.pkl"), os.path.join(repo_local_path, "index.pkl"))
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
repo.git_add(auto_lfs_track=True)
|
| 58 |
+
repo.git_commit("Update FAISS index files")
|
| 59 |
+
repo.git_push()
|
| 60 |
+
st.write("β
Uploaded FAISS index files to HF repo")
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
def add_documents(self, docs):
|
| 63 |
+
if self.index is None:
|
| 64 |
+
self.index = FAISS.from_documents(docs, self.embedding_model)
|
| 65 |
+
st.write("β
Created new FAISS index")
|
| 66 |
+
else:
|
| 67 |
+
self.index.add_documents(docs)
|
| 68 |
+
st.write("β
Added documents to FAISS index")
|
| 69 |
|
| 70 |
+
self.save_index()
|
| 71 |
+
|
| 72 |
+
def similarity_search(self, query, k=5):
|
| 73 |
+
if self.index is None:
|
| 74 |
+
st.write("β οΈ No index found, returning empty results")
|
| 75 |
+
return []
|
| 76 |
+
return self.index.similarity_search(query, k=k)
|