Add app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
GR_PLACEHOLDER_TXT = """
|
| 11 |
+
Aside from in vivo models, numerous studies investigating bacterial virulence and pathogenesis have also
|
| 12 |
+
employed in vitro cell line models to gain an initial understanding of the intricate host-pathogen interactions.
|
| 13 |
+
These studies, which are simpler and more cost-effective than those using in vivo models,
|
| 14 |
+
serve as the foundation for many in vivo studies by providing additional data to support any conclusions [12].
|
| 15 |
+
Epithelial mucous membrane cells are the primary focus of most in vitro investigations due to them being usually the initial point of contact for infections [12,13].
|
| 16 |
+
HeLa cells, which originate from human cervical epithelial cells, are thus frequently selected for bacterial adhesion and invasion and are particularly suitable for experiments [14].
|
| 17 |
+
A. baumannii frequently infects human epithelial tissues, such as the respiratory system, skin and mucosal linings [15].
|
| 18 |
+
HeLa cells are resilient and readily cultured in vitro, exhibiting a rapid growth rate.
|
| 19 |
+
This ensures the availability of a uniform and consistent cell population for studies, rendering them economical and reliable.
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
GR_MARKDOWN_TXT = """
|
| 24 |
+
|
| 25 |
+
# 🧬 OTAR3088 Biomedical NER Demo
|
| 26 |
+
|
| 27 |
+
Welcome to the **OTAR3088 Entity Extraction for Knowledge discovery Project demo**.
|
| 28 |
+
|
| 29 |
+
This space showcases models trained to recognize the following biomedical entities:
|
| 30 |
+
- **CellLine**
|
| 31 |
+
- **CellType**
|
| 32 |
+
- **Tissue**
|
| 33 |
+
|
| 34 |
+
These entities are collectively referred to as **"CELLaTe"**.
|
| 35 |
+
|
| 36 |
+
👉 Try it out:
|
| 37 |
+
1. Select a model from the dropdown menu.
|
| 38 |
+
2. Enter/paste your text into the input box, or use our provided example biomedical paragraphs.
|
| 39 |
+
3. View the extracted entities highlighted directly in "Tagged Entities" box.
|
| 40 |
+
|
| 41 |
+
**Note:** Models in this demo are continuously updated and improved as part of our ongoing research.
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
GR_THEME = gr.themes.Soft(
|
| 48 |
+
primary_hue="indigo",
|
| 49 |
+
secondary_hue="rose",
|
| 50 |
+
neutral_hue="gray"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
MODEL_REGISTRY = {
|
| 57 |
+
"CellFinder-V1-Model": "OTAR3088/bioformer-cellfinder_V1",
|
| 58 |
+
"CeLLaTe-V1-Model": "OTAR3088/bioformer-cellate_V1",
|
| 59 |
+
"Flair-CeLLaTe-Model": "OTAR3088/flair-microsoft-cellate_cellfinder-V1"
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
hf_pipes = {}
|
| 64 |
+
flair_pipes = {}
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def load_model(model_name):
|
| 69 |
+
if model_name.lower().startswith("flair"):
|
| 70 |
+
if not model_name in flair_pipes:
|
| 71 |
+
# Force default tensor type
|
| 72 |
+
torch.set_default_dtype(torch.float32)
|
| 73 |
+
|
| 74 |
+
flair_pipes[model_name] = SequenceTagger.load(MODEL_REGISTRY[model_name])
|
| 75 |
+
flair_pipes[model_name].to(torch.device("cpu"))
|
| 76 |
+
flair_pipes[model_name].float()
|
| 77 |
+
return flair_pipes[model_name], "flair"
|
| 78 |
+
else:
|
| 79 |
+
if not model_name in hf_pipes:
|
| 80 |
+
hf_pipes[model_name] = pipeline("ner", model=MODEL_REGISTRY[model_name], aggregation_strategy='simple')
|
| 81 |
+
return hf_pipes[model_name], "hf"
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def tagger(text, model_name):
|
| 86 |
+
model, model_type = load_model(model_name)
|
| 87 |
+
if model_type == "flair":
|
| 88 |
+
sentence = Sentence(text)
|
| 89 |
+
model.predict(sentence)
|
| 90 |
+
entities = [{"start": ent.start_position,
|
| 91 |
+
"end": ent.end_position,
|
| 92 |
+
"score": ent.score,
|
| 93 |
+
"entity": ent.tag} for ent in sentence.get_spans('ner')]
|
| 94 |
+
|
| 95 |
+
elif model_type == "hf":
|
| 96 |
+
entities = model(text)
|
| 97 |
+
|
| 98 |
+
return {"text": text, "entities": entities}
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def gradio_ui():
|
| 102 |
+
with gr.Blocks(theme=GR_THEME) as demo:
|
| 103 |
+
gr.Markdown(GR_MARKDOWN_TXT)
|
| 104 |
+
|
| 105 |
+
with gr.Row():
|
| 106 |
+
with gr.Column(scale=7):
|
| 107 |
+
model_choice = gr.Dropdown(choices=list(MODEL_REGISTRY.keys()), label="Select a model for Inference")
|
| 108 |
+
input_text = gr.Textbox(label="Enter your text here", type='text', placeholder="Biomedical Input text", lines=8)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
gr.Examples(
|
| 112 |
+
examples=[GR_PLACEHOLDER_TXT],
|
| 113 |
+
inputs=[input_text],
|
| 114 |
+
label= "Example Biomedical texts to try"
|
| 115 |
+
)
|
| 116 |
+
run_btn = gr.Button("Submit Text")
|
| 117 |
+
|
| 118 |
+
with gr.Column(scale=7):
|
| 119 |
+
output_highlight = gr.HighlightedText(label="Tagged Entities")
|
| 120 |
+
|
| 121 |
+
run_btn.click(
|
| 122 |
+
fn=tagger,
|
| 123 |
+
inputs=[input_text, model_choice],
|
| 124 |
+
outputs=[output_highlight]
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
return demo
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
if __name__ == "__main__":
|
| 135 |
+
app = gradio_ui()
|
| 136 |
+
app.launch(share=True, debug=True)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|