Spaces:
Running
Update app.py
Browse filesdef remove_pdf(filename, state):
if filename not in state["files"]:
return (
f"{filename} not found.",
state,
gr.update(choices=list(state["files"].keys()), value=None)
)
# Remove the file entry
state["files"].pop(filename, None)
# Rebuild chunks from remaining files
all_chunks = []
for chunks in state["files"].values():
all_chunks.extend(chunks)
state["chunks"] = all_chunks
state["pdf_count"] = len(state["files"])
if state["chunks"]:
index, _ = build_vectorstore(state["chunks"])
state["index"] = index
else:
state["index"] = None
return (
f"Removed {filename}. Total chunks: {len(state['chunks'])}",
state,
gr.update(choices=list(state["files"].keys()), value=None)
)
|
@@ -147,15 +147,35 @@ def generate_answer(question, context):
|
|
| 147 |
# ============================================================
|
| 148 |
|
| 149 |
def load_pdf(files, state):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
if not files:
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
try:
|
| 154 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
unique_files = list(dict.fromkeys(files))
|
| 156 |
|
| 157 |
for file in unique_files:
|
| 158 |
-
|
|
|
|
|
|
|
| 159 |
|
| 160 |
text = read_pdf(file)
|
| 161 |
new_chunks = chunk_text(text)
|
|
@@ -174,39 +194,45 @@ def load_pdf(files, state):
|
|
| 174 |
return (
|
| 175 |
f"{state['pdf_count']} PDF(s) loaded. Total chunks: {len(state['chunks'])}",
|
| 176 |
state,
|
| 177 |
-
list(state["files"].keys()) #
|
| 178 |
)
|
| 179 |
|
| 180 |
except Exception as e:
|
| 181 |
return (
|
| 182 |
f"Error processing PDF: {e}",
|
| 183 |
state,
|
| 184 |
-
list(state["files"].keys())
|
| 185 |
)
|
| 186 |
|
| 187 |
def remove_pdf(filename, state):
|
| 188 |
if filename not in state["files"]:
|
| 189 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
-
#
|
| 192 |
-
|
|
|
|
|
|
|
| 193 |
|
| 194 |
-
|
| 195 |
-
state["
|
| 196 |
|
| 197 |
-
# Rebuild FAISS
|
| 198 |
if state["chunks"]:
|
| 199 |
index, _ = build_vectorstore(state["chunks"])
|
| 200 |
state["index"] = index
|
| 201 |
else:
|
| 202 |
state["index"] = None
|
| 203 |
|
| 204 |
-
state["pdf_count"] -= 1
|
| 205 |
-
|
| 206 |
return (
|
| 207 |
f"Removed {filename}. Total chunks: {len(state['chunks'])}",
|
| 208 |
state,
|
| 209 |
-
list(state["files"].keys())
|
| 210 |
)
|
| 211 |
|
| 212 |
def answer_question(question, state):
|
|
|
|
| 147 |
# ============================================================
|
| 148 |
|
| 149 |
def load_pdf(files, state):
|
| 150 |
+
# files is the *current* list from the UI (after add/remove)
|
| 151 |
+
# We rebuild everything from scratch to stay in sync.
|
| 152 |
+
|
| 153 |
+
# If user cleared all files
|
| 154 |
if not files:
|
| 155 |
+
state["chunks"] = []
|
| 156 |
+
state["files"] = {}
|
| 157 |
+
state["pdf_count"] = 0
|
| 158 |
+
state["index"] = None
|
| 159 |
+
|
| 160 |
+
return (
|
| 161 |
+
"No file uploaded.",
|
| 162 |
+
state,
|
| 163 |
+
gr.update(choices=[], value=None) # empty dropdown
|
| 164 |
+
)
|
| 165 |
|
| 166 |
try:
|
| 167 |
+
# Reset state and rebuild from current files only
|
| 168 |
+
state["chunks"] = []
|
| 169 |
+
state["files"] = {}
|
| 170 |
+
state["pdf_count"] = 0
|
| 171 |
+
|
| 172 |
+
# Deduplicate while preserving order
|
| 173 |
unique_files = list(dict.fromkeys(files))
|
| 174 |
|
| 175 |
for file in unique_files:
|
| 176 |
+
# In Gradio 6, gr.Files usually gives you temp paths (strings)
|
| 177 |
+
# PdfReader accepts a path or file-like object, so we can pass `file` directly.
|
| 178 |
+
filename = os.path.basename(str(file))
|
| 179 |
|
| 180 |
text = read_pdf(file)
|
| 181 |
new_chunks = chunk_text(text)
|
|
|
|
| 194 |
return (
|
| 195 |
f"{state['pdf_count']} PDF(s) loaded. Total chunks: {len(state['chunks'])}",
|
| 196 |
state,
|
| 197 |
+
gr.update(choices=list(state["files"].keys()), value=None) # <-- key fix
|
| 198 |
)
|
| 199 |
|
| 200 |
except Exception as e:
|
| 201 |
return (
|
| 202 |
f"Error processing PDF: {e}",
|
| 203 |
state,
|
| 204 |
+
gr.update(choices=list(state["files"].keys()), value=None)
|
| 205 |
)
|
| 206 |
|
| 207 |
def remove_pdf(filename, state):
|
| 208 |
if filename not in state["files"]:
|
| 209 |
+
return (
|
| 210 |
+
f"{filename} not found.",
|
| 211 |
+
state,
|
| 212 |
+
gr.update(choices=list(state["files"].keys()), value=None)
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
# Remove the file entry
|
| 216 |
+
state["files"].pop(filename, None)
|
| 217 |
|
| 218 |
+
# Rebuild chunks from remaining files
|
| 219 |
+
all_chunks = []
|
| 220 |
+
for chunks in state["files"].values():
|
| 221 |
+
all_chunks.extend(chunks)
|
| 222 |
|
| 223 |
+
state["chunks"] = all_chunks
|
| 224 |
+
state["pdf_count"] = len(state["files"])
|
| 225 |
|
|
|
|
| 226 |
if state["chunks"]:
|
| 227 |
index, _ = build_vectorstore(state["chunks"])
|
| 228 |
state["index"] = index
|
| 229 |
else:
|
| 230 |
state["index"] = None
|
| 231 |
|
|
|
|
|
|
|
| 232 |
return (
|
| 233 |
f"Removed {filename}. Total chunks: {len(state['chunks'])}",
|
| 234 |
state,
|
| 235 |
+
gr.update(choices=list(state["files"].keys()), value=None)
|
| 236 |
)
|
| 237 |
|
| 238 |
def answer_question(question, state):
|