paragon-analytics commited on
Commit
8a754d8
·
1 Parent(s): 2695c04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -33
app.py CHANGED
@@ -42,10 +42,10 @@ explainer = shap.Explainer(pred)
42
  # score_1sym = x['score']
43
  # return round(score_1sym,3)
44
 
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):
@@ -60,41 +60,39 @@ 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
- # res = ner_pipe(x)
64
 
65
- # entity_colors = {
66
- # 'Severity': 'red',
67
- # 'Sign_symptom': 'green',
68
- # 'Medication': 'blue',
69
- # 'Age': 'yellow',
70
- # 'Sex':'yellow',
71
- # 'Diagnostic_procedure':'gray',
72
- # 'Biological_structure':'silver'}
73
-
74
- # htext = ""
75
- # prev_end = 0
76
-
77
- # for entity in res:
78
- # start = entity['start']
79
- # end = entity['end']
80
- # word = entity['word'].replace("##", "")
81
- # color = entity_colors[entity['entity_group']]
82
 
83
- # htext += f"{x[prev_end:start]}<mark style='background-color:{color};'>{word}</mark>"
84
- # prev_end = end
85
 
86
- # htext += x[prev_end:]
87
 
88
- return {"Severe Reaction": float(scores.numpy()[1]), "Non-severe Reaction": float(scores.numpy()[0])}, local_plot
89
- # ,htext
90
  # ,{"Contains Medication": float(med), "No Medications": float(1-med)} , {"Contains Symptoms": float(sym), "No Symptoms": float(1-sym)}
91
 
92
 
93
  def main(prob1):
94
  text = str(prob1).lower()
95
  obj = adr_predict(text)
96
- return obj[0],obj[1]
97
- # ,obj[2]
98
 
99
  title = "Welcome to **ADR Detector** 🪐"
100
  description1 = """This app takes text (up to a few sentences) and predicts to what extent the text describes severe (or non-severe) adverse reaction to medicaitons. Please do NOT use for medical diagnosis."""
@@ -114,7 +112,7 @@ with gr.Blocks(title=title) as demo:
114
 
115
  with gr.Column(visible=True) as output_col:
116
  local_plot = gr.HTML(label = 'Shap:')
117
- # htext = gr.HTML(label="NER")
118
  # med = gr.Label(label = "Contains Medication")
119
  # sym = gr.Label(label = "Contains Symptoms")
120
 
@@ -122,8 +120,7 @@ with gr.Blocks(title=title) as demo:
122
  main,
123
  [prob1],
124
  [label
125
- ,local_plot
126
- # , htext
127
  # , med, sym
128
  ], api_name="adr"
129
  )
@@ -132,8 +129,7 @@ with gr.Blocks(title=title) as demo:
132
  gr.Markdown("### Click on any of the examples below to see how it works:")
133
  gr.Examples([["A 35 year-old male had severe headache after taking Aspirin. The lab results were normal."],
134
  ["A 35 year-old female had minor pain in upper abdomen after taking Acetaminophen."]],
135
- [prob1], [label,local_plot
136
- # , htext
137
  # , med, sym
138
  ], main, cache_examples=True)
139
 
 
42
  # score_1sym = x['score']
43
  # return round(score_1sym,3)
44
 
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):
 
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
+ 'Age': 'yellow',
70
+ 'Sex':'yellow',
71
+ 'Diagnostic_procedure':'gray',
72
+ 'Biological_structure':'silver'}
73
+
74
+ htext = ""
75
+ prev_end = 0
76
+
77
+ for entity in res:
78
+ start = entity['start']
79
+ end = entity['end']
80
+ word = entity['word'].replace("##", "")
81
+ color = entity_colors[entity['entity_group']]
82
 
83
+ htext += f"{x[prev_end:start]}<mark style='background-color:{color};'>{word}</mark>"
84
+ prev_end = end
85
 
86
+ htext += x[prev_end:]
87
 
88
+ return {"Severe Reaction": float(scores.numpy()[1]), "Non-severe Reaction": float(scores.numpy()[0])}, local_plot,htext
 
89
  # ,{"Contains Medication": float(med), "No Medications": float(1-med)} , {"Contains Symptoms": float(sym), "No Symptoms": float(1-sym)}
90
 
91
 
92
  def main(prob1):
93
  text = str(prob1).lower()
94
  obj = adr_predict(text)
95
+ return obj[0],obj[1],obj[2]
 
96
 
97
  title = "Welcome to **ADR Detector** 🪐"
98
  description1 = """This app takes text (up to a few sentences) and predicts to what extent the text describes severe (or non-severe) adverse reaction to medicaitons. Please do NOT use for medical diagnosis."""
 
112
 
113
  with gr.Column(visible=True) as output_col:
114
  local_plot = gr.HTML(label = 'Shap:')
115
+ htext = gr.HTML(label="NER")
116
  # med = gr.Label(label = "Contains Medication")
117
  # sym = gr.Label(label = "Contains Symptoms")
118
 
 
120
  main,
121
  [prob1],
122
  [label
123
+ ,local_plot, htext
 
124
  # , med, sym
125
  ], api_name="adr"
126
  )
 
129
  gr.Markdown("### Click on any of the examples below to see how it works:")
130
  gr.Examples([["A 35 year-old male had severe headache after taking Aspirin. The lab results were normal."],
131
  ["A 35 year-old female had minor pain in upper abdomen after taking Acetaminophen."]],
132
+ [prob1], [label,local_plot, htext
 
133
  # , med, sym
134
  ], main, cache_examples=True)
135