|
| 1 | +# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions |
| 5 | +# are met: |
| 6 | +# * Redistributions of source code must retain the above copyright |
| 7 | +# notice, this list of conditions and the following disclaimer. |
| 8 | +# * Redistributions in binary form must reproduce the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer in the |
| 10 | +# documentation and/or other materials provided with the distribution. |
| 11 | +# * Neither the name of NVIDIA CORPORATION nor the names of its |
| 12 | +# contributors may be used to endorse or promote products derived |
| 13 | +# from this software without specific prior written permission. |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 16 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 19 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + |
| 27 | +#!/usr/bin/env python |
| 28 | + |
| 29 | +import argparse |
| 30 | +import os |
| 31 | +import sys |
| 32 | + |
| 33 | +import grpc |
| 34 | +import riva.client.proto.riva_nmt_pb2 as riva_nmt |
| 35 | +import riva.client.proto.riva_nmt_pb2_grpc as riva_nmt_srv |
| 36 | + |
| 37 | +import riva.client |
| 38 | +from riva.client.argparse_utils import add_connection_argparse_parameters |
| 39 | + |
| 40 | + |
| 41 | +def parse_args() -> argparse.Namespace: |
| 42 | + parser = argparse.ArgumentParser( |
| 43 | + description="Neural machine translation by Riva AI Services", |
| 44 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 45 | + ) |
| 46 | + inputs = parser.add_mutually_exclusive_group() |
| 47 | + inputs.add_argument( |
| 48 | + "--text", default="mir Das ist mir Wurs, bien ich ein berliner", type=str, help="Text to translate" |
| 49 | + ) |
| 50 | + inputs.add_argument("--text-file", type=str, help="Path to file for translation") |
| 51 | + parser.add_argument("--model-name", default="riva-nmt", type=str, help="model to use to translate") |
| 52 | + parser.add_argument( |
| 53 | + "--src-language", type=str, help="Source language (according to BCP-47 standard)" |
| 54 | + ) |
| 55 | + parser.add_argument( |
| 56 | + "--tgt-language", type=str, help="Target language (according to BCP-47 standard)" |
| 57 | + ) |
| 58 | + parser.add_argument("--text-file", type=str, help="Path to file for translation") |
| 59 | + parser.add_argument("--batch-size", type=int, default=8, help="Batch size to use for file translation") |
| 60 | + parser.add_argument("--list-models", default=False, action='store_true', help="List available models") |
| 61 | + parser = add_connection_argparse_parameters(parser) |
| 62 | + |
| 63 | + return parser.parse_args() |
| 64 | + |
| 65 | + |
| 66 | +def main() -> None: |
| 67 | + def request(inputs,args): |
| 68 | + try: |
| 69 | + response = nmt_client.translate(inputs, args.model_name, args.src_language, args.tgt_language) |
| 70 | + for translation in response.translations: |
| 71 | + print(translation.text) |
| 72 | + except grpc.RpcError as e: |
| 73 | + if e.code() == grpc.StatusCode.INVALID_ARGUMENT: |
| 74 | + result = {'msg': 'invalid arg error'} |
| 75 | + elif e.code() == grpc.StatusCode.ALREADY_EXISTS: |
| 76 | + result = {'msg': 'already exists error'} |
| 77 | + elif e.code() == grpc.StatusCode.UNAVAILABLE: |
| 78 | + result = {'msg': 'server unavailable check network'} |
| 79 | + print(f"{result['msg']} : {e.details()}") |
| 80 | + |
| 81 | + args = parse_args() |
| 82 | + |
| 83 | + auth = riva.client.Auth(args.ssl_cert, args.use_ssl, args.server) |
| 84 | + nmt_client = riva.client.NeuralMachineTranslationClient(auth) |
| 85 | + |
| 86 | + if args.list_models: |
| 87 | + |
| 88 | + response = nmt_client.get_config(args.model_name) |
| 89 | + print(response) |
| 90 | + return |
| 91 | + |
| 92 | + if args.text_file != None and os.path.exists(args.text_file): |
| 93 | + with open(args.text_file, "r") as f: |
| 94 | + batch = [] |
| 95 | + for line in f: |
| 96 | + line = line.strip() |
| 97 | + if line != "": |
| 98 | + batch.append(line) |
| 99 | + if len(batch) == args.batch_size: |
| 100 | + request(batch, args) |
| 101 | + batch = [] |
| 102 | + if len(batch) > 0: |
| 103 | + request(batch, args) |
| 104 | + return |
| 105 | + |
| 106 | + if args.text != "": |
| 107 | + request([args.text], args) |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == '__main__': |
| 111 | + main() |
0 commit comments