Spaces:
Sleeping
Sleeping
Commit
·
32eb862
1
Parent(s):
9631ab8
WP
Browse files
app.py
CHANGED
|
@@ -1,13 +1,11 @@
|
|
| 1 |
import json
|
| 2 |
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
api = gr.Interface.load("d4data/biomedical-ner-all", src="models")
|
| 8 |
-
result = api(text)
|
| 9 |
-
return result
|
| 10 |
-
|
| 11 |
|
| 12 |
EXAMPLE_TEXTS = []
|
| 13 |
with open("examples.json", "r") as f:
|
|
@@ -15,10 +13,31 @@ with open("examples.json", "r") as f:
|
|
| 15 |
EXAMPLE_TEXTS = [x["text"] for x in example_json]
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
interface = gr.Interface(
|
| 19 |
ner,
|
| 20 |
inputs=gr.Textbox(label="Input", value=""),
|
| 21 |
-
outputs=["json"],
|
| 22 |
examples=EXAMPLE_TEXTS,
|
| 23 |
)
|
| 24 |
|
|
|
|
| 1 |
import json
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 6 |
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("d4data/biomedical-ner-all")
|
| 8 |
+
model = AutoModelForTokenClassification.from_pretrained("d4data/biomedical-ner-all")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
EXAMPLE_TEXTS = []
|
| 11 |
with open("examples.json", "r") as f:
|
|
|
|
| 13 |
EXAMPLE_TEXTS = [x["text"] for x in example_json]
|
| 14 |
|
| 15 |
|
| 16 |
+
pipe = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def ner(text):
|
| 20 |
+
raw = pipe(text)
|
| 21 |
+
result = {
|
| 22 |
+
"text": text,
|
| 23 |
+
"entities": [
|
| 24 |
+
{
|
| 25 |
+
"entity": x["entity_group"],
|
| 26 |
+
"word": x["word"],
|
| 27 |
+
"score": x["score"],
|
| 28 |
+
"start": x["start"],
|
| 29 |
+
"end": x["end"],
|
| 30 |
+
}
|
| 31 |
+
for x in raw
|
| 32 |
+
],
|
| 33 |
+
}
|
| 34 |
+
return result, {}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
interface = gr.Interface(
|
| 38 |
ner,
|
| 39 |
inputs=gr.Textbox(label="Input", value=""),
|
| 40 |
+
outputs=[gr.HighlightedText(combine_adjacent=True), "json"],
|
| 41 |
examples=EXAMPLE_TEXTS,
|
| 42 |
)
|
| 43 |
|