<reponame>sagnik/baseline
import baseline as bl
import argparse
import os
import pandas as pd
from addons.nliservice import NLIService
from mead.utils import read_config_file_or_json


def main():
    parser = argparse.ArgumentParser(description='Classify text with a model')
    parser.add_argument('--model', help='The path to either the .zip file created by training or to the client bundle '
                                        'created by exporting', required=True, type=str)
    parser.add_argument('--config', type=str, required=True)
    parser.add_argument('--text1', type=str)
    parser.add_argument('--text2', type=str)
    parser.add_argument('--file', type=str)
    parser.add_argument('--backend', help='backend', choices={'tf', 'pytorch'}, default='pytorch')
    parser.add_argument('--device', help='device')
    parser.add_argument('--batchsz', help='batch size when --text is a file', default=100, type=int)
    parser.add_argument('--modules', default=[])
    args = parser.parse_args()

    if args.backend == 'tf':
        from eight_mile.tf.layers import set_tf_eager_mode
        set_tf_eager_mode(args.prefer_eager)

    for mod_name in args.modules:
        bl.import_user_module(mod_name)

    if os.path.exists(args.file) and os.path.isfile(args.file):
        df = pd.read_csv(args.file)
        text_1 = [x.strip().split() for x in df['hypothesis']]
        text_2 = [x.strip().split() for x in df['premise']]
    else:
        text_1 = [args.text1.split()]
        text_2 = [args.text2.split()]

    text_1_batched = [text_1[i:i + args.batchsz] for i in range(0, len(text_1), args.batchsz)]
    text_2_batched = [text_2[i:i + args.batchsz] for i in range(0, len(text_2), args.batchsz)]

    config = read_config_file_or_json(args.config)
    loader_config = config['loader']
    model_type = config['model']['model_type']
    model = NLIService.load(args.model, model_type=model_type, backend=args.backend, device=args.device, **loader_config)
    for text_1_batch, text_2_batch in zip(text_1_batched, text_2_batched):
        output_batch = model.predict(text_1_batch, text_2_batch)
        for text_1, text_2, output in zip(text_1_batch, text_2_batch, output_batch):
            print("text1: {}, text2: {}, prediction: {}".format(" ".join(text_1), " ".join(text_2), output[0][0]))
        print('='*30)


if __name__ == '__main__':
    main()

import baseline as bl
import argparse
import os
import pandas as pd
from addons.nliservice import NLIService
from mead.utils import read_config_file_or_json


def main():
    parser = argparse.ArgumentParser(description='Classify text with a model')
    parser.add_argument('--model', help='The path to either the .zip file created by training or to the client bundle '
                                        'created by exporting', required=True, type=str)
    parser.add_argument('--config', type=str, required=True)
    parser.add_argument('--text1', type=str)
    parser.add_argument('--text2', type=str)
    parser.add_argument('--file', type=str)
    parser.add_argument('--backend', help='backend', choices={'tf', 'pytorch'}, default='pytorch')
    parser.add_argument('--device', help='device')
    parser.add_argument('--batchsz', help='batch size when --text is a file', default=100, type=int)
    parser.add_argument('--modules', default=[])
    args = parser.parse_args()

    if args.backend == 'tf':
        from eight_mile.tf.layers import set_tf_eager_mode
        set_tf_eager_mode(args.prefer_eager)

    for mod_name in args.modules:
        bl.import_user_module(mod_name)

    if os.path.exists(args.file) and os.path.isfile(args.file):
        df = pd.read_csv(args.file)
        text_1 = [x.strip().split() for x in df['hypothesis']]
        text_2 = [x.strip().split() for x in df['premise']]
    else:
        text_1 = [args.text1.split()]
        text_2 = [args.text2.split()]

    text_1_batched = [text_1[i:i + args.batchsz] for i in range(0, len(text_1), args.batchsz)]
    text_2_batched = [text_2[i:i + args.batchsz] for i in range(0, len(text_2), args.batchsz)]

    config = read_config_file_or_json(args.config)
    loader_config = config['loader']
    model_type = config['model']['model_type']
    model = NLIService.load(args.model, model_type=model_type, backend=args.backend, device=args.device, **loader_config)
    for text_1_batch, text_2_batch in zip(text_1_batched, text_2_batched):
        output_batch = model.predict(text_1_batch, text_2_batch)
        for text_1, text_2, output in zip(text_1_batch, text_2_batch, output_batch):
            print("text1: {}, text2: {}, prediction: {}".format(" ".join(text_1), " ".join(text_2), output[0][0]))
        print('='*30)


if __name__ == '__main__':
    main()