SrujayReddy31 commited on
Commit
6c74f35
·
verified ·
1 Parent(s): 487613e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -23
app.py CHANGED
@@ -31,10 +31,10 @@ class BertClassifier(nn.Module):
31
  # Load the tokenizer and model
32
  tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
33
  bert_model = BertModel.from_pretrained('bert-base-uncased')
34
- model = BertClassifier(bert_model, num_classes=7)
35
 
36
  # Load the model weights from the .pkl file
37
- model.load_state_dict(torch.load('bert_classifier_mltc.pkl', map_location=torch.device('cpu')))
38
  model.eval()
39
 
40
  # Define prediction function
@@ -50,38 +50,33 @@ def predict(text):
50
  confidence_scores = outputs[0].tolist()
51
 
52
  # Convert to a dictionary mapping labels to confidence scores
53
- labels = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate', 'clean']
54
  prediction = {label: score for label, score in zip(labels, confidence_scores)}
55
 
56
  return prediction
57
 
58
  # Add example texts
59
  examples = [
60
- ["This is a positive and friendly comment."],
61
- ["I'm not happy with your service."],
62
- ["You are a great person!"],
63
- ["How can one defame someone who thinks the Fort Hood Shooting is justified by God's law?"],
64
- ["A Bisexual, like a homosexual or a heterosexual, is not defined by sexual activity. (Much like a 15 year old boy who is attracted to a girl sexually but has never had sex is still straight). A person who is actually sexually attracted/aroused by the same sex as well as the opposite sex is bisexual."],
65
- ["I'LL SAY NIGGER IF I WANT TO BRO"],
66
- ["Do you need a verifiable source for that statement? Check your own user page you fat, miserable balding fuck."],
67
- ["Fuck you bitch! I will go to your house and kill you with my gun as well! Prepare to die!"],
68
- ["you're hot, i will rape you ;)"],
69
- ["WOULDN'T BE THE FIRST TIME BITCH. FUCK YOU I'LL FIND OUT WHERE YOU LIVE, SODOMIZE YOUR WIFE AND THEN BURN YOUR HOUSE DOWN. FUCK YOU YOU FUCKING QUEER."],
70
- ["What a motherfucking piece of crap those fuckheads for blocking us!"],
71
- ["Get a life you animal fucker. Shut up you fucking nigger. Fuck off and shit your pants full of all the shit you can fill them with. 144.131.176.126"],
72
- ["HOPE YOUR HEAD GETS CUT OFF AND SOMEONE WIPS THERE ASS WITH IT AND THEN STABS YOU IN YOUR HEART"],
73
- ["you people are pretty overzealous with this whole free thing. get a fucking life, you fucking niggers !!!23 16!!!"],
74
- ["Stupid peace of shit stop deleting my stuff asshole go die and fall in a hole go to hell!"],
75
- ["Bye! Don't look, come or think of comming back! Tosser."]
76
  ]
 
77
  # Create Gradio interface
78
  iface = gr.Interface(
79
  fn=predict,
80
- inputs=gr.Textbox(lines=10, placeholder="Enter a comment here..."),
81
- outputs=gr.Label(num_top_classes=7),
82
  examples=examples,
83
- title="Toxic Comment Classification",
84
- description="Classify comments into toxic and non-toxic categories using BERT and GNN model.",
85
  )
86
 
87
  iface.launch()
 
31
  # Load the tokenizer and model
32
  tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
33
  bert_model = BertModel.from_pretrained('bert-base-uncased')
34
+ model = BertClassifier(bert_model, num_classes=10)
35
 
36
  # Load the model weights from the .pkl file
37
+ model.load_state_dict(torch.load('mimic4_icd_classifier.pkl', map_location=torch.device('cpu')))
38
  model.eval()
39
 
40
  # Define prediction function
 
50
  confidence_scores = outputs[0].tolist()
51
 
52
  # Convert to a dictionary mapping labels to confidence scores
53
+ labels = ['Cardiovascular', 'Respiratory', 'Neurological', 'Infectious', 'Endocrine', 'Musculoskeletal', 'Gastrointestinal', 'Renal', 'Psychiatric', 'Other']
54
  prediction = {label: score for label, score in zip(labels, confidence_scores)}
55
 
56
  return prediction
57
 
58
  # Add example texts
59
  examples = [
60
+ ["Patient admitted with chest pain, shortness of breath, and abnormal ECG findings."],
61
+ ["Elderly patient presented with symptoms of confusion, fever, and elevated white blood cell count."],
62
+ ["Patient complains of persistent cough, wheezing, and history of asthma."],
63
+ ["Admitted with severe abdominal pain, nausea, and vomiting. Suspected appendicitis."],
64
+ ["Patient has a history of diabetes mellitus and presented with high blood glucose levels and dehydration."],
65
+ ["Patient admitted following a fall, showing signs of fracture in the left femur."],
66
+ ["Patient experiencing severe headaches, dizziness, and a history of epilepsy."],
67
+ ["Acute kidney injury suspected due to elevated creatinine and reduced urine output."],
68
+ ["Patient diagnosed with major depressive disorder, experiencing prolonged sadness and loss of interest in activities."],
69
+ ["Presented with a bacterial skin infection requiring intravenous antibiotics."]
 
 
 
 
 
 
70
  ]
71
+
72
  # Create Gradio interface
73
  iface = gr.Interface(
74
  fn=predict,
75
+ inputs=gr.Textbox(lines=10, placeholder="Enter clinical notes here..."),
76
+ outputs=gr.Label(num_top_classes=10),
77
  examples=examples,
78
+ title="MIMIC-IV ICD Code Prediction",
79
+ description="Predict ICD code categories based on clinical text using a BERT-based model. The model outputs confidence scores for ten common ICD code categories.",
80
  )
81
 
82
  iface.launch()