File size: 4,709 Bytes
89e119e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c61557
c9f396c
7399091
89e119e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import pipeline
import gradio as gr
from flair.data import Sentence
from flair.models import SequenceTagger
import torch



#constants

GR_TXT1 = """
    Recent studies have identified several key mutations associated with familial breast cancer.
    The BRCA1 c.5266dupC mutation (p.Gln1756Profs74) was detected in 12% of patients, while the BRCA2 p.Ser1982fs variant showed reduced penetrance.
    Analysis revealed that the TP53 R175H substitution co-occurred with EGFR L858R in 8 cases.
    Additionally, the HER2 amplification correlated with response to trastuzumab therapy.
    Novel variants including PIK3CA E545K and AKT1 E17K were identified in endocrine-resistant tumors.
    The KRAS G12D mutation remained a negative predictive marker for anti-EGFR therapy.
"""

GR_TXT2 = """
    During the 12-week treatment period, 23 patients experienced adverse events.
    The most common was grade 2 nausea occurring in 15 participants, followed by fatigue in 11 cases.
    Three patients developed severe neutropenia requiring dose reduction, and two experienced thrombocytopenia.
    One case of drug-induced hepatotoxicity was documented with elevated ALT levels.
    Mild headache and dizziness were reported by 8 patients during the first week.
    Serious adverse events included one instance of anaphylaxis and two cases of Stevens-Johnson syndrome, leading to treatment discontinuation.
    No cardiovascular events or nephrotoxicity were observed.
  """


GR_MARKDOWN_TXT = """ 
# 🧬 OTAR3088 Work-in-progress NER-models demo space 
This space allows for the **visualisation of outputs / review of our WIP NER-models.** Presently these models include... 👀:  
- **Adverse Events**  
- **Variants**   

👉 Try it out:  
1. Select a model from the dropdown menu.  
2. Enter/paste your text into the input box, or use our provided example biomedical paragraphs.  
3. View the extracted entities highlighted directly in **"Tagged Entities"** box.  
**Note📢:** Models in this demo are continuously updated and improved as part of our ongoing research.  
"""


GR_THEME = gr.themes.Soft(
    primary_hue="indigo",
    secondary_hue="rose",
    neutral_hue="gray"
)




MODEL_REGISTRY = {
    "Variants-V1": "OTAR3088/Variants-V1",
    "Variants_New":"Mardiyyah/variant_tapt_freeze_llrd_LR_5e",
    "Variants-Reinit-LLRD": "OTAR3088/Variant_reinit-llrd_PuBMedBert_V1",
    "PDBe-Variants-V2.1": "PDBEurope/BiomedNLP-PubMedBERT-ProteinStructure-NER-v2.1",
    "AdverseEvent-PHEE-V1": "OTAR3088/hf-phee-V1"
   }


hf_pipes = {}
flair_pipes = {}



def load_model(model_name):
  if model_name.lower().startswith("flair"):
    if not model_name in flair_pipes:
      # Force default tensor type
      torch.set_default_dtype(torch.float32)

      flair_pipes[model_name] = SequenceTagger.load(MODEL_REGISTRY[model_name])
      flair_pipes[model_name].to(torch.device("cpu"))
      flair_pipes[model_name].float()
    return flair_pipes[model_name], "flair"
  else:
    if not model_name in hf_pipes:
      hf_pipes[model_name] = pipeline("ner", model=MODEL_REGISTRY[model_name], aggregation_strategy='simple')
    return hf_pipes[model_name], "hf"



def tagger(text, model_name):
  model, model_type = load_model(model_name)
  if model_type == "flair":
    sentence = Sentence(text)
    model.predict(sentence)
    entities = [{"start": ent.start_position, 
                 "end": ent.end_position, 
                 "score": ent.score, 
                 "entity": ent.tag} for ent in sentence.get_spans('ner')]

  elif model_type == "hf":
    entities = model(text)

  return {"text": text, "entities": entities}


def gradio_ui():
  with gr.Blocks(theme=GR_THEME) as demo:
    with gr.Row():
        with gr.Column(scale=7):
            gr.Markdown(GR_MARKDOWN_TXT,elem_classes="full-width")
            input_text = gr.Textbox(label="Enter your text here", type='text', placeholder="Biomedical Input text", lines=8)
        

            gr.Examples(
                    examples=[GR_TXT1, GR_TXT2],
                inputs=[input_text],
                label= "Example Biomedical texts to try (fabricated texts, not from literature)"
            )
            run_btn = gr.Button("Submit Text", variant="primary")

        with gr.Column(scale=7):
            model_choice = gr.Dropdown(choices=list(MODEL_REGISTRY.keys()), label="Select a model for Inference")
            output_highlight = gr.HighlightedText(label="Tagged Entities")
            


    run_btn.click(
          fn=tagger,
          inputs=[input_text, model_choice],
          outputs=[output_highlight]
      )

        

    return demo




if __name__ == "__main__":
  app = gradio_ui()
  app.launch()