File size: 3,400 Bytes
e669abf
 
f387393
1abbe27
 
e669abf
 
0e20e10
1b8c45e
e669abf
 
 
 
f057421
e669abf
9cc7c4e
 
e669abf
9cc7c4e
 
 
 
 
 
2888c51
9cc7c4e
7f48a24
 
68fbe9e
 
 
 
0e1a661
68fbe9e
55ecc4c
c78f4ac
 
0e1a661
55ecc4c
7f48a24
 
e669abf
579be1d
e669abf
 
 
518ac36
4a73816
3a53d21
7f48a24
c35e70a
55ecc4c
518ac36
55ecc4c
518ac36
e669abf
42c5082
 
e669abf
55ecc4c
e669abf
 
cde5ee9
e669abf
 
 
 
 
42c5082
e669abf
 
cde5ee9
c78f4ac
 
 
 
e669abf
cde5ee9
 
55ecc4c
 
e669abf
 
42c5082
9cc7c4e
55ecc4c
f1c8fb6
e669abf
3553235
 
aa5b3ba
3553235
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
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
import matplotlib.pyplot as plt

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)

##
classifier = transformers.pipeline("text-classification", model = "cross-encoder/qnli-electra-base")

def med_score(x):
    label = x['label']
    score_1 = x['score']
    return round(score_1,3)

def sym_score(x):
    label2sym= x['label']
    score_1sym = x['score']
    return round(score_1sym,3)

##

def adr_predict(x):
    encoded_input = tokenizer(x, return_tensors='pt')
    output = model(**encoded_input)
    scores = output[0][0].detach().numpy()
    scores = tf.nn.softmax(scores)
   
    shap_values = explainer([str(x).lower()])
    local_plot = shap.plots.text(shap_values[0], display=False)

    med = med_score(classifier(x+str(", There is a medication."))[0])
    sym = sym_score(classifier(x+str(", There is a symptom."))[0])
   
    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)}


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

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. Please do NOT use for medical diagnosis."""

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.Row():
        
        with gr.Column(visible=True) as output_col:
            label = gr.Label(label = "Predicted Label")
            local_plot = gr.HTML(label = 'Shap:')

        with gr.Column(visible=True) as output_col:
            med = gr.Label(label = "Contains Medication")
            sym = gr.Label(label = "Contains Symptoms")
            
    submit_btn.click(
        main,
        [prob1],
        [label
         ,local_plot, med, sym
        ], api_name="adr"
    )
    
    with gr.Row():
        gr.Markdown("### Click on any of the examples below to see how it works:")
        gr.Examples([["I had severe headache after taking Aspirin."],["I had minor headache after taking Acetaminophen."]], [prob1], [label,local_plot, med, sym
                                                                          ], main, cache_examples=True)
    
demo.launch()