Spaces:
Running
Running
before major change
Browse files- .gitignore +1 -1
- app copy.py +106 -0
.gitignore
CHANGED
@@ -7,7 +7,7 @@ __pycache__/
|
|
7 |
#*$py.class
|
8 |
#*.txt
|
9 |
#*.tsv
|
10 |
-
|
11 |
*.xlsx
|
12 |
*.pdf
|
13 |
*.nii
|
|
|
7 |
#*$py.class
|
8 |
#*.txt
|
9 |
#*.tsv
|
10 |
+
*.csv
|
11 |
*.xlsx
|
12 |
*.pdf
|
13 |
*.nii
|
app copy.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from OpenAITools.FetchTools import fetch_clinical_trials
|
4 |
+
from langchain_openai import ChatOpenAI
|
5 |
+
from langchain_groq import ChatGroq
|
6 |
+
from OpenAITools.CrinicalTrialTools import SimpleClinicalTrialAgent, GraderAgent, LLMTranslator, generate_ex_question_English
|
7 |
+
|
8 |
+
# モデルとエージェントの初期化
|
9 |
+
groq = ChatGroq(model_name="llama3-70b-8192", temperature=0)
|
10 |
+
translator = LLMTranslator(groq)
|
11 |
+
CriteriaCheckAgent = SimpleClinicalTrialAgent(groq)
|
12 |
+
grader_agent = GraderAgent(groq)
|
13 |
+
|
14 |
+
# データフレームを生成する関数
|
15 |
+
def generate_dataframe(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable):
|
16 |
+
# 日本語の腫瘍タイプを英語に翻訳
|
17 |
+
TumorName = translator.translate(tumor_type)
|
18 |
+
|
19 |
+
# 質問文を生成
|
20 |
+
ex_question = generate_ex_question_English(age, sex, TumorName, GeneMutation, Meseable, Biopsiable)
|
21 |
+
|
22 |
+
# 臨床試験データの取得
|
23 |
+
df = fetch_clinical_trials(TumorName)
|
24 |
+
df['AgentJudgment'] = None
|
25 |
+
df['AgentGrade'] = None
|
26 |
+
|
27 |
+
# 臨床試験の適格性の評価
|
28 |
+
NCTIDs = list(df['NCTID'])
|
29 |
+
progress = gr.Progress(track_tqdm=True)
|
30 |
+
for i, nct_id in enumerate(NCTIDs):
|
31 |
+
target_criteria = df.loc[df['NCTID'] == nct_id, 'Eligibility Criteria'].values[0]
|
32 |
+
agent_judgment = CriteriaCheckAgent.evaluate_eligibility(target_criteria, ex_question)
|
33 |
+
agent_grade = grader_agent.evaluate_eligibility(agent_judgment)
|
34 |
+
|
35 |
+
# データフレームの更新
|
36 |
+
df.loc[df['NCTID'] == nct_id, 'AgentJudgment'] = agent_judgment
|
37 |
+
df.loc[df['NCTID'] == nct_id, 'AgentGrade'] = agent_grade
|
38 |
+
progress((i + 1) / len(NCTIDs))
|
39 |
+
|
40 |
+
# 列を指定した順に並び替え
|
41 |
+
columns_order = ['NCTID', 'AgentGrade', 'Title', 'AgentJudgment', 'Japanes Locations',
|
42 |
+
'Primary Completion Date', 'Cancer', 'Summary', 'Eligibility Criteria']
|
43 |
+
df = df[columns_order]
|
44 |
+
|
45 |
+
return df, df # フィルタ用と表示用にデータフレームを返す
|
46 |
+
|
47 |
+
# 特定のAgentGrade(yes, no, unclear)に基づいて行をフィルタリングする関数
|
48 |
+
def filter_rows_by_grade(original_df, grade):
|
49 |
+
df_filtered = original_df[original_df['AgentGrade'] == grade]
|
50 |
+
return df_filtered, df_filtered
|
51 |
+
|
52 |
+
# CSVとして保存しダウンロードする関数
|
53 |
+
def download_filtered_csv(df):
|
54 |
+
file_path = "filtered_data.csv"
|
55 |
+
df.to_csv(file_path, index=False)
|
56 |
+
return file_path
|
57 |
+
|
58 |
+
# 全体結果をCSVとして保存しダウンロードする関数
|
59 |
+
def download_full_csv(df):
|
60 |
+
file_path = "full_data.csv"
|
61 |
+
df.to_csv(file_path, index=False)
|
62 |
+
return file_path
|
63 |
+
|
64 |
+
# Gradioインターフェースの作成
|
65 |
+
with gr.Blocks() as demo:
|
66 |
+
gr.Markdown("## 臨床試験適格性評価インターフェース")
|
67 |
+
|
68 |
+
# 各種入力フィールド
|
69 |
+
age_input = gr.Textbox(label="Age", placeholder="例: 65")
|
70 |
+
sex_input = gr.Dropdown(choices=["男性", "女性"], label="Sex")
|
71 |
+
tumor_type_input = gr.Textbox(label="Tumor Type", placeholder="例: gastric cancer, 日本でも良いですが英語の方が精度が高いです。")
|
72 |
+
gene_mutation_input = gr.Textbox(label="Gene Mutation", placeholder="例: HER2")
|
73 |
+
measurable_input = gr.Dropdown(choices=["有り", "無し", "不明"], label="Measurable Tumor")
|
74 |
+
biopsiable_input = gr.Dropdown(choices=["有り", "無し", "不明"], label="Biopsiable Tumor")
|
75 |
+
|
76 |
+
# データフレーム表示エリア
|
77 |
+
dataframe_output = gr.DataFrame()
|
78 |
+
original_df = gr.State()
|
79 |
+
filtered_df = gr.State()
|
80 |
+
|
81 |
+
# データフレーム生成ボタン
|
82 |
+
generate_button = gr.Button("Generate Clinical Trials Data")
|
83 |
+
|
84 |
+
# フィルタリングボタン
|
85 |
+
yes_button = gr.Button("Show Eligible Trials")
|
86 |
+
no_button = gr.Button("Show Ineligible Trials")
|
87 |
+
unclear_button = gr.Button("Show Unclear Trials")
|
88 |
+
|
89 |
+
# ダウンロードボタン
|
90 |
+
download_filtered_button = gr.Button("Download Filtered Data")
|
91 |
+
download_filtered_output = gr.File(label="Download Filtered Data")
|
92 |
+
|
93 |
+
download_full_button = gr.Button("Download Full Data")
|
94 |
+
download_full_output = gr.File(label="Download Full Data")
|
95 |
+
|
96 |
+
|
97 |
+
# ボタン動作の設定
|
98 |
+
generate_button.click(fn=generate_dataframe, inputs=[age_input, sex_input, tumor_type_input, gene_mutation_input, measurable_input, biopsiable_input], outputs=[dataframe_output, original_df])
|
99 |
+
yes_button.click(fn=filter_rows_by_grade, inputs=[original_df, gr.State("yes")], outputs=[dataframe_output, filtered_df])
|
100 |
+
no_button.click(fn=filter_rows_by_grade, inputs=[original_df, gr.State("no")], outputs=[dataframe_output, filtered_df])
|
101 |
+
unclear_button.click(fn=filter_rows_by_grade, inputs=[original_df, gr.State("unclear")], outputs=[dataframe_output, filtered_df])
|
102 |
+
download_filtered_button.click(fn=download_filtered_csv, inputs=filtered_df, outputs=download_filtered_output)
|
103 |
+
download_full_button.click(fn=download_full_csv, inputs=original_df, outputs=download_full_output)
|
104 |
+
|
105 |
+
if __name__ == "__main__":
|
106 |
+
demo.launch()
|