高橋慧 commited on
Commit
19a4966
·
1 Parent(s): 6badf8e

add progress note2

Browse files
Files changed (1) hide show
  1. app.py +45 -33
app.py CHANGED
@@ -279,19 +279,20 @@ def generate_basic_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable
279
  traceback.print_exc()
280
  return []
281
 
282
- # 完全版データ生成関数(AI評価付き)
283
- def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable):
284
- """完全版のデータ生成(実際のAPI使用 + AI評価)"""
285
  try:
286
  if not all([age, sex, tumor_type]):
287
- return []
 
288
 
289
- safe_progress_update(None, 0.1, "🔄 AI評価システムを初期化中...")
290
 
291
  # 日本語の腫瘍タイプを英語に翻訳
292
  try:
293
  if translator is not None:
294
- safe_progress_update(None, 0.15, "🌐 腫瘍タイプを英語に翻訳中...")
295
  TumorName = translator.translate(tumor_type)
296
  print(f"腫瘍タイプ翻訳: {tumor_type} → {TumorName}")
297
  else:
@@ -303,21 +304,23 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable)
303
 
304
  # 質問文を生成
305
  try:
306
- safe_progress_update(None, 0.2, "❓ 患者情報から評価用質問を生成中...")
307
  ex_question = generate_ex_question_English(age, sex, TumorName, GeneMutation, Meseable, Biopsiable)
308
  print(f"生成された質問: {ex_question}")
309
  except Exception as e:
310
  print(f"質問生成エラー: {e}")
311
- return []
 
312
 
313
  # 臨床試験データの取得
314
  try:
315
- safe_progress_update(None, 0.3, "🔍 ClinicalTrials.govから臨床試験データを検索中...")
316
  print(f"臨床試験データを検索中: {TumorName}")
317
  df = fetch_clinical_trials(TumorName)
318
  if df.empty:
319
  print("臨床試験データが見つかりませんでした")
320
- return []
 
321
  print(f"取得した臨床試験数: {len(df)}")
322
 
323
  # DataFrameを辞書のリストに変換
@@ -325,19 +328,19 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable)
325
 
326
  except Exception as e:
327
  print(f"臨床試験データ取得エラー: {e}")
328
- return []
 
329
 
330
  # AI評価の実行(最大10件まで)
331
  evaluation_limit = min(len(data_list), 10)
332
  print(f"AI評価実行: {evaluation_limit} 件を処理します")
333
 
334
- safe_progress_update(None, 0.4, f"🤖 AI評価開始: {evaluation_limit}件の臨床試験を分析中...")
335
 
336
  for i, item in enumerate(data_list[:evaluation_limit]):
337
  try:
338
- # プログレスバーの更新(0.4から0.9まで)
339
- progress_value = 0.4 + (0.5 * (i + 1) / evaluation_limit)
340
- safe_progress_update(None, progress_value, f"🧠 AI評価中: {i+1}/{evaluation_limit} - {item['NCTID']}")
341
 
342
  print(f"評価中 ({i+1}/{evaluation_limit}): {item['NCTID']}")
343
  target_criteria = item['Eligibility Criteria']
@@ -356,21 +359,20 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable)
356
  item['AgentGrade'] = "unclear"
357
 
358
  # 評価されなかった残りのアイテムにはプレースホルダーを設定
359
- safe_progress_update(None, 0.95, "📊 結果を整理中...")
360
 
361
  for item in data_list[evaluation_limit:]:
362
  item['AgentJudgment'] = f"完全版:{age}歳{sex}の{tumor_type}患者(評価制限により未処理)"
363
  item['AgentGrade'] = "unclear"
364
 
365
- safe_progress_update(None, 1.0, f"✅ 完了: {len(data_list)}件中{evaluation_limit}件をAI評価")
366
-
367
  print(f"完全版評価完了。結果: {len(data_list)} 件(うち{evaluation_limit}件をAI評価)")
368
- return data_list
 
 
369
 
370
  except Exception as e:
371
  print(f"完全版データ生成中に予期しないエラー: {e}")
372
- traceback.print_exc()
373
- return []
374
 
375
  # HTMLテーブル生成関数
376
  def create_html_table(data, show_grade=True):
@@ -619,24 +621,36 @@ with gr.Blocks(title="臨床試験適格性評価", theme=gr.themes.Soft()) as d
619
 
620
  # イベントハンドリング
621
  def update_data_and_display(age, sex, tumor_type, gene_mutation, measurable, biopsiable):
622
- """データ生成と表示更新(プログレステキスト付き)"""
623
  try:
624
  groq_available = bool(os.getenv("GROQ_API_KEY"))
625
 
626
  # 初期進行状況表示
627
- current_progress = safe_progress_update(None, 0.05, "🚀 処理を開始しています...")
 
628
 
629
  if FULL_VERSION and groq_available:
630
- current_progress = safe_progress_update(None, 0.1, "🤖 AI適格性評価モードで実行中...")
631
- data = generate_full_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable)
 
 
 
 
 
 
 
 
632
  elif FULL_VERSION:
633
- current_progress = safe_progress_update(None, 0.1, "🔍 基本モードで実行中...")
 
634
  data = generate_basic_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable)
635
  elif LANGCHAIN_AVAILABLE:
636
- current_progress = safe_progress_update(None, 0.1, "📡 基本検索モードで実行中...")
 
637
  data = generate_basic_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable)
638
  else:
639
- current_progress = safe_progress_update(None, 0.1, "📋 サンプルモードで実行中...")
 
640
  data = generate_sample_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable)
641
 
642
  if data:
@@ -645,19 +659,17 @@ with gr.Blocks(title="臨床試験適格性評価", theme=gr.themes.Soft()) as d
645
  if FULL_VERSION and groq_available:
646
  ai_evaluated_count = len([item for item in data if 'エラー' not in item.get('AgentJudgment', '') and '未処理' not in item.get('AgentJudgment', '')])
647
  final_progress += f"(うち{min(len(data), 10)}件をAI評価済み)"
648
- current_progress = safe_progress_update(None, 1.0, final_progress)
649
  else:
650
  html_table = "<div style='text-align: center; padding: 20px; color: #666;'>⚠️ 該当する臨床試験が見つかりませんでした</div>"
651
  final_progress = "⚠️ 該当する臨床試験が見つかりませんでした"
652
- current_progress = safe_progress_update(None, 1.0, final_progress)
653
 
654
- return html_table, data, current_progress
655
  except Exception as e:
656
  error_msg = f"❌ エラー: {str(e)}"
657
  error_html = f"<div style='text-align: center; padding: 20px; color: #d32f2f;'>{error_msg}</div>"
658
  print(f"データ更新エラー: {e}")
659
- error_progress = safe_progress_update(None, 1.0, error_msg)
660
- return error_html, [], error_progress
661
 
662
  def filter_and_show(data, grade):
663
  """フィルタリングと表示更新"""
 
279
  traceback.print_exc()
280
  return []
281
 
282
+ # AI評価付きデータ生成関数(リアルタイムプログレス付き)
283
+ def generate_full_data_with_progress(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable):
284
+ """完全版のデータ生成(リアルタイム進行状況付き)"""
285
  try:
286
  if not all([age, sex, tumor_type]):
287
+ yield []
288
+ return
289
 
290
+ yield "🔄 AI評価システムを初期化中..."
291
 
292
  # 日本語の腫瘍タイプを英語に翻訳
293
  try:
294
  if translator is not None:
295
+ yield "🌐 腫瘍タイプを英語に翻訳中..."
296
  TumorName = translator.translate(tumor_type)
297
  print(f"腫瘍タイプ翻訳: {tumor_type} → {TumorName}")
298
  else:
 
304
 
305
  # 質問文を生成
306
  try:
307
+ yield "❓ 患者情報から評価用質問を生成中..."
308
  ex_question = generate_ex_question_English(age, sex, TumorName, GeneMutation, Meseable, Biopsiable)
309
  print(f"生成された質問: {ex_question}")
310
  except Exception as e:
311
  print(f"質問生成エラー: {e}")
312
+ yield []
313
+ return
314
 
315
  # 臨床試験データの取得
316
  try:
317
+ yield "🔍 ClinicalTrials.govから臨床試験データを検索中..."
318
  print(f"臨床試験データを検索中: {TumorName}")
319
  df = fetch_clinical_trials(TumorName)
320
  if df.empty:
321
  print("臨床試験データが見つかりませんでした")
322
+ yield []
323
+ return
324
  print(f"取得した臨床試験数: {len(df)}")
325
 
326
  # DataFrameを辞書のリストに変換
 
328
 
329
  except Exception as e:
330
  print(f"臨床試験データ取得エラー: {e}")
331
+ yield []
332
+ return
333
 
334
  # AI評価の実行(最大10件まで)
335
  evaluation_limit = min(len(data_list), 10)
336
  print(f"AI評価実行: {evaluation_limit} 件を処理します")
337
 
338
+ yield f"🤖 AI評価開始: {evaluation_limit}件の臨床試験を分析中..."
339
 
340
  for i, item in enumerate(data_list[:evaluation_limit]):
341
  try:
342
+ # リアルタイム進行状況表示
343
+ yield f"🧠 AI評価中: {i+1}/{evaluation_limit} - {item['NCTID']}"
 
344
 
345
  print(f"評価中 ({i+1}/{evaluation_limit}): {item['NCTID']}")
346
  target_criteria = item['Eligibility Criteria']
 
359
  item['AgentGrade'] = "unclear"
360
 
361
  # 評価されなかった残りのアイテムにはプレースホルダーを設定
362
+ yield "📊 結果を整理中..."
363
 
364
  for item in data_list[evaluation_limit:]:
365
  item['AgentJudgment'] = f"完全版:{age}歳{sex}の{tumor_type}患者(評価制限により未処理)"
366
  item['AgentGrade'] = "unclear"
367
 
 
 
368
  print(f"完全版評価完了。結果: {len(data_list)} 件(うち{evaluation_limit}件をAI評価)")
369
+
370
+ # 最終結果を返す
371
+ yield data_list
372
 
373
  except Exception as e:
374
  print(f"完全版データ生成中に予期しないエラー: {e}")
375
+ yield []
 
376
 
377
  # HTMLテーブル生成関数
378
  def create_html_table(data, show_grade=True):
 
621
 
622
  # イベントハンドリング
623
  def update_data_and_display(age, sex, tumor_type, gene_mutation, measurable, biopsiable):
624
+ """データ生成と表示更新(リアルタイムプログレス付き)"""
625
  try:
626
  groq_available = bool(os.getenv("GROQ_API_KEY"))
627
 
628
  # 初期進行状況表示
629
+ initial_progress = "🚀 処理を開始しています..."
630
+ yield gr.update(), [], initial_progress
631
 
632
  if FULL_VERSION and groq_available:
633
+ mode_progress = "🤖 AI適格性評価モードで実行中..."
634
+ yield gr.update(), [], mode_progress
635
+
636
+ # AI評価付きデータ生成(ユーザーに進行状況を返すジェネレーター)
637
+ for progress_info in generate_full_data_with_progress(age, sex, tumor_type, gene_mutation, measurable, biopsiable):
638
+ if isinstance(progress_info, str): # 進行状況メッセージ
639
+ yield gr.update(), [], progress_info
640
+ else: # 最終結果
641
+ data = progress_info
642
+ break
643
  elif FULL_VERSION:
644
+ mode_progress = "🔍 基本モードで実行中..."
645
+ yield gr.update(), [], mode_progress
646
  data = generate_basic_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable)
647
  elif LANGCHAIN_AVAILABLE:
648
+ mode_progress = "📡 基本検索モードで実行中..."
649
+ yield gr.update(), [], mode_progress
650
  data = generate_basic_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable)
651
  else:
652
+ mode_progress = "📋 サンプルモードで実行中..."
653
+ yield gr.update(), [], mode_progress
654
  data = generate_sample_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable)
655
 
656
  if data:
 
659
  if FULL_VERSION and groq_available:
660
  ai_evaluated_count = len([item for item in data if 'エラー' not in item.get('AgentJudgment', '') and '未処理' not in item.get('AgentJudgment', '')])
661
  final_progress += f"(うち{min(len(data), 10)}件をAI評価済み)"
662
+ yield html_table, data, final_progress
663
  else:
664
  html_table = "<div style='text-align: center; padding: 20px; color: #666;'>⚠️ 該当する臨床試験が見つかりませんでした</div>"
665
  final_progress = "⚠️ 該当する臨床試験が見つかりませんでした"
666
+ yield html_table, [], final_progress
667
 
 
668
  except Exception as e:
669
  error_msg = f"❌ エラー: {str(e)}"
670
  error_html = f"<div style='text-align: center; padding: 20px; color: #d32f2f;'>{error_msg}</div>"
671
  print(f"データ更新エラー: {e}")
672
+ yield error_html, [], error_msg
 
673
 
674
  def filter_and_show(data, grade):
675
  """フィルタリングと表示更新"""