File size: 2,345 Bytes
a3e82d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import logging
import os
import pickle
import re
from pathlib import Path

import tensorflow as tf
from tensorflow.keras.layers import TextVectorization

from .data_preprocessing import clean_text


# Configure logging
def configure_logging(log_dir, log_filename, log_level=logging.INFO):
    
    log_dir = Path(log_dir)
    log_dir.mkdir(exist_ok=True)
    log_file = log_dir / log_filename
    
    # Configure logging to both console and file
    logging.basicConfig(level=log_level,
                        format='%(asctime)s - %(levelname)s - %(message)s',
                        handlers=[
                            logging.StreamHandler(),
                            logging.FileHandler(log_file)
                        ])
    return
    

def save_text_vectorizer(text_vectorizer, filename):
    config = text_vectorizer.get_config()
    with open(filename, 'wb') as f:
        pickle.dump({'config': config}, f)


def load_counter(filename):
    with open (filename,'rb')  as counter :
        return pickle.load(counter)
    

def load_model(model, model_dir):
    """Load the model from disk."""
    # Load the Keras model
    return model.load_weights(model_dir)


# to load text vectorizer
def load_text_vectorizer(vectorizer_path):
    from_disk = pickle.load(open(vectorizer_path, "rb"))
    return TextVectorization.from_config(from_disk['config'])

# Pickle the config and weights
def save_text_vectorizer(vectorizer_path):
    pickle.dump({'config': text_vectorizer.get_config()}
                , open(vectorizer_path, "wb"))


def load_model_and_vectorizer(vectorizer_path, model_path):
    try:
        text_vectorizer = load_text_vectorizer(vectorizer_path)
        lstm_model = tf.keras.models.load_model(model_path)
        return text_vectorizer, lstm_model
    except Exception as e:
        logging.error(f'Error loading vectorizer and model: {e}')
        return None, None



def predict_sentiment(title, text, text_vectorizer, lstm_model):
    review = f'{title} {text}' # concatenate the title and text
    clean_review = clean_text(review)
    review_sequence = text_vectorizer([clean_review])
    prediction = lstm_model.predict(review_sequence)
    sentiment_score = prediction[0][0]
    sentiment_label = 'Positive' if sentiment_score >= 0.5 else 'Negative'
    return sentiment_label, sentiment_score