Spaces:
Running
Running
高橋慧
commited on
Commit
·
e763ae6
1
Parent(s):
eaf91d7
add progress bar
Browse files
app.py
CHANGED
@@ -186,12 +186,15 @@ def fetch_clinical_trials_basic(cancer_name):
|
|
186 |
return []
|
187 |
|
188 |
# 軽量版データ生成関数
|
189 |
-
def generate_sample_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable):
|
190 |
"""サンプルデータを生成(辞書のリスト形式)"""
|
191 |
try:
|
192 |
if not all([age, sex, tumor_type]):
|
193 |
return []
|
194 |
|
|
|
|
|
|
|
195 |
sample_data = [
|
196 |
{
|
197 |
"NCTID": "NCT12345678",
|
@@ -228,6 +231,9 @@ def generate_sample_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiabl
|
|
228 |
}
|
229 |
]
|
230 |
|
|
|
|
|
|
|
231 |
return sample_data
|
232 |
|
233 |
except Exception as e:
|
@@ -235,15 +241,21 @@ def generate_sample_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiabl
|
|
235 |
return []
|
236 |
|
237 |
# 基本版データ生成関数(ClinicalTrials.gov API使用、AI評価なし)
|
238 |
-
def generate_basic_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable):
|
239 |
"""基本版のデータ生成(API使用、AI評価なし)"""
|
240 |
try:
|
241 |
if not all([age, sex, tumor_type]):
|
242 |
return []
|
243 |
|
|
|
|
|
|
|
244 |
# 実際のAPI呼び出し
|
245 |
data_list = fetch_clinical_trials_basic(tumor_type)
|
246 |
|
|
|
|
|
|
|
247 |
if not data_list:
|
248 |
print("臨床試験データが見つかりませんでした")
|
249 |
return []
|
@@ -253,6 +265,9 @@ def generate_basic_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable
|
|
253 |
item['AgentJudgment'] = f'基本版:{age}歳{sex}の{tumor_type}患者への詳細評価にはAI機能が必要です'
|
254 |
item['AgentGrade'] = 'unclear'
|
255 |
|
|
|
|
|
|
|
256 |
print(f"基本版評価完了。結果: {len(data_list)} 件")
|
257 |
return data_list
|
258 |
|
@@ -262,15 +277,20 @@ def generate_basic_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable
|
|
262 |
return []
|
263 |
|
264 |
# 完全版データ生成関数(AI評価付き)
|
265 |
-
def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable):
|
266 |
"""完全版のデータ生成(実際のAPI使用 + AI評価)"""
|
267 |
try:
|
268 |
if not all([age, sex, tumor_type]):
|
269 |
return []
|
270 |
|
|
|
|
|
|
|
271 |
# 日本語の腫瘍タイプを英語に翻訳
|
272 |
try:
|
273 |
if translator is not None:
|
|
|
|
|
274 |
TumorName = translator.translate(tumor_type)
|
275 |
print(f"腫瘍タイプ翻訳: {tumor_type} → {TumorName}")
|
276 |
else:
|
@@ -282,6 +302,8 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable)
|
|
282 |
|
283 |
# 質問文を生成
|
284 |
try:
|
|
|
|
|
285 |
ex_question = generate_ex_question_English(age, sex, TumorName, GeneMutation, Meseable, Biopsiable)
|
286 |
print(f"生成された質問: {ex_question}")
|
287 |
except Exception as e:
|
@@ -290,6 +312,8 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable)
|
|
290 |
|
291 |
# 臨床試験データの取得
|
292 |
try:
|
|
|
|
|
293 |
print(f"臨床試験データを検索中: {TumorName}")
|
294 |
df = fetch_clinical_trials(TumorName)
|
295 |
if df.empty:
|
@@ -308,8 +332,16 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable)
|
|
308 |
evaluation_limit = min(len(data_list), 10)
|
309 |
print(f"AI評価実行: {evaluation_limit} 件を処理します")
|
310 |
|
|
|
|
|
|
|
311 |
for i, item in enumerate(data_list[:evaluation_limit]):
|
312 |
try:
|
|
|
|
|
|
|
|
|
|
|
313 |
print(f"評価中 ({i+1}/{evaluation_limit}): {item['NCTID']}")
|
314 |
target_criteria = item['Eligibility Criteria']
|
315 |
|
@@ -327,10 +359,16 @@ def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable)
|
|
327 |
item['AgentGrade'] = "unclear"
|
328 |
|
329 |
# 評価されなかった残りのアイテムにはプレースホルダーを設定
|
|
|
|
|
|
|
330 |
for item in data_list[evaluation_limit:]:
|
331 |
item['AgentJudgment'] = f"完全版:{age}歳{sex}の{tumor_type}患者(評価制限により未処理)"
|
332 |
item['AgentGrade'] = "unclear"
|
333 |
|
|
|
|
|
|
|
334 |
print(f"完全版評価完了。結果: {len(data_list)} 件(うち{evaluation_limit}件をAI評価)")
|
335 |
return data_list
|
336 |
|
@@ -585,30 +623,32 @@ with gr.Blocks(title="臨床試験適格性評価", theme=gr.themes.Soft()) as d
|
|
585 |
progress_text = gr.Textbox(label="Processing Status", value="Ready", interactive=False)
|
586 |
|
587 |
# イベントハンドリング
|
588 |
-
def update_data_and_display(age, sex, tumor_type, gene_mutation, measurable, biopsiable):
|
589 |
"""データ生成と表示更新"""
|
590 |
try:
|
591 |
groq_available = bool(os.getenv("GROQ_API_KEY"))
|
592 |
|
|
|
|
|
593 |
if FULL_VERSION and groq_available:
|
594 |
-
|
595 |
-
data = generate_full_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable)
|
596 |
elif FULL_VERSION:
|
597 |
-
|
598 |
-
data = generate_basic_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable)
|
599 |
elif LANGCHAIN_AVAILABLE:
|
600 |
-
|
601 |
-
data = generate_basic_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable)
|
602 |
else:
|
603 |
-
|
604 |
-
data = generate_sample_data(age, sex, tumor_type, gene_mutation, measurable, biopsiable)
|
605 |
|
606 |
if data:
|
607 |
html_table = create_html_table(data)
|
608 |
final_progress = f"✅ 完了: {len(data)} 件の臨床試験が見つかりました"
|
609 |
-
if FULL_VERSION and
|
610 |
-
|
611 |
-
final_progress += f"
|
612 |
else:
|
613 |
html_table = "<div style='text-align: center; padding: 20px; color: #666;'>⚠️ 該当する臨床試験が見つかりませんでした</div>"
|
614 |
final_progress = "⚠️ 該当する臨床試験が見つかりませんでした"
|
|
|
186 |
return []
|
187 |
|
188 |
# 軽量版データ生成関数
|
189 |
+
def generate_sample_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable, progress=None):
|
190 |
"""サンプルデータを生成(辞書のリスト形式)"""
|
191 |
try:
|
192 |
if not all([age, sex, tumor_type]):
|
193 |
return []
|
194 |
|
195 |
+
if progress:
|
196 |
+
progress(0.3, desc="📋 サンプルデータを生成中...")
|
197 |
+
|
198 |
sample_data = [
|
199 |
{
|
200 |
"NCTID": "NCT12345678",
|
|
|
231 |
}
|
232 |
]
|
233 |
|
234 |
+
if progress:
|
235 |
+
progress(1.0, desc="✅ サンプルデータ生成完了")
|
236 |
+
|
237 |
return sample_data
|
238 |
|
239 |
except Exception as e:
|
|
|
241 |
return []
|
242 |
|
243 |
# 基本版データ生成関数(ClinicalTrials.gov API使用、AI評価なし)
|
244 |
+
def generate_basic_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable, progress=None):
|
245 |
"""基本版のデータ生成(API使用、AI評価なし)"""
|
246 |
try:
|
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("臨床試験データが見つかりませんでした")
|
261 |
return []
|
|
|
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
|
273 |
|
|
|
277 |
return []
|
278 |
|
279 |
# 完全版データ生成関数(AI評価付き)
|
280 |
+
def generate_full_data(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable, progress=None):
|
281 |
"""完全版のデータ生成(実際のAPI使用 + AI評価)"""
|
282 |
try:
|
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 |
|
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 |
|
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 |
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']
|
347 |
|
|
|
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
|
374 |
|
|
|
623 |
progress_text = gr.Textbox(label="Processing Status", value="Ready", interactive=False)
|
624 |
|
625 |
# イベントハンドリング
|
626 |
+
def update_data_and_display(age, sex, tumor_type, gene_mutation, measurable, biopsiable, progress=gr.Progress()):
|
627 |
"""データ生成と表示更新"""
|
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:
|
647 |
html_table = create_html_table(data)
|
648 |
final_progress = f"✅ 完了: {len(data)} 件の臨床試験が見つかりました"
|
649 |
+
if FULL_VERSION and groq_available:
|
650 |
+
ai_evaluated_count = len([item for item in data if 'エラー' not in item.get('AgentJudgment', '') and '未処理' not in item.get('AgentJudgment', '')])
|
651 |
+
final_progress += f"(うち{min(len(data), 10)}件をAI評価済み)"
|
652 |
else:
|
653 |
html_table = "<div style='text-align: center; padding: 20px; color: #666;'>⚠️ 該当する臨床試験が見つかりませんでした</div>"
|
654 |
final_progress = "⚠️ 該当する臨床試験が見つかりませんでした"
|