cache.py
· 473 B · Python
Raw
@st.cache_data
def load_index(data_version):
if not os.path.exists(INDEX_DIR) or not os.listdir(INDEX_DIR):
documents = SimpleDirectoryReader(DATA_DIR, recursive=True).load_data()
index = VectorStoreIndex.from_documents(documents)
index.storage_context.persist(persist_dir=INDEX_DIR)
else:
storage_context = StorageContext.from_defaults(persist_dir=INDEX_DIR)
index = load_index_from_storage(storage_context)
return index
1 | @st.cache_data |
2 | def load_index(data_version): |
3 | if not os.path.exists(INDEX_DIR) or not os.listdir(INDEX_DIR): |
4 | documents = SimpleDirectoryReader(DATA_DIR, recursive=True).load_data() |
5 | index = VectorStoreIndex.from_documents(documents) |
6 | index.storage_context.persist(persist_dir=INDEX_DIR) |
7 | else: |
8 | storage_context = StorageContext.from_defaults(persist_dir=INDEX_DIR) |
9 | index = load_index_from_storage(storage_context) |
10 | return index |