paragon-analytics's picture
Update app.py
6981fa0
raw
history blame
3.32 kB
import streamlit as st
import gradio as gr
import shap
import torch
import tensorflow as tf
import transformers
from transformers import RobertaTokenizer, RobertaModel
from transformers import AutoModelForSequenceClassification
from transformers import TFAutoModelForSequenceClassification
from transformers import AutoTokenizer
from transformers_interpret import SequenceClassificationExplainer
tokenizer = AutoTokenizer.from_pretrained("paragon-analytics/ADRv1")
model = AutoModelForSequenceClassification.from_pretrained("paragon-analytics/ADRv1")
cls_explainer = SequenceClassificationExplainer(
model,
tokenizer)
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)
# # 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))
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))]
return {"Severe Reaction": float(scores.numpy()[1]), "Non-severe Reaction": float(scores.numpy()[0])}, word_attributions
def main(text):
text = str(text).lower()
obj = adr_predict(text)
return obj[0],obj[1]
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("""---""")
text = 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"})
submit_btn.click(
main,
[text],
[label,intp], 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."]], [text], [label,intp], main, cache_examples=True)
demo.launch()