Files changed (1) hide show
  1. app.py +11 -33
app.py CHANGED
@@ -1,5 +1,3 @@
1
-
2
-
3
  import gradio as gr
4
  import shap
5
  import numpy as np
@@ -14,7 +12,6 @@ import csv
14
  import io
15
  import base64
16
 
17
-
18
  # Increase CSV field size limit
19
  csv.field_size_limit(sys.maxsize)
20
 
@@ -41,29 +38,10 @@ def predict_prob(texts):
41
 
42
  explainer = shap.Explainer(predict_prob, tokenizer)
43
 
44
-
45
  # NER pipeline
46
- #ner_tokenizer = AutoTokenizer.from_pretrained("d4data/biomedical-ner-all")
47
- #ner_model = AutoModelForTokenClassification.from_pretrained("d4data/biomedical-ner-all")
48
- #ner_pipe = pipeline("ner", model=ner_model, tokenizer=ner_tokenizer, aggregation_strategy="simple")
49
-
50
- import spacy
51
- import scispacy
52
- nlp = spacy.load("en_core_sci_sm") # Use small SciSpacy model
53
-
54
- def scispacy_ner(text_input):
55
- doc = nlp(text_input)
56
- highlighted = text_input
57
- offset = 0
58
- for ent in doc.ents:
59
- start = ent.start_char + offset
60
- end = ent.end_char + offset
61
- label = ent.label_
62
- color = "#a3e635" if "DISEASE" in label else "#1e3a8a"
63
- replacement = f"<mark style='background-color:{color}; border-radius: 4px;'>{ent.text} ({label})</mark>"
64
- highlighted = highlighted[:start] + replacement + highlighted[end:]
65
- offset += len(replacement) - (end - start)
66
- return highlighted
67
 
68
  # SHAP Plotting Function
69
  def generate_shap_plot(shap_values):
@@ -91,13 +69,13 @@ def adr_predict(x):
91
  local_plot = "<p>SHAP explanation not available.</p>"
92
 
93
  # NER Processing
94
- try:
95
- htext = scispacy_ner(text_input)
96
- except Exception as e:
97
- print(f"NER processing failed: {e}")
98
- htext = "<p>NER processing not available.</p>"
99
-
100
-
101
  htext = "<div style='line-height: 1.5; font-family: Poppins;'>"
102
  prev_end = 0
103
  res = sorted(res, key=lambda x: x['start'])
@@ -302,4 +280,4 @@ with gr.Blocks(title="AwareRx. Painless Input. Painless Life.") as demo:
302
  gr.HTML(generate_alerts())
303
 
304
  gr.Markdown("Data privacy is our priority. All information is securely stored following HIPAA guidelines.")
305
- demo.launch()
 
 
 
1
  import gradio as gr
2
  import shap
3
  import numpy as np
 
12
  import io
13
  import base64
14
 
 
15
  # Increase CSV field size limit
16
  csv.field_size_limit(sys.maxsize)
17
 
 
38
 
39
  explainer = shap.Explainer(predict_prob, tokenizer)
40
 
 
41
  # NER pipeline
42
+ ner_tokenizer = AutoTokenizer.from_pretrained("d4data/biomedical-ner-all")
43
+ ner_model = AutoModelForTokenClassification.from_pretrained("d4data/biomedical-ner-all")
44
+ ner_pipe = pipeline("ner", model=ner_model, tokenizer=ner_tokenizer, aggregation_strategy="simple")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  # SHAP Plotting Function
47
  def generate_shap_plot(shap_values):
 
69
  local_plot = "<p>SHAP explanation not available.</p>"
70
 
71
  # NER Processing
72
+ try:
73
+ res = ner_pipe(text_input)
74
+ entity_colors = {
75
+ 'Severity': '#a3e635', 'Sign_symptom': '#1e3a8a', 'Medication': '#c0c0c0',
76
+ 'Age': '#a3e635', 'Sex': '#a3e635', 'Diagnostic_procedure': '#c0c0c0',
77
+ 'Biological_structure': '#c0c0c0'
78
+ }
79
  htext = "<div style='line-height: 1.5; font-family: Poppins;'>"
80
  prev_end = 0
81
  res = sorted(res, key=lambda x: x['start'])
 
280
  gr.HTML(generate_alerts())
281
 
282
  gr.Markdown("Data privacy is our priority. All information is securely stored following HIPAA guidelines.")
283
+ demo.launch()