Spaces:
Running
Update app.py
Browse filesdef load_pdf(files, state):
if not files:
return "No file uploaded.", state, list(state["files"].keys())
try:
for file in files:
filename = file.name
text = read_pdf(file)
new_chunks = chunk_text(text)
if not new_chunks:
continue
# Save chunks for this file
state["files"][filename] = new_chunks
# Add to global chunk list
state["chunks"].extend(new_chunks)
state["pdf_count"] += 1
# Rebuild FAISS
index, _ = build_vectorstore(state["chunks"])
state["index"] = index
return (
f"{state['pdf_count']} PDF(s) loaded. Total chunks: {len(state['chunks'])}",
state,
list(state["files"].keys())
)
except Exception as e:
return (
f"Error processing PDF: {e}",
state,
list(state["files"].keys())
)
|
@@ -147,34 +147,43 @@ def generate_answer(question, context):
|
|
| 147 |
# ============================================================
|
| 148 |
|
| 149 |
def load_pdf(files, state):
|
| 150 |
-
print("FILES RECEIVED:", files)
|
| 151 |
-
print("TYPE:", type(files))
|
| 152 |
-
|
| 153 |
if not files:
|
| 154 |
-
return "No file uploaded.", state
|
| 155 |
|
| 156 |
try:
|
| 157 |
for file in files:
|
|
|
|
|
|
|
| 158 |
text = read_pdf(file)
|
| 159 |
new_chunks = chunk_text(text)
|
| 160 |
|
| 161 |
if not new_chunks:
|
| 162 |
continue
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
state["chunks"].extend(new_chunks)
|
|
|
|
| 165 |
state["pdf_count"] += 1
|
| 166 |
|
| 167 |
-
# Rebuild
|
| 168 |
index, _ = build_vectorstore(state["chunks"])
|
| 169 |
state["index"] = index
|
| 170 |
|
| 171 |
return (
|
| 172 |
f"{state['pdf_count']} PDF(s) loaded. Total chunks: {len(state['chunks'])}",
|
| 173 |
state,
|
|
|
|
| 174 |
)
|
| 175 |
|
| 176 |
except Exception as e:
|
| 177 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
def remove_pdf(filename, state):
|
| 180 |
if filename not in state["files"]:
|
|
|
|
| 147 |
# ============================================================
|
| 148 |
|
| 149 |
def load_pdf(files, state):
|
|
|
|
|
|
|
|
|
|
| 150 |
if not files:
|
| 151 |
+
return "No file uploaded.", state, list(state["files"].keys())
|
| 152 |
|
| 153 |
try:
|
| 154 |
for file in files:
|
| 155 |
+
filename = file.name
|
| 156 |
+
|
| 157 |
text = read_pdf(file)
|
| 158 |
new_chunks = chunk_text(text)
|
| 159 |
|
| 160 |
if not new_chunks:
|
| 161 |
continue
|
| 162 |
|
| 163 |
+
# Save chunks for this file
|
| 164 |
+
state["files"][filename] = new_chunks
|
| 165 |
+
|
| 166 |
+
# Add to global chunk list
|
| 167 |
state["chunks"].extend(new_chunks)
|
| 168 |
+
|
| 169 |
state["pdf_count"] += 1
|
| 170 |
|
| 171 |
+
# Rebuild FAISS
|
| 172 |
index, _ = build_vectorstore(state["chunks"])
|
| 173 |
state["index"] = index
|
| 174 |
|
| 175 |
return (
|
| 176 |
f"{state['pdf_count']} PDF(s) loaded. Total chunks: {len(state['chunks'])}",
|
| 177 |
state,
|
| 178 |
+
list(state["files"].keys())
|
| 179 |
)
|
| 180 |
|
| 181 |
except Exception as e:
|
| 182 |
+
return (
|
| 183 |
+
f"Error processing PDF: {e}",
|
| 184 |
+
state,
|
| 185 |
+
list(state["files"].keys())
|
| 186 |
+
)
|
| 187 |
|
| 188 |
def remove_pdf(filename, state):
|
| 189 |
if filename not in state["files"]:
|