Spaces:
Sleeping
Sleeping
File size: 8,731 Bytes
ce4a4e7 6aa59d5 ce4a4e7 6aa59d5 ce4a4e7 6aa59d5 ce4a4e7 6aa59d5 ce4a4e7 6aa59d5 ce4a4e7 6aa59d5 ce4a4e7 |
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 186 187 188 189 190 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/yw/qz00x75d7kb98f7vm8dkhkvw0000gn/T/ipykernel_63095/2786558369.py:6: LangChainDeprecationWarning: As of langchain-core 0.3.0, LangChain uses pydantic v2 internally. The langchain_core.pydantic_v1 module was a compatibility shim for pydantic v1, and should no longer be used. Please update the code to import from Pydantic directly.\n",
"\n",
"For example, replace imports like: `from langchain_core.pydantic_v1 import BaseModel`\n",
"with: `from pydantic import BaseModel`\n",
"or the v1 compatibility namespace if you are working in a code base that has not been fully upgraded to pydantic 2 yet. \tfrom pydantic.v1 import BaseModel\n",
"\n",
" from OpenAITools.CrinicalTrialTools import QuestionModifierEnglish, TumorNameExtractor, SimpleClinicalTrialAgent, GraderAgent\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7861\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7861/\" 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": 1,
"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, fetch_clinical_trials_jp\n",
"from langchain_openai import ChatOpenAI\n",
"from langchain_groq import ChatGroq\n",
"from OpenAITools.CrinicalTrialTools import QuestionModifierEnglish, TumorNameExtractor, SimpleClinicalTrialAgent, GraderAgent\n",
"\n",
"# モデルとエージェントの初期化\n",
"groq = ChatGroq(model_name=\"llama3-70b-8192\", temperature=0)\n",
"modifier = QuestionModifierEnglish(groq)\n",
"extractor = TumorNameExtractor(groq)\n",
"CriteriaCheckAgent = SimpleClinicalTrialAgent(groq)\n",
"grader_agent = GraderAgent(groq)\n",
"\n",
"# データフレームを生成する関数\n",
"def generate_dataframe_from_question(ex_question):\n",
" # Modify and extract tumor name\n",
" modified_question = modifier.modify_question(ex_question)\n",
" tumor_name = extractor.extract_tumor_name(ex_question)\n",
"\n",
" # Get clinical trials data based on tumor name\n",
" df = fetch_clinical_trials(tumor_name)\n",
" df['AgentJudgment'] = None\n",
" df['AgentGrade'] = None\n",
" \n",
" # NCTIDのリストを作成し、プログレスバーを表示\n",
" NCTIDs = list(df['NCTID'])\n",
" progress = gr.Progress(track_tqdm=True)\n",
" \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, modified_question)\n",
" agent_grade = grader_agent.evaluate_eligibility(agent_judgment)\n",
" \n",
" # Update DataFrame\n",
" df.loc[df['NCTID'] == nct_id, 'AgentJudgment'] = agent_judgment\n",
" df.loc[df['NCTID'] == nct_id, 'AgentGrade'] = agent_grade\n",
" \n",
" # プログレスバーを更新(進行状況を浮動小数点数で渡す)\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 # フィルタした結果を2つ返す\n",
"\n",
"# CSVとして保存しダウンロードする関数\n",
"def download_filtered_csv(df):\n",
" file_path = \"filtered_data.csv\" # 現在の作業ディレクトリに保存\n",
" df.to_csv(file_path, index=False) # CSVファイルとして保存\n",
" return file_path\n",
"\n",
"# Gradioインターフェースの作成\n",
"with gr.Blocks() as demo:\n",
" # 説明\n",
" gr.Markdown(\"## 質問を入力して、患者さんが参加可能な臨床治験の情報を収集。参加可能か否かを判断根拠も含めて提示します。結果はcsvとしてダウンロード可能です\")\n",
" \n",
" # 質問入力ボックス\n",
" question_input = gr.Textbox(label=\"質問を入力してください\", placeholder=\"例: 65歳男性でBRCA遺伝子の変異がある前立腺癌患者さんが参加できる臨床治験を教えて下さい。\")\n",
"\n",
" # データフレーム表示エリア\n",
" dataframe_output = gr.DataFrame()\n",
" \n",
" # データの元となるDataFrameを保存するためのstate\n",
" original_df = gr.State()\n",
" filtered_df = gr.State()\n",
"\n",
" # データフレームを生成するボタン\n",
" generate_button = gr.Button(\"日本で行われている患者さんの癌腫の臨床治験を全て取得する\")\n",
"\n",
" # ボタンでAgentGradeがyes, no, unclearの行のみ表示\n",
" yes_button = gr.Button(\"AI Agentが患者さんが参加可能であると判断した臨床治験のみを表示\")\n",
" no_button = gr.Button(\"I Agentが患者さんが参加不可であると判断した臨床治験のみを表示\")\n",
" unclear_button = gr.Button(\"AI Agentが与えられた情報だけでは判断不可能とした臨床治験のみを表示\")\n",
" \n",
" # フィルタ結果をダウンロードするボタン\n",
" download_button = gr.Button(\"フィルタ結果をCSVとしてダウンロード\")\n",
" download_output = gr.File() # ダウンロード用の出力エリア\n",
"\n",
" # データフレームを生成して保存\n",
" generate_button.click(fn=generate_dataframe_from_question, inputs=question_input, outputs=[dataframe_output, original_df])\n",
"\n",
" # yesボタン、noボタン、unclearボタンが押されたらフィルタしたデータを表示\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",
"\n",
" # ダウンロードボタンを押すとフィルタ結果のCSVをダウンロード\n",
" download_button.click(fn=download_filtered_csv, inputs=filtered_df, outputs=download_output)\n",
"\n",
"# Gradioインターフェースの起動\n",
"demo.launch()"
]
},
{
"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
}
|