<reponame>Non1ce/Transformer-Bert
# -*- coding: utf-8 -*-


from model_train import pipeline_model
from Data import InputData


"""

Created on 20.07.2021

@author: Nikita

    The module is designed to predict the topic of the entered text. To make a prediction, it is enough to run the 
    module as the main program.The longer the text, the better the neural network classifies.

"""


def text_prediction(model, tokenizer, tokenizer_vocabulary, weights_file: str, path: str):

    model.load_weights(fr'{path}\output_data\{weights_file}.hdf5')

    input_text = input("Enter the text: ")

    text_sequence = tokenizer.fast_encode([input_text], tokenizer_vocabulary)

    result = model.predict(text_sequence)

    if result > 0.785:

        print('Neural Network class: Тематика МЧС')

    else:

        print('Neural Network class: Всесторонняя тематика')


def prediction(const_prediction: bool = False):

    """

    If const_prediction=False, then the function triggers a single topic prediction for the text or
    Const_prediction=True, then there will be infinite texts predictions.

    """

    _, _, model_instance, model_bert = pipeline_model(path=path,
                                                      max_len=max_len,
                                                      batch_size=batch_size,
                                                      bert_model_name=bert_model_name)

    tokenizer = InputData(path=path,
                          max_len=max_len,
                          batch_size=batch_size)

    tokenizer_vocabulary = tokenizer.tokenizer()

    text_prediction(model=model_bert,
                    path=path,
                    weights_file=weights_file,
                    tokenizer=tokenizer,
                    tokenizer_vocabulary=tokenizer_vocabulary)

    while const_prediction:

        text_prediction(model=model_bert,
                        path=path,
                        weights_file=weights_file,
                        tokenizer=tokenizer,
                        tokenizer_vocabulary=tokenizer_vocabulary)


if __name__ == "__main__":

    weights_file = r'BERT_weights.0.97--04--0.09'
    bert_model_name = 'distilbert-base-multilingual-cased'
    path = r'C:\PythonProjects\Jobs\BERT_model'

    batch_size = 10
    max_len = 250

    prediction(const_prediction=True)

# -*- coding: utf-8 -*-


from model_train import pipeline_model
from Data import InputData


"""

Created on 20.07.2021

@author: Nikita

    The module is designed to predict the topic of the entered text. To make a prediction, it is enough to run the 
    module as the main program.The longer the text, the better the neural network classifies.

"""


def text_prediction(model, tokenizer, tokenizer_vocabulary, weights_file: str, path: str):

    model.load_weights(fr'{path}\output_data\{weights_file}.hdf5')

    input_text = input("Enter the text: ")

    text_sequence = tokenizer.fast_encode([input_text], tokenizer_vocabulary)

    result = model.predict(text_sequence)

    if result > 0.785:

        print('Neural Network class: Тематика МЧС')

    else:

        print('Neural Network class: Всесторонняя тематика')


def prediction(const_prediction: bool = False):

    """

    If const_prediction=False, then the function triggers a single topic prediction for the text or
    Const_prediction=True, then there will be infinite texts predictions.

    """

    _, _, model_instance, model_bert = pipeline_model(path=path,
                                                      max_len=max_len,
                                                      batch_size=batch_size,
                                                      bert_model_name=bert_model_name)

    tokenizer = InputData(path=path,
                          max_len=max_len,
                          batch_size=batch_size)

    tokenizer_vocabulary = tokenizer.tokenizer()

    text_prediction(model=model_bert,
                    path=path,
                    weights_file=weights_file,
                    tokenizer=tokenizer,
                    tokenizer_vocabulary=tokenizer_vocabulary)

    while const_prediction:

        text_prediction(model=model_bert,
                        path=path,
                        weights_file=weights_file,
                        tokenizer=tokenizer,
                        tokenizer_vocabulary=tokenizer_vocabulary)


if __name__ == "__main__":

    weights_file = r'BERT_weights.0.97--04--0.09'
    bert_model_name = 'distilbert-base-multilingual-cased'
    path = r'C:\PythonProjects\Jobs\BERT_model'

    batch_size = 10
    max_len = 250

    prediction(const_prediction=True)
# -*- coding: utf-8 -*- Created on 20.07.2021  @author: Nikita      The module is designed to predict the topic of the entered text. To make a prediction, it is enough to run the      module as the main program.The longer the text, the better the neural network classifies. If const_prediction=False, then the function triggers a single topic prediction for the text or     Const_prediction=True, then there will be infinite texts predictions.