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

add progress bar2

Browse files
Files changed (1) hide show
  1. app.py +30 -31
app.py CHANGED
@@ -27,6 +27,18 @@ except ImportError as e:
27
  print(f"⚠️ 完全版モジュールのインポートに失敗: {e}")
28
  print("軽量版モードで動作します")
29
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  # 環境変数チェック
31
  def check_environment():
32
  """環境変数をチェックし、不足している場合は警告"""
@@ -192,8 +204,7 @@ def generate_sample_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiabl
192
  if not all([age, sex, tumor_type]):
193
  return []
194
 
195
- if progress:
196
- progress(0.3, desc="📋 サンプルデータを生成中...")
197
 
198
  sample_data = [
199
  {
@@ -231,8 +242,7 @@ def generate_sample_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiabl
231
  }
232
  ]
233
 
234
- if progress:
235
- progress(1.0, desc="✅ サンプルデータ生成完了")
236
 
237
  return sample_data
238
 
@@ -247,14 +257,12 @@ def generate_basic_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable
247
  if not all([age, sex, tumor_type]):
248
  return []
249
 
250
- if progress:
251
- progress(0.2, desc="🔍 ClinicalTrials.govからデータを検索中...")
252
 
253
  # 実際のAPI呼び出し
254
  data_list = fetch_clinical_trials_basic(tumor_type)
255
 
256
- if progress:
257
- progress(0.7, desc="📊 データを処理中...")
258
 
259
  if not data_list:
260
  print("臨床試験データが見つかりませんでした")
@@ -265,8 +273,7 @@ def generate_basic_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable
265
  item['AgentJudgment'] = f'基本版:{age}歳{sex}の{tumor_type}患者への詳細評価にはAI機能が必要です'
266
  item['AgentGrade'] = 'unclear'
267
 
268
- if progress:
269
- progress(1.0, desc=f"✅ 完了: {len(data_list)}件の臨床試験を取得")
270
 
271
  print(f"基本版評価完了。結果: {len(data_list)} 件")
272
  return data_list
@@ -283,14 +290,12 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable,
283
  if not all([age, sex, tumor_type]):
284
  return []
285
 
286
- if progress:
287
- progress(0.1, desc="🔄 AI評価システムを初期化中...")
288
 
289
  # 日本語の腫瘍タイプを英語に翻訳
290
  try:
291
  if translator is not None:
292
- if progress:
293
- progress(0.15, desc="🌐 腫瘍タイプを英語に翻訳中...")
294
  TumorName = translator.translate(tumor_type)
295
  print(f"腫瘍タイプ翻訳: {tumor_type} → {TumorName}")
296
  else:
@@ -302,8 +307,7 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable,
302
 
303
  # 質問文を生成
304
  try:
305
- if progress:
306
- progress(0.2, desc="❓ 患者情報から評価用質問を生成中...")
307
  ex_question = generate_ex_question_English(age, sex, TumorName, GeneMutation, Meseable, Biopsiable)
308
  print(f"生成された質問: {ex_question}")
309
  except Exception as e:
@@ -312,8 +316,7 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable,
312
 
313
  # 臨床試験データの取得
314
  try:
315
- if progress:
316
- progress(0.3, desc="🔍 ClinicalTrials.govから臨床試験データを検索中...")
317
  print(f"臨床試験データを検索中: {TumorName}")
318
  df = fetch_clinical_trials(TumorName)
319
  if df.empty:
@@ -332,15 +335,13 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable,
332
  evaluation_limit = min(len(data_list), 10)
333
  print(f"AI評価実行: {evaluation_limit} 件を処理します")
334
 
335
- if progress:
336
- progress(0.4, desc=f"🤖 AI評価開始: {evaluation_limit}件の臨床試験を分析中...")
337
 
338
  for i, item in enumerate(data_list[:evaluation_limit]):
339
  try:
340
  # プログレスバーの更新(0.4から0.9まで)
341
  progress_value = 0.4 + (0.5 * (i + 1) / evaluation_limit)
342
- if progress:
343
- progress(progress_value, desc=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,15 +360,13 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable,
359
  item['AgentGrade'] = "unclear"
360
 
361
  # 評価されなかった残りのアイテムにはプレースホルダーを設定
362
- if progress:
363
- progress(0.95, desc="📊 結果を整理中...")
364
 
365
  for item in data_list[evaluation_limit:]:
366
  item['AgentJudgment'] = f"完全版:{age}歳{sex}の{tumor_type}患者(評価制限により未処理)"
367
  item['AgentGrade'] = "unclear"
368
 
369
- if progress:
370
- progress(1.0, desc=f"✅ 完了: {len(data_list)}件中{evaluation_limit}件をAI評価")
371
 
372
  print(f"完全版評価完了。結果: {len(data_list)} 件(うち{evaluation_limit}件をAI評価)")
373
  return data_list
@@ -628,19 +627,19 @@ with gr.Blocks(title="臨床試験適格性評価", theme=gr.themes.Soft()) as d
628
  try:
629
  groq_available = bool(os.getenv("GROQ_API_KEY"))
630
 
631
- progress(0.05, desc="🚀 処理を開始しています...")
632
 
633
  if FULL_VERSION and groq_available:
634
- progress(0.1, desc="🤖 AI適格性評価モードで実行中...")
635
  data = generate_full_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable, progress)
636
  elif FULL_VERSION:
637
- progress(0.1, desc="🔍 基本モードで実行中...")
638
  data = generate_basic_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable, progress)
639
  elif LANGCHAIN_AVAILABLE:
640
- progress(0.1, desc="📡 基本検索モードで実行中...")
641
  data = generate_basic_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable, progress)
642
  else:
643
- progress(0.1, desc="📋 サンプルモードで実行中...")
644
  data = generate_sample_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable, progress)
645
 
646
  if data:
 
27
  print(f"⚠️ 完全版モジュールのインポートに失敗: {e}")
28
  print("軽量版モードで動作します")
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():
44
  """環境変数をチェックし、不足している場合は警告"""
 
204
  if not all([age, sex, tumor_type]):
205
  return []
206
 
207
+ safe_progress_update(progress, 0.3, "📋 サンプルデータを生成中...")
 
208
 
209
  sample_data = [
210
  {
 
242
  }
243
  ]
244
 
245
+ safe_progress_update(progress, 1.0, "✅ サンプルデータ生成完了")
 
246
 
247
  return sample_data
248
 
 
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
  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
 
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
 
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
 
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
  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
  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
 
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: