willwim commited on
Commit
a9d0b27
·
verified ·
1 Parent(s): 32fcc81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -43
app.py CHANGED
@@ -3,7 +3,7 @@ import shap
3
  import numpy as np
4
  import scipy as sp
5
  import torch
6
- import tensorflow as tf
7
  import transformers
8
  from transformers import pipeline
9
  from transformers import RobertaTokenizer, RobertaModel
@@ -15,7 +15,6 @@ import csv
15
  import os
16
 
17
  HF_TOKEN = os.getenv("hf_token")
18
-
19
  csv.field_size_limit(sys.maxsize)
20
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
21
 
@@ -32,31 +31,26 @@ explainer = shap.Explainer(pred)
32
  # NER pipeline
33
  ner_tokenizer = AutoTokenizer.from_pretrained("d4data/biomedical-ner-all")
34
  ner_model = AutoModelForTokenClassification.from_pretrained("d4data/biomedical-ner-all")
35
- ner_pipe = pipeline("ner", model=ner_model, tokenizer=ner_tokenizer, aggregation_strategy="simple") # pass device=0 if using gpu
36
 
37
- # def adr_predict(x):
38
  def adr_predict(x):
39
  # Ensure input is treated as a string
40
  text_input = str(x).lower()
41
-
42
- encoded_input = tokenizer(text_input, return_tensors='pt').to(device) # Move input to device
43
  output = model(**encoded_input)
44
 
45
- scores = torch.softmax(output.logits, dim=-1)[0].detach().cpu().numpy() # Apply softmax on logits, move to cpu and convert to numpy
46
-
47
  try:
48
  shap_values = explainer([text_input])
49
-
50
- local_plot = shap.plots.text(shap_values[0], display=False)
51
-
52
  except Exception as e:
53
  print(f"SHAP explanation failed: {e}")
54
- local_plot = "<p>SHAP explanation not available.</p>" # Provide a fallback
55
 
56
  # NER processing
57
  try:
58
  res = ner_pipe(text_input)
59
-
60
  entity_colors = {
61
  'Severity': 'red',
62
  'Sign_symptom': 'green',
@@ -68,29 +62,24 @@ def adr_predict(x):
68
  }
69
  htext = ""
70
  prev_end = 0
71
-
72
  res = sorted(res, key=lambda x: x['start'])
73
-
74
  for entity in res:
75
  start = entity['start']
76
  end = entity['end']
77
- word = text_input[start:end] # Extract original text segment
78
  entity_type = entity['entity_group']
79
- color = entity_colors.get(entity_type, 'lightgray') # Use get with a default color
80
-
81
- # Append text before the entity
82
  htext += f"{text_input[prev_end:start]}"
83
- # Append the highlighted entity
84
  htext += f"<mark style='background-color:{color};'>{word}</mark>"
85
  prev_end = end
86
- # Append any remaining text after the last entity
87
  htext += text_input[prev_end:]
88
  except Exception as e:
89
  print(f"NER processing failed: {e}")
90
- htext = "<p>NER processing not available.</p>" # Provide a fallback
91
 
92
  label_output = {"Severe Reaction": float(scores[1]), "Non-severe Reaction": float(scores[0])}
93
-
94
  return label_output, local_plot, htext
95
 
96
  def main(prob1):
@@ -99,33 +88,26 @@ def main(prob1):
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."""
101
 
102
- # Use the 'with' syntax for Blocks
103
  with gr.Blocks(title=title) as demo:
104
  gr.Markdown(f"## {title}")
105
  gr.Markdown(description1)
106
  gr.Markdown("""---""")
107
-
108
- # Define input and output components
109
- prob1 = gr.Textbox(label="Enter Your Text Here:",lines=2, placeholder="Type it here ...")
110
-
111
- # Output components matching the return values of the main function
112
- label = gr.Label(label = "Predicted Label")
113
- local_plot = gr.HTML(label = 'Shap Explanation')
114
  htext = gr.HTML(label="Named Entity Recognition")
115
-
116
  submit_btn = gr.Button("Analyze")
117
-
118
  submit_btn.click(
119
- fn=main, # The function to call
120
- inputs=[prob1], # The input components
121
- outputs=[label, local_plot, htext], # The output components
122
- api_name="adr" # Keep the api_name if you intend to use the API
123
  )
124
-
125
- # Examples section
126
  gr.Markdown("### Click on any of the examples below to see how it works:")
127
- # Gradio 4.0+ Examples usage. Pass inputs and outputs components directly.
128
- # cache_examples is recommended for faster loading of examples.
129
  gr.Examples(
130
  examples=[
131
  ["A 35 year-old male had severe headache after taking Aspirin. The lab results were normal."],
@@ -133,10 +115,9 @@ with gr.Blocks(title=title) as demo:
133
  ],
134
  inputs=[prob1],
135
  outputs=[label, local_plot, htext],
136
- fn=main, # Provide the function to run for caching examples
137
  cache_examples=False,
138
  run_on_click=True
139
  )
140
 
141
- # Launch the demo
142
  demo.launch()
 
3
  import numpy as np
4
  import scipy as sp
5
  import torch
6
+ # import tensorflow as tf <-- Removed to match your requirements
7
  import transformers
8
  from transformers import pipeline
9
  from transformers import RobertaTokenizer, RobertaModel
 
15
  import os
16
 
17
  HF_TOKEN = os.getenv("hf_token")
 
18
  csv.field_size_limit(sys.maxsize)
19
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
20
 
 
31
  # NER pipeline
32
  ner_tokenizer = AutoTokenizer.from_pretrained("d4data/biomedical-ner-all")
33
  ner_model = AutoModelForTokenClassification.from_pretrained("d4data/biomedical-ner-all")
34
+ ner_pipe = pipeline("ner", model=ner_model, tokenizer=ner_tokenizer, aggregation_strategy="simple")
35
 
 
36
  def adr_predict(x):
37
  # Ensure input is treated as a string
38
  text_input = str(x).lower()
39
+ encoded_input = tokenizer(text_input, return_tensors='pt').to(device)
 
40
  output = model(**encoded_input)
41
 
42
+ scores = torch.softmax(output.logits, dim=-1)[0].detach().cpu().numpy()
43
+
44
  try:
45
  shap_values = explainer([text_input])
46
+ local_plot = shap.plots.text(shap_values[0], display=False)
 
 
47
  except Exception as e:
48
  print(f"SHAP explanation failed: {e}")
49
+ local_plot = "<p>SHAP explanation not available.</p>"
50
 
51
  # NER processing
52
  try:
53
  res = ner_pipe(text_input)
 
54
  entity_colors = {
55
  'Severity': 'red',
56
  'Sign_symptom': 'green',
 
62
  }
63
  htext = ""
64
  prev_end = 0
 
65
  res = sorted(res, key=lambda x: x['start'])
 
66
  for entity in res:
67
  start = entity['start']
68
  end = entity['end']
69
+ word = text_input[start:end]
70
  entity_type = entity['entity_group']
71
+ color = entity_colors.get(entity_type, 'lightgray')
72
+
 
73
  htext += f"{text_input[prev_end:start]}"
 
74
  htext += f"<mark style='background-color:{color};'>{word}</mark>"
75
  prev_end = end
76
+
77
  htext += text_input[prev_end:]
78
  except Exception as e:
79
  print(f"NER processing failed: {e}")
80
+ htext = "<p>NER processing not available.</p>"
81
 
82
  label_output = {"Severe Reaction": float(scores[1]), "Non-severe Reaction": float(scores[0])}
 
83
  return label_output, local_plot, htext
84
 
85
  def main(prob1):
 
88
  title = "Welcome to **ADR Detector** 🪐"
89
  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."""
90
 
 
91
  with gr.Blocks(title=title) as demo:
92
  gr.Markdown(f"## {title}")
93
  gr.Markdown(description1)
94
  gr.Markdown("""---""")
95
+
96
+ prob1 = gr.Textbox(label="Enter Your Text Here:", lines=2, placeholder="Type it here ...")
97
+
98
+ label = gr.Label(label="Predicted Label")
99
+ local_plot = gr.HTML(label='Shap Explanation')
 
 
100
  htext = gr.HTML(label="Named Entity Recognition")
101
+
102
  submit_btn = gr.Button("Analyze")
 
103
  submit_btn.click(
104
+ fn=main,
105
+ inputs=[prob1],
106
+ outputs=[label, local_plot, htext],
107
+ api_name="adr"
108
  )
109
+
 
110
  gr.Markdown("### Click on any of the examples below to see how it works:")
 
 
111
  gr.Examples(
112
  examples=[
113
  ["A 35 year-old male had severe headache after taking Aspirin. The lab results were normal."],
 
115
  ],
116
  inputs=[prob1],
117
  outputs=[label, local_plot, htext],
118
+ fn=main,
119
  cache_examples=False,
120
  run_on_click=True
121
  )
122
 
 
123
  demo.launch()