Spaces:
Sleeping
Sleeping
File size: 7,763 Bytes
6aa59d5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7866\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7866/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fetching data from: https://clinicaltrials.gov/api/v2/studies?query.titles=glioma SEARCH[Location](AREA[LocationCountry]Japan AND AREA[LocationStatus]Recruiting)&pageSize=100\n"
]
}
],
"source": [
"import gradio as gr\n",
"import pandas as pd\n",
"from OpenAITools.FetchTools import fetch_clinical_trials\n",
"from langchain_openai import ChatOpenAI\n",
"from langchain_groq import ChatGroq\n",
"from OpenAITools.CrinicalTrialTools import SimpleClinicalTrialAgent, GraderAgent, LLMTranslator, generate_ex_question_English\n",
"\n",
"# モデルとエージェントの初期化\n",
"groq = ChatGroq(model_name=\"llama3-70b-8192\", temperature=0)\n",
"translator = LLMTranslator(groq)\n",
"CriteriaCheckAgent = SimpleClinicalTrialAgent(groq)\n",
"grader_agent = GraderAgent(groq)\n",
"\n",
"# データフレームを生成する関数\n",
"def generate_dataframe(age, sex, tumor_type, GeneMutation, Meseable, Biopsiable):\n",
" # 日本語の腫瘍タイプを英語に翻訳\n",
" TumorName = translator.translate(tumor_type)\n",
"\n",
" # 質問文を生成\n",
" ex_question = generate_ex_question_English(age, sex, TumorName, GeneMutation, Meseable, Biopsiable)\n",
" \n",
" # 臨床試験データの取得\n",
" df = fetch_clinical_trials(TumorName)\n",
" df['AgentJudgment'] = None\n",
" df['AgentGrade'] = None\n",
" \n",
" # 臨床試験の適格性の評価\n",
" NCTIDs = list(df['NCTID'])\n",
" progress = gr.Progress(track_tqdm=True)\n",
" for i, nct_id in enumerate(NCTIDs):\n",
" target_criteria = df.loc[df['NCTID'] == nct_id, 'Eligibility Criteria'].values[0]\n",
" agent_judgment = CriteriaCheckAgent.evaluate_eligibility(target_criteria, ex_question)\n",
" agent_grade = grader_agent.evaluate_eligibility(agent_judgment)\n",
" \n",
" # データフレームの更新\n",
" df.loc[df['NCTID'] == nct_id, 'AgentJudgment'] = agent_judgment\n",
" df.loc[df['NCTID'] == nct_id, 'AgentGrade'] = agent_grade\n",
" progress((i + 1) / len(NCTIDs))\n",
" \n",
" # 列を指定した順に並び替え\n",
" columns_order = ['NCTID', 'AgentGrade', 'Title', 'AgentJudgment', 'Japanes Locations', \n",
" 'Primary Completion Date', 'Cancer', 'Summary', 'Eligibility Criteria']\n",
" df = df[columns_order]\n",
" \n",
" return df, df # フィルタ用と表示用にデータフレームを返す\n",
"\n",
"# 特定のAgentGrade(yes, no, unclear)に基づいて行をフィルタリングする関数\n",
"def filter_rows_by_grade(original_df, grade):\n",
" df_filtered = original_df[original_df['AgentGrade'] == grade]\n",
" return df_filtered, df_filtered\n",
"\n",
"# CSVとして保存しダウンロードする関数\n",
"def download_filtered_csv(df):\n",
" file_path = \"filtered_data.csv\"\n",
" df.to_csv(file_path, index=False)\n",
" return file_path\n",
"\n",
"# 全体結果をCSVとして保存しダウンロードする関数\n",
"def download_full_csv(df):\n",
" file_path = \"full_data.csv\"\n",
" df.to_csv(file_path, index=False)\n",
" return file_path\n",
"\n",
"# Gradioインターフェースの作成\n",
"with gr.Blocks() as demo:\n",
" gr.Markdown(\"## 臨床試験適格性評価インターフェース\")\n",
"\n",
" # 各種入力フィールド\n",
" age_input = gr.Textbox(label=\"Age\", placeholder=\"例: 65\")\n",
" sex_input = gr.Dropdown(choices=[\"男性\", \"女性\"], label=\"Sex\")\n",
" tumor_type_input = gr.Textbox(label=\"Tumor Type\", placeholder=\"例: gastric cancer, 日本でも良いですが英語の方が精度が高いです。\")\n",
" gene_mutation_input = gr.Textbox(label=\"Gene Mutation\", placeholder=\"例: HER2\")\n",
" measurable_input = gr.Dropdown(choices=[\"有り\", \"無し\", \"不明\"], label=\"Measurable Tumor\")\n",
" biopsiable_input = gr.Dropdown(choices=[\"有り\", \"無し\", \"不明\"], label=\"Biopsiable Tumor\")\n",
"\n",
" # データフレーム表示エリア\n",
" dataframe_output = gr.DataFrame()\n",
" original_df = gr.State()\n",
" filtered_df = gr.State()\n",
"\n",
" # データフレーム生成ボタン\n",
" generate_button = gr.Button(\"Generate Clinical Trials Data\")\n",
"\n",
" # フィルタリングボタン\n",
" yes_button = gr.Button(\"Show Eligible Trials\")\n",
" no_button = gr.Button(\"Show Ineligible Trials\")\n",
" unclear_button = gr.Button(\"Show Unclear Trials\")\n",
" \n",
" # ダウンロードボタン\n",
" download_filtered_button = gr.Button(\"Download Filtered Data\")\n",
" download_filtered_output = gr.File(label=\"Download Filtered Data\")\n",
"\n",
" download_full_button = gr.Button(\"Download Full Data\")\n",
" download_full_output = gr.File(label=\"Download Full Data\")\n",
"\n",
"\n",
" # ボタン動作の設定\n",
" 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])\n",
" yes_button.click(fn=filter_rows_by_grade, inputs=[original_df, gr.State(\"yes\")], outputs=[dataframe_output, filtered_df])\n",
" no_button.click(fn=filter_rows_by_grade, inputs=[original_df, gr.State(\"no\")], outputs=[dataframe_output, filtered_df])\n",
" unclear_button.click(fn=filter_rows_by_grade, inputs=[original_df, gr.State(\"unclear\")], outputs=[dataframe_output, filtered_df])\n",
" download_filtered_button.click(fn=download_filtered_csv, inputs=filtered_df, outputs=download_filtered_output)\n",
" download_full_button.click(fn=download_full_csv, inputs=original_df, outputs=download_full_output)\n",
"\n",
"\n",
"# インターフェースの起動\n",
"demo.launch()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "gradio",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|