awacke1 commited on
Commit
4f0303f
1 Parent(s): 1f6d9f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -1,23 +1,23 @@
 
 
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
8
-
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:
16
  example_json = json.load(f)
17
- EXAMPLE_MAP = {x["text"]: x["label"] for x in example_json}
18
-
19
- pipe = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
20
-
21
 
22
  def group_by_entity(raw):
23
  out = defaultdict(int)
@@ -51,9 +51,10 @@ def ner(text):
51
  for x in raw
52
  ],
53
  }
 
54
  grouped = group_by_entity(raw)
55
  figure = plot_to_figure(grouped)
56
- label = EXAMPLE_MAP.get(text, "Unknown")
57
 
58
  meta = {
59
  "entity_counts": grouped,
@@ -73,7 +74,7 @@ interface = gr.Interface(
73
  gr.Label(label="Rating"),
74
  gr.Plot(label="Bar"),
75
  ],
76
- examples=list(EXAMPLE_MAP.keys()),
77
  allow_flagging="never",
78
  )
79
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
  import json
4
  from collections import defaultdict
5
 
6
+ # Create tokenizer for biomed model
 
 
7
  from transformers import pipeline, AutoTokenizer, AutoModelForTokenClassification
 
8
  tokenizer = AutoTokenizer.from_pretrained("d4data/biomedical-ner-all")
9
  model = AutoModelForTokenClassification.from_pretrained("d4data/biomedical-ner-all")
10
+ pipe = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
11
 
12
+ # Matplotlib for entity graph
13
+ import matplotlib.pyplot as plt
14
  plt.switch_backend("Agg")
15
 
16
+ # Load examples from JSON
17
+ EXAMPLES = {}
18
  with open("examples.json", "r") as f:
19
  example_json = json.load(f)
20
+ EXAMPLES = {x["text"]: x["label"] for x in example_json}
 
 
 
21
 
22
  def group_by_entity(raw):
23
  out = defaultdict(int)
 
51
  for x in raw
52
  ],
53
  }
54
+
55
  grouped = group_by_entity(raw)
56
  figure = plot_to_figure(grouped)
57
+ label = EXAMPLES.get(text, "Unknown")
58
 
59
  meta = {
60
  "entity_counts": grouped,
 
74
  gr.Label(label="Rating"),
75
  gr.Plot(label="Bar"),
76
  ],
77
+ examples=list(EXAMPLES.keys()),
78
  allow_flagging="never",
79
  )
80