christine-withers commited on
Commit
89e119e
·
verified ·
1 Parent(s): bbc0bfe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +146 -0
app.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import gradio as gr
3
+ from flair.data import Sentence
4
+ from flair.models import SequenceTagger
5
+ import torch
6
+
7
+
8
+
9
+ #constants
10
+
11
+ GR_TXT1 = """
12
+ Recent studies have identified several key mutations associated with familial breast cancer.
13
+ The BRCA1 c.5266dupC mutation (p.Gln1756Profs74) was detected in 12% of patients, while the BRCA2 p.Ser1982fs variant showed reduced penetrance.
14
+ Analysis revealed that the TP53 R175H substitution co-occurred with EGFR L858R in 8 cases.
15
+ Additionally, the HER2 amplification correlated with response to trastuzumab therapy.
16
+ Novel variants including PIK3CA E545K and AKT1 E17K were identified in endocrine-resistant tumors.
17
+ The KRAS G12D mutation remained a negative predictive marker for anti-EGFR therapy.
18
+ """
19
+
20
+ GR_TXT2 = """
21
+ During the 12-week treatment period, 23 patients experienced adverse events.
22
+ The most common was grade 2 nausea occurring in 15 participants, followed by fatigue in 11 cases.
23
+ Three patients developed severe neutropenia requiring dose reduction, and two experienced thrombocytopenia.
24
+ One case of drug-induced hepatotoxicity was documented with elevated ALT levels.
25
+ Mild headache and dizziness were reported by 8 patients during the first week.
26
+ Serious adverse events included one instance of anaphylaxis and two cases of Stevens-Johnson syndrome, leading to treatment discontinuation.
27
+ No cardiovascular events or nephrotoxicity were observed.
28
+ """
29
+
30
+
31
+ GR_MARKDOWN_TXT = """
32
+ # 🧬 OTAR3088 Work-in-progress NER-models demo space
33
+ This space allows for the **visualisation of outputs / review of our WIP NER-models.** Presently these models include... 👀:
34
+ - **Adverse Events**
35
+ - **Variants**
36
+
37
+ 👉 Try it out:
38
+ 1. Select a model from the dropdown menu.
39
+ 2. Enter/paste your text into the input box, or use our provided example biomedical paragraphs.
40
+ 3. View the extracted entities highlighted directly in **"Tagged Entities"** box.
41
+ **Note📢:** Models in this demo are continuously updated and improved as part of our ongoing research.
42
+ """
43
+
44
+
45
+ GR_THEME = gr.themes.Soft(
46
+ primary_hue="indigo",
47
+ secondary_hue="rose",
48
+ neutral_hue="gray"
49
+ )
50
+
51
+
52
+
53
+
54
+ MODEL_REGISTRY = {
55
+ "Variants-V1": "OTAR3088/Variants-V1",
56
+ "AdverseEvent-PHEE-V1": "OTAR3088/hf-phee-V1"
57
+ }
58
+
59
+
60
+ hf_pipes = {}
61
+ flair_pipes = {}
62
+
63
+
64
+
65
+ def load_model(model_name):
66
+ if model_name.lower().startswith("flair"):
67
+ if not model_name in flair_pipes:
68
+ # Force default tensor type
69
+ torch.set_default_dtype(torch.float32)
70
+
71
+ flair_pipes[model_name] = SequenceTagger.load(MODEL_REGISTRY[model_name])
72
+ flair_pipes[model_name].to(torch.device("cpu"))
73
+ flair_pipes[model_name].float()
74
+ return flair_pipes[model_name], "flair"
75
+ else:
76
+ if not model_name in hf_pipes:
77
+ hf_pipes[model_name] = pipeline("ner", model=MODEL_REGISTRY[model_name], aggregation_strategy='simple')
78
+ return hf_pipes[model_name], "hf"
79
+
80
+
81
+
82
+ def tagger(text, model_name):
83
+ model, model_type = load_model(model_name)
84
+ if model_type == "flair":
85
+ sentence = Sentence(text)
86
+ model.predict(sentence)
87
+ entities = [{"start": ent.start_position,
88
+ "end": ent.end_position,
89
+ "score": ent.score,
90
+ "entity": ent.tag} for ent in sentence.get_spans('ner')]
91
+
92
+ elif model_type == "hf":
93
+ entities = model(text)
94
+
95
+ return {"text": text, "entities": entities}
96
+
97
+
98
+ def gradio_ui():
99
+ with gr.Blocks(theme=GR_THEME) as demo:
100
+ with gr.Row():
101
+ with gr.Column(scale=7):
102
+ gr.Markdown(GR_MARKDOWN_TXT,elem_classes="full-width")
103
+ input_text = gr.Textbox(label="Enter your text here", type='text', placeholder="Biomedical Input text", lines=8)
104
+
105
+
106
+ gr.Examples(
107
+ examples=[GR_TXT1, GR_TXT2],
108
+ inputs=[input_text],
109
+ label= "Example Biomedical texts to try (fabricated texts, not from literature)"
110
+ )
111
+ run_btn = gr.Button("Submit Text", variant="primary")
112
+
113
+ with gr.Column(scale=7):
114
+ model_choice = gr.Dropdown(choices=list(MODEL_REGISTRY.keys()), label="Select a model for Inference")
115
+ output_highlight = gr.HighlightedText(label="Tagged Entities")
116
+
117
+
118
+ # with gr.Row():
119
+ # with gr.Column(scale=7):
120
+ # input_text = gr.Textbox(label="Enter your text here", type='text', placeholder="Biomedical Input text", lines=8)
121
+
122
+
123
+ # gr.Examples(
124
+ # examples=[GR_TXT1, GR_TXT2],
125
+ # inputs=[input_text],
126
+ # label= "Example Biomedical texts to try"
127
+ # )
128
+ # run_btn = gr.Button("Submit Text", variant="primary")
129
+
130
+
131
+ run_btn.click(
132
+ fn=tagger,
133
+ inputs=[input_text, model_choice],
134
+ outputs=[output_highlight]
135
+ )
136
+
137
+
138
+
139
+ return demo
140
+
141
+
142
+
143
+
144
+ if __name__ == "__main__":
145
+ app = gradio_ui()
146
+ app.launch()