Spaces:
Sleeping
Sleeping
from sklearn.pipeline import Pipeline | |
from sklearn.compose import ColumnTransformer | |
from spacy.lang.en import English | |
import tensorflow as tf | |
import tensorflow_hub as hub | |
from tensorflow.keras.layers import TextVectorization | |
import gradio as gr | |
import gdown | |
# download the model | |
url = 'https://drive.google.com/drive/folders/1ATO7lCqHr3ZCkICAs2ensV4YBe_ORhhE?usp=drive_link' | |
gdown.download_folder(url) | |
# Load model | |
model_path = "skimlit_tribrid_model_100" | |
loaded_model_100 = tf.keras.models.load_model(model_path) | |
# create nlp english lang. pipline | |
nlp = English() | |
sentencizer = nlp.add_pipe("sentencizer") | |
# class to preprocess input abstract | |
class preprocessAbstract: | |
def __init__(self): | |
nlp = English() | |
sentencizer = nlp.add_pipe("sentencizer") | |
self.sample_lines = [] | |
def split_abstract_sentences(self, block): | |
doc1 = nlp(block) | |
sentences = [str(sent) for sent in list(doc1.sents)] | |
return sentences | |
# make block of text into list of sentences | |
def make_sentences_from_abstract(self,abstract): | |
doc = nlp(abstract) | |
abstract_lines = [str(sent) for sent in list(doc.sents)] | |
list_of_dict = self.make_feature_list_of_dict(abstract_lines) | |
sample_line_number_one_hot = self.one_hot_encode_line_number(list_of_dict) | |
sample_total_lines_one_hot = self.one_hot_encode_total_lines(list_of_dict) | |
sample_chars = self.char_tokens(abstract_lines) | |
return self.prepare_dataset(sample_line_number_one_hot, sample_total_lines_one_hot, abstract_lines, sample_chars) | |
# create a list of dict for sentences | |
def make_feature_list_of_dict(self,abstract_lines): | |
total_lines_in_abstract = len(abstract_lines) | |
for i, line in enumerate(abstract_lines): | |
sample_dict = {} | |
sample_dict["line"] = str(line) | |
sample_dict["line_number"] = i | |
sample_dict["total_lines"] = total_lines_in_abstract - 1 | |
self.sample_lines.append(sample_dict) | |
return self.sample_lines | |
# one hot encode line number | |
def one_hot_encode_line_number(self,sample_lines): | |
line_number = [line["line_number"] for line in sample_lines] | |
sample_line_number_one_hot = tf.one_hot(line_number, depth=15) | |
return sample_line_number_one_hot | |
# one hot encoded total lines | |
def one_hot_encode_total_lines(self,sample_lines): | |
total_lines = [line["total_lines"] for line in self.sample_lines] | |
sample_total_lines_one_hot = tf.one_hot(total_lines, depth=20) | |
return sample_total_lines_one_hot | |
def split_chars(self,text): | |
return " ".join(list(text)) | |
# create char inputs | |
def char_tokens(self,abstract_lines): | |
sample_chars = [self.split_chars(sentence) for sentence in abstract_lines] | |
return sample_chars | |
# prepare dataset | |
def prepare_dataset(self,sample_line_number_one_hot,sample_total_lines_one_hot,abstract_lines,sample_chars): | |
return (sample_line_number_one_hot, sample_total_lines_one_hot,tf.constant(abstract_lines), tf.constant(sample_chars)) | |
# gradio interface | |
class_labels = ['BACKGROUND', 'CONCLUSIONS', 'METHODS', 'OBJECTIVE', 'RESULTS'] | |
def skimAbstract(text): | |
preprocess = preprocessAbstract() | |
x = preprocess.make_sentences_from_abstract(text) | |
sample_pred_probs = loaded_model_100.predict(x=x) | |
sample_preds = tf.argmax(sample_pred_probs, axis=1) | |
pred_classes = [class_labels[i] for i in sample_preds] | |
abstract = preprocess.split_abstract_sentences(text) | |
result_list = [] | |
for i, line in enumerate(abstract): | |
# print(f"{pred_classes[i]} : {line}") | |
result_list.append(pred_classes[i] + ' : ' + line) | |
return "\n\n".join(result_list) | |
title = "Skim Lit" | |
description = f" Don't know in given abstract of a RCT, what role does each sentence serve in the abstract. Skim lit is here to help you." | |
examples = ['Mental illness, including depression, anxiety and bipolar disorder, accounts for a significant proportion of global disability and poses a substantial social, economic and heath burden. Treatment is presently dominated by pharmacotherapy, such as antidepressants, and psychotherapy, such as cognitive behavioural therapy; however, such treatments avert less than half of the disease burden, suggesting that additional strategies are needed to prevent and treat mental disorders. There are now consistent mechanistic, observational and interventional data to suggest diet quality may be a modifiable risk factor for mental illness. This review provides an overview of the nutritional psychiatry field. It includes a discussion of the neurobiological mechanisms likely modulated by diet, the use of dietary and nutraceutical interventions in mental disorders, and recommendations for further research. Potential biological pathways related to mental disorders include inflammation, oxidative stress, the gut microbiome, epigenetic modifications and neuroplasticity. Consistent epidemiological evidence, particularly for depression, suggests an association between measures of diet quality and mental health, across multiple populations and age groups; these do not appear to be explained by other demographic, lifestyle factors or reverse causality. Our recently published intervention trial provides preliminary clinical evidence that dietary interventions in clinically diagnosed populations are feasible and can provide significant clinical benefit. Furthermore, nutraceuticals including n-3 fatty acids, folate, S-adenosylmethionine, N-acetyl cysteine and probiotics, among others, are promising avenues for future research. Continued research is now required to investigate the efficacy of intervention studies in large cohorts and within clinically relevant populations, particularly in patients with schizophrenia, bipolar and anxiety disorders.'] | |
demo = gr.Interface( | |
fn=skimAbstract, | |
inputs=gr.Textbox(placeholder="Enter your Medical abstract here.",interactive=True), | |
outputs=gr.Textbox(placeholder="Output", #interactive=True, | |
container=True,show_copy_button=True,label=['BACKGROUND', 'OBJECTIVE', 'METHODS', 'RESULTS', 'CONCLUSIONS']), | |
title=title, | |
description=description, | |
examples=examples, | |
cache_examples=True, | |
) | |
demo.queue() | |
demo.launch(debug=True) | |