Spaces:
Sleeping
Sleeping
Commit
·
5826177
1
Parent(s):
24d9d43
WIP
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import json
|
| 2 |
from collections import defaultdict
|
| 3 |
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
| 6 |
from transformers import pipeline, AutoTokenizer, AutoModelForTokenClassification
|
|
@@ -8,6 +9,7 @@ from transformers import pipeline, AutoTokenizer, AutoModelForTokenClassificatio
|
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained("d4data/biomedical-ner-all")
|
| 9 |
model = AutoModelForTokenClassification.from_pretrained("d4data/biomedical-ner-all")
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
EXAMPLE_MAP = {}
|
| 13 |
with open("examples.json", "r") as f:
|
|
@@ -21,10 +23,17 @@ def group_by_entity(raw):
|
|
| 21 |
out = defaultdict(int)
|
| 22 |
for ent in raw:
|
| 23 |
out[ent["entity_group"]] += 1
|
| 24 |
-
out["total"] = sum(out.values())
|
| 25 |
return out
|
| 26 |
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def ner(text):
|
| 29 |
raw = pipe(text)
|
| 30 |
ner_content = {
|
|
@@ -41,9 +50,16 @@ def ner(text):
|
|
| 41 |
],
|
| 42 |
}
|
| 43 |
grouped = group_by_entity(raw)
|
| 44 |
-
|
| 45 |
-
label = EXAMPLE_MAP.get(text,
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
interface = gr.Interface(
|
|
@@ -53,9 +69,10 @@ interface = gr.Interface(
|
|
| 53 |
gr.HighlightedText(label="NER", combine_adjacent=True),
|
| 54 |
gr.JSON(label="Entity Counts"),
|
| 55 |
gr.Label(label="Rating"),
|
| 56 |
-
"
|
| 57 |
],
|
| 58 |
examples=list(EXAMPLE_MAP.keys()),
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
interface.launch()
|
|
|
|
| 1 |
import json
|
| 2 |
from collections import defaultdict
|
| 3 |
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
import gradio as gr
|
| 6 |
import pandas as pd
|
| 7 |
from transformers import pipeline, AutoTokenizer, AutoModelForTokenClassification
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained("d4data/biomedical-ner-all")
|
| 10 |
model = AutoModelForTokenClassification.from_pretrained("d4data/biomedical-ner-all")
|
| 11 |
|
| 12 |
+
plt.switch_backend("Agg")
|
| 13 |
|
| 14 |
EXAMPLE_MAP = {}
|
| 15 |
with open("examples.json", "r") as f:
|
|
|
|
| 23 |
out = defaultdict(int)
|
| 24 |
for ent in raw:
|
| 25 |
out[ent["entity_group"]] += 1
|
| 26 |
+
# out["total"] = sum(out.values())
|
| 27 |
return out
|
| 28 |
|
| 29 |
|
| 30 |
+
def plot_to_figure(grouped):
|
| 31 |
+
fig = plt.figure()
|
| 32 |
+
plt.bar(x=list(grouped.keys()), height=list(grouped.values()))
|
| 33 |
+
plt.xticks(rotation=90)
|
| 34 |
+
return fig
|
| 35 |
+
|
| 36 |
+
|
| 37 |
def ner(text):
|
| 38 |
raw = pipe(text)
|
| 39 |
ner_content = {
|
|
|
|
| 50 |
],
|
| 51 |
}
|
| 52 |
grouped = group_by_entity(raw)
|
| 53 |
+
figure = plot_to_figure(grouped)
|
| 54 |
+
label = EXAMPLE_MAP.get(text, "Unknown")
|
| 55 |
+
|
| 56 |
+
meta = {
|
| 57 |
+
"entity_counts": grouped,
|
| 58 |
+
"entities": len(set(grouped.keys())),
|
| 59 |
+
"counts": sum(grouped.values()),
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
return (ner_content, meta, label, figure)
|
| 63 |
|
| 64 |
|
| 65 |
interface = gr.Interface(
|
|
|
|
| 69 |
gr.HighlightedText(label="NER", combine_adjacent=True),
|
| 70 |
gr.JSON(label="Entity Counts"),
|
| 71 |
gr.Label(label="Rating"),
|
| 72 |
+
gr.Plot(label="Bar"),
|
| 73 |
],
|
| 74 |
examples=list(EXAMPLE_MAP.keys()),
|
| 75 |
+
allow_flagging="never",
|
| 76 |
)
|
| 77 |
|
| 78 |
interface.launch()
|