Upload split_utils.py with huggingface_hub
Browse files1234567858 unmodified lines6768697071727345 unmodified lines11912012112212312412579 unmodified lines20520620720820921021121221321421521621721821943 unmodified lines263264265266267268269270271272273274275276277278279280281282import itertoolsimport randomimport refrom .generator_utils import ReusableGeneratordef parse_random_mix_string(input_str):58 unmodified linesresult_dict = {}# Split the input string into a list of sourcessources = re.split("\+", input_str)for source in sources:# If the source has a slice, parse itmatch = re.fullmatch(r"(\w+)\[(\d*):(\d*)\]", source)45 unmodified linesthe new streams, which consist of parts of the old streams chained together.Raises:ValueError: If a stream is supposed to be sliced at an index greater than its length.Example:>>> old_streams = {"train": [1, 2, 3, 4, 5, 6, 7, 8, 9], "test": [10, 11, 12, 13, 14]}79 unmodified linesreturn stream_mappingdef random_mix_generator(new_stream_name, new_stream_sources, stream_routing, rand, input_streams):for old_stream_name in new_stream_sources:optinal_streams, weights = stream_routing[old_stream_name]rand.seed(old_stream_name)for item in input_streams[old_stream_name]:choice = rand.choices(optinal_streams, weights=weights, k=1)[0]if choice == new_stream_name:yield itemdef random_mix_streams(input_streams, mapping):43 unmodified lines# Build stream routingstream_routing = build_stream_routing(mapping)rand = random.Random()# Create new stream generatorsfor new_stream_name, new_stream_sources in mapping.items():new_streams[new_stream_name] = ReusableGenerator(random_mix_generator,gen_kwargs={"new_stream_name": new_stream_name,"new_stream_sources": new_stream_sources,"stream_routing": stream_routing,"rand": rand,"input_streams": input_streams,},)return new_streams1234567891058 unmodified lines6970717273747545 unmodified lines12112212312412512612779 unmodified lines20720820921021121221321421521621721821922022122222322422522622722822923023123223323423523643 unmodified lines280281282283284285286287288289290291292293294295296297import itertoolsimport refrom typing import Dictfrom .generator_utils import ReusableGeneratorfrom .random_utils import nested_seedfrom .stream import Streamdef parse_random_mix_string(input_str):58 unmodified linesresult_dict = {}# Split the input string into a list of sourcessources = re.split(r"\+", input_str)for source in sources:# If the source has a slice, parse itmatch = re.fullmatch(r"(\w+)\[(\d*):(\d*)\]", source)45 unmodified linesthe new streams, which consist of parts of the old streams chained together.Raises:ValueError: If a stream is supposed to be sliced at an index greater than its length or a negative one.Example:>>> old_streams = {"train": [1, 2, 3, 4, 5, 6, 7, 8, 9], "test": [10, 11, 12, 13, 14]}79 unmodified linesreturn stream_mappingdef rename_split(input_streams: Dict[str, Stream], mapping: Dict[str, str]):"""Renames the streamsArgs:input_streams (dict): A dictionary containing the input streams, where each key isthe name of the stream and the value is an iterable or generatorrepresenting the stream.mapping (dict): A dictionary specifying the mapping of old streams to new streams.Returns:dict: A dictionary containing the generated new streams, where each key is the nameof the new stream and the value is a generator representing the stream."""return {mapping.get(key, key): val for key, val in input_streams.items()}def random_mix_generator(new_stream_name, new_stream_sources, stream_routing, input_streams):for old_stream_name in new_stream_sources:optinal_streams, weights = stream_routing[old_stream_name]with nested_seed(old_stream_name) as rand:for item in input_streams[old_stream_name]:choice = rand.choices(optinal_streams, weights=weights, k=1)[0]if choice == new_stream_name:yield itemdef random_mix_streams(input_streams, mapping):43 unmodified lines# Build stream routingstream_routing = build_stream_routing(mapping)with nested_seed():# Create new stream generatorsfor new_stream_name, new_stream_sources in mapping.items():new_streams[new_stream_name] = ReusableGenerator(random_mix_generator,gen_kwargs={"new_stream_name": new_stream_name,"new_stream_sources": new_stream_sources,"stream_routing": stream_routing,"input_streams": input_streams,},)return new_streams
split_utils.py+39-24