Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
---
|
4 |
+
|
5 |
+
The following provides the code to implement the task of detecting personality from an input text.
|
6 |
+
|
7 |
+
|
8 |
+
#import packages
|
9 |
+
|
10 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
11 |
+
import torch
|
12 |
+
model = AutoModelForSequenceClassification.from_pretrained("Kevintu/Personality_LM")
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained("Kevintu/Personality_LM")
|
14 |
+
|
15 |
+
|
16 |
+
# Example new text input
|
17 |
+
#new_text = "I really enjoy working on complex problems and collaborating with others."
|
18 |
+
|
19 |
+
|
20 |
+
# Define the path to your text file
|
21 |
+
file_path = 'path/to/your/textfile.txt'
|
22 |
+
|
23 |
+
# Read the content of the file
|
24 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
25 |
+
new_text = file.read()
|
26 |
+
|
27 |
+
|
28 |
+
# Encode the text using the same tokenizer used during training
|
29 |
+
encoded_input = tokenizer(new_text, return_tensors='pt', padding=True, truncation=True, max_length=64)
|
30 |
+
|
31 |
+
|
32 |
+
# Move the model to the correct device (CPU in this case, or GPU if available)
|
33 |
+
model.eval() # Set the model to evaluation mode
|
34 |
+
|
35 |
+
# Perform the prediction
|
36 |
+
with torch.no_grad():
|
37 |
+
outputs = model(**encoded_input)
|
38 |
+
|
39 |
+
# Get the predictions (the output here depends on whether you are doing regression or classification)
|
40 |
+
predictions = outputs.logits.squeeze()
|
41 |
+
|
42 |
+
|
43 |
+
# Assuming the model is a regression model and outputs raw scores
|
44 |
+
predicted_scores = predictions.numpy() # Convert to numpy array if necessary
|
45 |
+
trait_names = ["Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism"]
|
46 |
+
|
47 |
+
# Print the predicted personality traits scores
|
48 |
+
for trait, score in zip(trait_names, predicted_scores):
|
49 |
+
print(f"{trait}: {score:.4f}")
|
50 |
+
|
51 |
+
##"output": "agreeableness: 0.4600000000; openness: 0.2700000000; conscientiousness: 0.3100000000; extraversion: 0.1000000000; neuroticism: 0.8400000000"
|