File size: 5,333 Bytes
e669abf
 
f387393
1abbe27
 
e669abf
 
0e20e10
1b8c45e
e669abf
 
 
 
01df1cf
e669abf
9cc7c4e
 
e669abf
9cc7c4e
 
 
 
 
 
2888c51
9cc7c4e
 
 
 
 
 
 
f1c8fb6
1abbe27
e669abf
9cc7c4e
 
 
0a1e37c
f1c8fb6
 
 
 
 
 
 
1abbe27
e669abf
2888c51
e669abf
 
 
f387393
0a1e37c
 
 
 
 
 
 
9cc7c4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a1e37c
9cc7c4e
 
0a1e37c
9cc7c4e
0a1e37c
f1c8fb6
 
 
 
 
 
 
9cc7c4e
0a1e37c
1abbe27
9cc7c4e
 
e669abf
42c5082
 
e669abf
f1c8fb6
 
e669abf
 
 
 
 
 
 
 
 
 
42c5082
e669abf
 
 
 
 
 
 
 
 
9cc7c4e
 
 
 
0a1e37c
9cc7c4e
1abbe27
e669abf
 
 
42c5082
9cc7c4e
 
 
f1c8fb6
e669abf
 
 
42c5082
9cc7c4e
 
f1c8fb6
e669abf
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import streamlit as st
import gradio as gr
import shap
import numpy as np
import scipy as sp
import torch
import tensorflow as tf
import transformers
from transformers import pipeline
from transformers import RobertaTokenizer, RobertaModel
from transformers import AutoModelForSequenceClassification
from transformers import TFAutoModelForSequenceClassification
from transformers import AutoTokenizer
# from transformers_interpret import SequenceClassificationExplainer

device = "cuda:0" if torch.cuda.is_available() else "cpu"

tokenizer = AutoTokenizer.from_pretrained("paragon-analytics/ADRv1")  
model = AutoModelForSequenceClassification.from_pretrained("paragon-analytics/ADRv1").to(device)

# build a pipeline object to do predictions
pred = transformers.pipeline("text-classification", model=model, 
                             tokenizer=tokenizer, return_all_scores=True)

explainer = shap.Explainer(pred)

def interpretation_function(text):
    shap_values = explainer([text])
    scores = list(zip(shap_values.data[0], shap_values.values[0, :, 1]))
    return scores

# model = AutoModelForSequenceClassification.from_pretrained("paragon-analytics/ADRv1")
# modelc = AutoModelForSequenceClassification.from_pretrained("paragon-analytics/ADRv1").cuda


# cls_explainer = SequenceClassificationExplainer(
#     model,
#     tokenizer)

# # define a prediction function
# def f(x):
#     tv = torch.tensor([tokenizer.encode(v, padding='max_length', max_length=500, truncation=True) for v in x]).cuda()
#     outputs = modelc(tv)[0].detach().cpu().numpy()
#     scores = (np.exp(outputs).T / np.exp(outputs).sum(-1)).T
#     val = sp.special.logit(scores[:,1]) # use one vs rest logit units
#     return val

def adr_predict(x):
    encoded_input = tokenizer(str(x), return_tensors='pt')
    output = model(**encoded_input)
    scores = output[0][0].detach().numpy()
    scores = tf.nn.softmax(scores)
    
    # # build a pipeline object to do predictions
    # pred = transformers.pipeline("text-classification", model=model, 
    #                          tokenizer=tokenizer, device=0, return_all_scores=True)
    # explainer = shap.Explainer(pred)
    # shap_values = explainer([x])
    # shap_plot = shap.plots.text(shap_values)

    # word_attributions = cls_explainer(str(x))
    # # scores = list(zip(shap_values.data[0], shap_values.values[0, :, 1]))
    # letter = []
    # score = []
    # for i in word_attributions:
    #     if i[1]>0.5:
    #         a = "++"
    #     elif (i[1]<=0.5) and (i[1]>0.1):
    #         a = "+"
    #     elif (i[1]>=-0.5) and (i[1]<-0.1):
    #         a = "-"
    #     elif i[1]<-0.5:
    #         a = "--"
    #     else: 
    #         a = "NA"
    
    #     letter.append(i[0])
    #     score.append(a)
    
    # word_attributions = [(letter[i], score[i]) for i in range(0, len(letter))]

    # # SHAP:
    # # build an explainer using a token masker
    # explainer = shap.Explainer(f, tokenizer)
    # shap_values = explainer(str(x), fixed_context=1)
    # scores = list(zip(shap_values.data[0], shap_values.values[0, :, 1]))
    # # plot the first sentence's explanation
    # # plt = shap.plots.text(shap_values[0],display=False)
    shap_scores = interpretation_function(str(x).lower())


    return {"Severe Reaction": float(scores.numpy()[1]), "Non-severe Reaction": float(scores.numpy()[0])}, shap_scores
    # , word_attributions ,scores

def main(prob1):
    text = str(prob1).lower()
    obj = adr_predict(text)
    return obj[0],obj[1]
    # ,obj[2]

title = "Welcome to **ADR Detector** 🪐"
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.
"""

with gr.Blocks(title=title) as demo:
    gr.Markdown(f"## {title}")
    gr.Markdown(description1)
    gr.Markdown("""---""")
    prob1 = gr.Textbox(label="Enter Your Text Here:",lines=2, placeholder="Type it here ...")
    submit_btn = gr.Button("Analyze")


    with gr.Column(visible=True) as output_col:
        label = gr.Label(label = "Predicted Label")
        # impplot = gr.HighlightedText(label="Important Words", combine_adjacent=False).style(
        # color_map={"+++": "royalblue","++": "cornflowerblue",
        #  "+": "lightsteelblue", "NA":"white"})
        # NER = gr.HTML(label = 'NER:')
        # intp = gr.HighlightedText(label="Word Scores",
        # combine_adjacent=False).style(color_map={"++": "darkred","+": "red", 
        #                                         "--": "darkblue",
        #                                         "-": "blue", "NA":"white"})

        interpretation = gr.components.Interpretation(text)


    submit_btn.click(
        main,
        [prob1],
        [label
         # ,intp
         ,interpretation
        ], api_name="adr"
    )

    gr.Markdown("### Click on any of the examples below to see to what extent they contain resilience messaging:")
    gr.Examples([["I have minor pain."],["I have severe pain."]], [prob1], [label
                                                                           # ,intp
                                                                           ,interpretation
                                                                          ], main, cache_examples=True)
    
demo.launch()