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