Update app.py
Browse files
app.py
CHANGED
|
@@ -3,17 +3,15 @@ import shap
|
|
| 3 |
import numpy as np
|
| 4 |
import scipy as sp
|
| 5 |
import torch
|
| 6 |
-
# import tensorflow as tf <-- Removed to match your requirements
|
| 7 |
import transformers
|
| 8 |
from transformers import pipeline
|
| 9 |
-
from transformers import RobertaTokenizer, RobertaModel
|
| 10 |
from transformers import AutoModelForSequenceClassification
|
| 11 |
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 12 |
-
import matplotlib.pyplot as plt
|
| 13 |
import sys
|
| 14 |
import csv
|
| 15 |
import os
|
| 16 |
|
|
|
|
| 17 |
HF_TOKEN = os.getenv("hf_token")
|
| 18 |
csv.field_size_limit(sys.maxsize)
|
| 19 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
@@ -34,13 +32,13 @@ ner_model = AutoModelForTokenClassification.from_pretrained("d4data/biomedical-n
|
|
| 34 |
ner_pipe = pipeline("ner", model=ner_model, tokenizer=ner_tokenizer, aggregation_strategy="simple")
|
| 35 |
|
| 36 |
def adr_predict(x):
|
| 37 |
-
# Ensure input is treated as a string
|
| 38 |
text_input = str(x).lower()
|
| 39 |
encoded_input = tokenizer(text_input, return_tensors='pt').to(device)
|
| 40 |
output = model(**encoded_input)
|
| 41 |
|
| 42 |
scores = torch.softmax(output.logits, dim=-1)[0].detach().cpu().numpy()
|
| 43 |
|
|
|
|
| 44 |
try:
|
| 45 |
shap_values = explainer([text_input])
|
| 46 |
local_plot = shap.plots.text(shap_values[0], display=False)
|
|
@@ -85,13 +83,14 @@ def adr_predict(x):
|
|
| 85 |
def main(prob1):
|
| 86 |
return adr_predict(prob1)
|
| 87 |
|
|
|
|
| 88 |
title = "Welcome to **ADR Detector** 🪐"
|
| 89 |
-
description1 = "
|
| 90 |
|
| 91 |
with gr.Blocks(title=title) as demo:
|
| 92 |
gr.Markdown(f"## {title}")
|
| 93 |
gr.Markdown(description1)
|
| 94 |
-
gr.Markdown("
|
| 95 |
|
| 96 |
prob1 = gr.Textbox(label="Enter Your Text Here:", lines=2, placeholder="Type it here ...")
|
| 97 |
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import scipy as sp
|
| 5 |
import torch
|
|
|
|
| 6 |
import transformers
|
| 7 |
from transformers import pipeline
|
|
|
|
| 8 |
from transformers import AutoModelForSequenceClassification
|
| 9 |
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
|
|
|
| 10 |
import sys
|
| 11 |
import csv
|
| 12 |
import os
|
| 13 |
|
| 14 |
+
# Environment setup
|
| 15 |
HF_TOKEN = os.getenv("hf_token")
|
| 16 |
csv.field_size_limit(sys.maxsize)
|
| 17 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 32 |
ner_pipe = pipeline("ner", model=ner_model, tokenizer=ner_tokenizer, aggregation_strategy="simple")
|
| 33 |
|
| 34 |
def adr_predict(x):
|
|
|
|
| 35 |
text_input = str(x).lower()
|
| 36 |
encoded_input = tokenizer(text_input, return_tensors='pt').to(device)
|
| 37 |
output = model(**encoded_input)
|
| 38 |
|
| 39 |
scores = torch.softmax(output.logits, dim=-1)[0].detach().cpu().numpy()
|
| 40 |
|
| 41 |
+
# SHAP Explanation
|
| 42 |
try:
|
| 43 |
shap_values = explainer([text_input])
|
| 44 |
local_plot = shap.plots.text(shap_values[0], display=False)
|
|
|
|
| 83 |
def main(prob1):
|
| 84 |
return adr_predict(prob1)
|
| 85 |
|
| 86 |
+
# Gradio Interface
|
| 87 |
title = "Welcome to **ADR Detector** 🪐"
|
| 88 |
+
description1 = "This app predicts severe or non-severe adverse reactions to medications. Do NOT use for medical diagnosis."
|
| 89 |
|
| 90 |
with gr.Blocks(title=title) as demo:
|
| 91 |
gr.Markdown(f"## {title}")
|
| 92 |
gr.Markdown(description1)
|
| 93 |
+
gr.Markdown("---")
|
| 94 |
|
| 95 |
prob1 = gr.Textbox(label="Enter Your Text Here:", lines=2, placeholder="Type it here ...")
|
| 96 |
|