-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
30 lines (25 loc) · 924 Bytes
/
Copy pathpredict.py
File metadata and controls
30 lines (25 loc) · 924 Bytes
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
import pickle
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
# Load model
model = load_model('emotion_model.h5')
# Load tokenizer and encoder
with open('tokenizer.pkl', 'rb') as f:
tokenizer = pickle.load(f)
with open('label_encoder.pkl', 'rb') as f:
label_encoder = pickle.load(f)
# Define predict function
def predict_emotion(text):
sequence = tokenizer.texts_to_sequences([text])
padded = pad_sequences(sequence, maxlen=50, padding='post', truncating='post')
pred = model.predict(padded)
emotion = label_encoder.inverse_transform([np.argmax(pred)])
return emotion[0]
# Try it
while True:
user_input = input("Type a sentence (or 'exit'): ")
if user_input.lower() == "exit":
break
emotion = predict_emotion(user_input)
print("💡 Emotion:", emotion)