|
--- |
|
license: apache-2.0 |
|
--- |
|
|
|
The following provides the code to implement the task of detecting personality from an input text. |
|
|
|
|
|
#import packages |
|
|
|
from transformers import AutoModelForSequenceClassification, AutoTokenizer |
|
import torch |
|
model = AutoModelForSequenceClassification.from_pretrained("Kevintu/Personality_LM") |
|
tokenizer = AutoTokenizer.from_pretrained("Kevintu/Personality_LM") |
|
|
|
|
|
# Example new text input |
|
#new_text = "I really enjoy working on complex problems and collaborating with others." |
|
|
|
|
|
# Define the path to your text file |
|
file_path = 'path/to/your/textfile.txt' |
|
|
|
# Read the content of the file |
|
with open(file_path, 'r', encoding='utf-8') as file: |
|
new_text = file.read() |
|
|
|
|
|
# Encode the text using the same tokenizer used during training |
|
encoded_input = tokenizer(new_text, return_tensors='pt', padding=True, truncation=True, max_length=64) |
|
|
|
|
|
# Move the model to the correct device (CPU in this case, or GPU if available) |
|
model.eval() # Set the model to evaluation mode |
|
|
|
# Perform the prediction |
|
with torch.no_grad(): |
|
outputs = model(**encoded_input) |
|
|
|
# Get the predictions (the output here depends on whether you are doing regression or classification) |
|
predictions = outputs.logits.squeeze() |
|
|
|
|
|
# Assuming the model is a regression model and outputs raw scores |
|
predicted_scores = predictions.numpy() # Convert to numpy array if necessary |
|
trait_names = ["Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism"] |
|
|
|
# Print the predicted personality traits scores |
|
for trait, score in zip(trait_names, predicted_scores): |
|
print(f"{trait}: {score:.4f}") |
|
|
|
##"output": "agreeableness: 0.4600000000; openness: 0.2700000000; conscientiousness: 0.3100000000; extraversion: 0.1000000000; neuroticism: 0.8400000000" |