ChienChung commited on
Commit
ea72b46
·
verified ·
1 Parent(s): 2ff6e79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -38
app.py CHANGED
@@ -1452,49 +1452,45 @@ def build_langgraph_pipeline():
1452
  from tempfile import mkdtemp
1453
 
1454
 
 
 
 
1455
  from tempfile import mkdtemp
1456
  import os
1457
 
1458
  def get_file_path_tab6(file):
1459
- # DEBUG: 印出接收到的 file 物件
1460
- print("DEBUG: Received file object:", file)
1461
-
1462
  # 如果傳入的是字串,確認該字串為存在的檔案路徑
1463
  if isinstance(file, str):
1464
  if os.path.exists(file):
1465
  return file
1466
  else:
1467
- print("DEBUG: String file path does not exist:", file)
1468
  return None
1469
  # 如果傳入的是字典(Gradio 上傳後常見格式)
1470
  elif isinstance(file, dict):
1471
  data = file.get("data")
1472
  name = file.get("name")
1473
- print("DEBUG: File dict - name:", name, "data type:", type(data))
1474
- if data:
1475
- # 如果 data 為字串且該路徑存在,就直接返回
1476
- if isinstance(data, str) and os.path.exists(data):
1477
- return data
1478
- else:
1479
- # 將 data 寫入臨時檔案
1480
- temp_dir = mkdtemp()
1481
- file_path = os.path.join(temp_dir, name if name else "uploaded_file")
1482
- with open(file_path, "wb") as f:
1483
- if isinstance(data, str):
1484
- f.write(data.encode("utf-8"))
1485
- else:
1486
- f.write(data)
1487
- # 檢查檔案是否成功寫入
1488
- if os.path.exists(file_path):
1489
- print("DEBUG: File successfully written to:", file_path)
1490
- return file_path
1491
- else:
1492
- print("DEBUG: File not created at:", file_path)
1493
- return None
1494
- else:
1495
- # 如果沒有 data,就返回 None 避免返回無效檔案名稱
1496
- print("DEBUG: No data field in file dict")
1497
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1498
  # 如果是具有 .save 屬性的物件,直接呼叫 save 並返回檔案路徑
1499
  elif hasattr(file, "save"):
1500
  temp_dir = mkdtemp()
@@ -1504,14 +1500,11 @@ def get_file_path_tab6(file):
1504
  print("DEBUG: File saved at:", file_path)
1505
  return file_path
1506
  else:
1507
- print("DEBUG: File not saved at:", file_path)
1508
  return None
1509
  else:
1510
- # 如果 file 有 .name 屬性,嘗試返回屬性
1511
- if hasattr(file, "name"):
1512
- if os.path.exists(file.name):
1513
- return file.name
1514
- print("DEBUG: File type not recognized.")
1515
  return None
1516
 
1517
  def langgraph_tab6_main(query: str, file=None):
@@ -1523,7 +1516,7 @@ def langgraph_tab6_main(query: str, file=None):
1523
  docs_by_file = [] # 每份文件的完整內文
1524
 
1525
  for f in files:
1526
- path = get_file_path_tab6(f) # 使用新版 get_file_path_tab6
1527
  if not path:
1528
  print("DEBUG: get_file_path_tab6 returned None for file:", f)
1529
  continue
@@ -1547,7 +1540,6 @@ def langgraph_tab6_main(query: str, file=None):
1547
  docs_by_file.append(text)
1548
  all_docs.extend(docs)
1549
 
1550
- # 建立檢索器(用於非多代理流程)
1551
  if not all_docs:
1552
  print("DEBUG: No valid document content read. file_names:", file_names)
1553
  retriever = None
@@ -1564,13 +1556,11 @@ def langgraph_tab6_main(query: str, file=None):
1564
  memory=ConversationBufferMemory(memory_key="chat_history", return_messages=True),
1565
  )
1566
 
1567
- # 解析查詢拆解子意圖
1568
  parsed = parse_query(query)
1569
  if (parsed.get("summarise") or parsed.get("compare")) and len(docs_by_file) > 0:
1570
  final_answer = execute_multi_agent(parsed, docs_by_file, file_names)
1571
  return final_answer
1572
 
1573
- # 否則,走原有 LangGraph pipeline
1574
  graph = build_langgraph_pipeline()
1575
  state = {"query": query, "file_names": file_names}
1576
  if retriever is not None:
 
1452
  from tempfile import mkdtemp
1453
 
1454
 
1455
+ from tempfile import mkdtemp
1456
+ import os
1457
+
1458
  from tempfile import mkdtemp
1459
  import os
1460
 
1461
  def get_file_path_tab6(file):
 
 
 
1462
  # 如果傳入的是字串,確認該字串為存在的檔案路徑
1463
  if isinstance(file, str):
1464
  if os.path.exists(file):
1465
  return file
1466
  else:
 
1467
  return None
1468
  # 如果傳入的是字典(Gradio 上傳後常見格式)
1469
  elif isinstance(file, dict):
1470
  data = file.get("data")
1471
  name = file.get("name")
1472
+ # 如果沒有 data 欄位,直接返回 None,避免僅返回檔名
1473
+ if not data:
1474
+ print("DEBUG: No data field in file dict; returning None for", name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1475
  return None
1476
+ # 如果 data 為字串且該路徑存在,就直接返回
1477
+ if isinstance(data, str) and os.path.exists(data):
1478
+ return data
1479
+ else:
1480
+ # 將 data 寫入臨時檔案
1481
+ temp_dir = mkdtemp()
1482
+ file_path = os.path.join(temp_dir, name if name else "uploaded_file")
1483
+ with open(file_path, "wb") as f:
1484
+ if isinstance(data, str):
1485
+ f.write(data.encode("utf-8"))
1486
+ else:
1487
+ f.write(data)
1488
+ if os.path.exists(file_path):
1489
+ print("DEBUG: File successfully written to:", file_path)
1490
+ return file_path
1491
+ else:
1492
+ print("DEBUG: File not created at:", file_path)
1493
+ return None
1494
  # 如果是具有 .save 屬性的物件,直接呼叫 save 並返回檔案路徑
1495
  elif hasattr(file, "save"):
1496
  temp_dir = mkdtemp()
 
1500
  print("DEBUG: File saved at:", file_path)
1501
  return file_path
1502
  else:
 
1503
  return None
1504
  else:
1505
+ # file 有 .name 屬性,檢查檔案是否存在
1506
+ if hasattr(file, "name") and os.path.exists(file.name):
1507
+ return file.name
 
 
1508
  return None
1509
 
1510
  def langgraph_tab6_main(query: str, file=None):
 
1516
  docs_by_file = [] # 每份文件的完整內文
1517
 
1518
  for f in files:
1519
+ path = get_file_path_tab6(f)
1520
  if not path:
1521
  print("DEBUG: get_file_path_tab6 returned None for file:", f)
1522
  continue
 
1540
  docs_by_file.append(text)
1541
  all_docs.extend(docs)
1542
 
 
1543
  if not all_docs:
1544
  print("DEBUG: No valid document content read. file_names:", file_names)
1545
  retriever = None
 
1556
  memory=ConversationBufferMemory(memory_key="chat_history", return_messages=True),
1557
  )
1558
 
 
1559
  parsed = parse_query(query)
1560
  if (parsed.get("summarise") or parsed.get("compare")) and len(docs_by_file) > 0:
1561
  final_answer = execute_multi_agent(parsed, docs_by_file, file_names)
1562
  return final_answer
1563
 
 
1564
  graph = build_langgraph_pipeline()
1565
  state = {"query": query, "file_names": file_names}
1566
  if retriever is not None: