KalooAI commited on
Commit
765eae2
·
verified ·
1 Parent(s): 340d4e8

Update app.py

Browse files

def remove_pdf(filename, state):
if filename not in state["files"]:
return f"{filename} not found.", state, list(state["files"].keys())

# Remove its chunks
chunks_to_remove = state["files"].pop(filename)

# Remove from global chunk list
state["chunks"] = [c for c in state["chunks"] if c not in chunks_to_remove]

# Rebuild FAISS
if state["chunks"]:
index, _ = build_vectorstore(state["chunks"])
state["index"] = index
else:
state["index"] = None

state["pdf_count"] -= 1

return (
f"Removed {filename}. Total chunks: {len(state['chunks'])}",
state,
list(state["files"].keys())
)

Files changed (1) hide show
  1. app.py +23 -0
app.py CHANGED
@@ -176,7 +176,30 @@ def load_pdf(files, state):
176
  except Exception as e:
177
  return f"Error processing PDF: {e}", state
178
 
 
 
 
179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
 
181
  def answer_question(question, state):
182
 
 
176
  except Exception as e:
177
  return f"Error processing PDF: {e}", state
178
 
179
+ def remove_pdf(filename, state):
180
+ if filename not in state["files"]:
181
+ return f"{filename} not found.", state, list(state["files"].keys())
182
 
183
+ # Remove its chunks
184
+ chunks_to_remove = state["files"].pop(filename)
185
+
186
+ # Remove from global chunk list
187
+ state["chunks"] = [c for c in state["chunks"] if c not in chunks_to_remove]
188
+
189
+ # Rebuild FAISS
190
+ if state["chunks"]:
191
+ index, _ = build_vectorstore(state["chunks"])
192
+ state["index"] = index
193
+ else:
194
+ state["index"] = None
195
+
196
+ state["pdf_count"] -= 1
197
+
198
+ return (
199
+ f"Removed {filename}. Total chunks: {len(state['chunks'])}",
200
+ state,
201
+ list(state["files"].keys())
202
+ )
203
 
204
  def answer_question(question, state):
205