Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1327,43 +1327,45 @@ def build_langgraph_gpt_like():
|
|
| 1327 |
return graph.compile()
|
| 1328 |
|
| 1329 |
# 用於 Gradio Tab 6 的主入口函數
|
| 1330 |
-
def langgraph_tab6_main(query: str,
|
| 1331 |
try:
|
| 1332 |
-
if not
|
| 1333 |
-
|
|
|
|
|
|
|
| 1334 |
|
|
|
|
|
|
|
| 1335 |
all_docs = []
|
| 1336 |
file_names = []
|
| 1337 |
|
| 1338 |
-
for
|
| 1339 |
-
|
| 1340 |
-
if not
|
| 1341 |
continue
|
| 1342 |
-
file_names.append(os.path.basename(
|
| 1343 |
-
|
| 1344 |
-
|
| 1345 |
-
|
| 1346 |
-
|
|
|
|
| 1347 |
else:
|
| 1348 |
-
loader = TextLoader(
|
|
|
|
| 1349 |
docs = loader.load()
|
| 1350 |
all_docs.extend(docs)
|
| 1351 |
|
| 1352 |
if not all_docs:
|
| 1353 |
-
return "
|
| 1354 |
|
| 1355 |
chunks = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50).split_documents(all_docs)
|
| 1356 |
db = FAISS.from_documents(chunks, embeddings)
|
| 1357 |
retriever = db.as_retriever()
|
| 1358 |
|
| 1359 |
-
# 建構 LangGraph QA Chain
|
| 1360 |
graph = build_langgraph_doc_qa_chain(llm=llm_gpt4, retriever=retriever, memory=None, prompt=custom_prompt)
|
| 1361 |
-
result = graph(
|
| 1362 |
-
"query": query,
|
| 1363 |
-
"file_names": file_names # Pass all file names for intent detection
|
| 1364 |
-
})
|
| 1365 |
return result.get("answer", "No answer generated.")
|
| 1366 |
-
|
| 1367 |
except Exception as e:
|
| 1368 |
return f"[Tab6 Error] {e}"
|
| 1369 |
|
|
|
|
| 1327 |
return graph.compile()
|
| 1328 |
|
| 1329 |
# 用於 Gradio Tab 6 的主入口函數
|
| 1330 |
+
def langgraph_tab6_main(query: str, file=None) -> str:
|
| 1331 |
try:
|
| 1332 |
+
if not file:
|
| 1333 |
+
# fallback to General Agent only when no file
|
| 1334 |
+
result = general_agent.kickoff(inputs={"query": query})
|
| 1335 |
+
return result.output
|
| 1336 |
|
| 1337 |
+
# ⬇️ 保證 file 是 list(支援多檔)
|
| 1338 |
+
files = file if isinstance(file, list) else [file]
|
| 1339 |
all_docs = []
|
| 1340 |
file_names = []
|
| 1341 |
|
| 1342 |
+
for f in files:
|
| 1343 |
+
path = get_file_path(f)
|
| 1344 |
+
if not path:
|
| 1345 |
continue
|
| 1346 |
+
file_names.append(os.path.basename(path))
|
| 1347 |
+
|
| 1348 |
+
if path.lower().endswith(".pdf"):
|
| 1349 |
+
loader = PyPDFLoader(path)
|
| 1350 |
+
elif path.lower().endswith(".docx"):
|
| 1351 |
+
loader = UnstructuredWordDocumentLoader(path)
|
| 1352 |
else:
|
| 1353 |
+
loader = TextLoader(path)
|
| 1354 |
+
|
| 1355 |
docs = loader.load()
|
| 1356 |
all_docs.extend(docs)
|
| 1357 |
|
| 1358 |
if not all_docs:
|
| 1359 |
+
return "Uploaded files are empty or unreadable."
|
| 1360 |
|
| 1361 |
chunks = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50).split_documents(all_docs)
|
| 1362 |
db = FAISS.from_documents(chunks, embeddings)
|
| 1363 |
retriever = db.as_retriever()
|
| 1364 |
|
| 1365 |
+
# 🧠 建構 LangGraph QA Chain
|
| 1366 |
graph = build_langgraph_doc_qa_chain(llm=llm_gpt4, retriever=retriever, memory=None, prompt=custom_prompt)
|
| 1367 |
+
result = graph(query)
|
|
|
|
|
|
|
|
|
|
| 1368 |
return result.get("answer", "No answer generated.")
|
|
|
|
| 1369 |
except Exception as e:
|
| 1370 |
return f"[Tab6 Error] {e}"
|
| 1371 |
|