Update README.md
Browse files
README.md
CHANGED
@@ -25,39 +25,109 @@ The project of predicting human cognition and emotion, and training details are
|
|
25 |
The following provides the code to implement the task of detecting personality from an input text.
|
26 |
|
27 |
```python
|
28 |
-
#
|
29 |
|
30 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
31 |
import torch
|
|
|
32 |
model = AutoModelForSequenceClassification.from_pretrained("KevSun/Personality_LM")
|
33 |
tokenizer = AutoTokenizer.from_pretrained("KevSun/Personality_LM")
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
40 |
|
41 |
# Encode the text using the same tokenizer used during training
|
42 |
encoded_input = tokenizer(new_text, return_tensors='pt', padding=True, truncation=True, max_length=64)
|
43 |
|
44 |
-
#
|
45 |
-
#model.eval() # Set the model to evaluation mode
|
46 |
|
47 |
# Perform the prediction
|
48 |
with torch.no_grad():
|
49 |
outputs = model(**encoded_input)
|
50 |
|
51 |
-
# Get the predictions
|
52 |
predictions = outputs.logits.squeeze()
|
53 |
|
54 |
-
#
|
55 |
-
predicted_scores = predictions.numpy()
|
|
|
56 |
trait_names = ["Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism"]
|
57 |
|
58 |
# Print the predicted personality traits scores
|
59 |
for trait, score in zip(trait_names, predicted_scores):
|
60 |
print(f"{trait}: {score:.4f}")
|
61 |
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
```
|
|
|
25 |
The following provides the code to implement the task of detecting personality from an input text.
|
26 |
|
27 |
```python
|
28 |
+
# install these packages before importing them (transformers, PyTorch)
|
29 |
|
30 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
31 |
import torch
|
32 |
+
|
33 |
model = AutoModelForSequenceClassification.from_pretrained("KevSun/Personality_LM")
|
34 |
tokenizer = AutoTokenizer.from_pretrained("KevSun/Personality_LM")
|
35 |
|
36 |
+
# Choose between direct text input or file input
|
37 |
+
use_file = False # Set to True if you want to read from a file
|
38 |
+
|
39 |
+
if use_file:
|
40 |
+
file_path = 'path/to/your/textfile.txt' # Replace with your file path
|
41 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
42 |
+
new_text = file.read()
|
43 |
+
else:
|
44 |
+
new_text = "I really enjoy working on complex problems and collaborating with others."
|
45 |
|
46 |
# Encode the text using the same tokenizer used during training
|
47 |
encoded_input = tokenizer(new_text, return_tensors='pt', padding=True, truncation=True, max_length=64)
|
48 |
|
49 |
+
model.eval() # Set the model to evaluation mode
|
|
|
50 |
|
51 |
# Perform the prediction
|
52 |
with torch.no_grad():
|
53 |
outputs = model(**encoded_input)
|
54 |
|
55 |
+
# Get the predictions
|
56 |
predictions = outputs.logits.squeeze()
|
57 |
|
58 |
+
# Convert to numpy array if necessary
|
59 |
+
predicted_scores = predictions.numpy()
|
60 |
+
|
61 |
trait_names = ["Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism"]
|
62 |
|
63 |
# Print the predicted personality traits scores
|
64 |
for trait, score in zip(trait_names, predicted_scores):
|
65 |
print(f"{trait}: {score:.4f}")
|
66 |
|
67 |
+
|
68 |
+
##"output":
|
69 |
+
#Agreeableness: 0.3965
|
70 |
+
#Openness: 0.6714
|
71 |
+
#Conscientiousness: 0.3283
|
72 |
+
#Extraversion: 0.0026
|
73 |
+
#Neuroticism: 0.4645
|
74 |
+
|
75 |
+
```
|
76 |
+
|
77 |
+
**Alternatively**, you can use the following code to make inference based on the **bash** terminal.
|
78 |
+
```
|
79 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
80 |
+
import torch
|
81 |
+
import argparse
|
82 |
+
|
83 |
+
def load_model_and_tokenizer(model_name):
|
84 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
85 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
86 |
+
return model, tokenizer
|
87 |
+
|
88 |
+
def process_input(input_text, tokenizer, max_length=64):
|
89 |
+
return tokenizer(input_text, return_tensors='pt', padding=True, truncation=True, max_length=max_length)
|
90 |
+
|
91 |
+
def predict_personality(model, encoded_input):
|
92 |
+
model.eval() # Set the model to evaluation mode
|
93 |
+
with torch.no_grad():
|
94 |
+
outputs = model(**encoded_input)
|
95 |
+
return outputs.logits.squeeze()
|
96 |
+
|
97 |
+
def print_predictions(predictions, trait_names):
|
98 |
+
for trait, score in zip(trait_names, predictions):
|
99 |
+
print(f"{trait}: {score:.4f}")
|
100 |
+
|
101 |
+
def main():
|
102 |
+
parser = argparse.ArgumentParser(description="Predict personality traits from text.")
|
103 |
+
parser.add_argument("--input", type=str, required=True, help="Input text or path to text file")
|
104 |
+
parser.add_argument("--model", type=str, default="KevSun/Personality_LM", help="Model name or path")
|
105 |
+
args = parser.parse_args()
|
106 |
+
|
107 |
+
model, tokenizer = load_model_and_tokenizer(args.model)
|
108 |
+
|
109 |
+
# Check if input is a file path or direct text
|
110 |
+
if args.input.endswith('.txt'):
|
111 |
+
with open(args.input, 'r', encoding='utf-8') as file:
|
112 |
+
input_text = file.read()
|
113 |
+
else:
|
114 |
+
input_text = args.input
|
115 |
+
|
116 |
+
encoded_input = process_input(input_text, tokenizer)
|
117 |
+
predictions = predict_personality(model, encoded_input)
|
118 |
+
|
119 |
+
trait_names = ["Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism"]
|
120 |
+
print_predictions(predictions.numpy(), trait_names)
|
121 |
+
|
122 |
+
if __name__ == "__main__":
|
123 |
+
main()
|
124 |
+
```
|
125 |
+
```
|
126 |
+
bash
|
127 |
+
python script_name.py --input "Your text here"
|
128 |
+
```
|
129 |
+
or
|
130 |
+
```
|
131 |
+
bash
|
132 |
+
python script_name.py --input path/to/your/textfile.txt
|
133 |
```
|