Spaces:
Runtime error
Runtime error
paragon-analytics
commited on
Commit
·
e669abf
1
Parent(s):
bec5d5d
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
import tensorflow as tf
|
5 |
+
from transformers import RobertaTokenizer, RobertaModel
|
6 |
+
from transformers import AutoModelForSequenceClassification
|
7 |
+
from transformers import TFAutoModelForSequenceClassification
|
8 |
+
from transformers import AutoTokenizer
|
9 |
+
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained("paragon-analytics/ADRv1")
|
11 |
+
model = AutoModelForSequenceClassification.from_pretrained("paragon-analytics/ADRv1")
|
12 |
+
|
13 |
+
def adr_predict(x):
|
14 |
+
encoded_input = tokenizer(x, return_tensors='pt')
|
15 |
+
output = model(**encoded_input)
|
16 |
+
scores = output[0][0].detach().numpy()
|
17 |
+
scores = tf.nn.softmax(scores)
|
18 |
+
return {"Severe Reaction": float(scores.numpy()[1]), "Non-severe Reaction": float(scores.numpy()[0])}
|
19 |
+
|
20 |
+
def main(text):
|
21 |
+
text = str(text)
|
22 |
+
obj = adr_predict(text)
|
23 |
+
return obj[0]
|
24 |
+
|
25 |
+
title = "Welcome to **ADR Detector** 🪐"
|
26 |
+
description1 = """
|
27 |
+
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.
|
28 |
+
"""
|
29 |
+
|
30 |
+
with gr.Blocks(title=title) as demo:
|
31 |
+
gr.Markdown(f"## {title}")
|
32 |
+
gr.Markdown(description1)
|
33 |
+
gr.Markdown("""---""")
|
34 |
+
prob1 = gr.Textbox(label="Enter Your Text Here:",lines=2, placeholder="Type it here ...")
|
35 |
+
submit_btn = gr.Button("Analyze")
|
36 |
+
|
37 |
+
|
38 |
+
with gr.Column(visible=True) as output_col:
|
39 |
+
label = gr.Label(label = "Predicted Label")
|
40 |
+
# impplot = gr.HighlightedText(label="Important Words", combine_adjacent=False).style(
|
41 |
+
# color_map={"+++": "royalblue","++": "cornflowerblue",
|
42 |
+
# "+": "lightsteelblue", "NA":"white"})
|
43 |
+
# NER = gr.HTML(label = 'NER:')
|
44 |
+
# intp =gr.HighlightedText(label="Word Scores",
|
45 |
+
# combine_adjacent=False).style(color_map={"++": "darkgreen","+": "green",
|
46 |
+
# "--": "darkred",
|
47 |
+
# "-": "red", "NA":"white"})
|
48 |
+
|
49 |
+
submit_btn.click(
|
50 |
+
main,
|
51 |
+
[text],
|
52 |
+
[label], api_name="adr"
|
53 |
+
)
|
54 |
+
|
55 |
+
gr.Markdown("### Click on any of the examples below to see to what extent they contain resilience messaging:")
|
56 |
+
gr.Examples([["Please stay at home and avoid unnecessary trips."],["Please stay at home and avoid unnecessary trips. We will survive this."],["We will survive this."],["Watch today’s news briefing with the latest updates on COVID-19 in Connecticut."],["So let's keep doing what we know works. Let's stay strong, and let's beat this virus. I know we can, and I know we can come out stronger on the other side."],["It is really wonderful how much resilience there is in human nature. Let any obstructing cause, no matter what, be removed in any way, even by death, and we fly back to first principles of hope and enjoyment."],["Resilience is accepting your new reality, even if it’s less good than the one you had before. You can fight it, you can do nothing but scream about what you’ve lost, or you can accept that and try to put together something that’s good."],["You survived all of the days you thought you couldn't, never underestimate your resilience."],["Like tiny seeds with potent power to push through tough ground and become mighty trees, we hold innate reserves of unimaginable strength. We are resilient."]], [prob1], [label,impplot,NER,intp], main, cache_examples=True)
|
57 |
+
|
58 |
+
demo.launch()
|