高橋慧 commited on
Commit
6badf8e
·
1 Parent(s): 58609be

add progress note

Browse files
Files changed (1) hide show
  1. app.py +38 -38
app.py CHANGED
@@ -29,15 +29,11 @@ except ImportError as e:
29
 
30
  # プログレスバーの安全な更新関数
31
  def safe_progress_update(progress, value, desc):
32
- """プログレスバーを安全に更新する関数"""
33
- try:
34
- if progress and hasattr(progress, '__call__'):
35
- progress(value, desc=desc)
36
- return True
37
- return False
38
- except Exception as e:
39
- print(f"プログレスバー更新エラー: {e}")
40
- return False
41
 
42
  # 環境変数チェック
43
  def check_environment():
@@ -198,13 +194,13 @@ def fetch_clinical_trials_basic(cancer_name):
198
  return []
199
 
200
  # 軽量版データ生成関数
201
- def generate_sample_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable, progress=None):
202
  """サンプルデータを生成(辞書のリスト形式)"""
203
  try:
204
  if not all([age, sex, tumor_type]):
205
  return []
206
 
207
- safe_progress_update(progress, 0.3, "📋 サンプルデータを生成中...")
208
 
209
  sample_data = [
210
  {
@@ -242,7 +238,7 @@ def generate_sample_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiabl
242
  }
243
  ]
244
 
245
- safe_progress_update(progress, 1.0, "✅ サンプルデータ生成完了")
246
 
247
  return sample_data
248
 
@@ -251,18 +247,18 @@ def generate_sample_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiabl
251
  return []
252
 
253
  # 基本版データ生成関数(ClinicalTrials.gov API使用、AI評価なし)
254
- def generate_basic_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable, progress=None):
255
  """基本版のデータ生成(API使用、AI評価なし)"""
256
  try:
257
  if not all([age, sex, tumor_type]):
258
  return []
259
 
260
- safe_progress_update(progress, 0.2, "🔍 ClinicalTrials.govからデータを検索中...")
261
 
262
  # 実際のAPI呼び出し
263
  data_list = fetch_clinical_trials_basic(tumor_type)
264
 
265
- safe_progress_update(progress, 0.7, "📊 データを処理中...")
266
 
267
  if not data_list:
268
  print("臨床試験データが見つかりませんでした")
@@ -273,7 +269,7 @@ def generate_basic_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable
273
  item['AgentJudgment'] = f'基本版:{age}歳{sex}の{tumor_type}患者への詳細評価にはAI機能が必要です'
274
  item['AgentGrade'] = 'unclear'
275
 
276
- safe_progress_update(progress, 1.0, f"✅ 完了: {len(data_list)}件の臨床試験を取得")
277
 
278
  print(f"基本版評価完了。結果: {len(data_list)} 件")
279
  return data_list
@@ -284,18 +280,18 @@ def generate_basic_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable
284
  return []
285
 
286
  # 完全版データ生成関数(AI評価付き)
287
- def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable, progress=None):
288
  """完全版のデータ生成(実際のAPI使用 + AI評価)"""
289
  try:
290
  if not all([age, sex, tumor_type]):
291
  return []
292
 
293
- safe_progress_update(progress, 0.1, "🔄 AI評価システムを初期化中...")
294
 
295
  # 日本語の腫瘍タイプを英語に翻訳
296
  try:
297
  if translator is not None:
298
- safe_progress_update(progress, 0.15, "🌐 腫瘍タイプを英語に翻訳中...")
299
  TumorName = translator.translate(tumor_type)
300
  print(f"腫瘍タイプ翻訳: {tumor_type} → {TumorName}")
301
  else:
@@ -307,7 +303,7 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable,
307
 
308
  # 質問文を生成
309
  try:
310
- safe_progress_update(progress, 0.2, "❓ 患者情報から評価用質問を生成中...")
311
  ex_question = generate_ex_question_English(age, sex, TumorName, GeneMutation, Meseable, Biopsiable)
312
  print(f"生成された質問: {ex_question}")
313
  except Exception as e:
@@ -316,7 +312,7 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable,
316
 
317
  # 臨床試験データの取得
318
  try:
319
- safe_progress_update(progress, 0.3, "🔍 ClinicalTrials.govから臨床試験データを検索中...")
320
  print(f"臨床試験データを検索中: {TumorName}")
321
  df = fetch_clinical_trials(TumorName)
322
  if df.empty:
@@ -335,13 +331,13 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable,
335
  evaluation_limit = min(len(data_list), 10)
336
  print(f"AI評価実行: {evaluation_limit} 件を処理します")
337
 
338
- safe_progress_update(progress, 0.4, f"🤖 AI評価開始: {evaluation_limit}件の臨床試験を分析中...")
339
 
340
  for i, item in enumerate(data_list[:evaluation_limit]):
341
  try:
342
  # プログレスバーの更新(0.4から0.9まで)
343
  progress_value = 0.4 + (0.5 * (i + 1) / evaluation_limit)
344
- safe_progress_update(progress, progress_value, f"🧠 AI評価中: {i+1}/{evaluation_limit} - {item['NCTID']}")
345
 
346
  print(f"評価中 ({i+1}/{evaluation_limit}): {item['NCTID']}")
347
  target_criteria = item['Eligibility Criteria']
@@ -360,13 +356,13 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable,
360
  item['AgentGrade'] = "unclear"
361
 
362
  # 評価されなかった残りのアイテムにはプレースホルダーを設定
363
- safe_progress_update(progress, 0.95, "📊 結果を整理中...")
364
 
365
  for item in data_list[evaluation_limit:]:
366
  item['AgentJudgment'] = f"完全版:{age}歳{sex}の{tumor_type}患者(評価制限により未処理)"
367
  item['AgentGrade'] = "unclear"
368
 
369
- safe_progress_update(progress, 1.0, f"✅ 完了: {len(data_list)}件中{evaluation_limit}件をAI評価")
370
 
371
  print(f"完全版評価完了。結果: {len(data_list)} 件(うち{evaluation_limit}件をAI評価)")
372
  return data_list
@@ -622,25 +618,26 @@ with gr.Blocks(title="臨床試験適格性評価", theme=gr.themes.Soft()) as d
622
  progress_text = gr.Textbox(label="Processing Status", value="Ready", interactive=False)
623
 
624
  # イベントハンドリング
625
- def update_data_and_display(age, sex, tumor_type, gene_mutation, measurable, biopsiable, progress=gr.Progress()):
626
- """データ生成と表示更新"""
627
  try:
628
  groq_available = bool(os.getenv("GROQ_API_KEY"))
629
 
630
- safe_progress_update(progress, 0.05, "🚀 処理を開始しています...")
 
631
 
632
  if FULL_VERSION and groq_available:
633
- safe_progress_update(progress, 0.1, "🤖 AI適格性評価モードで実行中...")
634
- data = generate_full_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable, progress)
635
  elif FULL_VERSION:
636
- safe_progress_update(progress, 0.1, "🔍 基本モードで実行中...")
637
- data = generate_basic_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable, progress)
638
  elif LANGCHAIN_AVAILABLE:
639
- safe_progress_update(progress, 0.1, "📡 基本検索モードで実行中...")
640
- data = generate_basic_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable, progress)
641
  else:
642
- safe_progress_update(progress, 0.1, "📋 サンプルモードで実行中...")
643
- data = generate_sample_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable, progress)
644
 
645
  if data:
646
  html_table = create_html_table(data)
@@ -648,16 +645,19 @@ with gr.Blocks(title="臨床試験適格性評価", theme=gr.themes.Soft()) as d
648
  if FULL_VERSION and groq_available:
649
  ai_evaluated_count = len([item for item in data if 'エラー' not in item.get('AgentJudgment', '') and '未処理' not in item.get('AgentJudgment', '')])
650
  final_progress += f"(うち{min(len(data), 10)}件をAI評価済み)"
 
651
  else:
652
  html_table = "<div style='text-align: center; padding: 20px; color: #666;'>⚠️ 該当する臨床試験が見つかりませんでした</div>"
653
  final_progress = "⚠️ 該当する臨床試験が見つかりませんでした"
 
654
 
655
- return html_table, data, final_progress
656
  except Exception as e:
657
  error_msg = f"❌ エラー: {str(e)}"
658
  error_html = f"<div style='text-align: center; padding: 20px; color: #d32f2f;'>{error_msg}</div>"
659
  print(f"データ更新エラー: {e}")
660
- return error_html, [], error_msg
 
661
 
662
  def filter_and_show(data, grade):
663
  """フィルタリングと表示更新"""
 
29
 
30
  # プログレスバーの安全な更新関数
31
  def safe_progress_update(progress, value, desc):
32
+ """プログレスバーを安全に更新する関数(テキスト表示のみ使用)"""
33
+ # Gradioプログレスバーは使用せず、テキスト表示のみ
34
+ progress_text = f"📊 進行状況 ({int(value*100)}%): {desc}"
35
+ print(progress_text)
36
+ return progress_text
 
 
 
 
37
 
38
  # 環境変数チェック
39
  def check_environment():
 
194
  return []
195
 
196
  # 軽量版データ生成関数
197
+ def generate_sample_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable):
198
  """サンプルデータを生成(辞書のリスト形式)"""
199
  try:
200
  if not all([age, sex, tumor_type]):
201
  return []
202
 
203
+ safe_progress_update(None, 0.3, "📋 サンプルデータを生成中...")
204
 
205
  sample_data = [
206
  {
 
238
  }
239
  ]
240
 
241
+ safe_progress_update(None, 1.0, "✅ サンプルデータ生成完了")
242
 
243
  return sample_data
244
 
 
247
  return []
248
 
249
  # 基本版データ生成関数(ClinicalTrials.gov API使用、AI評価なし)
250
+ def generate_basic_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable):
251
  """基本版のデータ生成(API使用、AI評価なし)"""
252
  try:
253
  if not all([age, sex, tumor_type]):
254
  return []
255
 
256
+ safe_progress_update(None, 0.2, "🔍 ClinicalTrials.govからデータを検索中...")
257
 
258
  # 実際のAPI呼び出し
259
  data_list = fetch_clinical_trials_basic(tumor_type)
260
 
261
+ safe_progress_update(None, 0.7, "📊 データを処理中...")
262
 
263
  if not data_list:
264
  print("臨床試験データが見つかりませんでした")
 
269
  item['AgentJudgment'] = f'基本版:{age}歳{sex}の{tumor_type}患者への詳細評価にはAI機能が必要です'
270
  item['AgentGrade'] = 'unclear'
271
 
272
+ safe_progress_update(None, 1.0, f"✅ 完了: {len(data_list)}件の臨床試験を取得")
273
 
274
  print(f"基本版評価完了。結果: {len(data_list)} 件")
275
  return data_list
 
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
 
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:
 
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:
 
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
  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
 
618
  progress_text = gr.Textbox(label="Processing Status", value="Ready", interactive=False)
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:
643
  html_table = create_html_table(data)
 
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
  """フィルタリングと表示更新"""