Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,84 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
---
|
| 4 |
+
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
|
| 5 |
+
import torch
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# Define the model and tokenizer
|
| 9 |
+
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
|
| 10 |
+
model = DistilBertForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2)
|
| 11 |
+
|
| 12 |
+
# Define the key words and their corresponding labels
|
| 13 |
+
key_words = ['ascites', 'cirrhosis', 'liver disease']
|
| 14 |
+
labels = [0, 1]
|
| 15 |
+
|
| 16 |
+
# Define a function to preprocess the input text
|
| 17 |
+
def preprocess_text(text):
|
| 18 |
+
inputs = tokenizer.encode_plus(
|
| 19 |
+
text,
|
| 20 |
+
add_special_tokens=True,
|
| 21 |
+
max_length=512,
|
| 22 |
+
return_attention_mask=True,
|
| 23 |
+
return_tensors='pt'
|
| 24 |
+
)
|
| 25 |
+
return inputs
|
| 26 |
+
|
| 27 |
+
# Define a function to make predictions
|
| 28 |
+
def make_prediction(text):
|
| 29 |
+
inputs = preprocess_text(text)
|
| 30 |
+
outputs = model(inputs['input_ids'], attention_mask=inputs['attention_mask'])
|
| 31 |
+
logits = outputs.logits
|
| 32 |
+
probabilities = torch.nn.functional.softmax(logits, dim=1)
|
| 33 |
+
predicted_class = torch.argmax(probabilities)
|
| 34 |
+
return predicted_class.item()
|
| 35 |
+
|
| 36 |
+
# Define a function to get the clinic that the referral should be directed to
|
| 37 |
+
def get_clinic(text):
|
| 38 |
+
predicted_class = make_prediction(text)
|
| 39 |
+
if predicted_class == 1:
|
| 40 |
+
return 'Liver Clinic'
|
| 41 |
+
else:
|
| 42 |
+
return 'Kidney Clinic'
|
| 43 |
+
|
| 44 |
+
# Define the model's configuration
|
| 45 |
+
model_config = {
|
| 46 |
+
'model_type': 'distilbert',
|
| 47 |
+
'num_labels': 2,
|
| 48 |
+
'key_words': key_words,
|
| 49 |
+
'labels': labels
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
# Define the model's metadata
|
| 53 |
+
model_metadata = {
|
| 54 |
+
'name': 'Referral Clinic Classifier',
|
| 55 |
+
'description': 'A model that classifies referrals to either the Liver Clinic or Kidney Clinic based on the presence of certain key words.',
|
| 56 |
+
'author': 'Your Name',
|
| 57 |
+
'version': '1.0'
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
# Train the model
|
| 61 |
+
train_data = [
|
| 62 |
+
('Patient has ascites and cirrhosis.', 1),
|
| 63 |
+
('Patient has liver disease.', 1),
|
| 64 |
+
('Patient has kidney disease.', 0),
|
| 65 |
+
('Patient has liver failure.', 1),
|
| 66 |
+
('Patient has kidney failure.', 0),
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
for text, label in train_data:
|
| 70 |
+
inputs = preprocess_text(text)
|
| 71 |
+
labels = torch.tensor(label)
|
| 72 |
+
outputs = model(inputs['input_ids'], attention_mask=inputs['attention_mask'], labels=labels)
|
| 73 |
+
loss = outputs.loss
|
| 74 |
+
model.zero_grad()
|
| 75 |
+
loss.backward()
|
| 76 |
+
optimizer = torch.optim.Adam(model.parameters(), lr=1e-5)
|
| 77 |
+
optimizer.step()
|
| 78 |
+
|
| 79 |
+
# Save the model to a file
|
| 80 |
+
torch.save(model.state_dict(),'referral_clinic_classifier.pth')
|
| 81 |
+
with open('model_config.json', 'w') as f:
|
| 82 |
+
json.dump(model_config, f)
|
| 83 |
+
with open('model_metadata.json', 'w') as f:
|
| 84 |
+
json.dump(model_metadata, f)
|