|
import torch |
|
import torch.nn as nn |
|
from transformers import RobertaTokenizer, RobertaModel |
|
import json |
|
import streamlit as st |
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
|
|
tokenizer = AutoTokenizer.from_pretrained("mavinsao/mi-roberta-mental-illness") |
|
model = AutoModelForSequenceClassification.from_pretrained("mavinsao/mi-roberta-mental-illness") |
|
|
|
|
|
common_label_map = {'ADHD': 0, 'Anxiety': 1, 'bipolar': 2, 'BPD': 3, 'depression': 4, 'OCD': 5, 'ptsd': 6, 'none': 7} |
|
num_classes = 8 |
|
|
|
|
|
def predict_labels(sentence, tokenizer, model, device, threshold=0.5, top_n=5): |
|
|
|
tokenized_input = tokenizer( |
|
sentence, |
|
add_special_tokens=True, |
|
max_length=512, |
|
padding="max_length", |
|
truncation=True, |
|
return_tensors="pt" |
|
) |
|
|
|
|
|
input_ids = tokenized_input['input_ids'].to(device) |
|
attention_mask = tokenized_input['attention_mask'].to(device) |
|
|
|
|
|
model.eval() |
|
|
|
|
|
with torch.no_grad(): |
|
output = model(input_ids, attention_mask) |
|
|
|
|
|
logits = output.logits |
|
sigmoid_output = torch.sigmoid(logits.squeeze(dim=0)) |
|
indices_above_threshold = torch.arange(logits.shape[-1], device=device)[sigmoid_output > threshold] |
|
|
|
|
|
sorted_indices = indices_above_threshold[torch.argsort(sigmoid_output[indices_above_threshold], descending=True)] |
|
|
|
|
|
predicted_labels_with_score = [{"label": list(common_label_map.keys())[index], "score": sigmoid_output[index].item()} for index in sorted_indices[:top_n]] |
|
|
|
|
|
json_result = [{"label": entry["label"], "score": entry["score"]} for entry in predicted_labels_with_score] |
|
|
|
return json.dumps(json_result, indent=4) |
|
|
|
|
|
|
|
st.title('Mental Illness Prediction') |
|
|
|
|
|
sentence = st.text_area("Enter the long sentence to predict your mental illness state:") |
|
|
|
|
|
if st.button('Predict'): |
|
|
|
predicted_response = predict_labels(sentence, tokenizer, model, device) |
|
st.json(predicted_response) |