Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
|
| 3 |
+
|
| 4 |
+
# Load BioBERT model for NER
|
| 5 |
+
model_name = "d4data/biobert_ner" # pretrained BioBERT for biomedical NER
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForTokenClassification.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
# Create a NER pipeline
|
| 10 |
+
ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
|
| 11 |
+
|
| 12 |
+
# Inference function
|
| 13 |
+
def bio_ner(text):
|
| 14 |
+
ner_results = ner_pipeline(text)
|
| 15 |
+
annotated = ""
|
| 16 |
+
last_end = 0
|
| 17 |
+
for ent in ner_results:
|
| 18 |
+
start, end, label = ent['start'], ent['end'], ent['entity_group']
|
| 19 |
+
annotated += text[last_end:start]
|
| 20 |
+
annotated += f"[{text[start:end]}]({label})"
|
| 21 |
+
last_end = end
|
| 22 |
+
annotated += text[last_end:]
|
| 23 |
+
return annotated
|
| 24 |
+
|
| 25 |
+
# Gradio interface
|
| 26 |
+
gr.Interface(
|
| 27 |
+
fn=bio_ner,
|
| 28 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter biomedical text here..."),
|
| 29 |
+
outputs="text",
|
| 30 |
+
title="🧬 BioBERT NER",
|
| 31 |
+
description="Uses BioBERT to perform Named Entity Recognition on biomedical text."
|
| 32 |
+
).launch()
|