Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -79,7 +79,7 @@ def get_hf_llm():
|
|
| 79 |
|
| 80 |
# --- Vector Store Functions (UPDATED) ---
|
| 81 |
def get_vector_store(_text_chunks):
|
| 82 |
-
"""Creates FAISS vector store from Document objects."""
|
| 83 |
if not _text_chunks:
|
| 84 |
st.error("No text chunks found.")
|
| 85 |
return
|
|
@@ -89,25 +89,23 @@ def get_vector_store(_text_chunks):
|
|
| 89 |
return
|
| 90 |
|
| 91 |
try:
|
| 92 |
-
#
|
| 93 |
vector_store = FAISS.from_documents(documents=_text_chunks, embedding=embeddings)
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
st.error(f"Error creating vector store: {e}")
|
| 98 |
|
| 99 |
def load_faiss_index():
|
|
|
|
| 100 |
try:
|
| 101 |
-
|
| 102 |
-
if
|
| 103 |
-
return
|
| 104 |
-
|
| 105 |
-
db = FAISS.load_local(
|
| 106 |
-
"faiss_index",
|
| 107 |
-
embeddings,
|
| 108 |
-
allow_dangerous_deserialization=True
|
| 109 |
-
)
|
| 110 |
-
return db
|
| 111 |
except Exception as e:
|
| 112 |
# Silent fail on startup if index doesn't exist yet
|
| 113 |
return None
|
|
@@ -195,12 +193,11 @@ def main():
|
|
| 195 |
st.set_page_config(page_title="Chat PDF (Hugging Face)")
|
| 196 |
st.header("Chat with PDF using mistralai/Mistral-7B-Instruct-v0.2")
|
| 197 |
|
|
|
|
| 198 |
if "faiss_db" not in st.session_state:
|
| 199 |
st.session_state.faiss_db = None
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
if st.session_state.faiss_db is None and os.path.exists("faiss_index"):
|
| 203 |
-
st.session_state.faiss_db = load_faiss_index()
|
| 204 |
|
| 205 |
with st.sidebar:
|
| 206 |
st.title("Menu:")
|
|
@@ -213,7 +210,7 @@ def main():
|
|
| 213 |
raw_documents = get_pdf_text(pdf_docs)
|
| 214 |
# 2. Split Documents (preserves metadata)
|
| 215 |
text_chunks = get_text_chunks(raw_documents)
|
| 216 |
-
# 3. Create Vector Store
|
| 217 |
get_vector_store(text_chunks)
|
| 218 |
|
| 219 |
st.session_state.faiss_db = load_faiss_index()
|
|
|
|
| 79 |
|
| 80 |
# --- Vector Store Functions (UPDATED) ---
|
| 81 |
def get_vector_store(_text_chunks):
|
| 82 |
+
"""Creates FAISS vector store from Document objects and stores in session state."""
|
| 83 |
if not _text_chunks:
|
| 84 |
st.error("No text chunks found.")
|
| 85 |
return
|
|
|
|
| 89 |
return
|
| 90 |
|
| 91 |
try:
|
| 92 |
+
# Create vector store in memory
|
| 93 |
vector_store = FAISS.from_documents(documents=_text_chunks, embedding=embeddings)
|
| 94 |
+
|
| 95 |
+
# Store in session state (persists during the session)
|
| 96 |
+
st.session_state.vector_store = vector_store
|
| 97 |
+
|
| 98 |
+
st.success("Vector store created and loaded successfully.")
|
| 99 |
except Exception as e:
|
| 100 |
st.error(f"Error creating vector store: {e}")
|
| 101 |
|
| 102 |
def load_faiss_index():
|
| 103 |
+
"""Load vector store from session state."""
|
| 104 |
try:
|
| 105 |
+
# Check if vector store exists in session state
|
| 106 |
+
if "vector_store" in st.session_state and st.session_state.vector_store is not None:
|
| 107 |
+
return st.session_state.vector_store
|
| 108 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
except Exception as e:
|
| 110 |
# Silent fail on startup if index doesn't exist yet
|
| 111 |
return None
|
|
|
|
| 193 |
st.set_page_config(page_title="Chat PDF (Hugging Face)")
|
| 194 |
st.header("Chat with PDF using mistralai/Mistral-7B-Instruct-v0.2")
|
| 195 |
|
| 196 |
+
# Initialize session state variables
|
| 197 |
if "faiss_db" not in st.session_state:
|
| 198 |
st.session_state.faiss_db = None
|
| 199 |
+
if "vector_store" not in st.session_state:
|
| 200 |
+
st.session_state.vector_store = None
|
|
|
|
|
|
|
| 201 |
|
| 202 |
with st.sidebar:
|
| 203 |
st.title("Menu:")
|
|
|
|
| 210 |
raw_documents = get_pdf_text(pdf_docs)
|
| 211 |
# 2. Split Documents (preserves metadata)
|
| 212 |
text_chunks = get_text_chunks(raw_documents)
|
| 213 |
+
# 3. Create Vector Store (stored in session state)
|
| 214 |
get_vector_store(text_chunks)
|
| 215 |
|
| 216 |
st.session_state.faiss_db = load_faiss_index()
|