mirror of
https://github.com/marian-nmt/marian.git
synced 2024-09-17 09:47:34 +03:00
Use single connection
This commit is contained in:
parent
410f4d05bc
commit
df526c1fe3
@ -9,24 +9,15 @@ import argparse
|
||||
from websocket import create_connection
|
||||
|
||||
|
||||
def translate(batch, port=8080):
|
||||
ws = create_connection("ws://localhost:{}/translate".format(port))
|
||||
#print(batch.rstrip())
|
||||
ws.send(batch)
|
||||
result = ws.recv()
|
||||
print(result.rstrip())
|
||||
ws.close()
|
||||
|
||||
|
||||
def parse_args():
|
||||
if __name__ == "__main__":
|
||||
# handle command-line options
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("-b", "--batch-size", type=int, default=1)
|
||||
parser.add_argument("-p", "--port", type=int, default=8080)
|
||||
return parser.parse_args()
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
args = parse_args()
|
||||
# open connection
|
||||
ws = create_connection("ws://localhost:{}/translate".format(args.port))
|
||||
|
||||
count = 0
|
||||
batch = ""
|
||||
@ -34,9 +25,19 @@ if __name__ == "__main__":
|
||||
count += 1
|
||||
batch += line.decode('utf-8') if sys.version_info < (3, 0) else line
|
||||
if count == args.batch_size:
|
||||
translate(batch, port=args.port)
|
||||
# translate the batch
|
||||
ws.send(batch)
|
||||
result = ws.recv()
|
||||
print(result.rstrip())
|
||||
|
||||
count = 0
|
||||
batch = ""
|
||||
|
||||
if count:
|
||||
translate(batch, port=args.port)
|
||||
# translate the remaining sentences
|
||||
ws.send(batch)
|
||||
result = ws.recv()
|
||||
print(result.rstrip())
|
||||
|
||||
# close connection
|
||||
ws.close()
|
||||
|
Loading…
Reference in New Issue
Block a user