ClinicalTrialV2 / app.py
高橋慧
light editon
c33e580
raw
history blame
5.96 kB
import gradio as gr
import pandas as pd
import os
# 環境変数チェック
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 filter_dataframe(df, grade):
"""データフレームをフィルタリング"""
if df is None or len(df) == 0:
return df
try:
if grade == "all":
return df
return df[df['AgentGrade'] == grade]
except Exception as e:
print(f"フィルタリングエラー: {e}")
return df
# Gradioインターフェースの作成
def create_interface():
missing_vars = check_environment()
with gr.Blocks(title="臨床試験適格性評価(軽量版)", theme=gr.themes.Soft()) as demo:
gr.Markdown("## 臨床試験適格性評価インターフェース(デバッグ版)")
# 環境変数状態の表示
if not missing_vars:
gr.Markdown("✅ **ステータス**: 全ての環境変数が設定されています")
else:
gr.Markdown(f"⚠️ **注意**: 環境変数が不足しています: {', '.join(missing_vars)}")
gr.Markdown("💡 **使用方法**: 患者情報を入力して「Generate Sample Data」をクリックしてください。")
# 入力フィールド
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():
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):
df = generate_sample_dataframe(age, sex, tumor_type, gene_mutation, measurable, biopsiable)
return df, df
def filter_and_show(df, grade):
filtered_df = filter_dataframe(df, grade)
return filtered_df
# ボタンイベント
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_and_show(df, "all"),
inputs=[df_state],
outputs=[dataframe_output]
)
yes_button.click(
fn=lambda df: filter_and_show(df, "yes"),
inputs=[df_state],
outputs=[dataframe_output]
)
no_button.click(
fn=lambda df: filter_and_show(df, "no"),
inputs=[df_state],
outputs=[dataframe_output]
)
unclear_button.click(
fn=lambda df: filter_and_show(df, "unclear"),
inputs=[df_state],
outputs=[dataframe_output]
)
return demo
# アプリケーション起動
if __name__ == "__main__":
demo = create_interface()
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
debug=True,
show_error=True
)