uwc / app.py
pr0mila's picture
Update app.py
e2711e5
raw
history blame
5.58 kB
# -*- coding: utf-8 -*-
"""Extractive_UWC_Case_History_Summarization.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1D2eqhloS0uZR5jXlRGvm10uibWdz2L4g
# **Texts**
"""
text1 = """The adolescent was previously diagnosed with major depressive disorder and treated intermittently with supportive psychotherapy and antidepressants. Her more recent episodes related to her parents’ marital problems and her academic/social difficulties at school. She was treated using cognitive-behavioral therapy (CBT)."""
text2 = """Sam was team captain of his soccer team, but an unexpected fight with another teammate prompted his parents to meet with a clinical psychologist. Sam was diagnosed with major depressive disorder after showing an increase in symptoms over the previous three months. Several recent challenges in his family and romantic life led the therapist to recommend interpersonal psychotherapy for adolescents (IPT-A)."""
text3 = """Mark had a history of depression and sought treatment after his second marriage ended. His depression was characterized as being “controlled by a pattern of interpersonal avoidance.” The behavior/activation therapist asked Mark to complete an activity record to help steer the treatment sessions."""
text4 = """Denise is described as having “nonchronic depression” which appeared most recently at the onset of her husband’s diagnosis with brain cancer. Her symptoms were loneliness, difficulty coping with daily life, and sadness. Treatment included filling out a weekly activity log and identifying/reconstructing automatic thoughts."""
text5 ="""Luke is described as having treatment-resistant depression and while not suicidal, hoped that a fatal illness would take his life or that he would just disappear. His treatment involved mindfulness-based cognitive therapy, which helps participants become aware of and recharacterize their overwhelming negative thoughts. It involves regular practice of mindfulness techniques and exercises as one component of therapy."""
text6 ="""Peggy had a history of chronic depression, which flared during her husband’s illness and ultimate death. Guilt was a driving factor of her depressive symptoms, which lasted six months after his death. The clinician treated Peggy with psychodynamic therapy over a period of two years."""
"""# **Extractive Summarization**
"""
from summarizer import Summarizer
bert_model = Summarizer()
# text = """One month after the United States began what has become a troubled rollout of a national COVID vaccination campaign, the effort is finally gathering real steam.
# Close to a million doses -- over 951,000, to be more exact -- made their way into the arms of Americans in the past 24 hours, the U.S. Centers for Disease Control and Prevention reported Wednesday. That's the largest number of shots given in one day since the rollout began and a big jump from the previous day, when just under 340,000 doses were given, CBS News reported.
# That number is likely to jump quickly after the federal government on Tuesday gave states the OK to vaccinate anyone over 65 and said it would release all the doses of vaccine it has available for distribution. Meanwhile, a number of states have now opened mass vaccination sites in an effort to get larger numbers of people inoculated, CBS News reported."""
def abstractive_text(text):
summary_text = bert_model(text, ratio=0.1)
return summary_text
import gradio as gr
sum_iface = gr.Interface(fn=abstractive_text, inputs= ["text"],outputs=["text"],title="Case summary generation").queue()
import transformers
from transformers import BloomForCausalLM
from transformers import BloomTokenizerFast
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import gradio as gr
tokenizer = AutoTokenizer.from_pretrained("bigscience/bloom-560m")
model = AutoModelForCausalLM.from_pretrained("bigscience/bloom-560m")
def get_result_with_bloom(text):
result_length = 200
inputs1 = tokenizer(text, return_tensors="pt")
output1 = tokenizer.decode(model.generate(inputs1["input_ids"],
max_length=result_length,
num_beams=2,
no_repeat_ngram_size=2,
early_stopping=True
)[0])
return output1
txtgen_iface = gr.Interface(fn=get_result_with_bloom,inputs = "text",outputs=["text"],title="Text generation with Bloom").queue()
import spacy.cli
import en_core_med7_lg
import spacy
import gradio as gr
spacy.cli.download("en_core_web_lg")
med7 = en_core_med7_lg.load()
# create distinct colours for labels
col_dict = {}
seven_colours = ['#e6194B', '#3cb44b', '#ffe119', '#ffd8b1', '#f58231', '#f032e6', '#42d4f4']
for label, colour in zip(med7.pipe_labels['ner'], seven_colours):
col_dict[label] = colour
options = {'ents': med7.pipe_labels['ner'], 'colors':col_dict}
#text = 'A patient was prescribed Magnesium hydroxide 400mg/5ml suspension PO of total 30ml bid for the next 5 days.'
def ner_drugs(text):
doc = med7(text)
spacy.displacy.render(doc, style='ent', jupyter=True, options=options)
return [(ent.text, ent.label_) for ent in doc.ents]
med_iface = gr.Interface(fn=ner_drugs,inputs = "text",outputs=["text"],title="Drugs Named Entity Recognition").queue()
demo = gr.TabbedInterface(
[txtgen_iface, sum_iface, med_iface], ["Text Generation", "Summary Generation", "Named-entity recognition"],
title=title,
)
demo.queue()
demo.launch(share=False)