Spaces:
Running
Running
import gradio as gr | |
import pandas as pd | |
import time | |
import traceback | |
import os | |
# 軽量版のimportエラー対策 | |
try: | |
from OpenAITools.FetchTools import fetch_clinical_trials | |
from langchain_openai import ChatOpenAI | |
from langchain_groq import ChatGroq | |
from OpenAITools.CrinicalTrialTools import SimpleClinicalTrialAgent, GraderAgent, LLMTranslator, generate_ex_question_English | |
FULL_VERSION = True | |
except ImportError as e: | |
print(f"完全版モジュールのインポートに失敗: {e}") | |
print("軽量版モードで動作します") | |
FULL_VERSION = False | |
# 環境変数チェック | |
def check_environment(): | |
"""環境変数をチェックし、不足している場合は警告""" | |
missing_vars = [] | |
if not os.getenv("GROQ_API_KEY"): | |
missing_vars.append("GROQ_API_KEY") | |
if not os.getenv("OPENAI_API_KEY"): | |
missing_vars.append("OPENAI_API_KEY") | |
return missing_vars | |
# 軽量版データ生成関数 | |
def generate_sample_dataframe(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable): | |
"""サンプルデータを生成(軽量版)""" | |
try: | |
# 入力検証 | |
if not all([age, sex, tumor_type]): | |
return pd.DataFrame() | |
# サンプルデータの作成 | |
sample_data = { | |
'NCTID': ['NCT12345678', 'NCT87654321', 'NCT11111111'], | |
'AgentGrade': ['yes', 'no', 'unclear'], | |
'Title': [ | |
f'Clinical Trial for {tumor_type} in {sex} patients', | |
f'Alternative treatment for {tumor_type}', | |
f'Experimental therapy for {tumor_type} with {GeneMutation}' | |
], | |
'AgentJudgment': [ | |
f'{age}歳{sex}の{tumor_type}患者は参加可能です', | |
f'{age}歳{sex}の{tumor_type}患者は参加できません', | |
f'{age}歳{sex}の{tumor_type}患者の参加は不明確です' | |
], | |
'Japanese_Locations': ['Tokyo', 'Osaka', 'Kyoto'], | |
'Cancer': [tumor_type, tumor_type, tumor_type] | |
} | |
df = pd.DataFrame(sample_data) | |
return df | |
except Exception as e: | |
print(f"データフレーム生成エラー: {e}") | |
return pd.DataFrame() | |
# 完全版データ生成関数 | |
def generate_full_dataframe(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable): | |
"""完全版のデータ生成(実際のAPI使用)""" | |
if not FULL_VERSION: | |
return generate_sample_dataframe(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable) | |
# 完全版の実装は元のコードと同じ | |
# ... (省略) | |
# Gradioインターフェース | |
def create_interface(): | |
missing_vars = check_environment() | |
with gr.Blocks(title="臨床試験適格性評価", theme=gr.themes.Soft()) as demo: | |
gr.Markdown("## 🏥 臨床試験適格性評価インターフェース") | |
# バージョン情報 | |
if FULL_VERSION: | |
gr.Markdown("✅ **モード**: 完全版(API連携)") | |
else: | |
gr.Markdown("⚠️ **モード**: 軽量版(サンプルデータ)") | |
# 環境変数状態の表示 | |
if not missing_vars: | |
gr.Markdown("✅ **ステータス**: 全ての環境変数が設定されています") | |
else: | |
gr.Markdown(f"⚠️ **注意**: 環境変数が不足しています: {', '.join(missing_vars)}") | |
# 入力フィールド | |
with gr.Row(): | |
with gr.Column(): | |
age_input = gr.Textbox(label="Age", placeholder="例: 65", value="65") | |
sex_input = gr.Dropdown(choices=["男性", "女性"], label="Sex", value="男性") | |
tumor_type_input = gr.Textbox(label="Tumor Type", placeholder="例: gastric cancer", value="gastric cancer") | |
with gr.Column(): | |
gene_mutation_input = gr.Textbox(label="Gene Mutation", placeholder="例: HER2", value="HER2") | |
measurable_input = gr.Dropdown(choices=["有り", "無し", "不明"], label="Measurable Tumor", value="有り") | |
biopsiable_input = gr.Dropdown(choices=["有り", "無し", "不明"], label="Biopsiable Tumor", value="有り") | |
# データフレーム表示 | |
dataframe_output = gr.DataFrame(label="Clinical Trials Results", interactive=False) | |
# ボタン群 | |
with gr.Row(): | |
if FULL_VERSION: | |
generate_button = gr.Button("Generate Clinical Trials Data", variant="primary") | |
else: | |
generate_button = gr.Button("Generate Sample Data", variant="primary") | |
all_button = gr.Button("Show All", variant="secondary") | |
yes_button = gr.Button("Show Eligible", variant="secondary") | |
no_button = gr.Button("Show Ineligible", variant="secondary") | |
unclear_button = gr.Button("Show Unclear", variant="secondary") | |
# 状態管理 | |
df_state = gr.State(value=None) | |
# イベントハンドリング | |
def update_dataframe(age, sex, tumor_type, gene_mutation, measurable, biopsiable): | |
if FULL_VERSION: | |
df = generate_full_dataframe(age, sex, tumor_type, gene_mutation, measurable, biopsiable) | |
else: | |
df = generate_sample_dataframe(age, sex, tumor_type, gene_mutation, measurable, biopsiable) | |
return df, df | |
def filter_dataframe(df, grade): | |
if df is None or len(df) == 0: | |
return df | |
if grade == "all": | |
return df | |
return df[df['AgentGrade'] == grade] | |
# ボタンイベント | |
generate_button.click( | |
fn=update_dataframe, | |
inputs=[age_input, sex_input, tumor_type_input, gene_mutation_input, measurable_input, biopsiable_input], | |
outputs=[dataframe_output, df_state] | |
) | |
all_button.click( | |
fn=lambda df: filter_dataframe(df, "all"), | |
inputs=[df_state], | |
outputs=[dataframe_output] | |
) | |
yes_button.click( | |
fn=lambda df: filter_dataframe(df, "yes"), | |
inputs=[df_state], | |
outputs=[dataframe_output] | |
) | |
no_button.click( | |
fn=lambda df: filter_dataframe(df, "no"), | |
inputs=[df_state], | |
outputs=[dataframe_output] | |
) | |
unclear_button.click( | |
fn=lambda df: filter_dataframe(df, "unclear"), | |
inputs=[df_state], | |
outputs=[dataframe_output] | |
) | |
return demo | |
if __name__ == "__main__": | |
demo = create_interface() | |
demo.launch() | |