-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
63 lines (47 loc) · 1.73 KB
/
predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import sys
import joblib
import tensorflow as tf
from transformers import AutoTokenizer
from train import train_model
import argparse
# Check if the saved model exists
if not os.path.exists("emotion_model"):
# Run the train script to train the model
train_model()
print("Model trained successfully.")
# Load the trained model
model = tf.keras.models.load_model("emotion_model")
# Load tokenizer
model_ckpt = "distilbert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
# Load label encoder
label_encoder = joblib.load('label_encoder.joblib')
def predict_emotion(input_text):
tokens = tokenizer(input_text, padding=True, truncation=True, return_tensors="tf")
inputs = {
'input_ids': tokens['input_ids'],
'attention_mask': tokens['attention_mask']
}
# Make prediction
predictions = model.predict(inputs)
# Extract logits from the predictions dictionary
logits = predictions['logits']
# Determine the predicted label
predicted_label = int(tf.argmax(logits, axis=-1)[0])
# Convert the predicted label to emotion
predicted_emotion = label_encoder.inverse_transform([predicted_label])[0]
return predicted_emotion
# Create an argument parser
parser = argparse.ArgumentParser()
parser.add_argument("--text", type=str, help="Input text for emotion prediction")
# Parse the command line arguments
args = parser.parse_args()
# Get the input text from the command line
input_text = args.text if args.text else "No input text provided"
# Make prediction
predicted_emotion = predict_emotion(input_text)
# Print the input text
print(f"The input text is: {input_text}")
# Print the result
print(f"The predicted emotion is: {predicted_emotion}")