File size: 1,034 Bytes
9177215 7d1720e 9177215 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import streamlit as st
from streamlit.runtime.caching.hashing import HashFuncsDict
import core.parsing as parsing
import core.chunking as chunking
import core.embedding as embedding
from core.parsing import File
def file_hash_func(file: File) -> str:
"""Get a unique hash for a file"""
return file.id
@st.cache_data(show_spinner=False)
def bootstrap_caching():
"""Patch module functions with caching"""
# Get all substypes of File from module
file_subtypes = [
cls
for cls in vars(parsing).values()
if isinstance(cls, type) and issubclass(cls, File) and cls != File
]
file_hash_funcs: HashFuncsDict = {cls: file_hash_func for cls in file_subtypes}
parsing.read_file = st.cache_data(show_spinner=False)(parsing.read_file)
chunking.chunk_file = st.cache_data(show_spinner=False, hash_funcs=file_hash_funcs)(
chunking.chunk_file
)
embedding.embed_files = st.cache_data(
show_spinner=False, hash_funcs=file_hash_funcs
)(embedding.embed_files)
|