File size: 5,091 Bytes
dfe58cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from flask import Flask, render_template, request, url_for
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import Tokenizer
import pickle
import json
import re

app = Flask(__name__, static_folder='static')

cls_biLSTM = load_model("Classification/biLSTM_model.h5")
cls_LSTM = load_model("Classification/LSTM_model.h5")
cls_GRU = load_model("Classification/GRU_model.h5")

gen_biLSTM = load_model("Generation/bilstm_model.h5")
gen_LSTM = load_model("Generation/lstm_model.h5")
gen_GRU = load_model("Generation/gru_model.h5")


# Post-process Texts
def postprocess_text(text):
    # Remove leading and trailing whitespace, consecutive spaces, and ensure a space after punctuation marks
    text = re.sub(r"\s+", " ", text.strip())
    text = re.sub(r"(\w)([.!?])(\w)", r"\1\2 \3", text)

    # Capitalize the first letter of the sentence
    text = text[0].upper() + text[1:]

    # # Add a period at the end if missing
    # if not text.endswith("."):
    #     text += "."

    return text



@app.route('/')
def index():
    return render_template('index.html')


@app.route('/classifier')
def classifier():
    return render_template('classifier.html')


@app.route('/classification', methods=['GET', 'POST'])
def classification():
    if request.method == 'POST':
        sentence = request.form['sentence']
        
        with open('Classification/data.json', 'r') as file:
            data = json.load(file)

        max_length = data['max_length']
        padding_type = data['padding_type']
        trunc_type = data['trunc_type']
        threshold = data['threshold']
        tokenizer = pickle.load(open('Classification/tokenizer.pkl', 'rb'))

        sequences = tokenizer.texts_to_sequences([sentence])
        padded = pad_sequences(sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)
        biLSTM_pred = cls_biLSTM.predict(padded)
        LSTM_pred = cls_LSTM.predict(padded)
        GRU_pred = cls_GRU.predict(padded)
        
        biLSTM_label = "Positive" if biLSTM_pred > threshold else "Negative"
        LSTM_label = "Positive" if LSTM_pred > threshold else "Negative"
        GRU_label = "Positive" if GRU_pred > threshold else "Negative"
        
        biLSTM_pred = "{:.9f}".format(biLSTM_pred[0][0])
        LSTM_pred = "{:.9f}".format(LSTM_pred[0][0])
        GRU_pred = "{:.9f}".format(GRU_pred[0][0])


        return render_template('classification.html', sentence=sentence,
                               biLSTM_pred=biLSTM_pred, biLSTM_label=biLSTM_label,
                               LSTM_pred=LSTM_pred, LSTM_label=LSTM_label,
                               GRU_pred=GRU_pred, GRU_label=GRU_label)

    return render_template('classification.html')



@app.route("/generation", methods=['GET', 'POST'])
def generation():
    if request.method == 'POST':
        sentence = postprocess_text(request.form['sentence'])
        next_words = int(request.form['valueradio'])

        # Generate text using LSTM
        LSTM_Pred = generate_text(sentence, next_words, "lstm")
    
        # Generate text using GRU
        GRU_Pred = generate_text(sentence, next_words, "gru")
    
        # Generate text using BiLSTM
        BILSTM_Pred = generate_text(sentence, next_words, "bilstm")
    
        return render_template("generation.html", sentence=sentence, next_words=next_words, LSTM_Pred=LSTM_Pred + ".", GRU_Pred=GRU_Pred + ".", BILSTM_Pred=BILSTM_Pred +".", valueradio=next_words)
    else:
        return render_template("generation.html")




def generate_text(sentence, next_words, model_name):
    models = {
        "lstm": gen_LSTM,
        "gru": gen_GRU,
        "bilstm": gen_biLSTM
    }

    model = models[model_name]
    with open('Classification/Reviews.json', 'r') as f:
        data = json.load(f)

    reviews = [item['Reviews'] for item in data]

    tokenizer = Tokenizer()
    tokenizer.fit_on_texts(reviews)
    total_words = len(tokenizer.word_index) + 1

    input_sequences = []
    for line in reviews:
        token_list = tokenizer.texts_to_sequences([line])[0]
        for i in range(1, len(token_list)):
            n_gram_sequence = token_list[:i+1]
            input_sequences.append(n_gram_sequence)

    max_sequence_len = max([len(x) for x in input_sequences])
    input_sequences = np.array(pad_sequences(input_sequences, maxlen=max_sequence_len, padding='pre'))

    generated_text = sentence

    for _ in range(next_words):
        token_list = tokenizer.texts_to_sequences([generated_text])[0]
        token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
        predicted = np.argmax(model.predict(token_list), axis=1)

        output_word = " "
        for word, index in tokenizer.word_index.items():
            if index == predicted:
                output_word = word
                break

        generated_text += " " + output_word

    return generated_text

if __name__ == '__main__':
    app.run(debug=True, port=8000)