| import streamlit as st |
| import torch |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification |
| import pandas as pd |
| import numpy as np |
| from sklearn.preprocessing import MultiLabelBinarizer |
|
|
| |
| |
|
|
| |
|
|
| @st.cache_resource |
| def load_model(): |
| model = AutoModelForSequenceClassification.from_pretrained( |
| "microsoft/BiomedNLP-BiomedBERT-base-uncased-abstract", |
| num_labels=8, |
| problem_type="multi_label_classification" |
| ) |
| |
| model.load_state_dict(torch.load('best_model_v2.pth', map_location=torch.device('cpu'))) |
| model.eval() |
| tokenizer = AutoTokenizer.from_pretrained("microsoft/BiomedNLP-BiomedBERT-base-uncased-abstract") |
| return model, tokenizer |
|
|
|
|
| @st.cache_resource |
| def load_mlb(): |
| |
| classes = ['81001.0','99213.0','99214.0','E11.9','I10','J45.909','M54.5','N39.0'] |
| |
| mlb = MultiLabelBinarizer(classes=classes) |
| mlb.fit([classes]) |
| |
| return mlb |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| model, tokenizer = load_model() |
| mlb = load_mlb() |
|
|
| |
| st.title("Automated Medical Coding") |
| |
|
|
| |
| clinical_note = st.text_area("Enter clinical notes") |
|
|
| |
| if st.button('Predict'): |
| if clinical_note: |
| |
| inputs = tokenizer(clinical_note, truncation=True, padding="max_length", max_length=512, return_tensors='pt') |
| |
| |
| |
| inputs = {key: val.to(torch.device('cpu')) for key, val in inputs.items()} |
|
|
| |
| |
| with torch.no_grad(): |
| outputs = model(**inputs) |
| logits = outputs.logits |
| |
| |
| pred_labels = (torch.sigmoid(logits) > 0.5).cpu().numpy() |
| |
| |
| predicted_codes = mlb.inverse_transform(pred_labels) |
| |
| |
| if predicted_codes: |
| st.write("**Predicted CPT and ICD Codes:**") |
| for codes in predicted_codes: |
| for code in codes: |
| if code in ['81001.0', '99213.0', '99214.0']: |
| st.write(f"- **CPT Code:** {code}") |
| else: |
| st.write(f"- **ICD Code:** {code}") |
| else: |
| st.write("No codes predicted.") |
| |
| else: |
| st.write("Please enter clinical notes for prediction.") |
|
|
|
|
|
|
|
|