update
Browse files
app.py
CHANGED
@@ -10,52 +10,8 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
10 |
# Load model directly
|
11 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
12 |
|
13 |
-
tokenizer = AutoTokenizer.from_pretrained("mavinsao/mi-roberta-
|
14 |
-
model = AutoModelForSequenceClassification.from_pretrained("mavinsao/mi-roberta-
|
15 |
-
|
16 |
-
# Create a common label map
|
17 |
-
common_label_map = {'ADHD': 0, 'Anxiety': 1, 'bipolar': 2, 'BPD': 3, 'depression': 4, 'OCD': 5, 'ptsd': 6, 'none': 7}
|
18 |
-
num_classes = 8
|
19 |
-
|
20 |
-
|
21 |
-
def predict_labels(sentence, tokenizer, model, device, threshold=0.5, top_n=5):
|
22 |
-
# Tokenize the sentence and create attention mask
|
23 |
-
tokenized_input = tokenizer(
|
24 |
-
sentence,
|
25 |
-
add_special_tokens=True,
|
26 |
-
max_length=512,
|
27 |
-
padding="max_length",
|
28 |
-
truncation=True,
|
29 |
-
return_tensors="pt"
|
30 |
-
)
|
31 |
-
|
32 |
-
# Move the input tensors to the device
|
33 |
-
input_ids = tokenized_input['input_ids'].to(device)
|
34 |
-
attention_mask = tokenized_input['attention_mask'].to(device)
|
35 |
-
|
36 |
-
# Set the model to evaluation mode
|
37 |
-
model.eval()
|
38 |
-
|
39 |
-
# Make a prediction
|
40 |
-
with torch.no_grad():
|
41 |
-
output = model(input_ids, attention_mask)
|
42 |
-
|
43 |
-
# Apply thresholding to the logits to obtain predicted labels
|
44 |
-
logits = output.logits
|
45 |
-
sigmoid_output = torch.sigmoid(logits.squeeze(dim=0))
|
46 |
-
indices_above_threshold = torch.arange(logits.shape[-1], device=device)[sigmoid_output > threshold]
|
47 |
-
|
48 |
-
# Sort the indices by their sigmoid values
|
49 |
-
sorted_indices = indices_above_threshold[torch.argsort(sigmoid_output[indices_above_threshold], descending=True)]
|
50 |
-
|
51 |
-
# Map the predicted label indices back to the original class labels using the common label map
|
52 |
-
predicted_labels_with_score = [{"label": list(common_label_map.keys())[index], "score": sigmoid_output[index].item()} for index in sorted_indices[:top_n]]
|
53 |
-
|
54 |
-
# Create a JSON object with labels, scores, and short forms
|
55 |
-
json_result = [{"label": entry["label"], "score": entry["score"]} for entry in predicted_labels_with_score]
|
56 |
-
|
57 |
-
return json.dumps(json_result, indent=4)
|
58 |
-
|
59 |
|
60 |
# Streamlit app
|
61 |
st.title('Mental Illness Prediction')
|
@@ -63,8 +19,14 @@ st.title('Mental Illness Prediction')
|
|
63 |
# Input text area for user input
|
64 |
sentence = st.text_area("Enter the long sentence to predict your mental illness state:")
|
65 |
|
|
|
66 |
# Prediction button
|
67 |
if st.button('Predict'):
|
68 |
# Predict label
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# Load model directly
|
11 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
12 |
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained("mavinsao/mi-roberta-classification")
|
14 |
+
model = AutoModelForSequenceClassification.from_pretrained("mavinsao/mi-roberta-classification")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Streamlit app
|
17 |
st.title('Mental Illness Prediction')
|
|
|
19 |
# Input text area for user input
|
20 |
sentence = st.text_area("Enter the long sentence to predict your mental illness state:")
|
21 |
|
22 |
+
|
23 |
# Prediction button
|
24 |
if st.button('Predict'):
|
25 |
# Predict label
|
26 |
+
|
27 |
+
with torch.no_grad():
|
28 |
+
logits = model(sentence).logits
|
29 |
+
|
30 |
+
predicted_class_ids = torch.arange(0, logits.shape[-1])[torch.sigmoid(logits).squeeze(dim=0) > 0.5]
|
31 |
+
|
32 |
+
st.json(predicted_class_ids)
|