SiangKai commited on
Commit
ddaa1d8
·
verified ·
1 Parent(s): 21e1e76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -66
app.py CHANGED
@@ -481,72 +481,72 @@ def extract_json(text: str) -> list | dict:
481
  raise ValueError(f"清理後仍然無法解析 JSON。原始錯誤: {e}")
482
 
483
 
484
- def reflect_post(api_key, api_key2, api_key3, api_key4, user_input):
485
- """
486
- API 呼叫總次數固定為 4 次。
487
- """
488
- # Step 1:拆解子問題 (API Call #1)
489
- decomposed_text = reply(api_key, system_reviewer, user_input)
490
- if "抱歉~" in decomposed_text:
491
- return "", decomposed_text
492
- try:
493
- parsed_list = extract_json(decomposed_text)
494
- if not isinstance(parsed_list, list): raise ValueError("回傳的不是列表格式")
495
- except Exception as e:
496
- return "", f"⚠️ 查詢意圖格式錯誤:{e}"
497
 
498
- direct_queries = [item for item in parsed_list if item.get("type") == "direct"]
499
- if not direct_queries:
500
- return "", "⚠️ 無法從輸入問題中擷取有效查詢項目。"
501
 
502
- all_querys_summary = "🔹 關鍵查詢:\n" + "\n".join(f"- {q['sub_query']}" for q in direct_queries)
503
 
504
- sub_query_texts = [q["sub_query"] for q in direct_queries]
505
 
506
- # Step 2:批次匹配表名 (API Call #2)
507
- table_map = batch_find_relevant_tables(api_key2, sub_query_texts)
508
- if not table_map:
509
- return all_querys_summary, "⚠️ 系統無法為您的查詢匹配到合適的資料表。"
510
 
511
- # Step 3: 批次解析所有子問題的參數 (API Call #3)
512
- params_map = batch_parse_sub_queries_with_gemini(api_key3, sub_query_texts)
513
- if not params_map:
514
- return all_querys_summary, "⚠️ 系統無法解析您問題中的查詢參數。"
515
-
516
- # Step 4:逐一執行查詢 (零 API 呼叫)
517
- context_list = []
518
- for q in direct_queries:
519
- sub_query = q["sub_query"]
520
- table_name = table_map.get(sub_query)
521
- params = params_map.get(sub_query)
522
-
523
- if not table_name or not params:
524
- context_list.append(f"【{sub_query}】\n查詢失敗:未能匹配到資料表或解析參數。")
525
- continue
526
 
527
- try:
528
- # 帶著所有精準參數執行查詢
529
- result_json = semantic_query_logic(
530
- time_query=params.get("time_query", ""),
531
- item_query=params.get("item_query", ""),
532
- district_query=params.get("district_query", ""),
533
- project_name=table_name
534
- )
535
- result_data = json.loads(result_json)
536
- if result_data:
537
- formatted_result = json.dumps(result_data, ensure_ascii=False, indent=2)
538
- context_list.append(f"【{sub_query}】\n查詢結果:\n{formatted_result}")
539
- else:
540
- context_list.append(f"【{sub_query}】\n查無資料。")
541
- except Exception as e:
542
- context_list.append(f"【{sub_query}】\n查詢失敗:{e}")
543
-
544
- combined_context = "\n\n".join(context_list)
545
-
546
- # Step 5:整合分析 (API Call #4)
547
- integration_prompt = f"使用者問題:{user_input}\n\n查詢資料如下:\n{combined_context}"
548
- integration_result = reply(api_key4, system_integration, integration_prompt)
549
- return all_querys_summary, integration_result
550
 
551
  # =======================================================================
552
  # 5. Gradio Web UI
@@ -687,7 +687,6 @@ def gradio_interface(user_input):
687
 
688
  # Step 4:執行查詢
689
  yield all_querys_summary, "⏳ 正在從資料庫中檢索資訊..."
690
- # ... (此處省略查詢邏輯,與原 reflect_post 相同) ...
691
  context_list = []
692
  for q in direct_queries:
693
  sub_query = q["sub_query"]
@@ -788,11 +787,12 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="orange"),
788
  lines=5,
789
  visible=True,
790
  )
791
- output_result = gr.Markdown(
792
- label="🧐 查詢結果",
793
- show_label=True,
794
- elem_id="output_markdown"
795
- )
 
796
 
797
  gr.Markdown(
798
  """
 
481
  raise ValueError(f"清理後仍然無法解析 JSON。原始錯誤: {e}")
482
 
483
 
484
+ # def reflect_post(api_key, api_key2, api_key3, api_key4, user_input):
485
+ # """
486
+ # API 呼叫總次數固定為 4 次。
487
+ # """
488
+ # # Step 1:拆解子問題 (API Call #1)
489
+ # decomposed_text = reply(api_key, system_reviewer, user_input)
490
+ # if "抱歉~" in decomposed_text:
491
+ # return "", decomposed_text
492
+ # try:
493
+ # parsed_list = extract_json(decomposed_text)
494
+ # if not isinstance(parsed_list, list): raise ValueError("回傳的不是列表格式")
495
+ # except Exception as e:
496
+ # return "", f"⚠️ 查詢意圖格式錯誤:{e}"
497
 
498
+ # direct_queries = [item for item in parsed_list if item.get("type") == "direct"]
499
+ # if not direct_queries:
500
+ # return "", "⚠️ 無法從輸入問題中擷取有效查詢項目。"
501
 
502
+ # all_querys_summary = "🔹 關鍵查詢:\n" + "\n".join(f"- {q['sub_query']}" for q in direct_queries)
503
 
504
+ # sub_query_texts = [q["sub_query"] for q in direct_queries]
505
 
506
+ # # Step 2:批次匹配表名 (API Call #2)
507
+ # table_map = batch_find_relevant_tables(api_key2, sub_query_texts)
508
+ # if not table_map:
509
+ # return all_querys_summary, "⚠️ 系統無法為您的查詢匹配到合適的資料表。"
510
 
511
+ # # Step 3: 批次解析所有子問題的參數 (API Call #3)
512
+ # params_map = batch_parse_sub_queries_with_gemini(api_key3, sub_query_texts)
513
+ # if not params_map:
514
+ # return all_querys_summary, "⚠️ 系統無法解析您問題中的查詢參數。"
515
+
516
+ # # Step 4:逐一執行查詢 (零 API 呼叫)
517
+ # context_list = []
518
+ # for q in direct_queries:
519
+ # sub_query = q["sub_query"]
520
+ # table_name = table_map.get(sub_query)
521
+ # params = params_map.get(sub_query)
522
+
523
+ # if not table_name or not params:
524
+ # context_list.append(f"【{sub_query}】\n查詢失敗:未能匹配到資料表或解析參數。")
525
+ # continue
526
 
527
+ # try:
528
+ # # 帶著所有精準參數執行查詢
529
+ # result_json = semantic_query_logic(
530
+ # time_query=params.get("time_query", ""),
531
+ # item_query=params.get("item_query", ""),
532
+ # district_query=params.get("district_query", ""),
533
+ # project_name=table_name
534
+ # )
535
+ # result_data = json.loads(result_json)
536
+ # if result_data:
537
+ # formatted_result = json.dumps(result_data, ensure_ascii=False, indent=2)
538
+ # context_list.append(f"【{sub_query}】\n查詢結果:\n{formatted_result}")
539
+ # else:
540
+ # context_list.append(f"【{sub_query}】\n查無資料。")
541
+ # except Exception as e:
542
+ # context_list.append(f"【{sub_query}】\n查詢失敗:{e}")
543
+
544
+ # combined_context = "\n\n".join(context_list)
545
+
546
+ # # Step 5:整合分析 (API Call #4)
547
+ # integration_prompt = f"使用者問題:{user_input}\n\n查詢資料如下:\n{combined_context}"
548
+ # integration_result = reply(api_key4, system_integration, integration_prompt)
549
+ # return all_querys_summary, integration_result
550
 
551
  # =======================================================================
552
  # 5. Gradio Web UI
 
687
 
688
  # Step 4:執行查詢
689
  yield all_querys_summary, "⏳ 正在從資料庫中檢索資訊..."
 
690
  context_list = []
691
  for q in direct_queries:
692
  sub_query = q["sub_query"]
 
787
  lines=5,
788
  visible=True,
789
  )
790
+ with gr.Group():
791
+ output_result = gr.Markdown(
792
+ label="🧐 查詢結果",
793
+ show_label=True,
794
+ elem_id="output_markdown"
795
+ )
796
 
797
  gr.Markdown(
798
  """