File size: 1,991 Bytes
901e06a | 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 | # Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import os
import tempfile
from pathlib import Path
import simuleval.cli as cli
from simuleval.agents import TextToTextAgent
from simuleval.agents.actions import ReadAction, WriteAction
from simuleval.data.segments import TextSegment
ROOT_PATH = Path(__file__).parents[2]
def test_agent(root_path=ROOT_PATH):
with tempfile.TemporaryDirectory() as tmpdirname:
cli.sys.argv[1:] = [
"--agent",
os.path.join(root_path, "examples", "quick_start", "first_agent.py"),
"--source",
os.path.join(root_path, "examples", "quick_start", "source.txt"),
"--target",
os.path.join(root_path, "examples", "quick_start", "target.txt"),
"--output",
tmpdirname,
]
cli.main()
def test_statelss_agent(root_path=ROOT_PATH):
class DummyWaitkTextAgent(TextToTextAgent):
waitk = 0
vocab = [chr(i) for i in range(ord("A"), ord("Z") + 1)]
def policy(self, states=None):
if states is None:
states = self.states
lagging = len(states.source) - len(states.target)
if lagging >= self.waitk or states.source_finished:
prediction = self.vocab[len(states.source)]
return WriteAction(prediction, finished=(lagging <= 1))
else:
return ReadAction()
args = None
agent_stateless = DummyWaitkTextAgent.from_args(args)
agent_state = agent_stateless.build_states()
agent_stateful = DummyWaitkTextAgent.from_args(args)
for _ in range(10):
segment = TextSegment(0, "A")
output_1 = agent_stateless.pushpop(segment, agent_state)
output_2 = agent_stateful.pushpop(segment)
assert output_1.content == output_2.content
|