Views
No views yet
1from huggingface_hub import from_pretrained_keras
2import re
3import numpy as np
4from tensorflow.keras.preprocessing.sequence import pad_sequences
5
6# Load model
7model = from_pretrained_keras("Ravinthiran/DistilSenti-Net42M")
8
9# Example prediction function
10def predict_sentiment(text, model, tokenizer, label_encoder):
11 text = text.lower()
12 text = re.sub(r'[^\w\s]', '', text)
13 sequence = tokenizer.texts_to_sequences([text])
14 padded_sequence = pad_sequences(sequence, maxlen=100)
15 pred = model.predict(padded_sequence)
16 sentiment = label_encoder.inverse_transform(pred.argmax(axis=1))
17 sentiment_score = pred[0]
18 return sentiment[0], sentiment_score
19
20# Example usage
21new_text = "I recently started a new fitness program at a local wellness center, and it has been an incredibly positive experience."
22predicted_sentiment, sentiment_score = predict_sentiment(new_text, model, tokenizer, label_encoder)
23
24print(f"Predicted Sentiment: {predicted_sentiment}")
25print(f"Sentiment Scores: {sentiment_score}")
261import numpy as np
2import pandas as pd
3import re
4import matplotlib.pyplot as plt
5import seaborn as sns
6from sklearn.preprocessing import LabelEncoder
7from tensorflow.keras.preprocessing.text import Tokenizer
8from tensorflow.keras.preprocessing.sequence import pad_sequences
9from tensorflow.keras.models import load_model
10
11# Load the saved Keras model
12model_hybrid = load_model('< DistilSentiNet-42M.h5 File Path > or < DistilSentiNet-42M.keras File Path >')
13
14# Sample data
15df = pd.read_csv("<Your Test Dataset>")
16
17# Preprocessing
18df['text'] = df['text'].str.lower().str.replace('[^\w\s]', '', regex=True)
19
20# Encode labels
21label_encoder = LabelEncoder()
22df['label'] = label_encoder.fit_transform(df['sentiment'])
23
24# Tokenization and padding
25tokenizer = Tokenizer(num_words=5000)
26tokenizer.fit_on_texts(df['text'])
27X = tokenizer.texts_to_sequences(df['text'])
28X = pad_sequences(X, maxlen=100)
29
30# Function to predict sentiment of new input text
31def predict_sentiment(text, tokenizer, model):
32 # Preprocess the input text
33 text = text.lower()
34 text = re.sub(r'[^\w\s]', '', text)
35 sequence = tokenizer.texts_to_sequences([text])
36 padded_sequence = pad_sequences(sequence, maxlen=100)
37
38 # Predict sentiment
39 pred = model.predict(padded_sequence)
40 sentiment = label_encoder.inverse_transform(pred.argmax(axis=1))
41 sentiment_score = pred[0]
42
43 return sentiment[0], sentiment_score
44
45# Example usage
46new_text = "I recently started a new fitness program at a local wellness center, and it has been an incredibly positive experience. The trainers are highly knowledgeable and provide personalized guidance to help me achieve my fitness goals. The facilities are state-of-the-art, with a wide range of equipment and classes to choose from. The supportive community and motivating environment have made working out enjoyable and rewarding. I have already noticed significant improvements in my health and fitness levels, and the positive changes have greatly enhanced my overall well-being."
47
48predicted_sentiment, sentiment_score = predict_sentiment(new_text, tokenizer, model_hybrid)
49
50print(f"The sentiment of the input text is: {predicted_sentiment} with scores {sentiment_score}")
51