mavinsao commited on
Commit
a7d2103
·
verified ·
1 Parent(s): ae12529
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -1,17 +1,13 @@
1
  import torch
2
- import torch.nn as nn
3
- from transformers import RobertaTokenizer, RobertaModel
4
- import json
5
  import streamlit as st
6
 
7
  # Set device (GPU if available, otherwise CPU)
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
 
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,14 +15,19 @@ 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)
 
1
  import torch
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
 
 
3
  import streamlit as st
4
 
5
  # Set device (GPU if available, otherwise CPU)
6
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
 
8
+ # Load tokenizer and model
 
 
9
  tokenizer = AutoTokenizer.from_pretrained("mavinsao/mi-roberta-classification")
10
+ model = AutoModelForSequenceClassification.from_pretrained("mavinsao/mi-roberta-classification").to(device)
11
 
12
  # Streamlit app
13
  st.title('Mental Illness Prediction')
 
15
  # Input text area for user input
16
  sentence = st.text_area("Enter the long sentence to predict your mental illness state:")
17
 
 
18
  # Prediction button
19
  if st.button('Predict'):
20
+ # Tokenize the input sentence
21
+ inputs = tokenizer(sentence, return_tensors="pt", padding=True, truncation=True).to(device)
22
 
23
+ # Forward pass
24
  with torch.no_grad():
25
+ outputs = model(**inputs)
26
+
27
+ # Get predicted probabilities
28
+ probabilities = torch.sigmoid(outputs.logits).squeeze(dim=0)
29
 
30
+ # Get predicted labels with probability greater than 0.5
31
+ predicted_labels = [label for i, label in enumerate(tokenizer.labels) if probabilities[i] > 0.5]
32
 
33
+ st.write("Predicted labels:", predicted_labels)