ser4ff chrisclark1016 commited on
Commit
9ff9f5a
0 Parent(s):

Duplicate from UVA-MSBA/Mod4Team3

Browse files

Co-authored-by: Chris Clark <chrisclark1016@users.noreply.huggingface.co>

Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. app.py +143 -0
  4. requirements.txt +12 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Mod4Team3
3
+ emoji: 🐨
4
+ colorFrom: indigo
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 3.28.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: UVA-MSBA/Mod4Team3
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import gradio as gr
3
+ import shap
4
+ import numpy as np
5
+ import scipy as sp
6
+ import torch
7
+ import tensorflow as tf
8
+ import transformers
9
+ from transformers import pipeline
10
+ from transformers import RobertaTokenizer, RobertaModel
11
+ from transformers import AutoModelForSequenceClassification
12
+ from transformers import TFAutoModelForSequenceClassification
13
+ from transformers import AutoTokenizer, AutoModelForTokenClassification
14
+
15
+ import matplotlib.pyplot as plt
16
+ import sys
17
+ import csv
18
+
19
+ csv.field_size_limit(sys.maxsize)
20
+
21
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
22
+
23
+ tokenizer = AutoTokenizer.from_pretrained("chrisclark1016/ADR_best")
24
+ model = AutoModelForSequenceClassification.from_pretrained("chrisclark1016/ADR_best").to(device)
25
+
26
+ # build a pipeline object to do predictions
27
+ pred = transformers.pipeline("text-classification", model=model,
28
+ tokenizer=tokenizer, return_all_scores=True)
29
+
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")
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')
53
+ output = model(**encoded_input)
54
+ scores = output[0][0].detach().numpy()
55
+ scores = tf.nn.softmax(scores)
56
+
57
+ shap_values = explainer([str(x).lower()])
58
+ # # Find the index of the class you want as the default reference (e.g., 'label_1')
59
+ # label_1_index = np.where(np.array(explainer.output_names) == 'label_1')[0][0]
60
+
61
+ # # Plot the SHAP values for a specific instance in your dataset (e.g., instance 0)
62
+ # shap.plots.text(shap_values[label_1_index][0])
63
+
64
+
65
+ local_plot = shap.plots.text(shap_values[0], display=False)
66
+
67
+ # med = med_score(classifier(x+str(", There is a medication."))[0])
68
+ # sym = sym_score(classifier(x+str(", There is a symptom."))[0])
69
+
70
+ res = ner_pipe(x)
71
+
72
+ entity_colors = {
73
+ 'Severity': 'red',
74
+ 'Sign_symptom': 'green',
75
+ 'Medication': 'lightblue',
76
+ 'Age': 'pink',
77
+ 'Sex':'yellow',
78
+ 'Diagnostic_procedure':'gray',
79
+ 'Biological_structure':'silver'}
80
+
81
+ htext = ""
82
+ prev_end = 0
83
+
84
+ for entity in res:
85
+ start = entity['start']
86
+ end = entity['end']
87
+ word = entity['word'].replace("##", "")
88
+ color = entity_colors[entity['entity_group']]
89
+
90
+ htext += f"{x[prev_end:start]}<mark style='background-color:{color};'>{word}</mark>"
91
+ prev_end = end
92
+
93
+ htext += x[prev_end:]
94
+
95
+ return {"Severe Reaction": float(scores.numpy()[1]), "Non-severe Reaction": float(scores.numpy()[0])}, local_plot,htext
96
+ # ,{"Contains Medication": float(med), "No Medications": float(1-med)} , {"Contains Symptoms": float(sym), "No Symptoms": float(1-sym)}
97
+
98
+
99
+ def main(prob1):
100
+ text = str(prob1).lower()
101
+ obj = adr_predict(text)
102
+ return obj[0],obj[1],obj[2]
103
+
104
+ title = "🦾 DAVA 🦾"
105
+ 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 medications. Please do NOT use for medical diagnosis."""
106
+
107
+ with gr.Blocks(title=title) as demo:
108
+ gr.Markdown(f"## {title}")
109
+ gr.Markdown(description1)
110
+ gr.Markdown("""---""")
111
+ prob1 = gr.Textbox(label="Enter Your Text Here:",lines=2, placeholder="Type it here ...")
112
+ submit_btn = gr.Button("Analyze")
113
+
114
+ with gr.Row():
115
+
116
+ with gr.Column(visible=True) as output_col:
117
+ label = gr.Label(label = "Predicted Label")
118
+
119
+
120
+ with gr.Column(visible=True) as output_col:
121
+ local_plot = gr.HTML(label = 'Shap:')
122
+ htext = gr.HTML(label="NER")
123
+ # med = gr.Label(label = "Contains Medication")
124
+ # sym = gr.Label(label = "Contains Symptoms")
125
+
126
+ submit_btn.click(
127
+ main,
128
+ [prob1],
129
+ [label
130
+ ,local_plot, htext
131
+ # , med, sym
132
+ ], api_name="adr"
133
+ )
134
+
135
+ with gr.Row():
136
+ gr.Markdown("### Click on any of the examples below to see how it works:")
137
+ gr.Examples([["A 35 year-old male had severe headache after taking Aspirin. The lab results were normal."],
138
+ ["A 35 year-old female had minor pain in upper abdomen after taking Acetaminophen."]],
139
+ [prob1], [label,local_plot, htext
140
+ # , med, sym
141
+ ], main, cache_examples=True)
142
+
143
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ transformers==4.20.1
2
+ gradio==3.1.3
3
+ tensorflow==2.11.0
4
+ tensorflow_hub
5
+ Pillow
6
+ matplotlib
7
+ spacy_streamlit
8
+ streamlit
9
+ torch==1.12.1
10
+ shap==0.41.0
11
+ numpy
12
+ scipy