Rulga commited on
Commit
0fbb073
·
verified ·
1 Parent(s): 56fd21a

Update app.py

Browse files

Knowledge base creation failed: [Errno 2] No such file or directory: 'index.faiss'

Files changed (1) hide show
  1. app.py +32 -5
app.py CHANGED
@@ -90,6 +90,10 @@ def build_knowledge_base(_embeddings):
90
  documents = []
91
 
92
  with st.status("Building knowledge base..."):
 
 
 
 
93
  for url in URLS:
94
  try:
95
  loader = WebBaseLoader(url)
@@ -98,28 +102,51 @@ def build_knowledge_base(_embeddings):
98
  st.write(f"✓ Loaded {url}")
99
  except Exception as e:
100
  st.error(f"Failed to load {url}: {str(e)}")
 
 
 
 
 
101
 
 
102
  text_splitter = RecursiveCharacterTextSplitter(
103
  chunk_size=500,
104
  chunk_overlap=100
105
  )
106
  chunks = text_splitter.split_documents(documents)
107
 
 
108
  vector_store = FAISS.from_documents(chunks, _embeddings)
109
- vector_store.save_local(VECTOR_STORE_PATH)
 
 
 
110
 
111
- # Update knowledge base info
 
 
 
 
112
  st.session_state.kb_info.update({
113
  'build_time': time.time() - start_time,
114
- 'size': sum(os.path.getsize(f) for f in os.listdir(VECTOR_STORE_PATH)) / (1024 ** 2),
 
 
 
115
  'version': datetime.now().strftime("%Y%m%d-%H%M%S")
116
  })
117
 
118
- return vector_store
 
 
119
  except Exception as e:
120
  st.error(f"Knowledge base creation failed: {str(e)}")
 
 
 
 
 
121
  st.stop()
122
-
123
  # --------------- Main Application ---------------
124
  def main():
125
  # Initialize session state first
 
90
  documents = []
91
 
92
  with st.status("Building knowledge base..."):
93
+ # Создаем папку заранее
94
+ os.makedirs(VECTOR_STORE_PATH, exist_ok=True)
95
+
96
+ # Загрузка документов
97
  for url in URLS:
98
  try:
99
  loader = WebBaseLoader(url)
 
102
  st.write(f"✓ Loaded {url}")
103
  except Exception as e:
104
  st.error(f"Failed to load {url}: {str(e)}")
105
+ continue # Продолжаем при ошибках загрузки
106
+
107
+ if not documents:
108
+ st.error("No documents loaded!")
109
+ return None
110
 
111
+ # Разделение на чанки
112
  text_splitter = RecursiveCharacterTextSplitter(
113
  chunk_size=500,
114
  chunk_overlap=100
115
  )
116
  chunks = text_splitter.split_documents(documents)
117
 
118
+ # Явное сохранение
119
  vector_store = FAISS.from_documents(chunks, _embeddings)
120
+ vector_store.save_local(
121
+ folder_path=VECTOR_STORE_PATH,
122
+ index_name="index"
123
+ )
124
 
125
+ # Проверка создания файлов
126
+ if not os.path.exists(os.path.join(VECTOR_STORE_PATH, "index.faiss")):
127
+ raise RuntimeError("FAISS index file not created!")
128
+
129
+ # Обновление информации
130
  st.session_state.kb_info.update({
131
  'build_time': time.time() - start_time,
132
+ 'size': sum(
133
+ os.path.getsize(os.path.join(VECTOR_STORE_PATH, f))
134
+ for f in ["index.faiss", "index.pkl"]
135
+ ) / (1024 ** 2),
136
  'version': datetime.now().strftime("%Y%m%d-%H%M%S")
137
  })
138
 
139
+ st.success("Knowledge base successfully created!")
140
+ return vector_store
141
+
142
  except Exception as e:
143
  st.error(f"Knowledge base creation failed: {str(e)}")
144
+ # Отладочная информация
145
+ st.write("Debug info:")
146
+ st.write(f"Documents loaded: {len(documents)}")
147
+ st.write(f"Chunks created: {len(chunks) if 'chunks' in locals() else 0}")
148
+ st.write(f"Vector store path exists: {os.path.exists(VECTOR_STORE_PATH)}")
149
  st.stop()
 
150
  # --------------- Main Application ---------------
151
  def main():
152
  # Initialize session state first