Views
No views yet
1from tensorflow.keras.models import load_model
2import pickle
3import numpy as np
4from tensorflow.keras.preprocessing.sequence import pad_sequences
5
6# Load the model
7model = load_model('path_to_model/hybrid_model.h5')
8
9# Load the tokenizer
10with open('path_to_tokenizer/tokenizer.pkl', 'rb') as f:
11 tokenizer = pickle.load(f)
12
13# Predict sentiment
14def predict_sentiment(text):
15 text = text.lower()
16 text = re.sub(r'[^\w\s]', '', text)
17 sequence = tokenizer.texts_to_sequences([text])
18 padded_sequence = pad_sequences(sequence, maxlen=100)
19 pred = model.predict(padded_sequence)
20 sentiment = np.argmax(pred)
21 return sentiment
22
23# Example usage
24text = "I love this product!"
25print(predict_sentiment(text))