paragon-analytics commited on
Commit
5809f13
1 Parent(s): 6f88e98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -20
app.py CHANGED
@@ -30,17 +30,17 @@ pred = transformers.pipeline("text-classification", model=model,
30
  explainer = shap.Explainer(pred)
31
 
32
  ##
33
- classifier = transformers.pipeline("text-classification", model = "cross-encoder/qnli-electra-base")
34
 
35
- def med_score(x):
36
- label = x['label']
37
- score_1 = x['score']
38
- return round(score_1,3)
39
 
40
- def sym_score(x):
41
- label2sym= x['label']
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")
@@ -57,8 +57,8 @@ def adr_predict(x):
57
  shap_values = explainer([str(x).lower()])
58
  local_plot = shap.plots.text(shap_values[0], display=False)
59
 
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
 
@@ -69,7 +69,7 @@ def adr_predict(x):
69
  'Age': 'yellow',
70
  'Sex':'yellow',
71
  'Diagnostic_procedure':'gray',
72
- 'Biological_structure':'black'}
73
 
74
  htext = ""
75
  prev_end = 0
@@ -85,13 +85,14 @@ def adr_predict(x):
85
 
86
  htext += x[prev_end:]
87
 
88
- 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
 
89
 
90
 
91
  def main(prob1):
92
  text = str(prob1).lower()
93
  obj = adr_predict(text)
94
- return obj[0],obj[1],obj[2],obj[3],obj[4]
95
 
96
  title = "Welcome to **ADR Detector** 🪐"
97
  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."""
@@ -107,18 +108,20 @@ with gr.Blocks(title=title) as demo:
107
 
108
  with gr.Column(visible=True) as output_col:
109
  label = gr.Label(label = "Predicted Label")
110
- local_plot = gr.HTML(label = 'Shap:')
111
- htext = gr.HTML(label="NER")
112
 
113
  with gr.Column(visible=True) as output_col:
114
- med = gr.Label(label = "Contains Medication")
115
- sym = gr.Label(label = "Contains Symptoms")
 
 
116
 
117
  submit_btn.click(
118
  main,
119
  [prob1],
120
  [label
121
- ,local_plot, med, sym, htext
 
122
  ], api_name="adr"
123
  )
124
 
@@ -126,6 +129,8 @@ with gr.Blocks(title=title) as demo:
126
  gr.Markdown("### Click on any of the examples below to see how it works:")
127
  gr.Examples([["A 35 year-old male had severe headache after taking Aspirin. The lab results were normal."],
128
  ["A 35 year-old female had minor pain in upper abdomen after taking Acetaminophen."]],
129
- [prob1], [label,local_plot, med, sym,htext], main, cache_examples=True)
 
 
130
 
131
  demo.launch()
 
30
  explainer = shap.Explainer(pred)
31
 
32
  ##
33
+ # classifier = transformers.pipeline("text-classification", model = "cross-encoder/qnli-electra-base")
34
 
35
+ # def med_score(x):
36
+ # label = x['label']
37
+ # score_1 = x['score']
38
+ # return round(score_1,3)
39
 
40
+ # def sym_score(x):
41
+ # label2sym= x['label']
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")
 
57
  shap_values = explainer([str(x).lower()])
58
  local_plot = shap.plots.text(shap_values[0], display=False)
59
 
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
 
 
69
  'Age': 'yellow',
70
  'Sex':'yellow',
71
  'Diagnostic_procedure':'gray',
72
+ 'Biological_structure':'silver'}
73
 
74
  htext = ""
75
  prev_end = 0
 
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."""
 
108
 
109
  with gr.Column(visible=True) as output_col:
110
  label = gr.Label(label = "Predicted Label")
111
+
 
112
 
113
  with gr.Column(visible=True) as output_col:
114
+ local_plot = gr.HTML(label = 'Shap:')
115
+ htext = gr.HighlightedText(label="NER")
116
+ # med = gr.Label(label = "Contains Medication")
117
+ # sym = gr.Label(label = "Contains Symptoms")
118
 
119
  submit_btn.click(
120
  main,
121
  [prob1],
122
  [label
123
+ ,local_plot, htext
124
+ # , med, sym
125
  ], api_name="adr"
126
  )
127
 
 
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
 
136
  demo.launch()