PHS-BERT / app.py
Chan Lee
removed 'other' due to memory limit
ff27340
import ktrain
from gradio import Interface, Parallel, TabbedInterface
vs_examples = [
["I only get my kids the ones I got....I've turned down many so called 'vaccines'"],
["In child protective services, further providing for definitions, for immunity from liability"],
["Lol what? Measles is a real thing. Get vaccinated"]]
vs_title = "Vaccine Sentiment Task"
# vs_desc = "Enter text to generate labels from 3 models (BERT, MentalBERT, PHS-BERT). The three provided examples have true labels 'vaccine critical', 'neutral', 'vaccine supportive' respectively (NOTE: you may have to click submit again to generate the correct labels)."# For more details, please refer to the VS2 dataset description in our paper (link provided in the corresponding Hugging Face repository)."
vs_desc = "Enter text to generate labels from 2 models (BERT, PHS-BERT). The three provided examples have true labels 'vaccine critical', 'neutral', 'vaccine supportive' respectively (NOTE: you may have to click submit again to generate the correct labels)."# For more details, please refer to the VS2 dataset description in our paper (link provided in the corresponding Hugging Face repository)."
hm_examples = [
["Serious as a heart attack question/thought Riddle me this. Why, oh why, does cold brew coffee get warm after sitting to long. Taste terrible. And Hot coffee get literally COLD after sitting too long. Tastes terrible. Like what. Why don't cold stay cold and hot only get warm?"],
# ["It's odd how humans are considered predators when they have a heart attack and run like mad when a small insect is running straight towards them."],
["The older we get the less likely we are to view a sudden massive heart attack as a tragedy than a stroke of good luck."],
["My son is not feeling well today."]]
hm_title = "Health Mention Task"
# hm_desc = "Enter text to generate labels from 3 models (BERT, MentalBERT, PHS-BERT). The three provided examples have true labels 'Figurative/Hyperbolic Health Mentions', 'Non-personal Health Mentions', 'Personal Health Mentions' respectively (NOTE: you may have to click submit again to generate the correct labels)."# For more details, please refer to the RHMD dataset description in our paper (link provided in the corresponding Hugging Face repository)."
hm_desc = "Enter text to generate labels from 2 models (BERT, PHS-BERT). The three provided examples have true labels 'Figurative/Hyperbolic Health Mentions', 'Non-personal Health Mentions', 'Personal Health Mentions' respectively (NOTE: you may have to click submit again to generate the correct labels)."# For more details, please refer to the RHMD dataset description in our paper (link provided in the corresponding Hugging Face repository)."
dep_examples = [
["Nothing major, some underage drinking, a little pot, but I wasn't a bad kid. Furthermore, I feel like I matured well and have gone on to be successful for my age (24) IMO. I've also had positive experiences with police. The resource officer at my high school was a really good guy and almost like a mentor to me. When I was 17 I was arrested when police raided a friends house because they were tipped off that there was to be some under-aged drinking. Furthermore, I told him before we got really serious that I have anxiety and depression, and if he can’t handle that then I’ll understand if we end things. But *he* chose to stay with me and help me if I wanted it. Honestly I’m just tired of defending myself to him after this fight. I shouldn’t have to. And he kept threatening to break up; then why don’t you do it?"],
["Yes, you should! And we planned for that. Because the data is stored in an encrypted Google Firebase server, you can download the app again and use their same username and password to retrieve their plan. So if you go to Walmart to get a burner phone (or when we provide one, in some cases), you can install it on that device as well. Here's the [iOS <url> In order to make a case for its commercialization, we have to do a strong market validation research and costumer insight so we decided to create a survey asking parents what are they looking for in a wearable device for their bundle of joy. Here's the link: <url> You will also enter for a chance to win an Amazon Gift card! Thanks! **SURVEY HAS BEEN EDITED FOR BETTER EXPERIENCE** * The survey usually only takes about 5 minutes (or less) to complete - but you can take as long as you like! * Please note that this survey is best viewed via a computer screen, rather than on a mobile phone. Interested? Here's the link: <url> Little over 3 hours ago, my dad had attacked me. I'm not sure who to go to for advice, hoping someone can share any here. Thank you. Here's the full story Preceding physical contact, me and my dad were having a verbal argument. Here's the link to my amazon wish list where the two items are: <url> If the link doesn't work, let me know. The extra $1.25 an hour doesn't seem like much but I'm the sole supporter at the moment for a family of 7 and it's going to make a small, but huge difference for us. Please and thank you!"],
["i have horrible vivid nightmares every night. sometimes they're trauma related, but most of the time they're about murder, torture and such depraved things that I can't talk about to anyone. every morning i wake up drenched in sweat, and sometimes vomiting. i have no idea what to do. it's ruining my life."]
]
dep_title = "Depression Task"
dep_desc = "Enter X to generate labels from 3 models (BERT, MentalBERT, PHS-BERT). Label 0='x', 1='y', 2='z'. The three provided examples have true labels 0,1,2 respectively (NOTE: you may have to click submit again to generate the correct labels). For more details, please refer to the X dataset description in our paper (link provided in the corresponding Hugging Face repository)."
covid_examples = [
["Second person-to-person coronavirus transmission reported in US, CDC says #FoxNews - I thought you fixed it?"],
# ["Do you expect a travel ban to work? #aircraft #airplane #airplanes #airport #Airports #avgeek #aviation #aviationdaily #aviationgeek #aviationlife #aviationlovers #AvSec #crewlife #Cybersecurity #flight #Flyer #flying #infosec #pilot #pilotlife #travel"],
["The latest Boarding Pass! #budget2020 #coronavirus"]
]
covid_title = "COVID Category Task"
# covid_desc = "Enter covid-related tweets to generate labels from 3 models (BERT, MentalBERT, PHS-BERT). The two provided examples have true labels 'news', 'personal narrative' respectively (NOTE: you may have to click submit again to generate the correct labels)."# For more details, please refer to the Covid category dataset description in our paper (link provided in the corresponding Hugging Face repository)."
covid_desc = "Enter covid-related tweets to generate labels from 2 models (BERT, PHS-BERT). The two provided examples have true labels 'news', 'personal narrative' respectively (NOTE: you may have to click submit again to generate the correct labels)."# For more details, please refer to the Covid category dataset description in our paper (link provided in the corresponding Hugging Face repository)."
# 2nd example looks like the most risk
suicide_examples = [
["I cannot say I have been in your shoes; however I have felt immense Ache as you have. I have had the hours, days, months filled with agony and Feeling despair and hopelessness. Ive lost a good near everything I ever once had or loved. Its devastating, sometimes I feel as though Ive lost my entire identity, consumed with self-loathing and hatred. I too, suffer from body dysmorphic disorder; in my turn I suffered from extreme Bulimia (down to 40 kg at 50) and it was, along with the lingering effects, awful.I had to start living for, in what my eyes was, success. Expanding my knowledge on the world around me, enveloping myself in global affairs; anything to take my mind off my Ache. I became encompassed in a world that didnt involve me. Distraction was my obvious get-away. This may be not for you (I never liked it to even be brought up) but I began to seek some form of spiritual journey. I tried to set some good into this world, Bulimia Nervosa my soul for a higher thinking. I took on philosophy and metaphysics, find reasoning behind my suffering. This is just my escape, I hope that youll find some relief from my advice. Im sorry for your experience and give you my sincerest best wishes."],
["Ive tried that. Im Exhaustion of trying and falling at everything. Im looking Att gun and trying to figure why not to do it. ', 'Heh, even reddit cant give me a reason to keep on going."],
["Friendships are complicated. Close friendships between three people are even more complicated. Maybe its time to expand your circle of friends. Theres no need to get rid of the two friends youre talking about. Maybe theyll come around and maybe they wont. Either way, theres nothing wrong with meeting new people.Try to keep in mind that a lot of the people at your school (if not all of them) are thinking the EXACT same things as you. 'Nobody likes me. I wish I had more friends.' If you were to make a point of chatting with a few new people every day, theyd probably be extremely grateful to you, and might end up being good friends of yours. What kind of activities are you involved in?It sounds Hyperactive behavior you Irritable Mood a lot to your mom, and you must care about her too if youre considering her feelings (not all teenagers would have the selflessness and empathy to do that). Maybe youd feel better if you talked to her about some of this. Is it possible to have dinner together tonight? Maybe you could plan some kind of event with her, Hyperactive behavior a movie or a manicure. Also, you could offer to run errands with her some evening or on the weekend. She might appreciate the company and Ive found it makes great visiting time.Please keep us updated! Im sending you positive thoughts.', 'That sounds really rough. Im sorry youre going through such a tough time. It sounds Hyperactive behavior your wife has been treating you horribly for a while now.I think the best thing you can do is get through the day. At some point youll need to feel Numbness and at some point youll need to get some release. Try to Sedated state or Numbness yourself by vegging in front of the TV or having a few beers (dont buy enough to get sick). Releasing the pent up frustration could be through a work out or venting to a friend. If you live in a safe neighborhood, long walks are helpful.Is there a friend or family member who could stay with you for a night or two? Venting to us is great, and Id be happy to PM, but hugs are hard to give online.All of the cliches are true. It gets better. Keep your chin up. What doesnt kill you make you stronger. If all those sound Hyperactive behavior bullshit (they often do), just remember to breathe."]
]
suicide_title = "Suicide Task"
suicide_desc = "Enter Reddit posts to generate labels from 3 models (BERT, MentalBERT, PHS-BERT). The 5 labels represent increasing risk to sucide ideation (0='minimum risk', 4='maximum risk'). The three provided examples have true labels 1,2,4 respectively (NOTE: you may have to click submit again to generate the correct labels). For more details, please refer to the R-SSD dataset description in our paper (link provided in the corresponding Hugging Face repository)."
stress_examples = [
["I'm not massively worried about it at the moment because it feels good."],
# ["My other brother is quite religious / catholic - not sure how this will impact how he reacts when he finds out. Also, my husband now finds it very very hard to be around my brother when my mom and him and his gf get together for family occasions. I don't find it totally difficult, because he's always been in my life and I've gotten so used to just burying it and forgetting about the abuse. But my husband, understandably, has a different perspective. He tries to stay 'strong' and act as 'normal' as possible when we get together so that no one thinks anything is 'wrong' or asks / puts us on the spot."],
["My daughter's father I was with for 5 years on and off. He was not abusive the first year, however when he became so I left and found out two weeks later I was pregnant. Of course he begged and pleaded for a second chance, and I believed that my daughter deserved me to at least give it a shot. I came back and surprise surprise it was worse than it ever was. I stuck it out until he disappeared for the eleventeenth time on a drunken bender, I checked my Facebook to find some scumbag girl who was dating one of his best friends was posting horrific, nasty, way out shit about me."]
]
stress_title = "Stress Detection Task"
# stress_desc = "Enter text to generate labels from 3 models (BERT, MentalBERT, PHS-BERT). The two provided examples have true labels 'no stress', 'stress' respectively (NOTE: you may have to click submit again to generate the correct label)."# For more details, please refer to the Dreaddit dataset description in our paper (link provided in the corresponding Hugging Face repository)."
stress_desc = "Enter text to generate labels from 2 models (BERT, PHS-BERT). The two provided examples have true labels 'no stress', 'stress' respectively (NOTE: you may have to click submit again to generate the correct label)."# For more details, please refer to the Dreaddit dataset description in our paper (link provided in the corresponding Hugging Face repository)."
other_examples = [
["@anxietyfighter suffered social anxiety for 4 yrs when i had my first panic attack,got worse when i went to uni, so have just started paxil"],
["@JessBarrett227 Taking someone off 150mg off Seroquel, mixing it with Olanzapine in 2 wks causes psychosis - a proper assess was not done. "]
]
other_title = "Adverse Drug Reaction Task"
# other_desc = "Enter text to generate labels from 3 models (BERT, MentalBERT, PHS-BERT). The two provided examples have true labels 'no adverse drug reaction', 'adverse drug reaction' respectively (NOTE: you may have to click submit again to generate the correct labels)."# For more details, please refer to the SMM4H T1 dataset description in our paper (link provided in the corresponding Hugging Face repository)."
other_desc = "Enter text to generate labels from 2 models (BERT, PHS-BERT). The two provided examples have true labels 'no adverse drug reaction', 'adverse drug reaction' respectively (NOTE: you may have to click submit again to generate the correct labels)."# For more details, please refer to the SMM4H T1 dataset description in our paper (link provided in the corresponding Hugging Face repository)."
# def make_interfaces(folder):
# predictor_bert = ktrain.load_predictor(folder + "bert")
# predictor_mental = ktrain.load_predictor(folder + "mentalbert")
# predictor_phs = ktrain.load_predictor(folder + "phsbert")
# def BERT(text):
# results = predictor_bert.predict(str(text))
# return "BERT:" + str(results)
# def MentalBERT(text):
# results = predictor_mental.predict(str(text))
# return "MentalBERT:" + str(results)
# def PHSBERT(text):
# results = predictor_phs.predict(str(text))
# return "PHS-BERT:" + str(results)
# bert_io = Interface(fn=BERT, inputs="text", outputs="text")
# mental_io = Interface(fn=MentalBERT, inputs="text", outputs="text")
# phs_io = Interface(fn=PHSBERT, inputs="text", outputs="text")
# return bert_io, mental_io, phs_io
folder = "vs/"
vs_predictor_bert = ktrain.load_predictor(folder + "bert")
vs_predictor_mental = ktrain.load_predictor(folder + "mentalbert")
vs_predictor_phs = ktrain.load_predictor(folder + "phsbert")
def vs_output(number):
if int(number) == 0:
return "vaccine critical"
if int(number) == 1:
return "neutral"
if int(number) == 2:
return "vaccine supportive"
return "N/A"
def vs_BERT(text):
return "BERT ➡ " + vs_output(vs_predictor_bert.predict(str(text)))
def vs_MentalBERT(text):
return "MentalBERT ➡ " + vs_output(vs_predictor_mental.predict(str(text)))
def vs_PHSBERT(text):
return "PHS-BERT ➡ " + vs_output(vs_predictor_phs.predict(str(text)))
vs_bert_io = Interface(fn=vs_BERT, inputs="text", outputs="text")
vs_mental_io = Interface(fn=vs_MentalBERT, inputs="text", outputs="text")
vs_phs_io = Interface(fn=vs_PHSBERT, inputs="text", outputs="text")
# vs = Parallel(vs_bert_io, vs_mental_io, vs_phs_io,
# examples=vs_examples,
# title=vs_title,
# description=vs_desc,
# theme="peach")
vs = Parallel(vs_bert_io, vs_phs_io,
examples=vs_examples,
title=vs_title,
description=vs_desc)
# theme="peach") # Run error?
folder = "hm/"
hm_predictor_bert = ktrain.load_predictor(folder + "bert")
hm_predictor_mental = ktrain.load_predictor(folder + "mentalbert")
hm_predictor_phs = ktrain.load_predictor(folder + "phsbert")
def hm_output(number):
if int(number) == 0:
return "Figurative/Hyperbolic Health Mentions"
if int(number) == 1:
return "Figurative/Hyperbolic Health Mentions"
if int(number) == 2:
return "Non-personal Health Mentions"
if int(number) == 3:
return "Personal Health Mentions"
return "N/A"
def hm_BERT(text):
return "BERT ➡ " + hm_output(hm_predictor_bert.predict(str(text)))
def hm_MentalBERT(text):
return "MentalBERT ➡ " + hm_output(hm_predictor_mental.predict(str(text)))
def hm_PHSBERT(text):
return "PHS-BERT ➡ " + hm_output(hm_predictor_phs.predict(str(text)))
hm_bert_io = Interface(fn=hm_BERT, inputs="text", outputs="text")
hm_mental_io = Interface(fn=hm_MentalBERT, inputs="text", outputs="text")
hm_phs_io = Interface(fn=hm_PHSBERT, inputs="text", outputs="text")
# hm = Parallel(hm_bert_io, hm_mental_io, hm_phs_io,
# examples=hm_examples,
# title=hm_title,
# description=hm_desc,
# theme="peach")
hm = Parallel(hm_bert_io, hm_phs_io,
examples=hm_examples,
title=hm_title,
description=hm_desc)
# theme="peach")
folder = "cv/"
covid_predictor_bert = ktrain.load_predictor(folder + "bert")
covid_predictor_mental = ktrain.load_predictor(folder + "mentalbert")
covid_predictor_phs = ktrain.load_predictor(folder + "phsbert")
def cv_output(number):
if int(number) == 0:
return "news"
if int(number) == 1:
return "personal narrative"
return "N/A"
def covid_BERT(text):
return "BERT ➡ " + cv_output(covid_predictor_bert.predict(str(text)))
def covid_MentalBERT(text):
return "MentalBERT ➡ " + cv_output(covid_predictor_mental.predict(str(text)))
def covid_PHSBERT(text):
return "PHS-BERT ➡ " + cv_output(covid_predictor_phs.predict(str(text)))
covid_bert_io = Interface(fn=covid_BERT, inputs="text", outputs="text")
covid_mental_io = Interface(fn=covid_MentalBERT, inputs="text", outputs="text")
covid_phs_io = Interface(fn=covid_PHSBERT, inputs="text", outputs="text")
# covid = Parallel(covid_bert_io, covid_mental_io, covid_phs_io,
# examples=covid_examples,
# title=covid_title,
# description=covid_desc,
# theme="peach")
covid = Parallel(covid_bert_io, covid_phs_io,
examples=covid_examples,
title=covid_title,
description=covid_desc)
# theme="peach")
folder = "st/"
stress_predictor_bert = ktrain.load_predictor(folder + "bert")
stress_predictor_mental = ktrain.load_predictor(folder + "mentalbert")
stress_predictor_phs = ktrain.load_predictor(folder + "phsbert")
def st_output(number):
if int(number) == 0:
return "no stress"
if int(number) == 1:
return "stress"
return "N/A"
def stress_BERT(text):
return "BERT ➡ " + st_output(stress_predictor_bert.predict(str(text)))
def stress_MentalBERT(text):
return "MentalBERT ➡ " + st_output(stress_predictor_mental.predict(str(text)))
def stress_PHSBERT(text):
return "PHS-BERT ➡ " + st_output(stress_predictor_phs.predict(str(text)))
stress_bert_io = Interface(fn=stress_BERT, inputs="text", outputs="text")
stress_mental_io = Interface(fn=stress_MentalBERT, inputs="text", outputs="text")
stress_phs_io = Interface(fn=stress_PHSBERT, inputs="text", outputs="text")
# stress = Parallel(stress_bert_io, stress_mental_io, stress_phs_io,
# examples=stress_examples,
# title=stress_title,
# description=stress_desc,
# theme="peach")
stress = Parallel(stress_bert_io, stress_phs_io,
examples=stress_examples,
title=stress_title,
description=stress_desc)
# theme="peach")
# folder = "ot/"
# other_predictor_bert = ktrain.load_predictor(folder + "bert")
# other_predictor_mental = ktrain.load_predictor(folder + "mentalbert")
# other_predictor_phs = ktrain.load_predictor(folder + "phsbert")
def ot_output(number):
if int(number) == 0:
return "no adverse drug reaction"
if int(number) == 1:
return "adverse drug reaction"
return "N/A"
def other_BERT(text):
return "BERT ➡ " + ot_output(other_predictor_bert.predict(str(text)))
def other_MentalBERT(text):
return "MentalBERT ➡ " + ot_output(other_predictor_mental.predict(str(text)))
def other_PHSBERT(text):
return "PHS-BERT ➡ " + ot_output(other_predictor_phs.predict(str(text)))
# other_bert_io = Interface(fn=other_BERT, inputs="text", outputs="text")
# other_mental_io = Interface(fn=other_MentalBERT, inputs="text", outputs="text")
# other_phs_io = Interface(fn=other_PHSBERT, inputs="text", outputs="text")
# other = Parallel(other_bert_io, other_mental_io, other_phs_io,
# examples=other_examples,
# title=other_title,
# description=other_desc,
# theme="peach")
# other = Parallel(other_bert_io, other_phs_io,
# examples=other_examples,
# title=other_title,
# description=other_desc)
# theme="peach")
# vs_bert_io, vs_mental_io, vs_phs_io = make_interfaces("vs/")
# vs = Parallel(vs_bert_io, vs_mental_io, vs_phs_io,
# examples=vs_examples,
# title=vs_title,
# description=vs_desc)
# hm_bert_io, hm_mental_io, hm_phs_io = make_interfaces("hm/")
# hm = Parallel(hm_bert_io, hm_mental_io, hm_phs_io,
# examples=hm_examples,
# title=hm_title,
# description=hm_desc)
# dep_bert_io, dep_mental_io, dep_phs_io = make_interfaces("dp/")
# dep = Parallel(dep_bert_io, dep_mental_io, dep_phs_io,
# examples=dep_examples,
# title=dep_title,
# description=dep_desc)
# covid_bert_io, covid_mental_io, covid_phs_io = make_interfaces("cv/")
# covid = Parallel(covid_bert_io, covid_mental_io, covid_phs_io,
# examples=covid_examples,
# title=covid_title,
# description=covid_desc)
# suicide_bert_io, suicide_mental_io, suicide_phs_io = make_interfaces("sc/")
# suicide = Parallel(suicide_bert_io, suicide_mental_io, suicide_phs_io,
# examples=suicide_examples,
# title=suicide_title,
# description=suicide_desc)
# stress_bert_io, stress_mental_io, stress_phs_io = make_interfaces("st/")
# stress = Parallel(stress_bert_io, stress_mental_io, stress_phs_io,
# examples=stress_examples,
# title=stress_title,
# description=stress_desc)
# other_bert_io, other_mental_io, other_phs_io = make_interfaces("ot/")
# other = Parallel(other_bert_io, other_mental_io, other_phs_io,
# examples=other_examples,
# title=other_title,
# description=other_desc)
# desc = "Task is currently unavailable."
# def model(text):
# return "Predictions are currently unavailable."
# dep = Interface(fn=model, inputs="text", outputs="text", title=dep_title, description=desc)
# covid = Interface(fn=model, inputs="text", outputs="text", title=covid_title, description=desc)
# suicide = Interface(fn=model, inputs="text", outputs="text", title=suicide_title, description=desc)
# stress = Interface(fn=model, inputs="text", outputs="text", title=stress_title, description=desc)
# other = Interface(fn=model, inputs="text", outputs="text", title=other_title, description=desc)
# interfaces = [vs, hm, dep, covid, suicide, stress, other]
# interface_names = [vs_title, hm_title, dep_title, covid_title, suicide_title, stress_title, other_title]
# interfaces = [vs, hm, covid, stress, other]
# interface_names = [vs_title, hm_title, covid_title, stress_title, other_title]
interfaces = [vs, hm, covid, stress]
interface_names = [vs_title, hm_title, covid_title, stress_title]
# interfaces = [covid, stress]
# interface_names = [covid_title, stress_title]
TabbedInterface(interfaces, interface_names).launch()