Anne31415 commited on
Commit
7f3e938
·
verified ·
1 Parent(s): e9402a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -9
app.py CHANGED
@@ -50,19 +50,20 @@ api_key = os.getenv("OPENAI_API_KEY")
50
 
51
 
52
 
53
- # Updated caching mechanism using st.cache_data
54
- @st.cache_data(persist="disk") # Using persist="disk" to save cache across sessions
55
  def load_vector_store(file_path, store_name, force_reload=False):
56
- vector_store_path = f"{store_name}.pkl"
 
57
 
58
  # Check if vector store already exists and force_reload is False
59
  if not force_reload and os.path.exists(vector_store_path):
60
  with open(vector_store_path, "rb") as f:
61
  VectorStore = pickle.load(f)
 
62
  else:
63
  # Load and process the PDF, then create the vector store
64
- text_splitter = RecursiveCharacterTextSplitter(
65
- chunk_size=1000, chunk_overlap=200, length_function=len)
66
  text = load_pdf_text(file_path)
67
  chunks = text_splitter.split_text(text=text)
68
  embeddings = OpenAIEmbeddings()
@@ -71,11 +72,23 @@ def load_vector_store(file_path, store_name, force_reload=False):
71
  # Serialize the vector store
72
  with open(vector_store_path, "wb") as f:
73
  pickle.dump(VectorStore, f)
 
74
 
75
- # Commit and push changes to the repository
76
- repo.git_add(vector_store_path)
77
- repo.git_commit(f"Update vector store: {store_name}")
78
- repo.git_push()
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  return VectorStore
81
 
 
50
 
51
 
52
 
53
+ # Updated load_vector_store function with Streamlit text outputs and directory handling for Git
54
+ @st.cache_data(persist="disk")
55
  def load_vector_store(file_path, store_name, force_reload=False):
56
+ local_repo_path = "Private_Book"
57
+ vector_store_path = os.path.join(local_repo_path, f"{store_name}.pkl")
58
 
59
  # Check if vector store already exists and force_reload is False
60
  if not force_reload and os.path.exists(vector_store_path):
61
  with open(vector_store_path, "rb") as f:
62
  VectorStore = pickle.load(f)
63
+ st.text(f"Loaded existing vector store from {vector_store_path}")
64
  else:
65
  # Load and process the PDF, then create the vector store
66
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200, length_function=len)
 
67
  text = load_pdf_text(file_path)
68
  chunks = text_splitter.split_text(text=text)
69
  embeddings = OpenAIEmbeddings()
 
72
  # Serialize the vector store
73
  with open(vector_store_path, "wb") as f:
74
  pickle.dump(VectorStore, f)
75
+ st.text(f"Created and saved vector store at {vector_store_path}")
76
 
77
+ # Change working directory for Git operations
78
+ original_dir = os.getcwd()
79
+ os.chdir(local_repo_path)
80
+
81
+ try:
82
+ # Commit and push changes to the repository
83
+ repo.git_add(vector_store_path) # Stage the file for commit
84
+ repo.git_commit(f"Update vector store: {store_name}") # Commit the file
85
+ repo.git_push() # Push the commit to the remote repository
86
+ st.text("Committed and pushed vector store to repository.")
87
+ except Exception as e:
88
+ st.error(f"Error during Git operations: {e}")
89
+ finally:
90
+ # Change back to the original directory
91
+ os.chdir(original_dir)
92
 
93
  return VectorStore
94