paragon-analytics commited on
Commit
cbc546e
1 Parent(s): 80582bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -6
app.py CHANGED
@@ -45,8 +45,8 @@ def sym_score(x):
45
  ner_tokenizer = AutoTokenizer.from_pretrained("d4data/biomedical-ner-all")
46
  ner_model = AutoModelForTokenClassification.from_pretrained("d4data/biomedical-ner-all")
47
 
48
- ner_pipe = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple") # pass device=0 if using gpu
49
- ##
50
 
51
  def adr_predict(x):
52
  encoded_input = tokenizer(x, return_tensors='pt')
@@ -60,7 +60,26 @@ def adr_predict(x):
60
  med = med_score(classifier(x+str(", There is a medication."))[0])
61
  sym = sym_score(classifier(x+str(", There is a symptom."))[0])
62
 
63
- htext = ner_pipe(x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  return {"Severe Reaction": float(scores.numpy()[1]), "Non-severe Reaction": float(scores.numpy()[0])}, local_plot, {"Contains Medication": float(med), "No Medications": float(1-med)} , {"Contains Symptoms": float(sym), "No Symptoms": float(1-sym)},htext
66
 
@@ -85,7 +104,7 @@ with gr.Blocks(title=title) as demo:
85
  with gr.Column(visible=True) as output_col:
86
  label = gr.Label(label = "Predicted Label")
87
  local_plot = gr.HTML(label = 'Shap:')
88
- htext = gr.HighlightedText(label="NER", combine_adjacent=True, show_legend=True)
89
 
90
  with gr.Column(visible=True) as output_col:
91
  med = gr.Label(label = "Contains Medication")
@@ -101,7 +120,8 @@ with gr.Blocks(title=title) as demo:
101
 
102
  with gr.Row():
103
  gr.Markdown("### Click on any of the examples below to see how it works:")
104
- gr.Examples([["I had severe headache after taking Aspirin."],["I had minor stomachache after taking Acetaminophen."]],
 
105
  [prob1], [label,local_plot, med, sym,htext], main, cache_examples=True)
106
 
107
- demo.launch()
 
45
  ner_tokenizer = AutoTokenizer.from_pretrained("d4data/biomedical-ner-all")
46
  ner_model = AutoModelForTokenClassification.from_pretrained("d4data/biomedical-ner-all")
47
 
48
+ ner_pipe = pipeline("ner", model=ner_model, tokenizer=ner_tokenizer, aggregation_strategy="simple") # pass device=0 if using gpu
49
+ #
50
 
51
  def adr_predict(x):
52
  encoded_input = tokenizer(x, return_tensors='pt')
 
60
  med = med_score(classifier(x+str(", There is a medication."))[0])
61
  sym = sym_score(classifier(x+str(", There is a symptom."))[0])
62
 
63
+ res = ner_pipe(x)
64
+
65
+ entity_colors = {
66
+ 'Severity': 'red',
67
+ 'Sign_symptom': 'green',
68
+ 'Medication': 'blue'}
69
+
70
+ htext = ""
71
+ prev_end = 0
72
+
73
+ for entity in res:
74
+ start = entity['start']
75
+ end = entity['end']
76
+ word = entity['word'].replace("##", "")
77
+ color = entity_colors[entity['entity_group']]
78
+
79
+ htext += f"{x[prev_end:start]}<mark style='background-color:{color};'>{word}</mark>"
80
+ prev_end = end
81
+
82
+ htext += x[prev_end:]
83
 
84
  return {"Severe Reaction": float(scores.numpy()[1]), "Non-severe Reaction": float(scores.numpy()[0])}, local_plot, {"Contains Medication": float(med), "No Medications": float(1-med)} , {"Contains Symptoms": float(sym), "No Symptoms": float(1-sym)},htext
85
 
 
104
  with gr.Column(visible=True) as output_col:
105
  label = gr.Label(label = "Predicted Label")
106
  local_plot = gr.HTML(label = 'Shap:')
107
+ htext = gr.HTML(label="NER")
108
 
109
  with gr.Column(visible=True) as output_col:
110
  med = gr.Label(label = "Contains Medication")
 
120
 
121
  with gr.Row():
122
  gr.Markdown("### Click on any of the examples below to see how it works:")
123
+ gr.Examples([["A 35 year-old male had severe headache after taking Aspirin. The lab results were normal."],
124
+ ["A 35 year-old female had minor pain in upper abdomen after taking Acetaminophen."]],
125
  [prob1], [label,local_plot, med, sym,htext], main, cache_examples=True)
126
 
127
+ demo.launch()