import streamlit as st def summarize_function(notes): notes_input = tokenizer(notes, return_tensors='pt') output = model(**notes_input) max_length = len(output['logits'][0]) + 50 input_ids = notes_input.input_ids gen_tokens = model.generate(input_ids, do_sample=True, temperature = 0.5, max_length=max_length) gen_text = tokenizer.batch_decode(gen_tokens)[0] return gen_text.replace(notes, '') st.markdown("

GPT Clinical Notes Summarizer

", unsafe_allow_html=True) st.markdown("
by Bryan Mildort
", unsafe_allow_html=True) from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("bryanmildort/gpt-clinical-notes-summarizer") from accelerate import init_empty_weights from transformers import AutoConfig, AutoModelForCausalLM checkpoint = "bryanmildort/gpt-notes-summarizer-demo" config = AutoConfig.from_pretrained(checkpoint) with init_empty_weights(): model = AutoModelForCausalLM.from_config(config) from accelerate import load_checkpoint_and_dispatch model = load_checkpoint_and_dispatch( model, checkpoint, device_map="auto", no_split_module_classes=["GPTJBlock"] ) # model = AutoModelForCausalLM.from_pretrained("bryanmildort/gpt-clinical-notes-summarizer", low_cpu_mem_usage=True, load_in_8bit=True, device_map="auto") prompt = """Edie Whelan is a 26 yo female who is here for follow-up following an ED visit for palpitations 2 weeks ago. She had a normal workup in the ED consisting of CBC, metabolic panel, cardiac enzymes and and ECG. She states that she has a 5 yr hx of palpitations with SOB, throat tightening, nause and clamminess. these have been increasing in frequency in the past few weeks to 1-2x a day until 2 weeks ago when she had associated numbness in her fingers. She states that there are no precipitating events and these stop on their own. She endorses recent life stressors having bought a condo 3 months ago and lost her job 2 months ago. She denies fevers, chills, changes in skin/hair/nails or chest pain/tightness. She denies vision or hearing changes. PMH: none, Meds: none, Allergies: none, Surgeries: None, Family hx: none Social: denies caffeine, tobacco, alcohol, and drug use. Is in a monogomous relationship and has sex with condoms""" prefix = """[Notes]: 64-year-old with copd (no pft in omr), cad, aaa, htn presented with several days of worsening shortness of breath with some intermittent nonproductive coughs. no fevers, chills, chest pain, abdominal pain, changes in bowel habits. no lower extremity edema. no sick contact, no flu shot, no myalgias. in the ed, initial vs: t 100, hr 93, bp 164/108, rr 22, 81%ra. he was started on bipap. he received albuterol and ipratropium nebs, methylpred 125 mg iv x 1, ceftriaxone 1 gm x 1, azithromycin 500 mg x 1, mg sulfate 2 gm x 1. trop 0.05, ck normal, ecg unchanged from prior. prior to transfer to micu, hr 80, bp 105/48, rr 22, 99% on bipap. on arrival here, abg was 7.35/59/63/34. he was breathing comfortably on 5lnc. patient admitted from: hospital1 1 er history obtained from hospital 31 medical records [Summary]: 64-year-old man with copd, cad, htn here with several days of shortness of breath, productive coughs, fever, most likely copd exacerbation in the setting of an infection. [Notes]: justin jefferson 64 yo male hx of pvd, dm s/p renal transplant 2130. and ulcers. patient states that he has been nauseated with vomiting and diarrhea for the past 2 weeks. he reports having decreased food intaked and that he has not been able to take or keep his medications down consistently. he states that in the past when he has been hyperglycemic, he has not had nausea and vomiting like this. he also reports sob and doe for the past 4-5 days. he denies orthopnea and pnd. he reports some edema in his legs bilat which is a chronic problem. came to the er today because his wife finally made him after his temperature spiked to 102. patient also has chronic ulcers. his wife helps care for them and has not noticed any increasing erythema 2 days ago when last she changed the dressing. however, in ed, found to have swollen rle with right ulcers some and surrounding erythema concerning for celluliti. in ed found to be hyperglycemic with bs of 420 and ag 24. they treated him with sq insulin rather than an insulin drip because they were concerned about hypoglycemia. ed with q 1-2 hr fingersticks brought ag down to 16 currently. ed concerned about possible cellulitis started on vanco and unsyn for cellulitis. xray of leg did not show osteomyelitis. cxr clear. vascular was consulted and they took a foot cx. podiatry also saw patient. patient was going go to floor when inr came back at 20.0. patient then given ffp x1 unit and vit k. patient admitted from: hospital1 5 er history obtained from patient [Summary]: mr. mills is a 64 yo male with a history of t1dm s/p cadoveric renal transplant, chronic rle ulcers, and ivc filter on coumadin, who was admitted with dka in the setting of one week of n/v, diarrhea and fevers. [Notes]: mr. whittle is a 75 year old male with a history of hemorrhagic stroke 2 months ago admitted for shortness of breath. he was noted by vna to have labored breathing and was found to be hypoxic at home to 70s on ra. he was initially taken to location (un) 78, where cta showed massive bilateral pes. he was also found to be in a.fib with rvr which resolved with oxygen, but no nodal agents. he was started on a nitropaste and transferred to hospital1 1 for further management. patient reports that that he has had 2-3 weeks of rle edema, but was without sob, cp, nausea, vomiting, diarrhea, melena, hematemasis. reports he is up to date on colonoscopy and psa and carries no cancer diagnosis. patient reports he was highly mobile over the summer, but over the past 2-3 months has been primarily bedbound due to sciatica symptoms and deconditioning after recent stroke. was prescribed lasix for le edema by pcp few weeks ago. also of note, patient underwent dental extraction of 7 teeth last week. in the ed, his vitals were 82, 108/92, 24, 96%. he got a cxr which showed no acute process. he got lenis with showed lle dvt. patient admitted from: hospital1 1 er history obtained from patient, family / friend [Summary]: in summary, mr. gibbs is a 75 year old male with recent hemorrhagic stroke, htn, bph, hl, admitted for bilateral pe and lle dvt. [Notes]: : this is a 53 y.o with ms, asthma with 2 days of cough, sob, fever to 102 per husband with increased agitation, ams and s/p unwitnessed fall per husband. in the ed, inital vitals were tm101.6, bp 129/60, hr 88, rr 16, sat 96% on 6l. abg 7.31/44/65. cxr found opacification of the l.lung with ?layering pleural effusion vs. lobar collapse. r.lung with patchy airspace opacity with concern for pna. pt given dose of levofloxacin and ceftriaxone. pt also given nebs and lorazepam. ekg unchanged, head/neck ct negative. per neurology, pt with limited o2 sats, advanced ms, sleep disturbances. upon speaking with pt's family, pt normal waxes and wanes in terms of alertness. has chronic pain. of note, pt admitted and tx for l.sided pna with flagyl/levo. however, abx switched to vanco/azithro/flagyl/cefepime. pt symptoms gradually improved. ros: unable to assess for complete ros as pt with altered ms. patient admitted from: hospital1 1 er history obtained from patient, family / hospital 380 medical records patient unable to provide history: encephalopathy [Summary]: 53 y.o woman with h.o ms, asthma, chronic pain who presents with sob/cough/ fever s/p fall. [Notes]: mr. poole is a 42 y.o. m with aids (cd4 65 on 2119-10-31), hepatitis c cirrhosis, recently discharged on 10-14 after a five day admission for abdominal pain, worsening ascites, jaundice and diarrhea, now presents with fever, and abdominal pain. . during his prior admission, he presented with abdominal pain, worsening ascites, jaundice, and diarrhea. ct scan on admission showed ?colitis. stool cultures and cmv were negative. he underwent egd and colonoscopy which were unrevealing. biopsies were negative. he underwent therapeutic paracentesis, removing 3.2 liters of ascites. he was started on immodium for diarrhea, lasix, and aldactone. the patient mentions that 1 week ago he started having diarrhea with dark stools. about 3 days ago, he had worsening abdominal pain and chills. he has been having 3-8 bowel movements per day, which is more than usual even with taking lactulose and january/day (3) 9723. he has had poor po intake over the past 3 days secondary to nausea and vomiting. emesis has been non bloody, non bilious. patient denies any cough, shortness of breath, or chest pain. no brbpr. no dysuria. he does not believe his abdomen is larger than baseline. . in the ed, initial vs: 98.2, 63, 81/41, 20, 100%. he was found to have diffuse abdominal tenderness, and brown, guaiac positive stool. abdominal paracentesis was attempted twice, however was stopped due to superficial bleeding. given inr of 3.3, further attempts were not pursued. hepatology was consulted. initial labs revealed hyponatremia to 126, bun/creat 58/4.4. lft's were elevated including ast 517, alt 174, alk phos 170, and tbil 26.8. lipase was elevated at 70. lactate 2.1. he had a leukocytosis of 11.2 with 71% pmn's. he received levofloxacin 750 mg, vancomycin 1 g empirically for community acquired pneumonia, with flagyl for abdominal coverage, and bactrim. he also received pantoprazole 40mg x1. ct abdomen/pelvis revealed colitis. he received 2l iv fluids. on transfer, vital signs were hr 56 bp 97/43 rr 23 100% on ra. . . currently, the patient has some mild abdominal pain and weakness, but no other complaints. . ros: denies night sweats, headache, vision changes, rhinorrhea, congestion, sore throat, cough, shortness of breath, chest pain, constipation, brbpr, hematochezia, dysuria, hematuria. [Summary]: mr. white is a 42 year old man with aids and hcv with decompensated cirrhosis who presented on 11-6 with fevers and abdominal pain with hypotension requiring icu admission.""" input_text = st.text_area("Notes:", prompt) if st.button('Summarize'): final_input = f"""{prefix}\n[Notes]: {input_text}\n[Summary]: """ st.write("Summary:") st.write(summarize_function(final_input))