ChienChung commited on
Commit
b56ff29
·
verified ·
1 Parent(s): 01b87b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -38
app.py CHANGED
@@ -1360,13 +1360,6 @@ def build_langgraph_pipeline():
1360
 
1361
  from tempfile import mkdtemp
1362
 
1363
- # 新增一個專用於 Tab6 的 get_file_path 函式,不影響其他部分
1364
- import os
1365
- from tempfile import mkdtemp
1366
-
1367
- import os
1368
- from tempfile import mkdtemp
1369
-
1370
  # 專用於 Tab6 的檔案路徑取得函式(僅用於 Tab6,不影響其他地方)
1371
  def get_file_path_tab6(file):
1372
  # 如果檔案是字串,直接回傳
@@ -1397,42 +1390,46 @@ def get_file_path_tab6(file):
1397
  # 修改後的 Tab6 主執行函式
1398
  def langgraph_tab6_main(query: str, file=None):
1399
  try:
1400
- # 先處理上傳檔案(支援檔案上傳
1401
- files = file if isinstance(file, list) else [file] if file else []
1402
- all_docs, file_names = [], []
1403
- for f in files:
1404
- path = get_file_path_tab6(f) # 使用專用於 Tab6 的函式
1405
- if not path:
1406
- print("get_file_path_tab6 returned None for file:", f)
1407
- continue
1408
- file_names.append(os.path.basename(path))
1409
- print("Tab6 Processing file:", path)
1410
- # 根據副檔名選擇 Loader
1411
- if path.lower().endswith(".pdf"):
1412
- loader = PyPDFLoader(path)
1413
- elif path.lower().endswith(".docx"):
1414
- loader = UnstructuredWordDocumentLoader(path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1415
  else:
1416
- loader = TextLoader(path)
1417
- docs = loader.load()
1418
- print("Docs loaded from", path, ":", docs)
1419
- all_docs.extend(docs)
1420
- # 若成功讀取到文件內容,建立檢索器
1421
- if all_docs:
1422
- chunks = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50).split_documents(all_docs)
1423
- db = FAISS.from_documents(chunks, embeddings)
1424
- retriever = db.as_retriever()
1425
- # 將建立好的 retriever 指派到全域變數 session_retriever,供 Summarise/Document QA 工具使用
1426
- global session_retriever
1427
- session_retriever = retriever
1428
  else:
1429
- print("No document content read. file_names:", file_names)
1430
- retriever = None
1431
 
1432
  # 建立 LangGraph 流程
1433
  graph = build_langgraph_pipeline()
 
1434
  state = {"query": query, "file_names": file_names}
1435
- # 有 retriever,將其放入 state,讓下游工具(例如 Document QA 或 Summarise)能從 state 中取用
1436
  if retriever is not None:
1437
  state["retriever"] = retriever
1438
 
@@ -1592,7 +1589,7 @@ demo = gr.TabbedInterface(
1592
  fn=langgraph_tab6_main,
1593
  inputs=[
1594
  gr.Textbox(label="Ask anything"),
1595
- gr.File(label="Upload one or more files", file_types=[".pdf", ".txt", ".docx"], file_count="multiple")
1596
  ],
1597
  outputs="text",
1598
  title="LangGraph GPT-like QA (Tab6)",
 
1360
 
1361
  from tempfile import mkdtemp
1362
 
 
 
 
 
 
 
 
1363
  # 專用於 Tab6 的檔案路徑取得函式(僅用於 Tab6,不影響其他地方)
1364
  def get_file_path_tab6(file):
1365
  # 如果檔案是字串,直接回傳
 
1390
  # 修改後的 Tab6 主執行函式
1391
  def langgraph_tab6_main(query: str, file=None):
1392
  try:
1393
+ # 支援檔案上傳:如果 file 為列表,取第一個檔案
1394
+ if isinstance(file, list):
1395
+ file = file[0] if file else None
1396
+
1397
+ file_names = []
1398
+ retriever = None # 預設沒有文件內容
1399
+ if file is not None:
1400
+ # 取得檔案路徑(使用原有的 get_file_path 函式,不做修改)
1401
+ path = get_file_path(file)
1402
+ if path:
1403
+ file_names.append(os.path.basename(path))
1404
+ # 根據檔案副檔名選擇 Loader
1405
+ if path.lower().endswith(".pdf"):
1406
+ loader = PyPDFLoader(path)
1407
+ elif path.lower().endswith(".docx"):
1408
+ loader = UnstructuredWordDocumentLoader(path)
1409
+ else:
1410
+ loader = TextLoader(path)
1411
+ docs = loader.load()
1412
+ # 若成功讀取到內容,建立檢索器
1413
+ if docs:
1414
+ chunks = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50).split_documents(docs)
1415
+ db = FAISS.from_documents(chunks, embeddings)
1416
+ retriever = db.as_retriever()
1417
+ # 將建立好的 retriever設定到全域 session_retriever,
1418
+ # 以供 Summarisation 或 Document QA 任務使用
1419
+ global session_retriever
1420
+ session_retriever = retriever
1421
+ else:
1422
+ print("Loader did not return any document content from:", path)
1423
  else:
1424
+ print("get_file_path returned None for the uploaded file.")
 
 
 
 
 
 
 
 
 
 
 
1425
  else:
1426
+ print("No file uploaded.")
 
1427
 
1428
  # 建立 LangGraph 流程
1429
  graph = build_langgraph_pipeline()
1430
+ # state 中包含查詢與檔案名稱(若有)
1431
  state = {"query": query, "file_names": file_names}
1432
+ # 如果有 retriever,放入 state下游任務可檢查 state 中的 retriever)
1433
  if retriever is not None:
1434
  state["retriever"] = retriever
1435
 
 
1589
  fn=langgraph_tab6_main,
1590
  inputs=[
1591
  gr.Textbox(label="Ask anything"),
1592
+ gr.File(label="Upload one or more files", file_types=[".pdf", ".txt", ".docx"], file_count="single")
1593
  ],
1594
  outputs="text",
1595
  title="LangGraph GPT-like QA (Tab6)",