Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1394,14 +1394,12 @@ def langgraph_tab6_main(query: str, file=None):
|
|
| 1394 |
files = file if isinstance(file, list) else [file] if file else []
|
| 1395 |
all_docs, file_names = [], []
|
| 1396 |
for f in files:
|
| 1397 |
-
# 使用原有的 get_file_path
|
| 1398 |
-
path = get_file_path(f)
|
| 1399 |
if not path:
|
| 1400 |
print("get_file_path returned None for file:", f)
|
| 1401 |
continue
|
| 1402 |
file_names.append(os.path.basename(path))
|
| 1403 |
print("Processing file:", path)
|
| 1404 |
-
# 根據副檔名選擇 Loader
|
| 1405 |
if path.lower().endswith(".pdf"):
|
| 1406 |
loader = PyPDFLoader(path)
|
| 1407 |
elif path.lower().endswith(".docx"):
|
|
@@ -1412,7 +1410,6 @@ def langgraph_tab6_main(query: str, file=None):
|
|
| 1412 |
print("Docs loaded from", path, ":", docs)
|
| 1413 |
all_docs.extend(docs)
|
| 1414 |
|
| 1415 |
-
# 建立檢索器,合併所有檔案內容
|
| 1416 |
if not all_docs:
|
| 1417 |
print("No document content read. file_names:", file_names)
|
| 1418 |
retriever = None
|
|
@@ -1420,11 +1417,17 @@ def langgraph_tab6_main(query: str, file=None):
|
|
| 1420 |
chunks = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50).split_documents(all_docs)
|
| 1421 |
db = FAISS.from_documents(chunks, embeddings)
|
| 1422 |
retriever = db.as_retriever()
|
| 1423 |
-
#
|
| 1424 |
global session_retriever
|
| 1425 |
session_retriever = retriever
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1426 |
|
| 1427 |
-
# 建立 LangGraph 流程
|
| 1428 |
graph = build_langgraph_pipeline()
|
| 1429 |
state = {"query": query, "file_names": file_names}
|
| 1430 |
if retriever is not None:
|
|
|
|
| 1394 |
files = file if isinstance(file, list) else [file] if file else []
|
| 1395 |
all_docs, file_names = [], []
|
| 1396 |
for f in files:
|
| 1397 |
+
path = get_file_path(f) # 使用原有的 get_file_path
|
|
|
|
| 1398 |
if not path:
|
| 1399 |
print("get_file_path returned None for file:", f)
|
| 1400 |
continue
|
| 1401 |
file_names.append(os.path.basename(path))
|
| 1402 |
print("Processing file:", path)
|
|
|
|
| 1403 |
if path.lower().endswith(".pdf"):
|
| 1404 |
loader = PyPDFLoader(path)
|
| 1405 |
elif path.lower().endswith(".docx"):
|
|
|
|
| 1410 |
print("Docs loaded from", path, ":", docs)
|
| 1411 |
all_docs.extend(docs)
|
| 1412 |
|
|
|
|
| 1413 |
if not all_docs:
|
| 1414 |
print("No document content read. file_names:", file_names)
|
| 1415 |
retriever = None
|
|
|
|
| 1417 |
chunks = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50).split_documents(all_docs)
|
| 1418 |
db = FAISS.from_documents(chunks, embeddings)
|
| 1419 |
retriever = db.as_retriever()
|
| 1420 |
+
# 將建立好的 retriever 指派到全域變數 session_retriever
|
| 1421 |
global session_retriever
|
| 1422 |
session_retriever = retriever
|
| 1423 |
+
# 同時建立 Document QA 的 ConversationalRetrievalChain,供 uploaded_qa_tool_func 使用
|
| 1424 |
+
global session_qa_chain
|
| 1425 |
+
session_qa_chain = ConversationalRetrievalChain.from_llm(
|
| 1426 |
+
llm=llm_gpt4,
|
| 1427 |
+
retriever=retriever,
|
| 1428 |
+
memory=ConversationBufferMemory(memory_key="chat_history", return_messages=True),
|
| 1429 |
+
)
|
| 1430 |
|
|
|
|
| 1431 |
graph = build_langgraph_pipeline()
|
| 1432 |
state = {"query": query, "file_names": file_names}
|
| 1433 |
if retriever is not None:
|