Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 8,873 Bytes
787eac9 |
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "d31b58d0-132d-4a98-b199-c3b1d2ed9eb5",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/klkehl/miniconda3/envs/vllm/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n",
"Loading checkpoint shards: 100%|ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ| 2/2 [00:03<00:00, 1.62s/it]\n"
]
}
],
"source": [
"import gradio as gr\n",
"import pandas as pd\n",
"import torch\n",
"import torch.nn.functional as F\n",
"from sentence_transformers import SentenceTransformer\n",
"from safetensors import safe_open\n",
"from transformers import pipeline, AutoTokenizer\n",
"\n",
"# Load trial spaces data\n",
"trial_spaces = pd.read_csv('ctgov_all_trials_trial_space_lineitems_10-31-24.csv')\n",
"\n",
"# Load embedding model\n",
"embedding_model = SentenceTransformer('reranker_round2.model', trust_remote_code=True, device='cuda')\n",
"\n",
"# Load precomputed trial space embeddings\n",
"with safe_open(\"trial_space_embeddings.safetensors\", framework=\"pt\", device=0) as f:\n",
" trial_space_embeddings = f.get_tensor(\"space_embeddings\")\n",
"\n",
"# Load checker pipeline\n",
"tokenizer = AutoTokenizer.from_pretrained(\"roberta-large\")\n",
"checker_pipe = pipeline('text-classification', './roberta-checker', tokenizer=tokenizer, \n",
" truncation=True, padding='max_length', max_length=512, device='cuda')\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "36d48a31-8514-4b0d-84a9-5fccc7ec7227",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7860\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7860/\" 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": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import gradio as gr\n",
"import pandas as pd\n",
"import torch\n",
"import torch.nn.functional as F\n",
"from sentence_transformers import SentenceTransformer\n",
"from safetensors import safe_open\n",
"from transformers import pipeline, AutoTokenizer\n",
"\n",
"# We assume the following objects have already been loaded:\n",
"# trial_spaces (DataFrame), embedding_model (SentenceTransformer),\n",
"# trial_space_embeddings (torch.tensor), checker_pipe (transformers pipeline)\n",
"\n",
"def match_clinical_trials(patient_summary: str):\n",
" # Encode patient summary\n",
" patient_embedding = embedding_model.encode([patient_summary], convert_to_tensor=True)\n",
" \n",
" # Compute similarities\n",
" similarities = F.cosine_similarity(patient_embedding, trial_space_embeddings)\n",
" \n",
" # Pull top 10\n",
" sorted_similarities, sorted_indices = torch.sort(similarities, descending=True)\n",
" top_indices = sorted_indices[0:10].cpu().numpy()\n",
" \n",
" relevant_spaces = trial_spaces.iloc[top_indices].this_space\n",
" relevant_nctid = trial_spaces.iloc[top_indices].nct_id\n",
" relevant_title = trial_spaces.iloc[top_indices].title\n",
" relevant_brief_summary = trial_spaces.iloc[top_indices].brief_summary\n",
" relevant_eligibility_criteria = trial_spaces.iloc[top_indices].eligibility_criteria\n",
"\n",
" analysis = pd.DataFrame({\n",
" 'patient_summary': patient_summary, \n",
" 'this_space': relevant_spaces,\n",
" 'nct_id': relevant_nctid, \n",
" 'trial_title': relevant_title,\n",
" 'trial_brief_summary': relevant_brief_summary, \n",
" 'trial_eligibility_criteria': relevant_eligibility_criteria\n",
" }).reset_index(drop=True)\n",
" \n",
" analysis['pt_trial_pair'] = analysis['this_space'] + \"\\nNow here is the patient summary:\" + analysis['patient_summary']\n",
" \n",
" # Run checker pipeline\n",
" classifier_results = checker_pipe(analysis.pt_trial_pair.tolist())\n",
" analysis['trial_checker_result'] = [x['label'] for x in classifier_results]\n",
" analysis['trial_checker_score'] = [x['score'] for x in classifier_results]\n",
" \n",
" # Return a subset of columns that are most relevant\n",
" return analysis[[\n",
" 'nct_id', \n",
" 'trial_title', \n",
" 'trial_brief_summary', \n",
" 'trial_eligibility_criteria', \n",
" 'trial_checker_result', \n",
" 'trial_checker_score'\n",
" ]]\n",
"\n",
"custom_css = \"\"\"\n",
"#input_box textarea {\n",
" width: 600px !important;\n",
" height: 250px !important;\n",
"}\n",
"\n",
"#output_df table {\n",
" width: 100% !important;\n",
" table-layout: auto !important;\n",
" border-collapse: collapse !important;\n",
"}\n",
"\n",
"#output_df table td, #output_df table th {\n",
" min-width: 100px;\n",
" overflow: hidden;\n",
" text-overflow: ellipsis;\n",
" white-space: nowrap;\n",
" border: 1px solid #ccc;\n",
" padding: 4px;\n",
"}\n",
"\"\"\"\n",
"\n",
"# JavaScript for enabling colResizable\n",
"js_script = \"\"\"\n",
"<script src=\"https://code.jquery.com/jquery-3.6.0.min.js\"></script>\n",
"<script src=\"https://cdn.jsdelivr.net/npm/colresizable@1.6.0/colResizable-1.6.min.js\"></script>\n",
"<script>\n",
"document.addEventListener('DOMContentLoaded', function() {\n",
" var interval = setInterval(function() {\n",
" var table = document.querySelector('#output_df table');\n",
" if (table && typeof jQuery !== 'undefined' && typeof jQuery(table).colResizable === 'function') {\n",
" jQuery('#output_df table').colResizable({liveDrag:true});\n",
" clearInterval(interval);\n",
" }\n",
" }, 500);\n",
"});\n",
"</script>\n",
"\"\"\"\n",
"\n",
"with gr.Blocks(css=custom_css) as demo:\n",
" gr.HTML(\"<h3>Clinical Trial Matcher</h3>\")\n",
" patient_summary_input = gr.Textbox(label=\"Enter Patient Summary\", elem_id=\"input_box\")\n",
" submit_btn = gr.Button(\"Find Matches\")\n",
" output_df = gr.DataFrame(\n",
" headers=[\n",
" \"nct_id\", \n",
" \"trial_title\", \n",
" \"trial_brief_summary\", \n",
" \"trial_eligibility_criteria\", \n",
" \"trial_checker_result\", \n",
" \"trial_checker_score\"\n",
" ], \n",
" elem_id=\"output_df\"\n",
" )\n",
"\n",
" submit_btn.click(fn=match_clinical_trials, \n",
" inputs=patient_summary_input, \n",
" outputs=output_df)\n",
" \n",
" gr.HTML(js_script)\n",
"\n",
"demo.launch()\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "80ba3cd2-6a76-44d0-b5f4-6d3debd510ff",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Closing server running on port: 7860\n"
]
}
],
"source": [
"demo.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e43df71-6f06-48d2-8dce-b1f27ab40d6c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|