Upload split_utils.py with huggingface_hub
Browse files123456789101112166 unmodified lines17918018118218318418544 unmodified lines23023123223323423523623723823924024124224332 unmodified lines276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305import itertoolsimport loggingimport refrom typing import Dictfrom .generator_utils import ReusableGeneratorfrom .random_utils import nested_seedfrom .stream import Streamdef parse_random_mix_string(input_str):"""Parses a string of format "source1[percentage1%]+source2[value2]+..." and returns a dictionary.166 unmodified lines}}stream_mapping = build_stream_mapping(mapping)logging.info(stream_mapping)# Output: {'my_old_stream1': (['my_new_stream', 'my_new_stream2'], [0.6, 0.4]),# 'my_old_stream2': (['my_new_stream', 'my_new_stream2'], [0.2, 0.8])}"""44 unmodified lines):for old_stream_name in new_stream_sources:optinal_streams, weights = stream_routing[old_stream_name]with nested_seed(old_stream_name) as rand:assert (old_stream_name in input_streams), f"'{old_stream_name}' split not found. Possibles options: {input_streams.keys()}"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):32 unmodified lines}new_streams = create_streams(input_streams, mapping)for new_stream_name, new_stream in new_streams.items():logging.info(f"{new_stream_name}:")for _, item in zip(range(10), new_stream):logging.info(item)"""new_streams = {}# 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_streamsif __name__ == "__main__":logging.info(parse_random_mix_string("dale[90%]+oren[0.7]+mike"))1234567891011121314166 unmodified lines18118218318418518618744 unmodified lines23223323423523623723823924024124224324424532 unmodified lines278279280281282283284285286287288289290291292293294295296297298299300301302303304305306import itertoolsimport refrom typing import Dictfrom .generator_utils import ReusableGeneratorfrom .logging_utils import get_loggerfrom .random_utils import new_random_generatorfrom .stream import Streamlogger = get_logger()def parse_random_mix_string(input_str):"""Parses a string of format "source1[percentage1%]+source2[value2]+..." and returns a dictionary.166 unmodified lines}}stream_mapping = build_stream_mapping(mapping)logger.info(stream_mapping)# Output: {'my_old_stream1': (['my_new_stream', 'my_new_stream2'], [0.6, 0.4]),# 'my_old_stream2': (['my_new_stream', 'my_new_stream2'], [0.2, 0.8])}"""44 unmodified lines):for old_stream_name in new_stream_sources:optinal_streams, weights = stream_routing[old_stream_name]random_generator = new_random_generator(sub_seed=old_stream_name)assert (old_stream_name in input_streams), f"'{old_stream_name}' split not found. Possibles options: {input_streams.keys()}"for item in input_streams[old_stream_name]:choice = random_generator.choices(optinal_streams, weights=weights, k=1)[0]if choice == new_stream_name:yield itemdef random_mix_streams(input_streams, mapping):32 unmodified lines}new_streams = create_streams(input_streams, mapping)for new_stream_name, new_stream in new_streams.items():logger.info(f"{new_stream_name}:")for _, item in zip(range(10), new_stream):logger.info(item)"""new_streams = {}# Build stream routingstream_routing = build_stream_routing(mapping)# 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_streamsif __name__ == "__main__":logger.info(parse_random_mix_string("dale[90%]+oren[0.7]+mike"))
split_utils.py+27-26