File size: 18,470 Bytes
6bf206f
5055394
 
a8be9e1
 
5055394
 
 
01f4616
196a7f0
 
58f7749
 
2896f25
f9b632a
 
 
 
a8be9e1
f85c47a
3fff86b
5055394
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f81b95a
f3ce64a
5055394
 
 
 
a8be9e1
6bf206f
5055394
 
 
bb9501b
a8be9e1
5055394
6bf206f
196a7f0
 
 
 
 
05c9e6a
 
 
 
 
 
 
6bf206f
5055394
6bf206f
5055394
 
 
 
a8be9e1
 
5055394
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7daca3
 
 
 
 
 
5055394
 
 
 
a493dba
5055394
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8be9e1
05c9e6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6bc335
05c9e6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc7b414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377d024
5055394
05c9e6a
cc7b414
6bf206f
5055394
f2e901b
a8be9e1
6bf206f
a8be9e1
6bf206f
a8be9e1
 
 
6bf206f
a8be9e1
6bf206f
a8be9e1
 
 
 
712d59b
f2e901b
6bf206f
5055394
 
 
 
 
d678393
5055394
 
 
 
 
 
 
 
 
d678393
5055394
 
d678393
5055394
 
d678393
5055394
 
 
 
dba0d48
5055394
 
 
 
 
 
 
 
 
 
 
74ad3c4
5055394
6c1c16e
753eeda
6c1c16e
 
 
 
5055394
 
 
 
 
 
6c1c16e
 
 
 
 
5055394
524acf2
 
5055394
05c9e6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a271eeb
05c9e6a
a271eeb
05c9e6a
6c1c16e
 
 
 
 
5055394
 
 
8c7099f
5055394
 
 
6c1c16e
5055394
 
 
6c1c16e
5055394
 
 
6c1c16e
5055394
 
 
6c1c16e
5055394
 
 
6c1c16e
 
 
 
 
5055394
 
6c1c16e
5055394
 
 
6c1c16e
 
 
05c9e6a
6c1c16e
 
 
 
 
 
 
cb9b179
6c1c16e
 
05c9e6a
 
 
 
 
 
 
cc7b414
 
 
 
 
 
 
 
 
 
05c9e6a
 
 
6c1c16e
05c9e6a
cc7b414
6c1c16e
 
 
 
 
 
05c9e6a
 
 
 
 
 
6c1c16e
05c9e6a
6c1c16e
cc7b414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c1c16e
 
 
 
 
cabc9dd
 
6c1c16e
 
 
 
 
ccb2b2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5055394
ccb2b2f
 
 
 
 
 
6c1c16e
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import os
import time
import asyncio
from llama_index.core.query_engine import CitationQueryEngine
from llama_index.core import VectorStoreIndex
from llama_index.core import Settings
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.gemini import Gemini
from llama_index.core.postprocessor import SimilarityPostprocessor
from llama_index.core.storage.docstore import SimpleDocumentStore
from llama_index.core import StorageContext, load_index_from_storage
import re
import pandas as pd
import gradio as gr
import logging

#Enable logging to see what's happening behind the scenes
logging.basicConfig(level=logging.INFO)

token_w = os.environ['token_w']
HF_TOKEN=os.environ['token_r']
API_KEY=os.environ["GOOGLE_API_KEY"]

generation_config = {
  "temperature": 0,
  # "top_p": 1,
  # "top_k": 1,
  "max_output_tokens":8192,
}

safety_settings = [
  {
    "category": "HARM_CATEGORY_HARASSMENT",
    "threshold": "BLOCK_NONE"
  },
  {
    "category": "HARM_CATEGORY_HATE_SPEECH",
    "threshold": "BLOCK_NONE"
  },
  {
    "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
    "threshold": "BLOCK_NONE"
  },
  {
    "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
    "threshold": "BLOCK_NONE"
  },
]

llm = Gemini(
    model="models/gemini-1.5-flash-002",
    generation_config=generation_config,
    safety_settings=safety_settings,
)

# Setup embedder
embed_model_name = "BAAI/bge-small-en-v1.5"
embed_model = HuggingFaceEmbedding(model_name=embed_model_name)

Settings.llm = llm
Settings.embed_model = embed_model

# rebuild storage context
storage_context = StorageContext.from_defaults(persist_dir="VectorStore")
# load index
index_persisted = load_index_from_storage(storage_context, index_id="vector_index")

async def remove_ref(text):
    split_text = re.split(r'\bReference Papers\b', text, flags=re.IGNORECASE)

    if len(split_text) > 1:
        return split_text[0].strip()
    
    return text.strip()

async def run_function_on_text(top_k,study_obj,study_type,phase,purpose,allocation,intervention_model,Masking,conditions,interventions,location_countries,removed_location_countries):

  # Set up query engine
  query_engine_get_study = CitationQueryEngine.from_args(
    index_persisted,
    similarity_top_k=top_k,
    citation_chunk_size=2048,
    verbose=True,
    node_postprocessors=[SimilarityPostprocessor(similarity_cutoff=0.8)],
    use_async=True
  )

  #Build prompt
  study_information = f"""
  #Study Objectives/Study Description
  {study_obj}

  #Intervention
  {interventions}

  #Location
    - Location_Countries: {location_countries}
    - Removed Location: {removed_location_countries}

  #Conditions
   Cancer {conditions}

  #Study Design
    - Study Type: {study_type}
    - Phase: {phase}
    - Primary Purpose: {purpose}
    - Allocation: {allocation}
    - Interventional Model: {intervention_model}
    - Masking: None {Masking}
  """

  # Query
  query_response = await query_engine_get_study.aquery(f"""
      Based on the provided instructions and clinical trial information, generate the new eligibility criteria by analyzing the related studies and clinical trial information.
      Find suitable papers that have relevant or similar to the clinical trial information(### Clinical Trial Information).
      Prioritize the following topics when finding related studies:
      1. Study Objectives
      2. Study Design and Phases
      3. Conditions
      4. Intervention/Treatment
      Criteria generation:
      As a clinical researcher, generate new eligibility criteria for given clinical trial information.
      Analyze the information from related studies for more precise new eligibility criteria generation.
      Ensure the criteria are clear, specific, and reasonable for a clinical research information.
      
      Reference Papers generation:
      Please give us NCT IDs and study names for {top_k} used papers.

      Please follows the pattern of the output(### Pattern of the output).
      --------------------------------------------------
      ### Clinical Trial Information
      {study_information}
      --------------------------------------------------
      ### Pattern of the output
      Inclusion Criteria
      1.
      2.
      .
      .
      .

      Exclusion Criteria
      1.
      2.
      .
      .
      .

      Reference Papers
      1.NCT ID:
        Study Name:
        Condition:
        Intervention/Treatment:
      2.NCT ID:
        Study Name:
        Condition:
        Intervention/Treatment:
      .
      .
      .


      """
    )

  #Extract ref
  if query_response.response != "Empty Response":
    pattern = r'Reference Papers:?\s*(.*?)(?:\n\n.*$|$)'
    match = re.search(pattern, query_response.response, re.DOTALL | re.IGNORECASE)
    ext_ref = match.group(1) if match and match.group(1) else ''
    split_ref = re.split(r'\d+\.\s+', ext_ref)[1:]

    formatted_ref = []
    n=0
    for ref in split_ref:
        nct_match = re.search(r'NCT[_ ]ID: (NCT\d+)', ref)
        if nct_match:
            nct_id = nct_match.group(1)
        else:
            nct_match = re.search(r'(NCT\d+)', ref)
            if nct_match:
                nct_id = nct_match.group(1)
            else:
                continue
        n+=1        
        study_name = re.search(r'Study Name:?\s*(.*?)(?=\n|Condition:|Intervention/Treatment:|$)', ref, re.DOTALL).group(1).strip()
        condition = re.search(r'Condition:?\s*(.*?)(?=\n|Intervention/Treatment:|$)', ref, re.DOTALL).group(1).strip()
        intervention = re.search(r'Intervention/Treatment:?\s*(.*?)(?=\n|$)', ref, re.DOTALL).group(1).strip()

        study_name = re.sub(r'\*+', '', study_name).strip()
        condition = re.sub(r'\*+', '', condition).strip()
        intervention = re.sub(r'\*+', '', intervention).strip()     

        formatted_trial = [
            n,
            f'<a href="https://clinicaltrials.gov/study/{nct_id}"><u>{nct_id}</u></a>',
            study_name,
            condition,
            intervention
        ]

        formatted_ref.append(formatted_trial)
      
  else:
    formatted_ref = []

  #Extract criteria
  if query_response.response == "Empty Response":
    return query_response,formatted_ref
  else:
    removed_ref = await remove_ref(query_response.response)
    combine_criteira = re.sub(r'##\s*', '', removed_ref).strip()
    combine_criteira = re.sub(r'#\s*', '', combine_criteira).strip()
    combine_criteira = re.sub(r'\*\*', '', combine_criteira).strip()
    combine_criteira = re.sub(r'(Criteria)\n\s*\n(\d+\.)', r'\1\n\2', combine_criteira).strip()

    return combine_criteira,formatted_ref

  # # LLM.complete
  # complete_response  = await llm.acomplete(f"""
  #     Based on the provided instructions and clinical trial information, generate the new eligibility criteria by analyzing clinical trial information(### Clinical Trial Information).
  #     ### Instruction:
  #     Criteria generation:
  #     As a clinical researcher, generate new eligibility criteria for given clinical trial information.
  #     Ensure the criteria are clear, specific, and reasonable for a clinical research information.

  #     Prioritize the following topics in clinical trial information.:
  #     1. Study Objectives
  #     2. Study Design and Phases
  #     3. Conditions
  #     4. Intervention/Treatment

  #     Please follow the pattern of the output(### Pattern of the output).
  #     --------------------------------------------------
  #     ### Clinical Trial Information
  #     {study_information}
  #     --------------------------------------------------
  #     ### Pattern of the output
  #     Inclusion Criteria
  #     1.
  #     2.
  #     .
  #     .
  #     .

  #     Exclusion Criteria
  #     1.
  #     2.
  #     .
  #     .
  #     .


  #     """
  #   )

  # combine_response = await llm.acomplete(f"""
  #     Based on the provided instructions clinical, clinical trial information, and criteria information, generate the appropriate eligibility criteria for ### Clinical Trial Information by analyze clinical trial information(### Clinical Trial Information), criteria 1 (### Criteria 1) and criteria 2 (### Criteria 2).
  #     ### Instruction:
  #     Criteria generation:
  #     As a clinical researcher, generate appropriate eligibility criteria by analyzing given information.
  #     Ensure the criteria are clear, specific, and reasonable for a clinical research information(### Clinical Trial Information).

  #     Prioritize the following topics in clinical trial information.:
  #     1. Study Objectives
  #     2. Study Design and Phases
  #     3. Conditions
  #     4. Intervention/Treatment

  #     Do not generate redundant inclusion and exclusion criteria. For example, if a criterion is included in one set of inclusion or exclusion criteria, do not include it again.

  #     Reference Papers generation:
  #     Please give us NCT IDs and study names from the references list in ### Criteria 1.

  #     Please follow the pattern of the output(### Pattern of the output).
  #     --------------------------------------------------
  #     ### Clinical Trial Information
  #     {study_information}
  #     --------------------------------------------------
  #     ### Criteria 1
  #     {query_response}
  #     --------------------------------------------------
  #     ### Criteria 2
  #     {complete_response}
  #     --------------------------------------------------
  #     ### Pattern of the output
  #     Inclusion Criteria
  #     1.
  #     2.
  #     .
  #     .
  #     .

  #     Exclusion Criteria
  #     1.
  #     2.
  #     .
  #     .
  #     .

  #     Reference Papers
  #     1.NCT ID:
  #       Study Name:
  #       Condition:
  #       Intervention/Treatment:
  #     2.NCT ID:
  #       Study Name:
  #       Condition:
  #       Intervention/Treatment:
  #     .
  #     .
  #     .
  #     """
    # )

  # return query_response
  # return query_response,complete_response,combine_response

# Place holder
place_holder = f"""Study Objectives
The purpose of this study is to evaluate the safety, tolerance and efficacy of Liposomal Paclitaxel With Nedaplatin as First-line in patients with Advanced or Recurrent Esophageal Carcinoma

Conditions: Esophageal Carcinoma

Intervention / Treatment:
DRUG: Liposomal Paclitaxel,
DRUG: Nedaplatin

Location: China

Study Design and Phases
Study Type: INTERVENTIONAL
Phase: PHASE2 Primary Purpose:
TREATMENT Allocation: NA
Interventional Model: SINGLE_GROUP Masking: NONE
"""

objective_place_holder = f"""Example: The purpose of this study is to evaluate the safety, tolerance and efficacy of Liposomal Paclitaxel With Nedaplatin as First-line in patients with Advanced or Recurrent Esophageal Carcinoma
"""

conditions_place_holder = f"""Example: Esophageal Carcinoma
"""

interventions_place_holder = f"""Example:
- Drug: irinotecan hydrochloride
   - Given IV
   - Other Names:
       - Campto
       - Camptosar
       - CPT-11
       - irinotecan
       - U-101440E

- Drug: Amoxicillin hydrate
   - Amoxicillin hydrate (potency)

- Procedure: Stem cell transplant
   - See Detailed Description section for details of treatment interventions.

- Biological: Pneumococcal Vaccine
   - Subcutaneously on Day 0
   - Other Names:
       - Prevnar

- Drug: Doxorubicin, Cotrimoxazole, Carboplatin, Ifosfamide

- Drug: Irinotecan
   - Irinotecan will be administered at a dose of 180mg/m2 IV over 90 minutes on day 21 every 42 days.
   - Other Names:
      - CAMPTOSAR™

- Drug: Placeblo
   - Placebo tablet
"""

prefilled_value = f"""[Clinicaltrials.gov](https://clinicaltrials.gov/) """

with gr.Blocks() as demo:

  with gr.Row():
      gr.Markdown("# Reference paper"),
  with gr.Row():
      top_k_box = gr.Slider(
          label="Amount of reference paper",
          value=5,
          minimum=0,
          maximum=30,
          step=1,
          )
  # Study description
  with gr.Row():
      gr.Markdown("# Research Information"),
  with gr.Row():
      study_obj_box = gr.Textbox(
          label="Study Objective / Study Description", # Study description
          # placeholder=prefilled_value,
          placeholder=objective_place_holder,
          lines=10)
  # Conditions
  with gr.Row():
      gr.Markdown("# Conditions"),
  with gr.Row():
      conditions_box = gr.Textbox(
          label="Conditions / Disease",
          info="Primary Disease or Condition of Cancer Being Studied in the Trial, or the Focus of the Study",
          placeholder=conditions_place_holder,
          )
  #Interventions
  with gr.Row():
      gr.Markdown("# Interventions / Drugs"),
  with gr.Row():
      intervention_box = gr.Textbox(
          label="Intervention type",
          info="A process or action studied in a clinical trial, including drugs, devices, procedures, vaccines, or noninvasive approaches.",
          placeholder=interventions_place_holder,
          # lines=5,
          )
  # Study Design
  with gr.Row():
      gr.Markdown("# Study Design"),
  with gr.Column():
      study_type_box = gr.Radio(
          ["Expanded Access", "Interventional", "Observational"],
          label="Study Type",
          )
      phase_box= gr.CheckboxGroup(
          ["Not Applicable", "Early Phase 1", "Phase 1", "Phase 2", "Phase 3", "Phase 4"],
          label="Phase"
          )
      purpose_box = gr.Radio(
          ["Treatment", "Prevention", "Diagnostic", "Educational/Counseling/Training", "Supportive Care", "Screening", "Health Services Research", "Basic Science", "Device Feasibility", "Other"],
          label="Primary Purpose"
          )
      allocation_box = gr.Radio(
          ["Randomized", "Non-Randomized", "N/A"],
          label="Allocation"
          )
      intervention_model_box = gr.Radio(
          ["Parallel", "Single-Group", "Crossover", "Factorial", "Sequential"],
          label="Interventional Model"
          )
      masking_box = gr.Radio(
          ["None (Open Label)", "Single", "Double", "Triple", "Quadruple"],
          label="Masking"
          )
  #Location
  with gr.Row():
      gr.Markdown("# Location"),
  with gr.Column():
      location_box = gr.Textbox(
          label="Location (Countries)",
          )
      removed_location_box = gr.Textbox(
          label="Removed Location (Countries)",
          )

  # Submit & Clear
  with gr.Row():
    submit_button = gr.Button("Submit")
    clear_button = gr.Button("Clear")

  # Output
  with gr.Row():
      gr.Markdown("# Eligibility Criteria Generation"),
  with gr.Row():
      with gr.Column():
          base_box = gr.Textbox(
              label="Response",
              lines=15,
              interactive=False)
  with gr.Row():
          ref_table = gr.Dataframe(
              label="Reference",
              headers=["No.",'Link', 'Study name', 'Condition', 'Intervention'],
              datatype=["markdown","html","markdown", "markdown","markdown"],
              wrap=True,
              interactive=False)
      # with gr.Column():
      #     rag_box = gr.Textbox(
      #         label="Response 2",
      #         lines=15,
      #         interactive=False)
      # with gr.Column():
      #     combine_box = gr.Textbox(
      #         label="Response 3",
      #         lines=15,
      #         interactive=False)
  with gr.Row():
    regenerate_button = gr.Button("Regenerate")
      
  inputs_information = [top_k_box, study_obj_box, study_type_box, phase_box, purpose_box, allocation_box, intervention_model_box, masking_box, conditions_box, intervention_box, location_box, removed_location_box]
  outputs_information = [base_box,ref_table]
  # outputs_information = [base_box, rag_box,combine_box]

  submit_button.click(
      run_function_on_text,
      inputs=inputs_information,
      outputs=outputs_information
      )
    
  regenerate_button.click(
      run_function_on_text,
      inputs=inputs_information,
      outputs=outputs_information
      )

  clear_button.click(lambda : [None] * len(inputs_information), outputs=inputs_information)

  # with gr.Row():
  #     selected_response = gr.Radio(
  #         choices=[
  #             "Response 1",
  #             "Response 2",
              # "Response 3",
  #             "All responses are equally good",
  #             "Neither response is satisfactory"
  #             ],
  #         label="Select the best response"
  #     )
  # with gr.Row():
  #     flag_button = gr.Button("Flag Selected Response")

  # #Flagging
  # dataset_name = "ravistech/feedback-demo-space"
  # hf_writer = gr.HuggingFaceDatasetSaver(hf_token=token_w, dataset_name=dataset_name, private=True)
  # hf_writer.setup([selected_response, study_obj_box, study_type_box, phase_box, purpose_box, allocation_box, intervention_model_box, masking_box, conditions_box, intervention_box, location_box, removed_location_box, top_k_box, base_box, rag_box, combine_box],dataset_name)

  # flag_button.click(lambda *args: hf_writer.flag(list(args)), [selected_response, study_obj_box, study_type_box, phase_box, purpose_box, allocation_box, intervention_model_box, masking_box, conditions_box, intervention_box, location_box, removed_location_box, top_k_box, base_box, rag_box, combine_box], None, preprocess=False)

  #Clear all
  with gr.Row():
    clear_all_button = gr.Button("Clear All")

  # flag_response = [selected_response]
  all_information = inputs_information + outputs_information #+ flag_response
  clear_all_button.click(lambda : [None] * len(all_information), outputs=all_information)

if __name__ == "__main__":
  demo.launch(debug=True)

# custom_css = """
# .gradio-container {
#     font-family: 'Roboto', sans-serif;
# }
# .main-header {
#     text-align: center;
#     color: #4a4a4a;
#     margin-bottom: 2rem;
# }
# .tab-header {
#     font-size: 1.2rem;
#     font-weight: bold;
#     margin-bottom: 1rem;
# }
# .custom-chatbot {
#     border-radius: 10px;
#     box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
# }
# .custom-button {
#     background-color: #3498db;
#     color: white;
#     border: none;
#     padding: 10px 20px;
#     border-radius: 5px;
#     cursor: pointer;
#     transition: background-color 0.3s ease;
# }
# .custom-button:hover {
#     background-color: #2980b9;
# }
# """

# # Define Gradio theme
# theme = gr.themes.Default(
#     primary_hue="zinc",
#     secondary_hue="red",
#     neutral_hue="neutral",
#     font=[gr.themes.GoogleFont('Roboto'), "sans-serif"]
# )