Dataset Viewer
Auto-converted to Parquet
input
stringlengths
33
5k
output
stringlengths
32
5k
import argparse import os from typing import List from jina.parsers.helper import CastHostAction def api_to_dict(show_all_args: bool = False): """Convert Jina API to a dict :param show_all_args: if set, then hidden args are also exported :return: dict """ if show_all_args: from jina.parsers import helper helper._SHOW_ALL_ARGS, old_val = True, helper._SHOW_ALL_ARGS from jina import __version__ from jina.parsers import get_main_parser all_d = { 'name': 'Jina', 'description': 'Build multimodal AI services via cloud native technologies', 'license': 'Apache 2.0', 'vendor': 'Jina AI Limited', 'source': 'https://github.com/jina-ai/jina/tree/' + os.environ.get('JINA_VCS_VERSION', 'master'), 'url': 'https://jina.ai', 'docs': 'https://jina.ai/serve', 'authors': 'dev-team@jina.ai', 'version': __version__, 'methods': [], 'revision': os.environ.get('JINA_VCS_VERSION'), } def get_p(p, parent_d): parsers = p()._actions[-1].choices if parsers: for p_name in parsers.keys(): d = {'name': p_name, 'options': [], 'help': parsers[p_name].description} for ddd in _export_parser_args( lambda *x: p()._actions[-1].choices[p_name], type_as_str=True ): d['options'].append(ddd) if not d['options']: d['methods'] = [] get_p(lambda *x: parsers[p_name], d) parent_d['methods'].append(d) get_p(get_main_parser, all_d) if show_all_args: helper._SHOW_ALL_ARGS = old_val return all_d def _export_parser_args(parser_fn, type_as_str: bool = False, **kwargs): from argparse import _StoreAction, _StoreTrueAction from jina.enums import BetterEnum from jina.parsers.helper import _SHOW_ALL_ARGS, CastToIntAction, KVAppendAction port_attr = ('help', 'choices', 'default', 'required', 'option_strings', 'dest') parser = parser_fn(**kwargs) parser2 = parser_fn(**kwargs) random_dest = set() for a, b in zip(parser._actions, parser2._actions): if a.default != b.default: random_dest.add(a.dest) for a in parser._actions: if isinstance( a, ( _StoreAction, _StoreTrueAction, KVAppendAction, CastToIntAction, CastHostAction, ), ): if not _SHOW_ALL_ARGS and a.help == argparse.SUPPRESS: continue ddd = {p: getattr(a, p) for p in port_attr} if isinstance(a, _StoreTrueAction): ddd['type'] = bool elif isinstance(a, KVAppendAction): ddd['type'] = dict elif isinstance(a, CastToIntAction): ddd['type'] = int elif isinstance(a, CastHostAction): ddd['type'] = str else: ddd['type'] = a.type if ddd['choices']: ddd['choices'] = [ str(k) if isinstance(k, BetterEnum) else k for k in ddd['choices'] ] ddd['type'] = str if isinstance(ddd['default'], BetterEnum): ddd['default'] = str(ddd['default']) ddd['type'] = str if ddd['type'] == str and (a.nargs == '*' or a.nargs == '+'): ddd['type'] = List[str] else: continue if a.dest in random_dest: ddd['default_random'] = True from jina.helper import random_identity, random_port if isinstance(a.default, str): ddd['default_factory'] = random_identity.__name__ elif isinstance(a.default, int): ddd['default_factory'] = random_port.__name__ else: ddd['default_random'] = False if type_as_str: ddd['type'] = getattr(ddd['type'], '__name__', str(ddd['type'])) ddd['name'] = ddd.pop('dest') yield ddd
import argparse import os from typing import List from jina.parsers.helper import CastHostAction def api_to_dict(show_all_args: bool = False): """Convert Jina API to a dict :param show_all_args: if set, then hidden args are also exported :return: dict """ if show_all_args: from jina.parsers import helper helper._SHOW_ALL_ARGS, old_val = True, helper._SHOW_ALL_ARGS from jina import __version__ from jina.parsers import get_main_parser all_d = { 'name': 'Jina', 'description': 'Build multimodal AI services via cloud native technologies', 'license': 'Apache 2.0', 'vendor': 'Jina AI Limited', 'source': 'https://github.com/jina-ai/jina/tree/' + os.environ.get('JINA_VCS_VERSION', 'master'), 'url': 'https://jina.ai', 'docs': 'https://docs.jina.ai', 'authors': 'dev-team@jina.ai', 'version': __version__, 'methods': [], 'revision': os.environ.get('JINA_VCS_VERSION'), } def get_p(p, parent_d): parsers = p()._actions[-1].choices if parsers: for p_name in parsers.keys(): d = {'name': p_name, 'options': [], 'help': parsers[p_name].description} for ddd in _export_parser_args( lambda *x: p()._actions[-1].choices[p_name], type_as_str=True ): d['options'].append(ddd) if not d['options']: d['methods'] = [] get_p(lambda *x: parsers[p_name], d) parent_d['methods'].append(d) get_p(get_main_parser, all_d) if show_all_args: helper._SHOW_ALL_ARGS = old_val return all_d def _export_parser_args(parser_fn, type_as_str: bool = False, **kwargs): from argparse import _StoreAction, _StoreTrueAction from jina.enums import BetterEnum from jina.parsers.helper import _SHOW_ALL_ARGS, CastToIntAction, KVAppendAction port_attr = ('help', 'choices', 'default', 'required', 'option_strings', 'dest') parser = parser_fn(**kwargs) parser2 = parser_fn(**kwargs) random_dest = set() for a, b in zip(parser._actions, parser2._actions): if a.default != b.default: random_dest.add(a.dest) for a in parser._actions: if isinstance( a, ( _StoreAction, _StoreTrueAction, KVAppendAction, CastToIntAction, CastHostAction, ), ): if not _SHOW_ALL_ARGS and a.help == argparse.SUPPRESS: continue ddd = {p: getattr(a, p) for p in port_attr} if isinstance(a, _StoreTrueAction): ddd['type'] = bool elif isinstance(a, KVAppendAction): ddd['type'] = dict elif isinstance(a, CastToIntAction): ddd['type'] = int elif isinstance(a, CastHostAction): ddd['type'] = str else: ddd['type'] = a.type if ddd['choices']: ddd['choices'] = [ str(k) if isinstance(k, BetterEnum) else k for k in ddd['choices'] ] ddd['type'] = str if isinstance(ddd['default'], BetterEnum): ddd['default'] = str(ddd['default']) ddd['type'] = str if ddd['type'] == str and (a.nargs == '*' or a.nargs == '+'): ddd['type'] = List[str] else: continue if a.dest in random_dest: ddd['default_random'] = True from jina.helper import random_identity, random_port if isinstance(a.default, str): ddd['default_factory'] = random_identity.__name__ elif isinstance(a.default, int): ddd['default_factory'] = random_port.__name__ else: ddd['default_random'] = False if type_as_str: ddd['type'] = getattr(ddd['type'], '__name__', str(ddd['type'])) ddd['name'] = ddd.pop('dest') yield ddd
import inspect import re from typing import Dict, List, Tuple from huggingface_hub.utils import insecure_hashlib from .arrow import arrow from .audiofolder import audiofolder from .cache import cache from .csv import csv from .imagefolder import imagefolder from .json import json from .pandas import pandas from .parquet import parquet from .sql import sql from .text import text from .videofolder import videofolder from .webdataset import webdataset from .xml import xml def _hash_python_lines(lines: list[str]) -> str: filtered_lines = [] for line in lines: line = re.sub(r"#.*", "", line) # remove comments if line: filtered_lines.append(line) full_str = "\n".join(filtered_lines) # Make a hash from all this code full_bytes = full_str.encode("utf-8") return insecure_hashlib.sha256(full_bytes).hexdigest() # get importable module names and hash for caching _PACKAGED_DATASETS_MODULES = { "csv": (csv.__name__, _hash_python_lines(inspect.getsource(csv).splitlines())), "json": (json.__name__, _hash_python_lines(inspect.getsource(json).splitlines())), "pandas": (pandas.__name__, _hash_python_lines(inspect.getsource(pandas).splitlines())), "parquet": (parquet.__name__, _hash_python_lines(inspect.getsource(parquet).splitlines())), "arrow": (arrow.__name__, _hash_python_lines(inspect.getsource(arrow).splitlines())), "text": (text.__name__, _hash_python_lines(inspect.getsource(text).splitlines())), "imagefolder": (imagefolder.__name__, _hash_python_lines(inspect.getsource(imagefolder).splitlines())), "audiofolder": (audiofolder.__name__, _hash_python_lines(inspect.getsource(audiofolder).splitlines())), "videofolder": (videofolder.__name__, _hash_python_lines(inspect.getsource(videofolder).splitlines())), "webdataset": (webdataset.__name__, _hash_python_lines(inspect.getsource(webdataset).splitlines())), "xml": (xml.__name__, _hash_python_lines(inspect.getsource(xml).splitlines())), } # get importable module names and hash for caching _PACKAGED_DATASETS_MODULES_2_15_HASHES = { "csv": "eea64c71ca8b46dd3f537ed218fc9bf495d5707789152eb2764f5c78fa66d59d", "json": "8bb11242116d547c741b2e8a1f18598ffdd40a1d4f2a2872c7a28b697434bc96", "pandas": "3ac4ffc4563c796122ef66899b9485a3f1a977553e2d2a8a318c72b8cc6f2202", "parquet": "ca31c69184d9832faed373922c2acccec0b13a0bb5bbbe19371385c3ff26f1d1", "arrow": "74f69db2c14c2860059d39860b1f400a03d11bf7fb5a8258ca38c501c878c137", "text": "c4a140d10f020282918b5dd1b8a49f0104729c6177f60a6b49ec2a365ec69f34", "imagefolder": "7b7ce5247a942be131d49ad4f3de5866083399a0f250901bd8dc202f8c5f7ce5", "audiofolder": "d3c1655c66c8f72e4efb5c79e952975fa6e2ce538473a6890241ddbddee9071c", } # Used to infer the module to use based on the data files extensions _EXTENSION_TO_MODULE: dict[str, tuple[str, dict]] = { ".csv": ("csv", {}), ".tsv": ("csv", {"sep": "\t"}), ".json": ("json", {}), ".jsonl": ("json", {}), # ndjson is no longer maintained (see: https://github.com/ndjson/ndjson-spec/issues/35#issuecomment-1285673417) ".ndjson": ("json", {}), ".parquet": ("parquet", {}), ".geoparquet": ("parquet", {}), ".gpq": ("parquet", {}), ".arrow": ("arrow", {}), ".txt": ("text", {}), ".tar": ("webdataset", {}), ".xml": ("xml", {}), } _EXTENSION_TO_MODULE.update({ext: ("imagefolder", {}) for ext in imagefolder.ImageFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext.upper(): ("imagefolder", {}) for ext in imagefolder.ImageFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext: ("audiofolder", {}) for ext in audiofolder.AudioFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext.upper(): ("audiofolder", {}) for ext in audiofolder.AudioFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext: ("videofolder", {}) for ext in videofolder.VideoFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext.upper(): ("videofolder", {}) for ext in videofolder.VideoFolder.EXTENSIONS}) # Used to filter data files based on extensions given a module name _MODULE_TO_EXTENSIONS: dict[str, list[str]] = {} for _ext, (_module, _) in _EXTENSION_TO_MODULE.items(): _MODULE_TO_EXTENSIONS.setdefault(_module, []).append(_ext) for _module in _MODULE_TO_EXTENSIONS: _MODULE_TO_EXTENSIONS[_module].append(".zip") # Used to filter data files based on file names _MODULE_TO_METADATA_FILE_NAMES: Dict[str, List[str]] = {} for _module in _MODULE_TO_EXTENSIONS: _MODULE_TO_METADATA_FILE_NAMES[_module] = [] _MODULE_TO_METADATA_FILE_NAMES["imagefolder"] = imagefolder.ImageFolder.METADATA_FILENAMES _MODULE_TO_METADATA_FILE_NAMES["audiofolder"] = imagefolder.ImageFolder.METADATA_FILENAMES _MODULE_TO_METADATA_FILE_NAMES["videofolder"] = imagefolder.ImageFolder.METADATA_FILENAMES
import inspect import re from typing import Dict, List, Tuple from huggingface_hub.utils import insecure_hashlib from .arrow import arrow from .audiofolder import audiofolder from .cache import cache from .csv import csv from .imagefolder import imagefolder from .json import json from .pandas import pandas from .parquet import parquet from .sql import sql from .text import text from .videofolder import videofolder from .webdataset import webdataset from .xml import xml def _hash_python_lines(lines: list[str]) -> str: filtered_lines = [] for line in lines: line = re.sub(r"#.*", "", line) # remove comments if line: filtered_lines.append(line) full_str = "\n".join(filtered_lines) # Make a hash from all this code full_bytes = full_str.encode("utf-8") return insecure_hashlib.sha256(full_bytes).hexdigest() # get importable module names and hash for caching _PACKAGED_DATASETS_MODULES = { "csv": (csv.__name__, _hash_python_lines(inspect.getsource(csv).splitlines())), "json": (json.__name__, _hash_python_lines(inspect.getsource(json).splitlines())), "pandas": (pandas.__name__, _hash_python_lines(inspect.getsource(pandas).splitlines())), "parquet": (parquet.__name__, _hash_python_lines(inspect.getsource(parquet).splitlines())), "arrow": (arrow.__name__, _hash_python_lines(inspect.getsource(arrow).splitlines())), "text": (text.__name__, _hash_python_lines(inspect.getsource(text).splitlines())), "imagefolder": (imagefolder.__name__, _hash_python_lines(inspect.getsource(imagefolder).splitlines())), "audiofolder": (audiofolder.__name__, _hash_python_lines(inspect.getsource(audiofolder).splitlines())), "videofolder": (videofolder.__name__, _hash_python_lines(inspect.getsource(videofolder).splitlines())), "webdataset": (webdataset.__name__, _hash_python_lines(inspect.getsource(webdataset).splitlines())), "xml": (xml.__name__, _hash_python_lines(inspect.getsource(xml).splitlines())), } # get importable module names and hash for caching _PACKAGED_DATASETS_MODULES_2_15_HASHES = { "csv": "eea64c71ca8b46dd3f537ed218fc9bf495d5707789152eb2764f5c78fa66d59d", "json": "8bb11242116d547c741b2e8a1f18598ffdd40a1d4f2a2872c7a28b697434bc96", "pandas": "3ac4ffc4563c796122ef66899b9485a3f1a977553e2d2a8a318c72b8cc6f2202", "parquet": "ca31c69184d9832faed373922c2acccec0b13a0bb5bbbe19371385c3ff26f1d1", "arrow": "74f69db2c14c2860059d39860b1f400a03d11bf7fb5a8258ca38c501c878c137", "text": "c4a140d10f020282918b5dd1b8a49f0104729c6177f60a6b49ec2a365ec69f34", "imagefolder": "7b7ce5247a942be131d49ad4f3de5866083399a0f250901bd8dc202f8c5f7ce5", "audiofolder": "d3c1655c66c8f72e4efb5c79e952975fa6e2ce538473a6890241ddbddee9071c", } # Used to infer the module to use based on the data files extensions _EXTENSION_TO_MODULE: dict[str, tuple[str, dict]] = { ".csv": ("csv", {}), ".tsv": ("csv", {"sep": "\t"}), ".json": ("json", {}), ".jsonl": ("json", {}), # ndjson is no longer maintained (see: https://github.com/ndjson/ndjson-spec/issues/35#issuecomment-1285673417) ".ndjson": ("json", {}), ".parquet": ("parquet", {}), ".geoparquet": ("parquet", {}), ".gpq": ("parquet", {}), ".arrow": ("arrow", {}), ".txt": ("text", {}), ".tar": ("webdataset", {}), ".xml": ("xml", {}), } _EXTENSION_TO_MODULE.update({ext: ("imagefolder", {}) for ext in imagefolder.ImageFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext.upper(): ("imagefolder", {}) for ext in imagefolder.ImageFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext: ("audiofolder", {}) for ext in audiofolder.AudioFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext.upper(): ("audiofolder", {}) for ext in audiofolder.AudioFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext: ("videofolder", {}) for ext in videofolder.VideoFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext.upper(): ("videofolder", {}) for ext in videofolder.VideoFolder.EXTENSIONS}) _MODULE_SUPPORTS_METADATA = {"imagefolder", "audiofolder", "videofolder"} # Used to filter data files based on extensions given a module name _MODULE_TO_EXTENSIONS: dict[str, list[str]] = {} for _ext, (_module, _) in _EXTENSION_TO_MODULE.items(): _MODULE_TO_EXTENSIONS.setdefault(_module, []).append(_ext) for _module in _MODULE_TO_EXTENSIONS: _MODULE_TO_EXTENSIONS[_module].append(".zip")
_base_ = './mask-rcnn_regnetx-3.2GF_fpn_1x_coco.py' model = dict( backbone=dict( type='RegNet', arch='regnetx_6.4gf', out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch', init_cfg=dict( type='Pretrained', checkpoint='open-mmlab://regnetx_6.4gf')), neck=dict( type='FPN', in_channels=[168, 392, 784, 1624], out_channels=256, num_outs=5))
_base_ = './mask_rcnn_regnetx-3.2GF_fpn_1x_coco.py' model = dict( backbone=dict( type='RegNet', arch='regnetx_6.4gf', out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch', init_cfg=dict( type='Pretrained', checkpoint='open-mmlab://regnetx_6.4gf')), neck=dict( type='FPN', in_channels=[168, 392, 784, 1624], out_channels=256, num_outs=5))
import argparse import copy import os import re import sys import boto3 import botocore from metadata import AMI_ID, COMMON_STACK_PARAMS, STACK_PARAMS current_dir = os.path.dirname(__file__) sys.path.append(os.path.join(current_dir, "..")) from common_blocks.utils import create_or_update_stack, wait TEMPLATE_URL = "https://s3.amazonaws.com/buildkite-aws-stack/latest/aws-stack.yml" def get_availability_zones(*, aws_region): client = boto3.client("ec2", region_name=aws_region) r = client.describe_availability_zones( Filters=[ {"Name": "region-name", "Values": [aws_region]}, {"Name": "zone-type", "Values": ["availability-zone"]}, ] ) return sorted([x["ZoneName"] for x in r["AvailabilityZones"]]) def get_default_vpc(*, aws_region): ec2 = boto3.resource("ec2", region_name=aws_region) default_vpc_id = None for x in ec2.vpcs.filter(Filters=[{"Name": "is-default", "Values": ["true"]}]): return x # Create default VPC if not exist client = boto3.client("ec2", region_name=aws_region) r = client.create_default_vpc() default_vpc_id = r["Vpc"]["VpcId"] return ec2.Vpc(default_vpc_id) def format_params(args, *, stack_id, agent_iam_policy): default_vpc = get_default_vpc(aws_region=args.aws_region) azs = get_availability_zones(aws_region=args.aws_region) # For each of the first two availability zones (AZs), choose the default subnet subnets = [ x.id for x in default_vpc.subnets.filter( Filters=[ {"Name": "default-for-az", "Values": ["true"]}, {"Name": "availability-zone", "Values": azs[:2]}, ] ) ] assert len(subnets) == 2 params = copy.deepcopy(STACK_PARAMS[stack_id]) params["ImageId"] = AMI_ID[stack_id][args.aws_region] params["BuildkiteQueue"] = stack_id params["CostAllocationTagValue"] = f"buildkite-{stack_id}" params["BuildkiteAgentToken"] = args.agent_token params["VpcId"] = default_vpc.id params["Subnets"] = ",".join(subnets) params["ManagedPolicyARNs"] = agent_iam_policy params.update(COMMON_STACK_PARAMS) return [{"ParameterKey": k, "ParameterValue": v} for k, v in params.items()] def get_full_stack_id(stack_id): return f"buildkite-{stack_id}-autoscaling-group" def create_agent_iam_policy(args, *, client): policy_stack_name = "buildkite-agent-iam-policy" print(f"Creating stack {policy_stack_name} for agent IAM policy...") with open( os.path.join(current_dir, "agent-iam-policy-template.yml"), encoding="utf-8", ) as f: policy_template = f.read() promise = create_or_update_stack( args, client=client, stack_name=policy_stack_name, template_body=policy_template ) wait(promise, client=client) cf = boto3.resource("cloudformation", region_name=args.aws_region) policy = cf.StackResource(policy_stack_name, "BuildkiteAgentManagedPolicy") return policy.physical_resource_id def main(args): client = boto3.client("cloudformation", region_name=args.aws_region) agent_iam_policy = create_agent_iam_policy(args, client=client) promises = [] for stack_id in AMI_ID: stack_id_full = get_full_stack_id(stack_id) print(f"Creating elastic CI stack {stack_id_full}...") params = format_params( args, stack_id=stack_id, agent_iam_policy=agent_iam_policy ) promise = create_or_update_stack( args, client=client, stack_name=stack_id_full, template_url=TEMPLATE_URL, params=params, ) promises.append(promise) print(f"CI stack {stack_id_full} is in progress in the background") for promise in promises: wait(promise, client=client) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--aws-region", type=str, required=True) parser.add_argument("--agent-token", type=str, required=True) args = parser.parse_args() main(args)
import argparse import copy import os import re import sys import boto3 import botocore from metadata import AMI_ID, COMMON_STACK_PARAMS, STACK_PARAMS current_dir = os.path.dirname(__file__) sys.path.append(os.path.join(current_dir, "..")) from common_blocks.utils import create_or_update_stack, wait TEMPLATE_URL = "https://s3.amazonaws.com/buildkite-aws-stack/latest/aws-stack.yml" def get_availability_zones(*, aws_region): client = boto3.client("ec2", region_name=aws_region) r = client.describe_availability_zones( Filters=[ {"Name": "region-name", "Values": [aws_region]}, {"Name": "zone-type", "Values": ["availability-zone"]}, ] ) return sorted([x["ZoneName"] for x in r["AvailabilityZones"]]) def get_default_vpc(*, aws_region): ec2 = boto3.resource("ec2", region_name=aws_region) default_vpc_id = None for x in ec2.vpcs.filter(Filters=[{"Name": "is-default", "Values": ["true"]}]): return x # Create default VPC if not exist client = boto3.client("ec2", region_name=aws_region) r = client.create_default_vpc() default_vpc_id = r["Vpc"]["VpcId"] return ec2.Vpc(default_vpc_id) def format_params(args, *, stack_id, agent_iam_policy): default_vpc = get_default_vpc(aws_region=args.aws_region) azs = get_availability_zones(aws_region=args.aws_region) # For each of the first two availability zones (AZs), choose the default subnet subnets = [ x.id for x in default_vpc.subnets.filter( Filters=[ {"Name": "default-for-az", "Values": ["true"]}, {"Name": "availability-zone", "Values": azs[:2]}, ] ) ] assert len(subnets) == 2 params = copy.deepcopy(STACK_PARAMS[stack_id]) params["ImageId"] = AMI_ID[stack_id][args.aws_region] params["BuildkiteQueue"] = stack_id params["CostAllocationTagValue"] = f"buildkite-{stack_id}" params["BuildkiteAgentToken"] = args.agent_token params["VpcId"] = default_vpc.id params["Subnets"] = ",".join(subnets) params["ManagedPolicyARN"] = agent_iam_policy params.update(COMMON_STACK_PARAMS) return [{"ParameterKey": k, "ParameterValue": v} for k, v in params.items()] def get_full_stack_id(stack_id): return f"buildkite-{stack_id}-autoscaling-group" def create_agent_iam_policy(args, *, client): policy_stack_name = "buildkite-agent-iam-policy" print(f"Creating stack {policy_stack_name} for agent IAM policy...") with open( os.path.join(current_dir, "agent-iam-policy-template.yml"), encoding="utf-8", ) as f: policy_template = f.read() promise = create_or_update_stack( args, client=client, stack_name=policy_stack_name, template_body=policy_template ) wait(promise, client=client) cf = boto3.resource("cloudformation", region_name=args.aws_region) policy = cf.StackResource(policy_stack_name, "BuildkiteAgentManagedPolicy") return policy.physical_resource_id def main(args): client = boto3.client("cloudformation", region_name=args.aws_region) agent_iam_policy = create_agent_iam_policy(args, client=client) promises = [] for stack_id in AMI_ID: stack_id_full = get_full_stack_id(stack_id) print(f"Creating elastic CI stack {stack_id_full}...") params = format_params( args, stack_id=stack_id, agent_iam_policy=agent_iam_policy ) promise = create_or_update_stack( args, client=client, stack_name=stack_id_full, template_url=TEMPLATE_URL, params=params, ) promises.append(promise) print(f"CI stack {stack_id_full} is in progress in the background") for promise in promises: wait(promise, client=client) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--aws-region", type=str, required=True) parser.add_argument("--agent-token", type=str, required=True) args = parser.parse_args() main(args)
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.backend.config import backend as backend from keras.src.backend.config import ( disable_flash_attention as disable_flash_attention, ) from keras.src.backend.config import ( enable_flash_attention as enable_flash_attention, ) from keras.src.backend.config import epsilon as epsilon from keras.src.backend.config import floatx as floatx from keras.src.backend.config import image_data_format as image_data_format from keras.src.backend.config import ( is_flash_attention_enabled as is_flash_attention_enabled, ) from keras.src.backend.config import set_epsilon as set_epsilon from keras.src.backend.config import set_floatx as set_floatx from keras.src.backend.config import ( set_image_data_format as set_image_data_format, ) from keras.src.dtype_policies.dtype_policy import dtype_policy as dtype_policy from keras.src.dtype_policies.dtype_policy import ( set_dtype_policy as set_dtype_policy, ) from keras.src.saving.serialization_lib import ( enable_unsafe_deserialization as enable_unsafe_deserialization, ) from keras.src.utils.backend_utils import set_backend as set_backend from keras.src.utils.io_utils import ( disable_interactive_logging as disable_interactive_logging, ) from keras.src.utils.io_utils import ( enable_interactive_logging as enable_interactive_logging, ) from keras.src.utils.io_utils import ( is_interactive_logging_enabled as is_interactive_logging_enabled, ) from keras.src.utils.traceback_utils import ( disable_traceback_filtering as disable_traceback_filtering, ) from keras.src.utils.traceback_utils import ( enable_traceback_filtering as enable_traceback_filtering, ) from keras.src.utils.traceback_utils import ( is_traceback_filtering_enabled as is_traceback_filtering_enabled, )
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.backend.config import backend from keras.src.backend.config import disable_flash_attention from keras.src.backend.config import enable_flash_attention from keras.src.backend.config import epsilon from keras.src.backend.config import floatx from keras.src.backend.config import image_data_format from keras.src.backend.config import is_flash_attention_enabled from keras.src.backend.config import set_epsilon from keras.src.backend.config import set_floatx from keras.src.backend.config import set_image_data_format from keras.src.dtype_policies.dtype_policy import dtype_policy from keras.src.dtype_policies.dtype_policy import set_dtype_policy from keras.src.saving.serialization_lib import enable_unsafe_deserialization from keras.src.utils.backend_utils import set_backend from keras.src.utils.io_utils import disable_interactive_logging from keras.src.utils.io_utils import enable_interactive_logging from keras.src.utils.io_utils import is_interactive_logging_enabled from keras.src.utils.traceback_utils import disable_traceback_filtering from keras.src.utils.traceback_utils import enable_traceback_filtering from keras.src.utils.traceback_utils import is_traceback_filtering_enabled
import logging from fastapi import Request from backend.data import integrations from backend.data.model import APIKeyCredentials, Credentials from backend.integrations.providers import ProviderName from backend.integrations.webhooks._base import BaseWebhooksManager from backend.util.request import Requests logger = logging.getLogger(__name__) class Slant3DWebhooksManager(BaseWebhooksManager): """Manager for Slant3D webhooks""" PROVIDER_NAME = ProviderName.SLANT3D BASE_URL = "https://www.slant3dapi.com/api" async def _register_webhook( self, credentials: Credentials, webhook_type: str, resource: str, events: list[str], ingress_url: str, secret: str, ) -> tuple[str, dict]: """Register a new webhook with Slant3D""" if not isinstance(credentials, APIKeyCredentials): raise ValueError("API key is required to register a webhook") headers = { "api-key": credentials.api_key.get_secret_value(), "Content-Type": "application/json", } # Slant3D's API doesn't use events list, just register for all order updates payload = {"endPoint": ingress_url} response = Requests().post( f"{self.BASE_URL}/customer/webhookSubscribe", headers=headers, json=payload ) if not response.ok: error = response.json().get("error", "Unknown error") raise RuntimeError(f"Failed to register webhook: {error}") webhook_config = { "endpoint": ingress_url, "provider": self.PROVIDER_NAME, "events": ["order.shipped"], # Currently the only supported event "type": webhook_type, } return "", webhook_config @classmethod async def validate_payload( cls, webhook: integrations.Webhook, request: Request ) -> tuple[dict, str]: """Validate incoming webhook payload from Slant3D""" payload = await request.json() # Validate required fields from Slant3D API spec required_fields = ["orderId", "status", "trackingNumber", "carrierCode"] missing_fields = [field for field in required_fields if field not in payload] if missing_fields: raise ValueError(f"Missing required fields: {', '.join(missing_fields)}") # Normalize payload structure normalized_payload = { "orderId": payload["orderId"], "status": payload["status"], "trackingNumber": payload["trackingNumber"], "carrierCode": payload["carrierCode"], } # Currently Slant3D only sends shipping notifications # Convert status to lowercase for event format compatibility event_type = f"order.{payload['status'].lower()}" return normalized_payload, event_type async def _deregister_webhook( self, webhook: integrations.Webhook, credentials: Credentials ) -> None: """ Note: Slant3D API currently doesn't provide a deregistration endpoint. This would need to be handled through support. """ # Log warning since we can't properly deregister logger.warning( f"Warning: Manual deregistration required for webhook {webhook.id}" ) pass
import logging import requests from fastapi import Request from backend.data import integrations from backend.data.model import APIKeyCredentials, Credentials from backend.integrations.providers import ProviderName from backend.integrations.webhooks._base import BaseWebhooksManager logger = logging.getLogger(__name__) class Slant3DWebhooksManager(BaseWebhooksManager): """Manager for Slant3D webhooks""" PROVIDER_NAME = ProviderName.SLANT3D BASE_URL = "https://www.slant3dapi.com/api" async def _register_webhook( self, credentials: Credentials, webhook_type: str, resource: str, events: list[str], ingress_url: str, secret: str, ) -> tuple[str, dict]: """Register a new webhook with Slant3D""" if not isinstance(credentials, APIKeyCredentials): raise ValueError("API key is required to register a webhook") headers = { "api-key": credentials.api_key.get_secret_value(), "Content-Type": "application/json", } # Slant3D's API doesn't use events list, just register for all order updates payload = {"endPoint": ingress_url} response = requests.post( f"{self.BASE_URL}/customer/webhookSubscribe", headers=headers, json=payload ) if not response.ok: error = response.json().get("error", "Unknown error") raise RuntimeError(f"Failed to register webhook: {error}") webhook_config = { "endpoint": ingress_url, "provider": self.PROVIDER_NAME, "events": ["order.shipped"], # Currently the only supported event "type": webhook_type, } return "", webhook_config @classmethod async def validate_payload( cls, webhook: integrations.Webhook, request: Request ) -> tuple[dict, str]: """Validate incoming webhook payload from Slant3D""" payload = await request.json() # Validate required fields from Slant3D API spec required_fields = ["orderId", "status", "trackingNumber", "carrierCode"] missing_fields = [field for field in required_fields if field not in payload] if missing_fields: raise ValueError(f"Missing required fields: {', '.join(missing_fields)}") # Normalize payload structure normalized_payload = { "orderId": payload["orderId"], "status": payload["status"], "trackingNumber": payload["trackingNumber"], "carrierCode": payload["carrierCode"], } # Currently Slant3D only sends shipping notifications # Convert status to lowercase for event format compatibility event_type = f"order.{payload['status'].lower()}" return normalized_payload, event_type async def _deregister_webhook( self, webhook: integrations.Webhook, credentials: Credentials ) -> None: """ Note: Slant3D API currently doesn't provide a deregistration endpoint. This would need to be handled through support. """ # Log warning since we can't properly deregister logger.warning( f"Warning: Manual deregistration required for webhook {webhook.id}" ) pass
""" Feature agglomeration. Base classes and functions for performing feature agglomeration. """ # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause import numpy as np from scipy.sparse import issparse from ..base import TransformerMixin from ..utils.validation import check_is_fitted, validate_data ############################################################################### # Mixin class for feature agglomeration. class AgglomerationTransform(TransformerMixin): """ A class for feature agglomeration via the transform interface. """ def transform(self, X): """ Transform a new matrix using the built clustering. Parameters ---------- X : array-like of shape (n_samples, n_features) or \ (n_samples, n_samples) A M by N array of M observations in N dimensions or a length M array of M one-dimensional observations. Returns ------- Y : ndarray of shape (n_samples, n_clusters) or (n_clusters,) The pooled values for each feature cluster. """ check_is_fitted(self) X = validate_data(self, X, reset=False) if self.pooling_func == np.mean and not issparse(X): size = np.bincount(self.labels_) n_samples = X.shape[0] # a fast way to compute the mean of grouped features nX = np.array( [np.bincount(self.labels_, X[i, :]) / size for i in range(n_samples)] ) else: nX = [ self.pooling_func(X[:, self.labels_ == l], axis=1) for l in np.unique(self.labels_) ] nX = np.array(nX).T return nX def inverse_transform(self, X): """ Inverse the transformation and return a vector of size `n_features`. Parameters ---------- X : array-like of shape (n_samples, n_clusters) or (n_clusters,) The values to be assigned to each cluster of samples. Returns ------- X : ndarray of shape (n_samples, n_features) or (n_features,) A vector of size `n_samples` with the values of `Xred` assigned to each of the cluster of samples. """ check_is_fitted(self) unil, inverse = np.unique(self.labels_, return_inverse=True) return X[..., inverse]
""" Feature agglomeration. Base classes and functions for performing feature agglomeration. """ # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause import numpy as np from scipy.sparse import issparse from ..base import TransformerMixin from ..utils import metadata_routing from ..utils.deprecation import _deprecate_Xt_in_inverse_transform from ..utils.validation import check_is_fitted, validate_data ############################################################################### # Mixin class for feature agglomeration. class AgglomerationTransform(TransformerMixin): """ A class for feature agglomeration via the transform interface. """ # This prevents ``set_split_inverse_transform`` to be generated for the # non-standard ``Xt`` arg on ``inverse_transform``. # TODO(1.7): remove when Xt is removed for inverse_transform. __metadata_request__inverse_transform = {"Xt": metadata_routing.UNUSED} def transform(self, X): """ Transform a new matrix using the built clustering. Parameters ---------- X : array-like of shape (n_samples, n_features) or \ (n_samples, n_samples) A M by N array of M observations in N dimensions or a length M array of M one-dimensional observations. Returns ------- Y : ndarray of shape (n_samples, n_clusters) or (n_clusters,) The pooled values for each feature cluster. """ check_is_fitted(self) X = validate_data(self, X, reset=False) if self.pooling_func == np.mean and not issparse(X): size = np.bincount(self.labels_) n_samples = X.shape[0] # a fast way to compute the mean of grouped features nX = np.array( [np.bincount(self.labels_, X[i, :]) / size for i in range(n_samples)] ) else: nX = [ self.pooling_func(X[:, self.labels_ == l], axis=1) for l in np.unique(self.labels_) ] nX = np.array(nX).T return nX def inverse_transform(self, X=None, *, Xt=None): """ Inverse the transformation and return a vector of size `n_features`. Parameters ---------- X : array-like of shape (n_samples, n_clusters) or (n_clusters,) The values to be assigned to each cluster of samples. Xt : array-like of shape (n_samples, n_clusters) or (n_clusters,) The values to be assigned to each cluster of samples. .. deprecated:: 1.5 `Xt` was deprecated in 1.5 and will be removed in 1.7. Use `X` instead. Returns ------- X : ndarray of shape (n_samples, n_features) or (n_features,) A vector of size `n_samples` with the values of `Xred` assigned to each of the cluster of samples. """ X = _deprecate_Xt_in_inverse_transform(X, Xt) check_is_fitted(self) unil, inverse = np.unique(self.labels_, return_inverse=True) return X[..., inverse]
# Copyright 2024 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ This script is used to get the list of folders under `tests/models` and split the list into `NUM_SLICES` splits. The main use case is a GitHub Actions workflow file calling this script to get the (nested) list of folders allowing it to split the list of jobs to run into multiple slices each containing a smaller number of jobs. This way, we can bypass the maximum of 256 jobs in a matrix. See the `setup` and `run_models_gpu` jobs defined in the workflow file `.github/workflows/self-scheduled.yml` for more details. Usage: This script is required to be run under `tests` folder of `transformers` root directory. Assume we are under `transformers` root directory: ```bash cd tests python ../utils/split_model_tests.py --num_splits 64 ``` """ import argparse import os if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "--num_splits", type=int, default=1, help="the number of splits into which the (flat) list of folders will be split.", ) args = parser.parse_args() tests = os.getcwd() model_tests = os.listdir(os.path.join(tests, "models")) d1 = sorted(filter(os.path.isdir, os.listdir(tests))) d2 = sorted(filter(os.path.isdir, [f"models/{x}" for x in model_tests])) d1.remove("models") d = d2 + d1 num_jobs = len(d) num_jobs_per_splits = num_jobs // args.num_splits model_splits = [] end = 0 for idx in range(args.num_splits): start = end end = start + num_jobs_per_splits + (1 if idx < num_jobs % args.num_splits else 0) model_splits.append(d[start:end]) print(model_splits)
# Copyright 2024 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ This script is used to get the list of folders under `tests/models` and split the list into `NUM_SLICES` splits. The main use case is a GitHub Actions workflow file calling this script to get the (nested) list of folders allowing it to split the list of jobs to run into multiple slices each containing a smaller number of jobs. This way, we can bypass the maximum of 256 jobs in a matrix. See the `setup` and `run_models_gpu` jobs defined in the workflow file `.github/workflows/self-scheduled.yml` for more details. Usage: This script is required to be run under `tests` folder of `transformers` root directory. Assume we are under `transformers` root directory: ```bash cd tests python ../utils/split_model_tests.py --num_splits 64 ``` """ import argparse import os if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "--num_splits", type=int, default=1, help="the number of splits into which the (flat) list of folders will be split.", ) args = parser.parse_args() tests = os.getcwd() model_tests = os.listdir(os.path.join(tests, "models")) d1 = sorted(filter(os.path.isdir, os.listdir(tests))) d2 = sorted(filter(os.path.isdir, [f"models/{x}" for x in model_tests])) d1.remove("models") d = d2 + d1 num_jobs = len(d) num_jobs_per_splits = num_jobs // args.num_splits model_splits = [] end = 0 for idx in range(args.num_splits): start = end end = start + num_jobs_per_splits + (1 if idx < num_jobs % args.num_splits else 0) model_splits.append(d[start:end]) print(model_splits)
import pytest import datasets import datasets.config # Import fixture modules as plugins pytest_plugins = ["tests.fixtures.files", "tests.fixtures.hub", "tests.fixtures.fsspec"] def pytest_collection_modifyitems(config, items): # Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit") for item in items: if any(marker in item.keywords for marker in ["integration", "unit"]): continue item.add_marker(pytest.mark.unit) @pytest.fixture(autouse=True) def set_test_cache_config(tmp_path_factory, monkeypatch): # test_hf_cache_home = tmp_path_factory.mktemp("cache") # TODO: why a cache dir per test function does not work? test_hf_cache_home = tmp_path_factory.getbasetemp() / "cache" test_hf_datasets_cache = test_hf_cache_home / "datasets" monkeypatch.setattr("datasets.config.HF_DATASETS_CACHE", str(test_hf_datasets_cache)) test_downloaded_datasets_path = test_hf_datasets_cache / "downloads" monkeypatch.setattr("datasets.config.DOWNLOADED_DATASETS_PATH", str(test_downloaded_datasets_path)) test_extracted_datasets_path = test_hf_datasets_cache / "downloads" / "extracted" monkeypatch.setattr("datasets.config.EXTRACTED_DATASETS_PATH", str(test_extracted_datasets_path)) @pytest.fixture(autouse=True) def disable_implicit_token(monkeypatch): monkeypatch.setattr("huggingface_hub.constants.HF_HUB_DISABLE_IMPLICIT_TOKEN", True) @pytest.fixture(autouse=True, scope="session") def disable_tqdm_output(): datasets.disable_progress_bar() @pytest.fixture(autouse=True) def set_update_download_counts_to_false(monkeypatch): # don't take tests into account when counting downloads monkeypatch.setattr("datasets.config.HF_UPDATE_DOWNLOAD_COUNTS", False) @pytest.fixture def set_sqlalchemy_silence_uber_warning(monkeypatch): # Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0 # To be removed once SQLAlchemy 2.0 supported try: monkeypatch.setattr("sqlalchemy.util.deprecations.SILENCE_UBER_WARNING", True) except (ModuleNotFoundError, AttributeError): pass @pytest.fixture(autouse=True, scope="session") def zero_time_out_for_remote_code(): datasets.config.TIME_OUT_REMOTE_CODE = 0
import pytest import datasets import datasets.config # Import fixture modules as plugins pytest_plugins = ["tests.fixtures.files", "tests.fixtures.hub", "tests.fixtures.fsspec"] def pytest_collection_modifyitems(config, items): # Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit") for item in items: if any(marker in item.keywords for marker in ["integration", "unit"]): continue item.add_marker(pytest.mark.unit) @pytest.fixture(autouse=True) def set_test_cache_config(tmp_path_factory, monkeypatch): # test_hf_cache_home = tmp_path_factory.mktemp("cache") # TODO: why a cache dir per test function does not work? test_hf_cache_home = tmp_path_factory.getbasetemp() / "cache" test_hf_datasets_cache = test_hf_cache_home / "datasets" test_hf_modules_cache = test_hf_cache_home / "modules" monkeypatch.setattr("datasets.config.HF_DATASETS_CACHE", str(test_hf_datasets_cache)) monkeypatch.setattr("datasets.config.HF_MODULES_CACHE", str(test_hf_modules_cache)) test_downloaded_datasets_path = test_hf_datasets_cache / "downloads" monkeypatch.setattr("datasets.config.DOWNLOADED_DATASETS_PATH", str(test_downloaded_datasets_path)) test_extracted_datasets_path = test_hf_datasets_cache / "downloads" / "extracted" monkeypatch.setattr("datasets.config.EXTRACTED_DATASETS_PATH", str(test_extracted_datasets_path)) @pytest.fixture(autouse=True) def disable_implicit_token(monkeypatch): monkeypatch.setattr("huggingface_hub.constants.HF_HUB_DISABLE_IMPLICIT_TOKEN", True) @pytest.fixture(autouse=True, scope="session") def disable_tqdm_output(): datasets.disable_progress_bar() @pytest.fixture(autouse=True) def set_update_download_counts_to_false(monkeypatch): # don't take tests into account when counting downloads monkeypatch.setattr("datasets.config.HF_UPDATE_DOWNLOAD_COUNTS", False) @pytest.fixture def set_sqlalchemy_silence_uber_warning(monkeypatch): # Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0 # To be removed once SQLAlchemy 2.0 supported try: monkeypatch.setattr("sqlalchemy.util.deprecations.SILENCE_UBER_WARNING", True) except (ModuleNotFoundError, AttributeError): pass @pytest.fixture(autouse=True, scope="session") def zero_time_out_for_remote_code(): datasets.config.TIME_OUT_REMOTE_CODE = 0
from __future__ import annotations from sentence_transformers.sparse_encoder.data_collator import SparseEncoderDataCollator from sentence_transformers.sparse_encoder.evaluation import ( SparseBinaryClassificationEvaluator, SparseEmbeddingSimilarityEvaluator, SparseInformationRetrievalEvaluator, SparseMSEEvaluator, SparseNanoBEIREvaluator, SparseRerankingEvaluator, SparseTranslationEvaluator, SparseTripletEvaluator, ) from sentence_transformers.sparse_encoder.losses import ( CSRLoss, CSRReconstructionLoss, FlopsLoss, SparseAnglELoss, SparseCachedGISTEmbedLoss, SparseCachedMultipleNegativesRankingLoss, SparseCoSENTLoss, SparseCosineSimilarityLoss, SparseDistillKLDivLoss, SparseGISTEmbedLoss, SparseMarginMSELoss, SparseMSELoss, SparseMultipleNegativesRankingLoss, SparseTripletLoss, SpladeLoss, ) from sentence_transformers.sparse_encoder.models import ( CSRSparsity, MLMTransformer, SpladePooling, ) from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder from sentence_transformers.sparse_encoder.trainer import SparseEncoderTrainer from sentence_transformers.sparse_encoder.training_args import ( SparseEncoderTrainingArguments, ) __all__ = [ # Core components "SparseEncoder", "SparseEncoderDataCollator", "SparseEncoderTrainer", "SparseEncoderTrainingArguments", # Models "CSRSparsity", "MLMTransformer", "SpladePooling", # Losses "CSRLoss", "CSRReconstructionLoss", "SparseMultipleNegativesRankingLoss", "SparseCoSENTLoss", "SparseTripletLoss", "SparseCachedMultipleNegativesRankingLoss", "SparseMarginMSELoss", "SparseGISTEmbedLoss", "SparseCachedGISTEmbedLoss", "SparseCosineSimilarityLoss", "SparseMSELoss", "SparseAnglELoss", "SparseDistillKLDivLoss", "FlopsLoss", "SpladeLoss", # Evaluators "SparseBinaryClassificationEvaluator", "SparseEmbeddingSimilarityEvaluator", "SparseInformationRetrievalEvaluator", "SparseMSEEvaluator", "SparseNanoBEIREvaluator", "SparseTranslationEvaluator", "SparseRerankingEvaluator", "SparseTripletEvaluator", ] # TODO : Complete the SparseEncoder class # TODO : Add tests for all the components # TODO : Add the equivalent of the quantization file for the sparse encoder
from __future__ import annotations from sentence_transformers.sparse_encoder.data_collator import SparseEncoderDataCollator from sentence_transformers.sparse_encoder.evaluation import ( SparseBinaryClassificationEvaluator, SparseEmbeddingSimilarityEvaluator, SparseInformationRetrievalEvaluator, SparseMSEEvaluator, SparseNanoBEIREvaluator, SparseRerankingEvaluator, SparseTranslationEvaluator, SparseTripletEvaluator, ) from sentence_transformers.sparse_encoder.losses import ( CSRLoss, CSRReconstructionLoss, SparseAnglELoss, SparseCachedGISTEmbedLoss, SparseCachedMultipleNegativesRankingLoss, SparseCoSENTLoss, SparseCosineSimilarityLoss, SparseDistillKLDivLoss, SparseGISTEmbedLoss, SparseMarginMSELoss, SparseMSELoss, SparseMultipleNegativesRankingLoss, SparseTripletLoss, ) from sentence_transformers.sparse_encoder.models import ( CSRSparsity, MLMTransformer, SpladePooling, ) from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder from sentence_transformers.sparse_encoder.trainer import SparseEncoderTrainer from sentence_transformers.sparse_encoder.training_args import ( SparseEncoderTrainingArguments, ) __all__ = [ # Core components "SparseEncoder", "SparseEncoderDataCollator", "SparseEncoderTrainer", "SparseEncoderTrainingArguments", # Models "CSRSparsity", "MLMTransformer", "SpladePooling", # Losses "CSRLoss", "CSRReconstructionLoss", "SparseMultipleNegativesRankingLoss", "SparseCoSENTLoss", "SparseTripletLoss", "SparseCachedMultipleNegativesRankingLoss", "SparseMarginMSELoss", "SparseGISTEmbedLoss", "SparseCachedGISTEmbedLoss", "SparseCosineSimilarityLoss", "SparseMSELoss", "SparseAnglELoss", "SparseDistillKLDivLoss", # Evaluators "SparseBinaryClassificationEvaluator", "SparseEmbeddingSimilarityEvaluator", "SparseInformationRetrievalEvaluator", "SparseMSEEvaluator", "SparseNanoBEIREvaluator", "SparseTranslationEvaluator", "SparseRerankingEvaluator", "SparseTripletEvaluator", ] # TODO : Complete the SparseEncoder class # TODO : Add tests for all the components # TODO : Add the equivalent of the quantization file for the sparse encoder
_base_ = '../_base_/default_runtime.py' # dataset settings dataset_type = 'CocoDataset' data_root = 'data/coco/' image_size = (1024, 1024) file_client_args = dict(backend='disk') # comment out the code below to use different file client # file_client_args = dict( # backend='petrel', # path_mapping=dict({ # './data/': 's3://openmmlab/datasets/detection/', # 'data/': 's3://openmmlab/datasets/detection/' # })) train_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict( type='RandomResize', scale=image_size, ratio_range=(0.1, 2.0), keep_ratio=True), dict( type='RandomCrop', crop_type='absolute_range', crop_size=image_size, recompute_bbox=True, allow_negative_crop=True), dict(type='FilterAnnotations', min_gt_bbox_wh=(1e-2, 1e-2)), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='Resize', scale=(1333, 800), keep_ratio=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] # Use RepeatDataset to speed up training train_dataloader = dict( batch_size=2, num_workers=2, persistent_workers=True, sampler=dict(type='DefaultSampler', shuffle=True), dataset=dict( type='RepeatDataset', times=4, # simply change this from 2 to 16 for 50e - 400e training. dataset=dict( type=dataset_type, data_root=data_root, ann_file='annotations/instances_train2017.json', data_prefix=dict(img='train2017/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=train_pipeline))) val_dataloader = dict( batch_size=1, num_workers=2, persistent_workers=True, drop_last=False, sampler=dict(type='DefaultSampler', shuffle=False), dataset=dict( type=dataset_type, data_root=data_root, ann_file='annotations/instances_val2017.json', data_prefix=dict(img='val2017/'), test_mode=True, pipeline=test_pipeline)) test_dataloader = val_dataloader val_evaluator = dict( type='CocoMetric', ann_file=data_root + 'annotations/instances_val2017.json', metric=['bbox', 'segm'], format_only=False) test_evaluator = val_evaluator max_epochs = 25 train_cfg = dict( type='EpochBasedTrainLoop', max_epochs=max_epochs, val_interval=5) val_cfg = dict(type='ValLoop') test_cfg = dict(type='TestLoop') # optimizer assumes bs=64 optim_wrapper = dict( type='OptimWrapper', optimizer=dict(type='SGD', lr=0.1, momentum=0.9, weight_decay=0.00004)) # learning rate param_scheduler = [ dict( type='LinearLR', start_factor=0.067, by_epoch=False, begin=0, end=500), dict( type='MultiStepLR', begin=0, end=max_epochs, by_epoch=True, milestones=[22, 24], gamma=0.1) ] # only keep latest 2 checkpoints default_hooks = dict(checkpoint=dict(max_keep_ckpts=2)) # NOTE: `auto_scale_lr` is for automatically scaling LR, # USER SHOULD NOT CHANGE ITS VALUES. # base_batch_size = (32 GPUs) x (2 samples per GPU) auto_scale_lr = dict(base_batch_size=64)
_base_ = '../_base_/default_runtime.py' # dataset settings dataset_type = 'CocoDataset' data_root = 'data/coco/' image_size = (1024, 1024) file_client_args = dict(backend='disk') # comment out the code below to use different file client # file_client_args = dict( # backend='petrel', # path_mapping=dict({ # './data/': 's3://openmmlab/datasets/detection/', # 'data/': 's3://openmmlab/datasets/detection/' # })) train_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict(type='RandomResize', scale=image_size, ratio_range=(0.1, 2.0)), dict( type='RandomCrop', crop_type='absolute_range', crop_size=image_size, recompute_bbox=True, allow_negative_crop=True), dict(type='FilterAnnotations', min_gt_bbox_wh=(1e-2, 1e-2)), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='Resize', scale=(1333, 800), keep_ratio=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] # Use RepeatDataset to speed up training train_dataloader = dict( batch_size=2, num_workers=2, persistent_workers=True, sampler=dict(type='DefaultSampler', shuffle=True), batch_sampler=dict(type='AspectRatioBatchSampler'), dataset=dict( type='RepeatDataset', times=4, # simply change this from 2 to 16 for 50e - 400e training. dataset=dict( type=dataset_type, data_root=data_root, ann_file='annotations/instances_train2017.json', data_prefix=dict(img='train2017/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=train_pipeline))) val_dataloader = dict( batch_size=1, num_workers=2, persistent_workers=True, drop_last=False, sampler=dict(type='DefaultSampler', shuffle=False), dataset=dict( type=dataset_type, data_root=data_root, ann_file='annotations/instances_val2017.json', data_prefix=dict(img='val2017/'), test_mode=True, pipeline=test_pipeline)) test_dataloader = val_dataloader val_evaluator = dict( type='CocoMetric', ann_file=data_root + 'annotations/instances_val2017.json', metric=['bbox', 'segm'], format_only=False) test_evaluator = val_evaluator train_cfg = dict(type='EpochBasedTrainLoop', max_epochs=25, val_interval=5) val_cfg = dict(type='ValLoop') test_cfg = dict(type='TestLoop') # optimizer assumes bs=64 optim_wrapper = dict( type='OptimWrapper', optimizer=dict(type='SGD', lr=0.1, momentum=0.9, weight_decay=0.00004)) # learning rate param_scheduler = [ dict( type='LinearLR', start_factor=0.067, by_epoch=False, begin=0, end=500), dict( type='MultiStepLR', begin=0, end=12, by_epoch=True, milestones=[22, 24], gamma=0.1) ]
from typing import Tuple, Iterator import pytest import requests import itertools from docarray import DocumentArray, Document def test_weaviate_hnsw(start_storage): da = DocumentArray( storage='weaviate', config={ 'n_dim': 100, 'ef': 100, 'ef_construction': 100, 'max_connections': 16, 'dynamic_ef_min': 50, 'dynamic_ef_max': 300, 'dynamic_ef_factor': 4, 'vector_cache_max_objects': 1000000, 'flat_search_cutoff': 20000, 'cleanup_interval_seconds': 1000, 'skip': True, 'distance': 'l2-squared', }, ) result = requests.get('http://localhost:8080/v1/schema').json() classes = result.get('classes', []) main_class = list( filter(lambda class_element: class_element['class'] == da._config.name, classes) ) assert len(main_class) == 1 main_class = main_class[0] assert main_class.get('vectorIndexConfig', {}).get('maxConnections') == 16 assert main_class.get('vectorIndexConfig', {}).get('efConstruction') == 100 assert main_class.get('vectorIndexConfig', {}).get('ef') == 100 assert main_class.get('vectorIndexConfig', {}).get('dynamicEfMin') == 50 assert main_class.get('vectorIndexConfig', {}).get('dynamicEfMax') == 300 assert main_class.get('vectorIndexConfig', {}).get('dynamicEfFactor') == 4 assert ( main_class.get('vectorIndexConfig', {}).get('vectorCacheMaxObjects') == 1000000 ) assert main_class.get('vectorIndexConfig', {}).get('flatSearchCutoff') == 20000 assert main_class.get('vectorIndexConfig', {}).get('cleanupIntervalSeconds') == 1000 assert main_class.get('vectorIndexConfig', {}).get('skip') is True assert main_class.get('vectorIndexConfig', {}).get('distance') == 'l2-squared' def test_weaviate_da_w_protobuff(start_storage): N = 10 index = DocumentArray( storage='weaviate', config={ 'name': 'Test', 'columns': [('price', 'int')], }, ) docs = DocumentArray([Document(tags={'price': i}) for i in range(N)]) docs = DocumentArray.from_protobuf( docs.to_protobuf() ) # same as streaming the da in jina index.extend(docs) assert len(index) == N @pytest.mark.parametrize('type_da', [int, float, str]) @pytest.mark.parametrize('type_column', ['int', 'float', 'str']) def test_cast_columns_weaviate(start_storage, type_da, type_column, request): test_id = request.node.callspec.id.replace( '-', '' ) # remove '-' from the test id for the weaviate name N = 10 index = DocumentArray( storage='weaviate', config={ 'name': f'Test{test_id}', 'columns': [('price', type_column)], }, ) docs = DocumentArray([Document(tags={'price': type_da(i)}) for i in range(10)]) index.extend(docs) assert len(index) == N @pytest.mark.parametrize('type_da', [int, float, str]) @pytest.mark.parametrize('type_column', ['int', 'float', 'str']) def test_cast_columns_annlite(start_storage, type_da, type_column): N = 10 index = DocumentArray( storage='annlite', config={ 'n_dim': 3, 'columns': [('price', type_column)], }, ) docs = DocumentArray([Document(tags={'price': type_da(i)}) for i in range(10)]) index.extend(docs) assert len(index) == N @pytest.mark.parametrize('type_da', [int, float, str]) @pytest.mark.parametrize('type_column', ['int', 'float', 'str']) def test_cast_columns_qdrant(start_storage, type_da, type_column, request): test_id = request.node.callspec.id.replace( '-', '' ) # remove '-' from the test id for the weaviate name N = 10 index = DocumentArray( storage='qdrant', config={ 'collection_name': f'test{test_id}', 'n_dim': 3, 'columns': [('price', type_column)], }, ) docs = DocumentArray([Document(tags={'price': type_da(i)}) for i in range(10)]) index.extend(docs) assert len(index) == N
import requests from docarray import DocumentArray def test_weaviate_hnsw(start_storage): da = DocumentArray( storage='weaviate', config={ 'n_dim': 100, 'ef': 100, 'ef_construction': 100, 'max_connections': 16, 'dynamic_ef_min': 50, 'dynamic_ef_max': 300, 'dynamic_ef_factor': 4, 'vector_cache_max_objects': 1000000, 'flat_search_cutoff': 20000, 'cleanup_interval_seconds': 1000, 'skip': True, 'distance': 'l2-squared', }, ) result = requests.get('http://localhost:8080/v1/schema').json() classes = result.get('classes', []) main_class = list( filter(lambda class_element: class_element['class'] == da._config.name, classes) ) assert len(main_class) == 1 main_class = main_class[0] assert main_class.get('vectorIndexConfig', {}).get('maxConnections') == 16 assert main_class.get('vectorIndexConfig', {}).get('efConstruction') == 100 assert main_class.get('vectorIndexConfig', {}).get('ef') == 100 assert main_class.get('vectorIndexConfig', {}).get('dynamicEfMin') == 50 assert main_class.get('vectorIndexConfig', {}).get('dynamicEfMax') == 300 assert main_class.get('vectorIndexConfig', {}).get('dynamicEfFactor') == 4 assert ( main_class.get('vectorIndexConfig', {}).get('vectorCacheMaxObjects') == 1000000 ) assert main_class.get('vectorIndexConfig', {}).get('flatSearchCutoff') == 20000 assert main_class.get('vectorIndexConfig', {}).get('cleanupIntervalSeconds') == 1000 assert main_class.get('vectorIndexConfig', {}).get('skip') is True assert main_class.get('vectorIndexConfig', {}).get('distance') == 'l2-squared'
import os from functools import lru_cache from subprocess import CalledProcessError, run from typing import Optional, Union import numpy as np import torch import torch.nn.functional as F from .utils import exact_div # hard-coded audio hyperparameters SAMPLE_RATE = 16000 N_FFT = 400 N_MELS = 80 HOP_LENGTH = 160 CHUNK_LENGTH = 30 N_SAMPLES = CHUNK_LENGTH * SAMPLE_RATE # 480000 samples in a 30-second chunk N_FRAMES = exact_div(N_SAMPLES, HOP_LENGTH) # 3000 frames in a mel spectrogram input N_SAMPLES_PER_TOKEN = HOP_LENGTH * 2 # the initial convolutions has stride 2 FRAMES_PER_SECOND = exact_div(SAMPLE_RATE, HOP_LENGTH) # 10ms per audio frame TOKENS_PER_SECOND = exact_div(SAMPLE_RATE, N_SAMPLES_PER_TOKEN) # 20ms per audio token def load_audio(file: str, sr: int = SAMPLE_RATE): """ Open an audio file and read as mono waveform, resampling as necessary Parameters ---------- file: str The audio file to open sr: int The sample rate to resample the audio if necessary Returns ------- A NumPy array containing the audio waveform, in float32 dtype. """ # This launches a subprocess to decode audio while down-mixing # and resampling as necessary. Requires the ffmpeg CLI in PATH. # fmt: off cmd = [ "ffmpeg", "-nostdin", "-threads", "0", "-i", file, "-f", "s16le", "-ac", "1", "-acodec", "pcm_s16le", "-ar", str(sr), "-" ] # fmt: on try: out = run(cmd, capture_output=True, check=True).stdout except CalledProcessError as e: raise RuntimeError(f"Failed to load audio: {e.stderr.decode()}") from e return np.frombuffer(out, np.int16).flatten().astype(np.float32) / 32768.0 def pad_or_trim(array, length: int = N_SAMPLES, *, axis: int = -1): """ Pad or trim the audio array to N_SAMPLES, as expected by the encoder. """ if torch.is_tensor(array): if array.shape[axis] > length: array = array.index_select( dim=axis, index=torch.arange(length, device=array.device) ) if array.shape[axis] < length: pad_widths = [(0, 0)] * array.ndim pad_widths[axis] = (0, length - array.shape[axis]) array = F.pad(array, [pad for sizes in pad_widths[::-1] for pad in sizes]) else: if array.shape[axis] > length: array = array.take(indices=range(length), axis=axis) if array.shape[axis] < length: pad_widths = [(0, 0)] * array.ndim pad_widths[axis] = (0, length - array.shape[axis]) array = np.pad(array, pad_widths) return array @lru_cache(maxsize=None) def mel_filters(device, n_mels: int = N_MELS) -> torch.Tensor: """ load the mel filterbank matrix for projecting STFT into a Mel spectrogram. Allows decoupling librosa dependency; saved using: np.savez_compressed( "mel_filters.npz", mel_80=librosa.filters.mel(sr=16000, n_fft=400, n_mels=80), ) """ assert n_mels == 80, f"Unsupported n_mels: {n_mels}" filters_path = os.path.join(os.path.dirname(__file__), "assets", "mel_filters.npz") with np.load(filters_path, allow_pickle=False) as f: return torch.from_numpy(f[f"mel_{n_mels}"]).to(device) def log_mel_spectrogram( audio: Union[str, np.ndarray, torch.Tensor], n_mels: int = N_MELS, padding: int = 0, device: Optional[Union[str, torch.device]] = None, ): """ Compute the log-Mel spectrogram of Parameters ---------- audio: Union[str, np.ndarray, torch.Tensor], shape = (*) The path to audio or either a NumPy array or Tensor containing the audio waveform in 16 kHz n_mels: int The number of Mel-frequency filters, only 80 is supported padding: int Number of zero samples to pad to the right device: Optional[Union[str, torch.device]] If given, the audio tensor is moved to this device before STFT Returns ------- torch.Tensor, shape = (80, n_frames) A Tensor that contains the Mel spectrogram """ if not torch.is_tensor(audio): if isinstance(audio, str): audio = load_audio(audio) audio = torch.from_numpy(audio) if device is not None: audio = audio.to(device) if padding > 0: audio = F.pad(audio, (0, padding)) window = torch.hann_window(N_FFT).to(audio.device) stft = torch.stft(audio, N_FFT, HOP_LENGTH, window=window, return_complex=True) magnitudes = stft[..., :-1].abs() ** 2 filters = mel_filters(audio.device, n_mels) mel_spec = filters @ magnitudes log_spec = torch.clamp(mel_spec, min=1e-10).log10() log_spec = torch.maximum(log_spec, log_spec.max() - 8.0) log_spec = (log_spec + 4.0) / 4.0 return log_spec
import os from functools import lru_cache from subprocess import CalledProcessError, run from typing import Optional, Union import numpy as np import torch import torch.nn.functional as F from .utils import exact_div # hard-coded audio hyperparameters SAMPLE_RATE = 16000 N_FFT = 400 N_MELS = 80 HOP_LENGTH = 160 CHUNK_LENGTH = 30 N_SAMPLES = CHUNK_LENGTH * SAMPLE_RATE # 480000 samples in a 30-second chunk N_FRAMES = exact_div(N_SAMPLES, HOP_LENGTH) # 3000 frames in a mel spectrogram input N_SAMPLES_PER_TOKEN = HOP_LENGTH * 2 # the initial convolutions has stride 2 FRAMES_PER_SECOND = exact_div(SAMPLE_RATE, HOP_LENGTH) # 10ms per audio frame TOKENS_PER_SECOND = exact_div(SAMPLE_RATE, N_SAMPLES_PER_TOKEN) # 20ms per audio token def load_audio(file: str, sr: int = SAMPLE_RATE): """ Open an audio file and read as mono waveform, resampling as necessary Parameters ---------- file: str The audio file to open sr: int The sample rate to resample the audio if necessary Returns ------- A NumPy array containing the audio waveform, in float32 dtype. """ # This launches a subprocess to decode audio while down-mixing # and resampling as necessary. Requires the ffmpeg CLI in PATH. # fmt: off cmd = [ "ffmpeg", "-nostdin", "-threads", "0", "-i", file, "-f", "s16le", "-ac", "1", "-acodec", "pcm_s16le", "-ar", str(sr), "-" ] # fmt: on try: out = run(cmd, capture_output=True, check=True).stdout except CalledProcessError as e: raise RuntimeError(f"Failed to load audio: {e.stderr.decode()}") from e return np.frombuffer(out, np.int16).flatten().astype(np.float32) / 32768.0 def pad_or_trim(array, length: int = N_SAMPLES, *, axis: int = -1): """ Pad or trim the audio array to N_SAMPLES, as expected by the encoder. """ if torch.is_tensor(array): if array.shape[axis] > length: array = array.index_select( dim=axis, index=torch.arange(length, device=array.device) ) if array.shape[axis] < length: pad_widths = [(0, 0)] * array.ndim pad_widths[axis] = (0, length - array.shape[axis]) array = F.pad(array, [pad for sizes in pad_widths[::-1] for pad in sizes]) else: if array.shape[axis] > length: array = array.take(indices=range(length), axis=axis) if array.shape[axis] < length: pad_widths = [(0, 0)] * array.ndim pad_widths[axis] = (0, length - array.shape[axis]) array = np.pad(array, pad_widths) return array @lru_cache(maxsize=None) def mel_filters(device, n_mels: int = N_MELS) -> torch.Tensor: """ load the mel filterbank matrix for projecting STFT into a Mel spectrogram. Allows decoupling librosa dependency; saved using: np.savez_compressed( "mel_filters.npz", mel_80=librosa.filters.mel(sr=16000, n_fft=400, n_mels=80), ) """ assert n_mels == 80, f"Unsupported n_mels: {n_mels}" with np.load( os.path.join(os.path.dirname(__file__), "assets", "mel_filters.npz") ) as f: return torch.from_numpy(f[f"mel_{n_mels}"]).to(device) def log_mel_spectrogram( audio: Union[str, np.ndarray, torch.Tensor], n_mels: int = N_MELS, padding: int = 0, device: Optional[Union[str, torch.device]] = None, ): """ Compute the log-Mel spectrogram of Parameters ---------- audio: Union[str, np.ndarray, torch.Tensor], shape = (*) The path to audio or either a NumPy array or Tensor containing the audio waveform in 16 kHz n_mels: int The number of Mel-frequency filters, only 80 is supported padding: int Number of zero samples to pad to the right device: Optional[Union[str, torch.device]] If given, the audio tensor is moved to this device before STFT Returns ------- torch.Tensor, shape = (80, n_frames) A Tensor that contains the Mel spectrogram """ if not torch.is_tensor(audio): if isinstance(audio, str): audio = load_audio(audio) audio = torch.from_numpy(audio) if device is not None: audio = audio.to(device) if padding > 0: audio = F.pad(audio, (0, padding)) window = torch.hann_window(N_FFT).to(audio.device) stft = torch.stft(audio, N_FFT, HOP_LENGTH, window=window, return_complex=True) magnitudes = stft[..., :-1].abs() ** 2 filters = mel_filters(audio.device, n_mels) mel_spec = filters @ magnitudes log_spec = torch.clamp(mel_spec, min=1e-10).log10() log_spec = torch.maximum(log_spec, log_spec.max() - 8.0) log_spec = (log_spec + 4.0) / 4.0 return log_spec
"""**Load** module helps with serialization and deserialization.""" from importlib import import_module from typing import TYPE_CHECKING if TYPE_CHECKING: from langchain_core.load.dump import dumpd, dumps from langchain_core.load.load import loads from langchain_core.load.serializable import Serializable # Unfortunately, we have to eagerly import load from langchain_core/load/load.py # eagerly to avoid a namespace conflict. We want users to still be able to use # `from langchain_core.load import load` to get the load function, but # the `from langchain_core.load.load import load` absolute import should also work. from langchain_core.load.load import load __all__ = ["dumpd", "dumps", "load", "loads", "Serializable"] _dynamic_imports = { "dumpd": "dump", "dumps": "dump", "load": "load", "loads": "load", "Serializable": "serializable", } def __getattr__(attr_name: str) -> object: module_name = _dynamic_imports.get(attr_name) package = __spec__.parent if module_name == "__module__" or module_name is None: result = import_module(f".{attr_name}", package=package) else: module = import_module(f".{module_name}", package=package) result = getattr(module, attr_name) globals()[attr_name] = result return result def __dir__() -> list[str]: return list(__all__)
"""**Load** module helps with serialization and deserialization.""" from importlib import import_module from typing import TYPE_CHECKING if TYPE_CHECKING: from langchain_core.load.dump import dumpd, dumps from langchain_core.load.load import load, loads from langchain_core.load.serializable import Serializable __all__ = ["dumpd", "dumps", "load", "loads", "Serializable"] _dynamic_imports = { "dumpd": "dump", "dumps": "dump", "load": "load", "loads": "load", "Serializable": "serializable", } def __getattr__(attr_name: str) -> object: module_name = _dynamic_imports.get(attr_name) package = __spec__.parent if module_name == "__module__" or module_name is None: result = import_module(f".{attr_name}", package=package) else: module = import_module(f".{module_name}", package=package) result = getattr(module, attr_name) globals()[attr_name] = result return result def __dir__() -> list[str]: return list(__all__)
__version__ = '0.18.1' import os from docarray.document import Document from docarray.array import DocumentArray from docarray.dataclasses import dataclass, field if 'DA_RICH_HANDLER' in os.environ: from rich.traceback import install install()
__version__ = '0.18.0' import os from docarray.document import Document from docarray.array import DocumentArray from docarray.dataclasses import dataclass, field if 'DA_RICH_HANDLER' in os.environ: from rich.traceback import install install()
import logging from datasets import load_dataset from sentence_transformers import SparseEncoder from sentence_transformers.sparse_encoder.evaluation import SparseBinaryClassificationEvaluator logging.basicConfig(format="%(message)s", level=logging.INFO) # Initialize the SPLADE model model = SparseEncoder("naver/splade-cocondenser-ensembledistil") # Load a dataset with two text columns and a class label column (https://huggingface.co/datasets/sentence-transformers/quora-duplicates) eval_dataset = load_dataset("sentence-transformers/quora-duplicates", "pair-class", split="train[-1000:]") # Initialize the evaluator binary_acc_evaluator = SparseBinaryClassificationEvaluator( sentences1=eval_dataset["sentence1"], sentences2=eval_dataset["sentence2"], labels=eval_dataset["label"], name="quora_duplicates_dev", show_progress_bar=True, similarity_fn_names=["cosine", "dot", "euclidean", "manhattan"], ) results = binary_acc_evaluator(model) """ Accuracy with Cosine-Similarity: 74.90 (Threshold: 0.8668) F1 with Cosine-Similarity: 67.37 (Threshold: 0.5959) Precision with Cosine-Similarity: 54.15 Recall with Cosine-Similarity: 89.13 Average Precision with Cosine-Similarity: 67.81 Matthews Correlation with Cosine-Similarity: 49.89 Accuracy with Dot-Product: 76.50 (Threshold: 24.3460) F1 with Dot-Product: 66.93 (Threshold: 20.0762) Precision with Dot-Product: 57.62 Recall with Dot-Product: 79.81 Average Precision with Dot-Product: 65.94 Matthews Correlation with Dot-Product: 48.82 Accuracy with Euclidean-Distance: 67.70 (Threshold: -10.0062) F1 with Euclidean-Distance: 48.60 (Threshold: -0.2346) Precision with Euclidean-Distance: 32.13 Recall with Euclidean-Distance: 99.69 Average Precision with Euclidean-Distance: 20.52 Matthews Correlation with Euclidean-Distance: -4.59 Accuracy with Manhattan-Distance: 67.70 (Threshold: -103.1993) F1 with Manhattan-Distance: 48.60 (Threshold: -1.1565) Precision with Manhattan-Distance: 32.13 Recall with Manhattan-Distance: 99.69 Average Precision with Manhattan-Distance: 21.05 Matthews Correlation with Manhattan-Distance: -4.59 Model Sparsity: Active Dimensions: 63.1, Sparsity Ratio: 0.9979 """ # Print the results print(f"Primary metric: {binary_acc_evaluator.primary_metric}") # => Primary metric: quora_duplicates_dev_max_ap print(f"Primary metric value: {results[binary_acc_evaluator.primary_metric]:.4f}") # => Primary metric value: 0.6781
import logging from datasets import load_dataset from sentence_transformers import SparseEncoder from sentence_transformers.sparse_encoder.evaluation import SparseBinaryClassificationEvaluator logging.basicConfig(format="%(message)s", level=logging.INFO) # Initialize the SPLADE model model = SparseEncoder("naver/splade-cocondenser-ensembledistil") # Load a dataset with two text columns and a class label column (https://huggingface.co/datasets/sentence-transformers/quora-duplicates) eval_dataset = load_dataset("sentence-transformers/quora-duplicates", "pair-class", split="train[-1000:]") # Initialize the evaluator binary_acc_evaluator = SparseBinaryClassificationEvaluator( sentences1=eval_dataset["sentence1"], sentences2=eval_dataset["sentence2"], labels=eval_dataset["label"], name="quora_duplicates_dev", show_progress_bar=True, similarity_fn_names=["cosine", "dot", "euclidean", "manhattan"], ) results = binary_acc_evaluator(model) """ Accuracy with Cosine-Similarity: 74.90 (Threshold: 0.8668) F1 with Cosine-Similarity: 67.37 (Threshold: 0.5959) Precision with Cosine-Similarity: 54.15 Recall with Cosine-Similarity: 89.13 Average Precision with Cosine-Similarity: 67.81 Matthews Correlation with Cosine-Similarity: 49.89 Accuracy with Dot-Product: 76.50 (Threshold: 24.3460) F1 with Dot-Product: 66.93 (Threshold: 20.0762) Precision with Dot-Product: 57.62 Recall with Dot-Product: 79.81 Average Precision with Dot-Product: 65.94 Matthews Correlation with Dot-Product: 48.82 Accuracy with Euclidean-Distance: 67.70 (Threshold: -10.0062) F1 with Euclidean-Distance: 48.60 (Threshold: -0.2346) Precision with Euclidean-Distance: 32.13 Recall with Euclidean-Distance: 99.69 Average Precision with Euclidean-Distance: 20.52 Matthews Correlation with Euclidean-Distance: -4.59 Accuracy with Manhattan-Distance: 67.70 (Threshold: -103.1993) F1 with Manhattan-Distance: 48.60 (Threshold: -1.1565) Precision with Manhattan-Distance: 32.13 Recall with Manhattan-Distance: 99.69 Average Precision with Manhattan-Distance: 21.05 Matthews Correlation with Manhattan-Distance: -4.59 Model Sparsity Stats: Row Non-Zero Mean: 63.13884735107422, Row Sparsity Mean: 0.9979313611984253 """ # Print the results print(f"Primary metric: {binary_acc_evaluator.primary_metric}") # => Primary metric: quora_duplicates_dev_max_ap print(f"Primary metric value: {results[binary_acc_evaluator.primary_metric]:.4f}") # => Primary metric value: 0.6781
import os import numpy as np import pytest from pydantic.tools import parse_obj_as, schema_json_of from docarray.base_doc.io.json import orjson_dumps from docarray.typing import Mesh3DUrl, NdArray from docarray.typing.url.mimetypes import ( OBJ_MIMETYPE, AUDIO_MIMETYPE, VIDEO_MIMETYPE, IMAGE_MIMETYPE, TEXT_MIMETYPE, ) from tests import TOYDATA_DIR MESH_FILES = { 'obj': str(TOYDATA_DIR / 'tetrahedron.obj'), 'glb': str(TOYDATA_DIR / 'test.glb'), 'ply': str(TOYDATA_DIR / 'cube.ply'), } REMOTE_OBJ_FILE = 'https://people.sc.fsu.edu/~jburkardt/data/obj/al.obj' @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize( 'file_format, file_path', [ ('obj', MESH_FILES['obj']), ('glb', MESH_FILES['glb']), ('ply', MESH_FILES['ply']), ('remote-obj', REMOTE_OBJ_FILE), ], ) def test_load(file_format, file_path): url = parse_obj_as(Mesh3DUrl, file_path) tensors = url.load() assert isinstance(tensors.vertices, np.ndarray) assert isinstance(tensors.vertices, NdArray) assert isinstance(tensors.faces, np.ndarray) assert isinstance(tensors.faces, NdArray) assert tensors.vertices.shape[1] == 3 assert tensors.faces.shape[1] == 3 @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize( 'file_path', [*MESH_FILES.values(), REMOTE_OBJ_FILE], ) @pytest.mark.parametrize('field', ['vertices', 'faces']) def test_load_one_of_fields(file_path, field): url = parse_obj_as(Mesh3DUrl, file_path) field = getattr(url.load(), field) assert isinstance(field, np.ndarray) assert isinstance(field, NdArray) def test_json_schema(): schema_json_of(Mesh3DUrl) def test_dump_json(): url = parse_obj_as(Mesh3DUrl, REMOTE_OBJ_FILE) orjson_dumps(url) @pytest.mark.parametrize( 'path_to_file', [*MESH_FILES.values(), REMOTE_OBJ_FILE], ) def test_validation(path_to_file): url = parse_obj_as(Mesh3DUrl, path_to_file) assert isinstance(url, Mesh3DUrl) assert isinstance(url, str) @pytest.mark.proto def test_proto_mesh_url(): uri = parse_obj_as(Mesh3DUrl, REMOTE_OBJ_FILE) uri._to_node_protobuf() @pytest.mark.parametrize( 'file_type, file_source', [ (OBJ_MIMETYPE, MESH_FILES['obj']), (OBJ_MIMETYPE, MESH_FILES['glb']), (OBJ_MIMETYPE, MESH_FILES['ply']), (OBJ_MIMETYPE, REMOTE_OBJ_FILE), (AUDIO_MIMETYPE, os.path.join(TOYDATA_DIR, 'hello.aac')), (AUDIO_MIMETYPE, os.path.join(TOYDATA_DIR, 'hello.mp3')), (AUDIO_MIMETYPE, os.path.join(TOYDATA_DIR, 'hello.ogg')), (VIDEO_MIMETYPE, os.path.join(TOYDATA_DIR, 'mov_bbb.mp4')), (IMAGE_MIMETYPE, os.path.join(TOYDATA_DIR, 'test.png')), (TEXT_MIMETYPE, os.path.join(TOYDATA_DIR, 'test' 'test.html')), (TEXT_MIMETYPE, os.path.join(TOYDATA_DIR, 'test' 'test.md')), (TEXT_MIMETYPE, os.path.join(TOYDATA_DIR, 'penal_colony.txt')), ], ) def test_file_validation(file_type, file_source): if file_type != Mesh3DUrl.mime_type(): with pytest.raises(ValueError): parse_obj_as(Mesh3DUrl, file_source) else: parse_obj_as(Mesh3DUrl, file_source)
import numpy as np import pytest from pydantic.tools import parse_obj_as, schema_json_of from docarray.base_doc.io.json import orjson_dumps from docarray.typing import Mesh3DUrl, NdArray from tests import TOYDATA_DIR MESH_FILES = { 'obj': str(TOYDATA_DIR / 'tetrahedron.obj'), 'glb': str(TOYDATA_DIR / 'test.glb'), 'ply': str(TOYDATA_DIR / 'cube.ply'), } REMOTE_OBJ_FILE = 'https://people.sc.fsu.edu/~jburkardt/data/obj/al.obj' @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize( 'file_format, file_path', [ ('obj', MESH_FILES['obj']), ('glb', MESH_FILES['glb']), ('ply', MESH_FILES['ply']), ('remote-obj', REMOTE_OBJ_FILE), ], ) def test_load(file_format, file_path): url = parse_obj_as(Mesh3DUrl, file_path) tensors = url.load() assert isinstance(tensors.vertices, np.ndarray) assert isinstance(tensors.vertices, NdArray) assert isinstance(tensors.faces, np.ndarray) assert isinstance(tensors.faces, NdArray) assert tensors.vertices.shape[1] == 3 assert tensors.faces.shape[1] == 3 @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize( 'file_path', [*MESH_FILES.values(), REMOTE_OBJ_FILE], ) @pytest.mark.parametrize('field', ['vertices', 'faces']) def test_load_one_of_fields(file_path, field): url = parse_obj_as(Mesh3DUrl, file_path) field = getattr(url.load(), field) assert isinstance(field, np.ndarray) assert isinstance(field, NdArray) def test_json_schema(): schema_json_of(Mesh3DUrl) def test_dump_json(): url = parse_obj_as(Mesh3DUrl, REMOTE_OBJ_FILE) orjson_dumps(url) @pytest.mark.parametrize( 'path_to_file', [*MESH_FILES.values(), REMOTE_OBJ_FILE], ) def test_validation(path_to_file): url = parse_obj_as(Mesh3DUrl, path_to_file) assert isinstance(url, Mesh3DUrl) assert isinstance(url, str) @pytest.mark.proto def test_proto_mesh_url(): uri = parse_obj_as(Mesh3DUrl, REMOTE_OBJ_FILE) uri._to_node_protobuf()
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.datasets.fashion_mnist import load_data as load_data
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.datasets.fashion_mnist import load_data
import csv from contextlib import nullcontext from typing import Union, TextIO, Optional, Dict, TYPE_CHECKING, Type, Sequence import numpy as np if TYPE_CHECKING: # pragma: no cover from docarray.typing import T class CsvIOMixin: """CSV IO helper. can be applied to DA & DAM """ def save_embeddings_csv( self, file: Union[str, TextIO], encoding: str = 'utf-8', **kwargs ) -> None: """Save embeddings to a CSV file This function utilizes :meth:`numpy.savetxt` internal. :param file: File or filename to which the data is saved. :param encoding: encoding used to save the data into a file. By default, ``utf-8`` is used. :param kwargs: extra kwargs will be passed to :meth:`numpy.savetxt`. """ if hasattr(file, 'write'): file_ctx = nullcontext(file) else: file_ctx = open(file, 'w', encoding=encoding) with file_ctx: np.savetxt(file_ctx, self.embeddings, **kwargs) def save_csv( self, file: Union[str, TextIO], flatten_tags: bool = True, exclude_fields: Optional[Sequence[str]] = None, dialect: Union[str, 'csv.Dialect'] = 'excel', with_header: bool = True, encoding: str = 'utf-8', ) -> None: """Save array elements into a CSV file. :param file: File or filename to which the data is saved. :param flatten_tags: if set, then all fields in ``Document.tags`` will be flattened into ``tag__fieldname`` and stored as separated columns. It is useful when ``tags`` contain a lot of information. :param exclude_fields: if set, those fields wont show up in the output CSV :param dialect: define a set of parameters specific to a particular CSV dialect. could be a string that represents predefined dialects in your system, or could be a :class:`csv.Dialect` class that groups specific formatting parameters together. :param encoding: encoding used to save the data into a CSV file. By default, ``utf-8`` is used. """ if hasattr(file, 'write'): file_ctx = nullcontext(file) else: file_ctx = open(file, 'w', encoding=encoding) with file_ctx as fp: if flatten_tags and self[0].tags: keys = list(self[0].non_empty_fields) + list( f'tag__{k}' for k in self[0].tags ) keys.remove('tags') else: flatten_tags = False keys = list(self[0].non_empty_fields) if exclude_fields: for k in exclude_fields: if k in keys: keys.remove(k) writer = csv.DictWriter(fp, fieldnames=keys, dialect=dialect) if with_header: writer.writeheader() for d in self: pd = d.to_dict( protocol='jsonschema', exclude=set(exclude_fields) if exclude_fields else None, exclude_none=True, ) if flatten_tags: t = pd.pop('tags') pd.update({f'tag__{k}': v for k, v in t.items()}) writer.writerow(pd) @classmethod def load_csv( cls: Type['T'], file: Union[str, TextIO], field_resolver: Optional[Dict[str, str]] = None, encoding: str = 'utf-8', ) -> 'T': """Load array elements from a binary file. :param file: File or filename to which the data is saved. :param field_resolver: a map from field names defined in JSON, dict to the field names defined in Document. :param encoding: encoding used to read a CSV file. By default, ``utf-8`` is used. :return: a DocumentArray object """ from docarray.document.generators import from_csv return cls(from_csv(file, field_resolver=field_resolver, encoding=encoding))
import csv from contextlib import nullcontext from typing import Union, TextIO, Optional, Dict, TYPE_CHECKING, Type, Sequence import numpy as np if TYPE_CHECKING: from docarray.typing import T class CsvIOMixin: """CSV IO helper. can be applied to DA & DAM """ def save_embeddings_csv( self, file: Union[str, TextIO], encoding: str = 'utf-8', **kwargs ) -> None: """Save embeddings to a CSV file This function utilizes :meth:`numpy.savetxt` internal. :param file: File or filename to which the data is saved. :param encoding: encoding used to save the data into a file. By default, ``utf-8`` is used. :param kwargs: extra kwargs will be passed to :meth:`numpy.savetxt`. """ if hasattr(file, 'write'): file_ctx = nullcontext(file) else: file_ctx = open(file, 'w', encoding=encoding) with file_ctx: np.savetxt(file_ctx, self.embeddings, **kwargs) def save_csv( self, file: Union[str, TextIO], flatten_tags: bool = True, exclude_fields: Optional[Sequence[str]] = None, dialect: Union[str, 'csv.Dialect'] = 'excel', with_header: bool = True, encoding: str = 'utf-8', ) -> None: """Save array elements into a CSV file. :param file: File or filename to which the data is saved. :param flatten_tags: if set, then all fields in ``Document.tags`` will be flattened into ``tag__fieldname`` and stored as separated columns. It is useful when ``tags`` contain a lot of information. :param exclude_fields: if set, those fields wont show up in the output CSV :param dialect: define a set of parameters specific to a particular CSV dialect. could be a string that represents predefined dialects in your system, or could be a :class:`csv.Dialect` class that groups specific formatting parameters together. :param encoding: encoding used to save the data into a CSV file. By default, ``utf-8`` is used. """ if hasattr(file, 'write'): file_ctx = nullcontext(file) else: file_ctx = open(file, 'w', encoding=encoding) with file_ctx as fp: if flatten_tags and self[0].tags: keys = list(self[0].non_empty_fields) + list( f'tag__{k}' for k in self[0].tags ) keys.remove('tags') else: flatten_tags = False keys = list(self[0].non_empty_fields) if exclude_fields: for k in exclude_fields: if k in keys: keys.remove(k) writer = csv.DictWriter(fp, fieldnames=keys, dialect=dialect) if with_header: writer.writeheader() for d in self: pd = d.to_dict( protocol='jsonschema', exclude=set(exclude_fields) if exclude_fields else None, exclude_none=True, ) if flatten_tags: t = pd.pop('tags') pd.update({f'tag__{k}': v for k, v in t.items()}) writer.writerow(pd) @classmethod def load_csv( cls: Type['T'], file: Union[str, TextIO], field_resolver: Optional[Dict[str, str]] = None, encoding: str = 'utf-8', ) -> 'T': """Load array elements from a binary file. :param file: File or filename to which the data is saved. :param field_resolver: a map from field names defined in JSON, dict to the field names defined in Document. :param encoding: encoding used to read a CSV file. By default, ``utf-8`` is used. :return: a DocumentArray object """ from docarray.document.generators import from_csv return cls(from_csv(file, field_resolver=field_resolver, encoding=encoding))
from __future__ import annotations from pathlib import Path from unittest.mock import Mock, PropertyMock import pytest import torch from sentence_transformers import SentenceTransformer from sentence_transformers.evaluation import InformationRetrievalEvaluator from sentence_transformers.util import cos_sim @pytest.fixture def mock_model(): def mock_encode(sentences: str | list[str], **kwargs) -> torch.Tensor: """ We simply one-hot encode the sentences; if a sentence contains a keyword, the corresponding one-hot encoding is added to the sentence embedding. """ one_hot_encodings = { "pokemon": torch.tensor([1.0, 0.0, 0.0, 0.0, 0.0]), "car": torch.tensor([0.0, 1.0, 0.0, 0.0, 0.0]), "vehicle": torch.tensor([0.0, 0.0, 1.0, 0.0, 0.0]), "fruit": torch.tensor([0.0, 0.0, 0.0, 1.0, 0.0]), "vegetable": torch.tensor([0.0, 0.0, 0.0, 0.0, 1.0]), } if isinstance(sentences, str): sentences = [sentences] embeddings = [] for sentence in sentences: encoding = torch.zeros(5) for keyword, one_hot in one_hot_encodings.items(): if keyword in sentence: encoding += one_hot embeddings.append(encoding) return torch.stack(embeddings) model = Mock(spec=SentenceTransformer) model.similarity_fn_name = "cosine" model.similarity.side_effect = cos_sim model.encode.side_effect = mock_encode model.encode_query.side_effect = mock_encode model.encode_document.side_effect = mock_encode model.model_card_data = PropertyMock(return_value=Mock()) return model @pytest.fixture def test_data(): queries = { "0": "What is a pokemon?", "1": "What is a vegetable?", "2": "What is a fruit?", "3": "What is a vehicle?", "4": "What is a car?", } corpus = { "0": "A pokemon is a fictional creature", "1": "A vegetable is a plant", "2": "A fruit is a plant", "3": "A vehicle is a machine", "4": "A car is a vehicle", } relevant_docs = {"0": {"0"}, "1": {"1"}, "2": {"2"}, "3": {"3", "4"}, "4": {"4"}} return queries, corpus, relevant_docs def test_simple(test_data, tmp_path: Path): queries, corpus, relevant_docs = test_data model = SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-safetensors") ir_evaluator = InformationRetrievalEvaluator( queries=queries, corpus=corpus, relevant_docs=relevant_docs, name="test", accuracy_at_k=[1, 3], precision_recall_at_k=[1, 3], mrr_at_k=[3], ndcg_at_k=[3], map_at_k=[5], ) results = ir_evaluator(model, output_path=str(tmp_path)) expected_keys = [ "test_cosine_accuracy@1", "test_cosine_accuracy@3", "test_cosine_precision@1", "test_cosine_precision@3", "test_cosine_recall@1", "test_cosine_recall@3", "test_cosine_ndcg@3", "test_cosine_mrr@3", "test_cosine_map@5", ] assert set(results.keys()) == set(expected_keys) def test_metrices(test_data, mock_model, tmp_path: Path): queries, corpus, relevant_docs = test_data ir_evaluator = InformationRetrievalEvaluator( queries=queries, corpus=corpus, relevant_docs=relevant_docs, name="test", accuracy_at_k=[1, 3], precision_recall_at_k=[1, 3], mrr_at_k=[3], ndcg_at_k=[3], map_at_k=[5], ) results = ir_evaluator(mock_model, output_path=str(tmp_path)) # We expect test_cosine_precision@3 to be 0.4, since 6 out of 15 (5 queries * 3) are True Positives # We expect test_cosine_recall@1 to be 0.9; the average of 4 times a recall of 1 and once a recall of 0.5 expected_results = { "test_cosine_accuracy@1": 1.0, "test_cosine_accuracy@3": 1.0, "test_cosine_precision@1": 1.0, "test_cosine_precision@3": 0.4, "test_cosine_recall@1": 0.9, "test_cosine_recall@3": 1.0, "test_cosine_ndcg@3": 1.0, "test_cosine_mrr@3": 1.0, "test_cosine_map@5": 1.0, } for key, expected_value in expected_results.items(): assert results[key] == pytest.approx(expected_value, abs=1e-9)
from __future__ import annotations from pathlib import Path from unittest.mock import Mock, PropertyMock import pytest import torch from sentence_transformers import SentenceTransformer from sentence_transformers.evaluation import InformationRetrievalEvaluator from sentence_transformers.util import cos_sim @pytest.fixture def mock_model(): def mock_encode(sentences: str | list[str], **kwargs) -> torch.Tensor: """ We simply one-hot encode the sentences; if a sentence contains a keyword, the corresponding one-hot encoding is added to the sentence embedding. """ one_hot_encodings = { "pokemon": torch.tensor([1.0, 0.0, 0.0, 0.0, 0.0]), "car": torch.tensor([0.0, 1.0, 0.0, 0.0, 0.0]), "vehicle": torch.tensor([0.0, 0.0, 1.0, 0.0, 0.0]), "fruit": torch.tensor([0.0, 0.0, 0.0, 1.0, 0.0]), "vegetable": torch.tensor([0.0, 0.0, 0.0, 0.0, 1.0]), } if isinstance(sentences, str): sentences = [sentences] embeddings = [] for sentence in sentences: encoding = torch.zeros(5) for keyword, one_hot in one_hot_encodings.items(): if keyword in sentence: encoding += one_hot embeddings.append(encoding) return torch.stack(embeddings) model = Mock(spec=SentenceTransformer) model.similarity_fn_name = "cosine" model.similarity.side_effect = cos_sim model.encode.side_effect = mock_encode model.model_card_data = PropertyMock(return_value=Mock()) return model @pytest.fixture def test_data(): queries = { "0": "What is a pokemon?", "1": "What is a vegetable?", "2": "What is a fruit?", "3": "What is a vehicle?", "4": "What is a car?", } corpus = { "0": "A pokemon is a fictional creature", "1": "A vegetable is a plant", "2": "A fruit is a plant", "3": "A vehicle is a machine", "4": "A car is a vehicle", } relevant_docs = {"0": {"0"}, "1": {"1"}, "2": {"2"}, "3": {"3", "4"}, "4": {"4"}} return queries, corpus, relevant_docs def test_simple(test_data, tmp_path: Path): queries, corpus, relevant_docs = test_data model = SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-safetensors") ir_evaluator = InformationRetrievalEvaluator( queries=queries, corpus=corpus, relevant_docs=relevant_docs, name="test", accuracy_at_k=[1, 3], precision_recall_at_k=[1, 3], mrr_at_k=[3], ndcg_at_k=[3], map_at_k=[5], ) results = ir_evaluator(model, output_path=str(tmp_path)) expected_keys = [ "test_cosine_accuracy@1", "test_cosine_accuracy@3", "test_cosine_precision@1", "test_cosine_precision@3", "test_cosine_recall@1", "test_cosine_recall@3", "test_cosine_ndcg@3", "test_cosine_mrr@3", "test_cosine_map@5", ] assert set(results.keys()) == set(expected_keys) def test_metrices(test_data, mock_model, tmp_path: Path): queries, corpus, relevant_docs = test_data ir_evaluator = InformationRetrievalEvaluator( queries=queries, corpus=corpus, relevant_docs=relevant_docs, name="test", accuracy_at_k=[1, 3], precision_recall_at_k=[1, 3], mrr_at_k=[3], ndcg_at_k=[3], map_at_k=[5], ) results = ir_evaluator(mock_model, output_path=str(tmp_path)) # We expect test_cosine_precision@3 to be 0.4, since 6 out of 15 (5 queries * 3) are True Positives # We expect test_cosine_recall@1 to be 0.9; the average of 4 times a recall of 1 and once a recall of 0.5 expected_results = { "test_cosine_accuracy@1": 1.0, "test_cosine_accuracy@3": 1.0, "test_cosine_precision@1": 1.0, "test_cosine_precision@3": 0.4, "test_cosine_recall@1": 0.9, "test_cosine_recall@3": 1.0, "test_cosine_ndcg@3": 1.0, "test_cosine_mrr@3": 1.0, "test_cosine_map@5": 1.0, } for key, expected_value in expected_results.items(): assert results[key] == pytest.approx(expected_value, abs=1e-9)
""" This script contains an example how to perform semantic search with OpenSearch. You need OpenSearch up and running locally: https://docs.opensearch.org/docs/latest/getting-started/quickstart/ Further, you need the Python OpenSearch Client installed: https://docs.opensearch.org/docs/latest/clients/python-low-level/, e.g.: ``` pip install opensearch-py ``` This script was created for `opensearch` v2.15.0+. """ import time from datasets import load_dataset from sentence_transformers import SparseEncoder, models from sentence_transformers.sparse_encoder.models import IDF, MLMTransformer, SpladePooling from sentence_transformers.sparse_encoder.search_engines import semantic_search_opensearch # 1. Load the natural-questions dataset with 100K answers dataset = load_dataset("sentence-transformers/natural-questions", split="train") num_docs = 10_000 corpus = dataset["answer"][:num_docs] print(f"Finish loading data. Corpus size: {len(corpus)}") # 2. Come up with some queries queries = dataset["query"][:2] # 3. Load the model model_id = "opensearch-project/opensearch-neural-sparse-encoding-doc-v3-distill" doc_encoder = MLMTransformer(model_id) asym = models.Asym( { "query": [ IDF.from_json( model_id, tokenizer=doc_encoder.tokenizer, frozen=True, ), ], "doc": [ doc_encoder, SpladePooling("max", activation_function="log1p_relu"), ], } ) sparse_model = SparseEncoder( modules=[asym], similarity_fn_name="dot", ) print("Start encoding corpus...") start_time = time.time() # 4. Encode the corpus corpus_embeddings = sparse_model.encode( [{"doc": doc} for doc in corpus], convert_to_sparse_tensor=True, batch_size=32, show_progress_bar=True ) corpus_embeddings_decoded = sparse_model.decode(corpus_embeddings) print(f"Corpus encoding time: {time.time() - start_time:.6f} seconds") corpus_index = None while True: # 5. Encode the queries using inference-free mode start_time = time.time() query_embeddings = sparse_model.encode([{"query": query} for query in queries], convert_to_sparse_tensor=True) query_embeddings_decoded = sparse_model.decode(query_embeddings) print(f"Query encoding time: {time.time() - start_time:.6f} seconds") # 6. Perform semantic search using OpenSearch results, search_time, corpus_index = semantic_search_opensearch( query_embeddings_decoded, corpus_embeddings_decoded=corpus_embeddings_decoded if corpus_index is None else None, corpus_index=corpus_index, top_k=5, output_index=True, ) # 7. Output the results print(f"Search time: {search_time:.6f} seconds") for query, result in zip(queries, results): print(f"Query: {query}") for entry in result: print(f"(Score: {entry['score']:.4f}) {corpus[entry['corpus_id']]}, corpus_id: {entry['corpus_id']}") print("") # 8. Prompt for more queries queries = [input("Please enter a question: ")]
""" This script contains an example how to perform semantic search with OpenSearch. You need OpenSearch up and running locally: https://docs.opensearch.org/docs/latest/getting-started/quickstart/ Further, you need the Python OpenSearch Client installed: https://docs.opensearch.org/docs/latest/clients/python-low-level/, e.g.: ``` pip install opensearch-py ``` This script was created for `opensearch` v2.15.0+. """ import time from datasets import load_dataset from sentence_transformers import SparseEncoder, models from sentence_transformers.sparse_encoder.models import IDF, MLMTransformer, SpladePooling from sentence_transformers.sparse_encoder.search_engines import semantic_search_opensearch # 1. Load the natural-questions dataset with 100K answers dataset = load_dataset("sentence-transformers/natural-questions", split="train") num_docs = 10_000 corpus = dataset["answer"][:num_docs] print(f"Finish loading data. Corpus size: {len(corpus)}") # 2. Come up with some queries queries = dataset["query"][:2] # 3. Load the model model_id = "opensearch-project/opensearch-neural-sparse-encoding-doc-v3-distill" doc_encoder = MLMTransformer(model_id) asym = models.Asym( { "query": [ IDF.from_json( model_id, tokenizer=doc_encoder.tokenizer, frozen=True, ), ], "doc": [ doc_encoder, SpladePooling("max", activation_function="log1p_relu"), ], } ) sparse_model = SparseEncoder( modules=[asym], similarity_fn_name="dot", ) print("Start encoding corpus...") start_time = time.time() # 4. Encode the corpus corpus_embeddings = sparse_model.encode( [{"doc": doc} for doc in corpus], convert_to_sparse_tensor=True, batch_size=32, show_progress_bar=True ) corpus_embeddings_decoded = sparse_model.decode(corpus_embeddings) print(f"Corpus encoding time: {time.time() - start_time:.6f} seconds") corpus_index = None while True: # 5. Encode the queries using inference-free mode start_time = time.time() query_embeddings = sparse_model.encode([{"query": query} for query in queries], convert_to_sparse_tensor=True) query_embeddings_decoded = sparse_model.decode(query_embeddings) print(f"Query encoding time: {time.time() - start_time:.6f} seconds") # 6. Perform semantic search using OpenSearch results, search_time, corpus_index = semantic_search_opensearch( query_embeddings_decoded, corpus_index=corpus_index, corpus_embeddings_decoded=corpus_embeddings_decoded if corpus_index is None else None, top_k=5, output_index=True, ) # 7. Output the results print(f"Search time: {search_time:.6f} seconds") for query, result in zip(queries, results): print(f"Query: {query}") for entry in result: print(f"(Score: {entry['score']:.4f}) {corpus[entry['corpus_id']]}, corpus_id: {entry['corpus_id']}") print("") # 8. Prompt for more queries queries = [input("Please enter a question: ")]
_base_ = [ '../_base_/models/faster-rcnn_r50_fpn.py', '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ]
_base_ = [ '../_base_/models/faster_rcnn_r50_fpn.py', '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ]
import numpy as np import pytest from keras.src import testing from keras.src.layers.activations import softmax class SoftmaxTest(testing.TestCase): @pytest.mark.requires_trainable_backend def test_softmax(self): self.run_layer_test( softmax.Softmax, init_kwargs={}, input_shape=(2, 3, 4), supports_masking=True, assert_built_after_instantiation=True, ) def test_softmax_correctness(self): softmax_layer = softmax.Softmax() input = np.array([[1.0, 2.0, 1.0], [1.0, 2.0, 1.0]]) expected_output = np.array( [ [0.21194157, 0.5761169, 0.21194157], [0.21194157, 0.5761169, 0.21194157], ] ) result = softmax_layer(input) self.assertAllClose(result, expected_output) def test_softmax_correctness_with_mask(self): softmax_layer = softmax.Softmax(axis=(1, 0)) input = np.array([[1.0, 2.0, 1.0], [1.0, 2.0, 1.0]]) mask = np.array([[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]) expected_output = np.array( [[0.21194154, 0.0, 0.21194154], [0.0, 0.57611686, 0.0]] ) result = softmax_layer(input, mask=mask) self.assertAllClose(result, expected_output) def test_softmax_correctness_with_axis(self): softmax_layer = softmax.Softmax(axis=(1)) input = np.array([[1.0, 2.0, 1.0], [1.0, 2.0, 1.0]]) expected_output = np.array( [ [0.21194157, 0.5761169, 0.21194157], [0.21194157, 0.5761169, 0.21194157], ] ) result = softmax_layer(input) self.assertAllClose(result, expected_output)
import numpy as np import pytest from keras.src import testing from keras.src.layers.activations import softmax class SoftmaxTest(testing.TestCase): @pytest.mark.requires_trainable_backend def test_softmax(self): self.run_layer_test( softmax.Softmax, init_kwargs={}, input_shape=(2, 3, 4), supports_masking=True, ) def test_softmax_correctness(self): softmax_layer = softmax.Softmax() input = np.array([[1.0, 2.0, 1.0], [1.0, 2.0, 1.0]]) expected_output = np.array( [ [0.21194157, 0.5761169, 0.21194157], [0.21194157, 0.5761169, 0.21194157], ] ) result = softmax_layer(input) self.assertAllClose(result, expected_output) def test_softmax_correctness_with_mask(self): softmax_layer = softmax.Softmax(axis=(1, 0)) input = np.array([[1.0, 2.0, 1.0], [1.0, 2.0, 1.0]]) mask = np.array([[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]) expected_output = np.array( [[0.21194154, 0.0, 0.21194154], [0.0, 0.57611686, 0.0]] ) result = softmax_layer(input, mask=mask) self.assertAllClose(result, expected_output) def test_softmax_correctness_with_axis(self): softmax_layer = softmax.Softmax(axis=(1)) input = np.array([[1.0, 2.0, 1.0], [1.0, 2.0, 1.0]]) expected_output = np.array( [ [0.21194157, 0.5761169, 0.21194157], [0.21194157, 0.5761169, 0.21194157], ] ) result = softmax_layer(input) self.assertAllClose(result, expected_output)
import pytest from pydantic import Field from docarray import BaseDoc from docarray.index import ElasticV7DocIndex from tests.index.elastic.fixture import start_storage_v7 # noqa: F401 pytestmark = [pytest.mark.slow, pytest.mark.index] def test_column_config(): class MyDoc(BaseDoc): text: str color: str = Field(col_type='keyword') index = ElasticV7DocIndex[MyDoc]() index_docs = [ MyDoc(id='0', text='hello world', color='red'), MyDoc(id='1', text='never gonna give you up', color='blue'), MyDoc(id='2', text='we are the world', color='green'), ] index.index(index_docs) query = 'world' docs, _ = index.text_search(query, search_field='text') assert [doc.id for doc in docs] == ['0', '2'] filter_query = {'terms': {'color': ['red', 'blue']}} docs = index.filter(filter_query) assert [doc.id for doc in docs] == ['0', '1'] def test_field_object(): class MyDoc(BaseDoc): manager: dict = Field( properties={ 'age': {'type': 'integer'}, 'name': { 'properties': { 'first': {'type': 'keyword'}, 'last': {'type': 'keyword'}, } }, } ) index = ElasticV7DocIndex[MyDoc]() doc = [ MyDoc(manager={'age': 25, 'name': {'first': 'Rachel', 'last': 'Green'}}), MyDoc(manager={'age': 30, 'name': {'first': 'Monica', 'last': 'Geller'}}), MyDoc(manager={'age': 35, 'name': {'first': 'Phoebe', 'last': 'Buffay'}}), ] index.index(doc) id_ = doc[0].id assert index[id_].id == id_ assert index[id_].manager == doc[0].manager filter_query = {'range': {'manager.age': {'gte': 30}}} docs = index.filter(filter_query) assert [doc.id for doc in docs] == [doc[1].id, doc[2].id] def test_field_geo_point(): class MyDoc(BaseDoc): location: dict = Field(col_type='geo_point') index = ElasticV7DocIndex[MyDoc]() doc = [ MyDoc(location={'lat': 40.12, 'lon': -72.34}), MyDoc(location={'lat': 41.12, 'lon': -73.34}), MyDoc(location={'lat': 42.12, 'lon': -74.34}), ] index.index(doc) query = { 'query': { 'geo_bounding_box': { 'location': { 'top_left': {'lat': 42, 'lon': -74}, 'bottom_right': {'lat': 40, 'lon': -72}, } } }, } docs, _ = index.execute_query(query) assert [doc['id'] for doc in docs] == [doc[0].id, doc[1].id] def test_field_range(): class MyDoc(BaseDoc): expected_attendees: dict = Field(col_type='integer_range') time_frame: dict = Field(col_type='date_range', format='yyyy-MM-dd') index = ElasticV7DocIndex[MyDoc]() doc = [ MyDoc( expected_attendees={'gte': 10, 'lt': 20}, time_frame={'gte': '2023-01-01', 'lt': '2023-02-01'}, ), MyDoc( expected_attendees={'gte': 20, 'lt': 30}, time_frame={'gte': '2023-02-01', 'lt': '2023-03-01'}, ), MyDoc( expected_attendees={'gte': 30, 'lt': 40}, time_frame={'gte': '2023-03-01', 'lt': '2023-04-01'}, ), ] index.index(doc) query = { 'query': { 'bool': { 'should': [ {'term': {'expected_attendees': {'value': 15}}}, { 'range': { 'time_frame': { 'gte': '2023-02-05', 'lt': '2023-02-10', 'relation': 'contains', } } }, ] } }, } docs, _ = index.execute_query(query) assert [doc['id'] for doc in docs] == [doc[0].id, doc[1].id]
import pytest from pydantic import Field from docarray import BaseDoc from docarray.index import ElasticV7DocIndex from tests.index.elastic.fixture import start_storage_v7 # noqa: F401 pytestmark = [pytest.mark.slow, pytest.mark.index] def test_column_config(): class MyDoc(BaseDoc): text: str color: str = Field(col_type='keyword') store = ElasticV7DocIndex[MyDoc]() index_docs = [ MyDoc(id='0', text='hello world', color='red'), MyDoc(id='1', text='never gonna give you up', color='blue'), MyDoc(id='2', text='we are the world', color='green'), ] store.index(index_docs) query = 'world' docs, _ = store.text_search(query, search_field='text') assert [doc.id for doc in docs] == ['0', '2'] filter_query = {'terms': {'color': ['red', 'blue']}} docs = store.filter(filter_query) assert [doc.id for doc in docs] == ['0', '1'] def test_field_object(): class MyDoc(BaseDoc): manager: dict = Field( properties={ 'age': {'type': 'integer'}, 'name': { 'properties': { 'first': {'type': 'keyword'}, 'last': {'type': 'keyword'}, } }, } ) store = ElasticV7DocIndex[MyDoc]() doc = [ MyDoc(manager={'age': 25, 'name': {'first': 'Rachel', 'last': 'Green'}}), MyDoc(manager={'age': 30, 'name': {'first': 'Monica', 'last': 'Geller'}}), MyDoc(manager={'age': 35, 'name': {'first': 'Phoebe', 'last': 'Buffay'}}), ] store.index(doc) id_ = doc[0].id assert store[id_].id == id_ assert store[id_].manager == doc[0].manager filter_query = {'range': {'manager.age': {'gte': 30}}} docs = store.filter(filter_query) assert [doc.id for doc in docs] == [doc[1].id, doc[2].id] def test_field_geo_point(): class MyDoc(BaseDoc): location: dict = Field(col_type='geo_point') store = ElasticV7DocIndex[MyDoc]() doc = [ MyDoc(location={'lat': 40.12, 'lon': -72.34}), MyDoc(location={'lat': 41.12, 'lon': -73.34}), MyDoc(location={'lat': 42.12, 'lon': -74.34}), ] store.index(doc) query = { 'query': { 'geo_bounding_box': { 'location': { 'top_left': {'lat': 42, 'lon': -74}, 'bottom_right': {'lat': 40, 'lon': -72}, } } }, } docs, _ = store.execute_query(query) assert [doc['id'] for doc in docs] == [doc[0].id, doc[1].id] def test_field_range(): class MyDoc(BaseDoc): expected_attendees: dict = Field(col_type='integer_range') time_frame: dict = Field(col_type='date_range', format='yyyy-MM-dd') store = ElasticV7DocIndex[MyDoc]() doc = [ MyDoc( expected_attendees={'gte': 10, 'lt': 20}, time_frame={'gte': '2023-01-01', 'lt': '2023-02-01'}, ), MyDoc( expected_attendees={'gte': 20, 'lt': 30}, time_frame={'gte': '2023-02-01', 'lt': '2023-03-01'}, ), MyDoc( expected_attendees={'gte': 30, 'lt': 40}, time_frame={'gte': '2023-03-01', 'lt': '2023-04-01'}, ), ] store.index(doc) query = { 'query': { 'bool': { 'should': [ {'term': {'expected_attendees': {'value': 15}}}, { 'range': { 'time_frame': { 'gte': '2023-02-05', 'lt': '2023-02-10', 'relation': 'contains', } } }, ] } }, } docs, _ = store.execute_query(query) assert [doc['id'] for doc in docs] == [doc[0].id, doc[1].id]
import logging import random from datasets import load_dataset from sentence_transformers.sparse_encoder import ( MLMTransformer, SparseEncoder, SparseInformationRetrievalEvaluator, SpladePooling, ) logging.basicConfig(format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO) # Initialize the SPLADE model model_name = "naver/splade-cocondenser-ensembledistil" model = SparseEncoder( modules=[ MLMTransformer(model_name), SpladePooling(pooling_strategy="max"), # You can also use 'sum' ], device="cuda:0", ) # Load the Touche-2020 IR dataset (https://huggingface.co/datasets/BeIR/webis-touche2020, https://huggingface.co/datasets/BeIR/webis-touche2020-qrels) corpus = load_dataset("BeIR/webis-touche2020", "corpus", split="corpus") queries = load_dataset("BeIR/webis-touche2020", "queries", split="queries") relevant_docs_data = load_dataset("BeIR/webis-touche2020-qrels", split="test") # For this dataset, we want to concatenate the title and texts for the corpus corpus = corpus.map(lambda x: {"text": x["title"] + " " + x["text"]}, remove_columns=["title"]) # Shrink the corpus size heavily to only the relevant documents + 30,000 random documents required_corpus_ids = set(map(str, relevant_docs_data["corpus-id"])) required_corpus_ids |= set(random.sample(corpus["_id"], k=30_000)) corpus = corpus.filter(lambda x: x["_id"] in required_corpus_ids) # Convert the datasets to dictionaries corpus = dict(zip(corpus["_id"], corpus["text"])) # Our corpus (cid => document) queries = dict(zip(queries["_id"], queries["text"])) # Our queries (qid => question) relevant_docs = {} # Query ID to relevant documents (qid => set([relevant_cids]) for qid, corpus_ids in zip(relevant_docs_data["query-id"], relevant_docs_data["corpus-id"]): qid = str(qid) corpus_ids = str(corpus_ids) if qid not in relevant_docs: relevant_docs[qid] = set() relevant_docs[qid].add(corpus_ids) # Given queries, a corpus and a mapping with relevant documents, the SparseInformationRetrievalEvaluator computes different IR metrics. ir_evaluator = SparseInformationRetrievalEvaluator( queries=queries, corpus=corpus, relevant_docs=relevant_docs, name="BeIR-touche2020-subset-test", show_progress_bar=True, batch_size=16, ) # Run evaluation results = ir_evaluator(model) # Print the results print(f"Primary metric: {ir_evaluator.primary_metric}") print(f"Primary metric value: {results[ir_evaluator.primary_metric]:.4f}")
import random from datasets import load_dataset from sentence_transformers.sparse_encoder import ( MLMTransformer, SparseEncoder, SparseInformationRetrievalEvaluator, SpladePooling, ) # Initialize the SPLADE model model_name = "naver/splade-cocondenser-ensembledistil" model = SparseEncoder( modules=[ MLMTransformer(model_name), SpladePooling(pooling_strategy="max"), # You can also use 'sum' ], device="cuda:0", ) # Load the Touche-2020 IR dataset (https://huggingface.co/datasets/BeIR/webis-touche2020, https://huggingface.co/datasets/BeIR/webis-touche2020-qrels) corpus = load_dataset("BeIR/webis-touche2020", "corpus", split="corpus") queries = load_dataset("BeIR/webis-touche2020", "queries", split="queries") relevant_docs_data = load_dataset("BeIR/webis-touche2020-qrels", split="test") # For this dataset, we want to concatenate the title and texts for the corpus corpus = corpus.map(lambda x: {"text": x["title"] + " " + x["text"]}, remove_columns=["title"]) # Shrink the corpus size heavily to only the relevant documents + 30,000 random documents required_corpus_ids = set(map(str, relevant_docs_data["corpus-id"])) required_corpus_ids |= set(random.sample(corpus["_id"], k=30_000)) corpus = corpus.filter(lambda x: x["_id"] in required_corpus_ids) # Convert the datasets to dictionaries corpus = dict(zip(corpus["_id"], corpus["text"])) # Our corpus (cid => document) queries = dict(zip(queries["_id"], queries["text"])) # Our queries (qid => question) relevant_docs = {} # Query ID to relevant documents (qid => set([relevant_cids]) for qid, corpus_ids in zip(relevant_docs_data["query-id"], relevant_docs_data["corpus-id"]): qid = str(qid) corpus_ids = str(corpus_ids) if qid not in relevant_docs: relevant_docs[qid] = set() relevant_docs[qid].add(corpus_ids) # Given queries, a corpus and a mapping with relevant documents, the SparseInformationRetrievalEvaluator computes different IR metrics. ir_evaluator = SparseInformationRetrievalEvaluator( queries=queries, corpus=corpus, relevant_docs=relevant_docs, name="BeIR-touche2020-subset-test", show_progress_bar=True, batch_size=32, ) # Run evaluation print("Starting evaluation ") results = ir_evaluator(model) print(f"Primary metric: {ir_evaluator.primary_metric}") print(f"Primary metric value: {results[ir_evaluator.primary_metric]:.4f}") # Print results for each dataset for key, value in results.items(): if key.startswith("Nano"): print(f"{key}: {value:.4f}")
import warnings from typing import Optional, Tuple, TypeVar from docarray.typing import AudioNdArray from docarray.typing.bytes.audio_bytes import AudioBytes from docarray.typing.proto_register import _register_proto from docarray.typing.url.any_url import AnyUrl from docarray.utils._internal.misc import is_notebook T = TypeVar('T', bound='AudioUrl') @_register_proto(proto_type_name='audio_url') class AudioUrl(AnyUrl): """ URL to an audio file. Can be remote (web) URL, or a local file path. """ def load(self: T) -> Tuple[AudioNdArray, int]: """ Load the data from the url into an AudioNdArray and the frame rate. --- ```python from typing import Optional from docarray import BaseDoc from docarray.typing import AudioNdArray, AudioUrl class MyDoc(BaseDoc): audio_url: AudioUrl audio_tensor: Optional[AudioNdArray] doc = MyDoc(audio_url='https://www.kozco.com/tech/piano2.wav') doc.audio_tensor, _ = doc.audio_url.load() assert isinstance(doc.audio_tensor, AudioNdArray) ``` --- :return: tuple of an AudioNdArray representing the Audio file content, and an integer representing the frame rate. """ bytes_ = self.load_bytes() return bytes_.load() def load_bytes(self, timeout: Optional[float] = None) -> AudioBytes: """ Convert url to AudioBytes. This will either load or download the file and save it into an AudioBytes object. :param timeout: timeout for urlopen. Only relevant if url is not local :return: AudioBytes object """ bytes_ = super().load_bytes(timeout=timeout) return AudioBytes(bytes_) def display(self): """ Play the audio sound from url in notebook. """ if is_notebook(): from IPython.display import Audio, display remote_url = True if self.startswith('http') else False if remote_url: display(Audio(data=self)) else: display(Audio(filename=self)) else: warnings.warn('Display of image is only possible in a notebook.')
import warnings from typing import TYPE_CHECKING, Any, Optional, Tuple, Type, TypeVar, Union from docarray.typing import AudioNdArray from docarray.typing.bytes.audio_bytes import AudioBytes from docarray.typing.proto_register import _register_proto from docarray.typing.url.any_url import AnyUrl from docarray.typing.url.filetypes import AUDIO_FILE_FORMATS from docarray.utils._internal.misc import is_notebook if TYPE_CHECKING: from pydantic import BaseConfig from pydantic.fields import ModelField T = TypeVar('T', bound='AudioUrl') @_register_proto(proto_type_name='audio_url') class AudioUrl(AnyUrl): """ URL to a audio file. Can be remote (web) URL, or a local file path. """ @classmethod def validate( cls: Type[T], value: Union[T, str, Any], field: 'ModelField', config: 'BaseConfig', ) -> T: import os from urllib.parse import urlparse url = super().validate(value, field, config) # basic url validation path = urlparse(url).path ext = os.path.splitext(path)[1][1:].lower() # pass test if extension is valid or no extension has_audio_extension = ext in AUDIO_FILE_FORMATS or ext == '' if not has_audio_extension: raise ValueError('Audio URL must have a valid extension') return cls(str(url), scheme=None) def load(self: T) -> Tuple[AudioNdArray, int]: """ Load the data from the url into an AudioNdArray and the frame rate. --- ```python from typing import Optional from docarray import BaseDoc from docarray.typing import AudioNdArray, AudioUrl class MyDoc(BaseDoc): audio_url: AudioUrl audio_tensor: Optional[AudioNdArray] doc = MyDoc(audio_url='https://www.kozco.com/tech/piano2.wav') doc.audio_tensor, _ = doc.audio_url.load() assert isinstance(doc.audio_tensor, AudioNdArray) ``` --- :return: tuple of an AudioNdArray representing the Audio file content, and an integer representing the frame rate. """ bytes_ = self.load_bytes() return bytes_.load() def load_bytes(self, timeout: Optional[float] = None) -> AudioBytes: """ Convert url to AudioBytes. This will either load or download the file and save it into an AudioBytes object. :param timeout: timeout for urlopen. Only relevant if url is not local :return: AudioBytes object """ bytes_ = super().load_bytes(timeout=timeout) return AudioBytes(bytes_) def display(self): """ Play the audio sound from url in notebook. """ if is_notebook(): from IPython.display import Audio, display remote_url = True if self.startswith('http') else False if remote_url: display(Audio(data=self)) else: display(Audio(filename=self)) else: warnings.warn('Display of image is only possible in a notebook.')
from datetime import datetime, timedelta from langchain_core.exceptions import OutputParserException from langchain_core.output_parsers import BaseOutputParser from langchain_core.utils import comma_list class DatetimeOutputParser(BaseOutputParser[datetime]): """Parse the output of an LLM call to a datetime.""" format: str = "%Y-%m-%dT%H:%M:%S.%fZ" """The string value that is used as the datetime format. Update this to match the desired datetime format for your application. """ def get_format_instructions(self) -> str: """Returns the format instructions for the given format.""" if self.format == "%Y-%m-%dT%H:%M:%S.%fZ": examples = comma_list( [ "2023-07-04T14:30:00.000000Z", "1999-12-31T23:59:59.999999Z", "2025-01-01T00:00:00.000000Z", ] ) else: try: now = datetime.now() examples = comma_list( [ now.strftime(self.format), (now.replace(year=now.year - 1)).strftime(self.format), (now - timedelta(days=1)).strftime(self.format), ] ) except ValueError: # Fallback if the format is very unusual examples = f"e.g., a valid string in the format {self.format}" return ( f"Write a datetime string that matches the " f"following pattern: '{self.format}'.\n\n" f"Examples: {examples}\n\n" f"Return ONLY this string, no other words!" ) def parse(self, response: str) -> datetime: """Parse a string into a datetime object.""" try: return datetime.strptime(response.strip(), self.format) except ValueError as e: msg = f"Could not parse datetime string: {response}" raise OutputParserException(msg) from e @property def _type(self) -> str: return "datetime"
from datetime import datetime, timedelta from langchain_core.exceptions import OutputParserException from langchain_core.output_parsers import BaseOutputParser from langchain_core.utils import comma_list class DatetimeOutputParser(BaseOutputParser[datetime]): """Parse the output of an LLM call to a datetime.""" format: str = "%Y-%m-%dT%H:%M:%S.%fZ" """The string value that is used as the datetime format. Update this to match the desired datetime format for your application. """ def get_format_instructions(self) -> str: """Returns the format instructions for the given format.""" if self.format == "%Y-%m-%dT%H:%M:%S.%fZ": examples = comma_list( [ "2023-07-04T14:30:00.000000Z", "1999-12-31T23:59:59.999999Z", "2025-01-01T00:00:00.000000Z", ] ) else: try: now = datetime.now() examples = comma_list( [ now.strftime(self.format), (now.replace(year=now.year - 1)).strftime(self.format), (now - timedelta(days=1)).strftime(self.format), ] ) except ValueError: # Fallback if the format is very unusual examples = f"e.g., a valid string in the format {self.format}" return ( f"Write a datetime string that matches the " f"following pattern: '{self.format}'.\n\n" f"Examples: {examples}\n\n" f"Return ONLY this string, no other words!" ) def parse(self, response: str) -> datetime: """Parse a string into a datetime object.""" try: return datetime.strptime(response.strip(), self.format) except ValueError as e: raise OutputParserException( f"Could not parse datetime string: {response}" ) from e @property def _type(self) -> str: return "datetime"
# Copyright (c) OpenMMLab. All rights reserved. from mmdet.registry import MODELS from mmdet.utils import ConfigType, OptConfigType, OptMultiConfig from .single_stage import SingleStageDetector @MODELS.register_module() class FSAF(SingleStageDetector): """Implementation of `FSAF <https://arxiv.org/abs/1903.00621>`_""" def __init__(self, backbone: ConfigType, neck: ConfigType, bbox_head: ConfigType, train_cfg: OptConfigType = None, test_cfg: OptConfigType = None, data_preprocessor: OptConfigType = None, init_cfg: OptMultiConfig = None): super().__init__( backbone=backbone, neck=neck, bbox_head=bbox_head, train_cfg=train_cfg, test_cfg=test_cfg, data_preprocessor=data_preprocessor, init_cfg=init_cfg)
# Copyright (c) OpenMMLab. All rights reserved. from mmdet.core import ConfigType, OptConfigType, OptMultiConfig from mmdet.registry import MODELS from .single_stage import SingleStageDetector @MODELS.register_module() class FSAF(SingleStageDetector): """Implementation of `FSAF <https://arxiv.org/abs/1903.00621>`_""" def __init__(self, backbone: ConfigType, neck: ConfigType, bbox_head: ConfigType, train_cfg: OptConfigType = None, test_cfg: OptConfigType = None, data_preprocessor: OptConfigType = None, init_cfg: OptMultiConfig = None): super().__init__( backbone=backbone, neck=neck, bbox_head=bbox_head, train_cfg=train_cfg, test_cfg=test_cfg, data_preprocessor=data_preprocessor, init_cfg=init_cfg)
# coding: utf-8 """Script for generating files with NuGet package metadata.""" import datetime import sys from pathlib import Path from shutil import copyfile if __name__ == "__main__": source = Path(sys.argv[1]) current_dir = Path(__file__).absolute().parent linux_folder_path = current_dir / "runtimes" / "linux-x64" / "native" linux_folder_path.mkdir(parents=True, exist_ok=True) osx_folder_path = current_dir / "runtimes" / "osx-x64" / "native" osx_folder_path.mkdir(parents=True, exist_ok=True) windows_folder_path = current_dir / "runtimes" / "win-x64" / "native" windows_folder_path.mkdir(parents=True, exist_ok=True) build_folder_path = current_dir / "build" build_folder_path.mkdir(parents=True, exist_ok=True) copyfile(source / "lib_lightgbm.so", linux_folder_path / "lib_lightgbm.so") copyfile(source / "lib_lightgbm.dylib", osx_folder_path / "lib_lightgbm.dylib") copyfile(source / "lib_lightgbm.dll", windows_folder_path / "lib_lightgbm.dll") copyfile(source / "lightgbm.exe", windows_folder_path / "lightgbm.exe") version = (current_dir.parent / 'VERSION.txt').read_text(encoding='utf-8').strip().replace('rc', '-rc') nuget_str = rf"""<?xml version="1.0"?> <package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"> <metadata> <id>LightGBM</id> <version>{version}</version> <authors>Guolin Ke</authors> <owners>Guolin Ke</owners> <license type="expression">MIT</license> <projectUrl>https://github.com/microsoft/LightGBM</projectUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>A fast, distributed, high performance gradient boosting framework</description> <copyright>Copyright {datetime.datetime.now().year} @ Microsoft</copyright> <tags>machine-learning data-mining distributed native boosting gbdt</tags> <dependencies> </dependencies> </metadata> <files> <file src="build\**" target="build"/> <file src="runtimes\**" target="runtimes"/> </files> </package> """ prop_str = r""" <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup Condition="Exists('packages.config') OR Exists('$(MSBuildProjectName).packages.config') OR Exists('packages.$(MSBuildProjectName).config')"> <Content Include="$(MSBuildThisFileDirectory)/../runtimes/win-x64/native/*.dll" Condition="'$(PlatformTarget)' == 'x64'"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Visible>false</Visible> </Content> <Content Include="$(MSBuildThisFileDirectory)/../runtimes/win-x64/native/*.exe" Condition="'$(PlatformTarget)' == 'x64'"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Visible>false</Visible> </Content> </ItemGroup> </Project> """ target_str = r""" <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <EnableLightGBMUnsupportedPlatformTargetCheck Condition="'$(EnableLightGBMUnsupportedPlatformTargetCheck)' == ''">true</EnableLightGBMUnsupportedPlatformTargetCheck> </PropertyGroup> <Target Name="_LightGBMCheckForUnsupportedPlatformTarget" Condition="'$(EnableLightGBMUnsupportedPlatformTargetCheck)' == 'true'" AfterTargets="_CheckForInvalidConfigurationAndPlatform"> <Error Condition="'$(PlatformTarget)' != 'x64' AND ('$(OutputType)' == 'Exe' OR '$(OutputType)'=='WinExe') AND !('$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND '$(PlatformTarget)' == '')" Text="LightGBM currently supports 'x64' processor architectures. Please ensure your application is targeting 'x64'." /> </Target> </Project> """ (current_dir / "LightGBM.nuspec").write_text(nuget_str, encoding='utf-8') (current_dir / "build" / "LightGBM.props").write_text(prop_str, encoding='utf-8') (current_dir / "build" / "LightGBM.targets").write_text(target_str, encoding='utf-8')
# coding: utf-8 """Script for generating files with NuGet package metadata.""" import datetime import sys from pathlib import Path from shutil import copyfile if __name__ == "__main__": source = Path(sys.argv[1]) current_dir = Path(__file__).absolute().parent linux_folder_path = current_dir / "runtimes" / "linux-x64" / "native" linux_folder_path.mkdir(parents=True, exist_ok=True) osx_folder_path = current_dir / "runtimes" / "osx-x64" / "native" osx_folder_path.mkdir(parents=True, exist_ok=True) windows_folder_path = current_dir / "runtimes" / "win-x64" / "native" windows_folder_path.mkdir(parents=True, exist_ok=True) build_folder_path = current_dir / "build" build_folder_path.mkdir(parents=True, exist_ok=True) copyfile(source / "lib_lightgbm.so", linux_folder_path / "lib_lightgbm.so") copyfile(source / "lib_lightgbm.dylib", osx_folder_path / "lib_lightgbm.dylib") copyfile(source / "lib_lightgbm.dll", windows_folder_path / "lib_lightgbm.dll") copyfile(source / "lightgbm.exe", windows_folder_path / "lightgbm.exe") version = (current_dir.parent / 'VERSION.txt').read_text(encoding='utf-8').strip().replace('rc', '-rc') nuget_str = rf"""<?xml version="1.0"?> <package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"> <metadata> <id>LightGBM</id> <version>{version}</version> <authors>Guolin Ke</authors> <owners>Guolin Ke</owners> <licenseUrl>https://github.com/microsoft/LightGBM/blob/master/LICENSE</licenseUrl> <projectUrl>https://github.com/microsoft/LightGBM</projectUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>A fast, distributed, high performance gradient boosting framework</description> <copyright>Copyright {datetime.datetime.now().year} @ Microsoft</copyright> <tags>machine-learning data-mining distributed native boosting gbdt</tags> <dependencies> </dependencies> </metadata> <files> <file src="build\**" target="build"/> <file src="runtimes\**" target="runtimes"/> </files> </package> """ prop_str = r""" <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup Condition="Exists('packages.config') OR Exists('$(MSBuildProjectName).packages.config') OR Exists('packages.$(MSBuildProjectName).config')"> <Content Include="$(MSBuildThisFileDirectory)/../runtimes/win-x64/native/*.dll" Condition="'$(PlatformTarget)' == 'x64'"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Visible>false</Visible> </Content> <Content Include="$(MSBuildThisFileDirectory)/../runtimes/win-x64/native/*.exe" Condition="'$(PlatformTarget)' == 'x64'"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Visible>false</Visible> </Content> </ItemGroup> </Project> """ target_str = r""" <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <EnableLightGBMUnsupportedPlatformTargetCheck Condition="'$(EnableLightGBMUnsupportedPlatformTargetCheck)' == ''">true</EnableLightGBMUnsupportedPlatformTargetCheck> </PropertyGroup> <Target Name="_LightGBMCheckForUnsupportedPlatformTarget" Condition="'$(EnableLightGBMUnsupportedPlatformTargetCheck)' == 'true'" AfterTargets="_CheckForInvalidConfigurationAndPlatform"> <Error Condition="'$(PlatformTarget)' != 'x64' AND ('$(OutputType)' == 'Exe' OR '$(OutputType)'=='WinExe') AND !('$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND '$(PlatformTarget)' == '')" Text="LightGBM currently supports 'x64' processor architectures. Please ensure your application is targeting 'x64'." /> </Target> </Project> """ (current_dir / "LightGBM.nuspec").write_text(nuget_str, encoding='utf-8') (current_dir / "build" / "LightGBM.props").write_text(prop_str, encoding='utf-8') (current_dir / "build" / "LightGBM.targets").write_text(target_str, encoding='utf-8')
"""Module to test base parser implementations.""" from typing_extensions import override from langchain_core.exceptions import OutputParserException from langchain_core.language_models import GenericFakeChatModel from langchain_core.messages import AIMessage from langchain_core.output_parsers import ( BaseGenerationOutputParser, BaseTransformOutputParser, ) from langchain_core.outputs import ChatGeneration, Generation def test_base_generation_parser() -> None: """Test Base Generation Output Parser.""" class StrInvertCase(BaseGenerationOutputParser[str]): """An example parser that inverts the case of the characters in the message.""" @override def parse_result( self, result: list[Generation], *, partial: bool = False ) -> str: """Parse a list of model Generations into a specific format. Args: result: A list of Generations to be parsed. The Generations are assumed to be different candidate outputs for a single model input. Many parsers assume that only a single generation is passed it in. We will assert for that partial: Whether to allow partial results. This is used for parsers that support streaming """ if len(result) != 1: msg = "This output parser can only be used with a single generation." raise NotImplementedError(msg) generation = result[0] if not isinstance(generation, ChatGeneration): # Say that this one only works with chat generations msg = "This output parser can only be used with a chat generation." raise OutputParserException(msg) content = generation.message.content assert isinstance(content, str) return content.swapcase() StrInvertCase.model_rebuild() model = GenericFakeChatModel(messages=iter([AIMessage(content="hEllo")])) chain = model | StrInvertCase() assert chain.invoke("") == "HeLLO" def test_base_transform_output_parser() -> None: """Test base transform output parser.""" class StrInvertCase(BaseTransformOutputParser[str]): """An example parser that inverts the case of the characters in the message.""" def parse(self, text: str) -> str: """Parse a single string into a specific format.""" raise NotImplementedError @override def parse_result( self, result: list[Generation], *, partial: bool = False ) -> str: """Parse a list of model Generations into a specific format. Args: result: A list of Generations to be parsed. The Generations are assumed to be different candidate outputs for a single model input. Many parsers assume that only a single generation is passed it in. We will assert for that partial: Whether to allow partial results. This is used for parsers that support streaming """ if len(result) != 1: msg = "This output parser can only be used with a single generation." raise NotImplementedError(msg) generation = result[0] if not isinstance(generation, ChatGeneration): # Say that this one only works with chat generations msg = "This output parser can only be used with a chat generation." raise OutputParserException(msg) content = generation.message.content assert isinstance(content, str) return content.swapcase() model = GenericFakeChatModel(messages=iter([AIMessage(content="hello world")])) chain = model | StrInvertCase() # inputs to models are ignored, response is hard-coded in model definition chunks = list(chain.stream("")) assert chunks == ["HELLO", " ", "WORLD"]
"""Module to test base parser implementations.""" from typing_extensions import override from langchain_core.exceptions import OutputParserException from langchain_core.language_models import GenericFakeChatModel from langchain_core.messages import AIMessage from langchain_core.output_parsers import ( BaseGenerationOutputParser, BaseTransformOutputParser, ) from langchain_core.outputs import ChatGeneration, Generation def test_base_generation_parser() -> None: """Test Base Generation Output Parser.""" class StrInvertCase(BaseGenerationOutputParser[str]): """An example parser that inverts the case of the characters in the message.""" @override def parse_result( self, result: list[Generation], *, partial: bool = False ) -> str: """Parse a list of model Generations into a specific format. Args: result: A list of Generations to be parsed. The Generations are assumed to be different candidate outputs for a single model input. Many parsers assume that only a single generation is passed it in. We will assert for that partial: Whether to allow partial results. This is used for parsers that support streaming """ if len(result) != 1: msg = "This output parser can only be used with a single generation." raise NotImplementedError(msg) generation = result[0] if not isinstance(generation, ChatGeneration): # Say that this one only works with chat generations msg = "This output parser can only be used with a chat generation." raise OutputParserException(msg) content = generation.message.content assert isinstance(content, str) return content.swapcase() # type: ignore StrInvertCase.model_rebuild() model = GenericFakeChatModel(messages=iter([AIMessage(content="hEllo")])) chain = model | StrInvertCase() assert chain.invoke("") == "HeLLO" def test_base_transform_output_parser() -> None: """Test base transform output parser.""" class StrInvertCase(BaseTransformOutputParser[str]): """An example parser that inverts the case of the characters in the message.""" def parse(self, text: str) -> str: """Parse a single string into a specific format.""" raise NotImplementedError @override def parse_result( self, result: list[Generation], *, partial: bool = False ) -> str: """Parse a list of model Generations into a specific format. Args: result: A list of Generations to be parsed. The Generations are assumed to be different candidate outputs for a single model input. Many parsers assume that only a single generation is passed it in. We will assert for that partial: Whether to allow partial results. This is used for parsers that support streaming """ if len(result) != 1: msg = "This output parser can only be used with a single generation." raise NotImplementedError(msg) generation = result[0] if not isinstance(generation, ChatGeneration): # Say that this one only works with chat generations msg = "This output parser can only be used with a chat generation." raise OutputParserException(msg) content = generation.message.content assert isinstance(content, str) return content.swapcase() # type: ignore model = GenericFakeChatModel(messages=iter([AIMessage(content="hello world")])) chain = model | StrInvertCase() # inputs to models are ignored, response is hard-coded in model definition chunks = list(chain.stream("")) assert chunks == ["HELLO", " ", "WORLD"]
"""Interface for tools.""" from typing import Optional from langchain_core.callbacks import ( AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) from langchain_core.tools import BaseTool, tool class InvalidTool(BaseTool): """Tool that is run when invalid tool name is encountered by agent.""" name: str = "invalid_tool" """Name of the tool.""" description: str = "Called when tool name is invalid. Suggests valid tool names." """Description of the tool.""" def _run( self, requested_tool_name: str, available_tool_names: list[str], run_manager: Optional[CallbackManagerForToolRun] = None, ) -> str: """Use the tool.""" available_tool_names_str = ", ".join([tool for tool in available_tool_names]) return ( f"{requested_tool_name} is not a valid tool, " f"try one of [{available_tool_names_str}]." ) async def _arun( self, requested_tool_name: str, available_tool_names: list[str], run_manager: Optional[AsyncCallbackManagerForToolRun] = None, ) -> str: """Use the tool asynchronously.""" available_tool_names_str = ", ".join([tool for tool in available_tool_names]) return ( f"{requested_tool_name} is not a valid tool, " f"try one of [{available_tool_names_str}]." ) __all__ = ["InvalidTool", "tool"]
"""Interface for tools.""" from typing import Optional from langchain_core.callbacks import ( AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) from langchain_core.tools import BaseTool, tool class InvalidTool(BaseTool): # type: ignore[override] """Tool that is run when invalid tool name is encountered by agent.""" name: str = "invalid_tool" """Name of the tool.""" description: str = "Called when tool name is invalid. Suggests valid tool names." """Description of the tool.""" def _run( self, requested_tool_name: str, available_tool_names: list[str], run_manager: Optional[CallbackManagerForToolRun] = None, ) -> str: """Use the tool.""" available_tool_names_str = ", ".join([tool for tool in available_tool_names]) return ( f"{requested_tool_name} is not a valid tool, " f"try one of [{available_tool_names_str}]." ) async def _arun( self, requested_tool_name: str, available_tool_names: list[str], run_manager: Optional[AsyncCallbackManagerForToolRun] = None, ) -> str: """Use the tool asynchronously.""" available_tool_names_str = ", ".join([tool for tool in available_tool_names]) return ( f"{requested_tool_name} is not a valid tool, " f"try one of [{available_tool_names_str}]." ) __all__ = ["InvalidTool", "tool"]
# Copyright (c) OpenMMLab. All rights reserved. from typing import Any, List, Optional, Tuple, Type, Union import cv2 import matplotlib import numpy as np import torch def tensor2ndarray(value: Union[np.ndarray, torch.Tensor]) -> np.ndarray: """If the type of value is torch.Tensor, convert the value to np.ndarray. Args: value (np.ndarray, torch.Tensor): value. Returns: Any: value. """ if isinstance(value, torch.Tensor): value = value.detach().cpu().numpy() return value def value2list(value: Any, valid_type: Union[Type, Tuple[Type, ...]], expand_dim: int) -> List[Any]: """If the type of ``value`` is ``valid_type``, convert the value to list and expand to ``expand_dim``. Args: value (Any): value. valid_type (Union[Type, Tuple[Type, ...]): valid type. expand_dim (int): expand dim. Returns: List[Any]: value. """ if isinstance(value, valid_type): value = [value] * expand_dim return value def check_type(name: str, value: Any, valid_type: Union[Type, Tuple[Type, ...]]) -> None: """Check whether the type of value is in ``valid_type``. Args: name (str): value name. value (Any): value. valid_type (Type, Tuple[Type, ...]): expected type. """ if not isinstance(value, valid_type): raise TypeError(f'`{name}` should be {valid_type} ' f' but got {type(value)}') def check_length(name: str, value: Any, valid_length: int) -> None: """If type of the ``value`` is list, check whether its length is equal with or greater than ``valid_length``. Args: name (str): value name. value (Any): value. valid_length (int): expected length. """ if isinstance(value, list): if len(value) < valid_length: raise AssertionError( f'The length of {name} must equal with or ' f'greater than {valid_length}, but got {len(value)}') def check_type_and_length(name: str, value: Any, valid_type: Union[Type, Tuple[Type, ...]], valid_length: int) -> None: """Check whether the type of value is in ``valid_type``. If type of the ``value`` is list, check whether its length is equal with or greater than ``valid_length``. Args: value (Any): value. legal_type (Type, Tuple[Type, ...]): legal type. valid_length (int): expected length. Returns: List[Any]: value. """ check_type(name, value, valid_type) check_length(name, value, valid_length) def color_val_matplotlib(colors): """Convert various input in RGB order to normalized RGB matplotlib color tuples, Args: color (:obj:`mmcv.Color`/str/tuple/int/ndarray): Color inputs Returns: tuple[float]: A tuple of 3 normalized floats indicating RGB channels. """ if isinstance(colors, str): return colors elif isinstance(colors, tuple): assert len(colors) == 3 for channel in colors: assert 0 <= channel <= 255 colors = [channel / 255 for channel in colors] return tuple(colors) elif isinstance(colors, list): colors = [color_val_matplotlib(color) for color in colors] return colors else: raise TypeError(f'Invalid type for color: {type(colors)}') def str_color_to_rgb(color): color = matplotlib.colors.to_rgb(color) color = tuple([int(c * 255) for c in color]) return color def convert_overlay_heatmap(feat_map: Union[np.ndarray, torch.Tensor], img: Optional[np.ndarray] = None, alpha: float = 0.5) -> np.ndarray: """Convert feat_map to heatmap and overlay on image, if image is not None. Args: feat_map (np.ndarray, torch.Tensor): The feat_map to convert with of shape (H, W), where H is the image height and W is the image width. img (np.ndarray, optional): The origin image. The format should be RGB. Defaults to None. alpha (float): The transparency of origin image. Defaults to 0.5. Returns: np.ndarray: heatmap """ if isinstance(feat_map, torch.Tensor): feat_map = feat_map.detach().cpu().numpy() norm_img = np.zeros(feat_map.shape) norm_img = cv2.normalize(feat_map, norm_img, 0, 255, cv2.NORM_MINMAX) norm_img = np.asarray(norm_img, dtype=np.uint8) heat_img = cv2.applyColorMap(norm_img, cv2.COLORMAP_JET) heat_img = cv2.cvtColor(heat_img, cv2.COLOR_BGR2RGB) if img is not None: heat_img = cv2.addWeighted(img, alpha, heat_img, 1 - alpha, 0) return heat_img
# Copyright (c) OpenMMLab. All rights reserved. from typing import Any, List, Tuple, Type, Union import numpy as np import torch def tensor2ndarray(value: Union[np.ndarray, torch.Tensor]) -> np.ndarray: """If the type of value is torch.Tensor, convert the value to np.ndarray. Args: value (np.ndarray, torch.Tensor): value. Returns: Any: value. """ if isinstance(value, torch.Tensor): value = value.detach().cpu().numpy() return value def value2list(value: Any, valid_type: Union[Type, Tuple[Type, ...]], expand_dim: int) -> List[Any]: """If the type of ``value`` is ``valid_type``, convert the value to list and expand to ``expand_dim``. Args: value (Any): value. valid_type (Union[Type, Tuple[Type, ...]): valid type. expand_dim (int): expand dim. Returns: List[Any]: value. """ if isinstance(value, valid_type): value = [value] * expand_dim return value def check_type(name: str, value: Any, valid_type: Union[Type, Tuple[Type, ...]]) -> None: """Check whether the type of value is in ``valid_type``. Args: name (str): value name. value (Any): value. valid_type (Type, Tuple[Type, ...]): expected type. """ if not isinstance(value, valid_type): raise TypeError(f'`{name}` should be {valid_type} ' f' but got {type(value)}') def check_length(name: str, value: Any, valid_length: int) -> None: """If type of the ``value`` is list, check whether its length is equal with or greater than ``valid_length``. Args: name (str): value name. value (Any): value. valid_length (int): expected length. """ if isinstance(value, list): if len(value) < valid_length: raise AssertionError( f'The length of {name} must equal with or ' f'greater than {valid_length}, but got {len(value)}') def check_type_and_length(name: str, value: Any, valid_type: Union[Type, Tuple[Type, ...]], valid_length: int) -> None: """Check whether the type of value is in ``valid_type``. If type of the ``value`` is list, check whether its length is equal with or greater than ``valid_length``. Args: value (Any): value. legal_type (Type, Tuple[Type, ...]): legal type. valid_length (int): expected length. Returns: List[Any]: value. """ check_type(name, value, valid_type) check_length(name, value, valid_length)
_base_ = './fast-rcnn_r50-caffe_fpn_1x_coco.py' model = dict( backbone=dict( depth=101, init_cfg=dict( type='Pretrained', checkpoint='open-mmlab://detectron2/resnet101_caffe')))
_base_ = './fast_rcnn_r50_caffe_fpn_1x_coco.py' model = dict( backbone=dict( depth=101, init_cfg=dict( type='Pretrained', checkpoint='open-mmlab://detectron2/resnet101_caffe')))
from typing import Union from langchain_core.agents import AgentAction, AgentFinish from langchain_core.messages import BaseMessage from langchain_core.outputs import ChatGeneration, Generation from langchain.agents.agent import MultiActionAgentOutputParser from langchain.agents.output_parsers.tools import ( ToolAgentAction, parse_ai_message_to_tool_action, ) OpenAIToolAgentAction = ToolAgentAction def parse_ai_message_to_openai_tool_action( message: BaseMessage, ) -> Union[list[AgentAction], AgentFinish]: """Parse an AI message potentially containing tool_calls.""" tool_actions = parse_ai_message_to_tool_action(message) if isinstance(tool_actions, AgentFinish): return tool_actions final_actions: list[AgentAction] = [] for action in tool_actions: if isinstance(action, ToolAgentAction): final_actions.append( OpenAIToolAgentAction( tool=action.tool, tool_input=action.tool_input, log=action.log, message_log=action.message_log, tool_call_id=action.tool_call_id, ) ) else: final_actions.append(action) return final_actions class OpenAIToolsAgentOutputParser(MultiActionAgentOutputParser): """Parses a message into agent actions/finish. Is meant to be used with OpenAI models, as it relies on the specific tool_calls parameter from OpenAI to convey what tools to use. If a tool_calls parameter is passed, then that is used to get the tool names and tool inputs. If one is not passed, then the AIMessage is assumed to be the final output. """ @property def _type(self) -> str: return "openai-tools-agent-output-parser" def parse_result( self, result: list[Generation], *, partial: bool = False ) -> Union[list[AgentAction], AgentFinish]: if not isinstance(result[0], ChatGeneration): raise ValueError("This output parser only works on ChatGeneration output") message = result[0].message return parse_ai_message_to_openai_tool_action(message) def parse(self, text: str) -> Union[list[AgentAction], AgentFinish]: raise ValueError("Can only parse messages")
from typing import List, Union from langchain_core.agents import AgentAction, AgentFinish from langchain_core.messages import BaseMessage from langchain_core.outputs import ChatGeneration, Generation from langchain.agents.agent import MultiActionAgentOutputParser from langchain.agents.output_parsers.tools import ( ToolAgentAction, parse_ai_message_to_tool_action, ) OpenAIToolAgentAction = ToolAgentAction def parse_ai_message_to_openai_tool_action( message: BaseMessage, ) -> Union[List[AgentAction], AgentFinish]: """Parse an AI message potentially containing tool_calls.""" tool_actions = parse_ai_message_to_tool_action(message) if isinstance(tool_actions, AgentFinish): return tool_actions final_actions: List[AgentAction] = [] for action in tool_actions: if isinstance(action, ToolAgentAction): final_actions.append( OpenAIToolAgentAction( tool=action.tool, tool_input=action.tool_input, log=action.log, message_log=action.message_log, tool_call_id=action.tool_call_id, ) ) else: final_actions.append(action) return final_actions class OpenAIToolsAgentOutputParser(MultiActionAgentOutputParser): """Parses a message into agent actions/finish. Is meant to be used with OpenAI models, as it relies on the specific tool_calls parameter from OpenAI to convey what tools to use. If a tool_calls parameter is passed, then that is used to get the tool names and tool inputs. If one is not passed, then the AIMessage is assumed to be the final output. """ @property def _type(self) -> str: return "openai-tools-agent-output-parser" def parse_result( self, result: List[Generation], *, partial: bool = False ) -> Union[List[AgentAction], AgentFinish]: if not isinstance(result[0], ChatGeneration): raise ValueError("This output parser only works on ChatGeneration output") message = result[0].message return parse_ai_message_to_openai_tool_action(message) def parse(self, text: str) -> Union[List[AgentAction], AgentFinish]: raise ValueError("Can only parse messages")
__version__ = '0.33.1' import logging from docarray.array import DocList, DocVec from docarray.base_doc.doc import BaseDoc from docarray.utils._internal.misc import _get_path_from_docarray_root_level __all__ = ['BaseDoc', 'DocList', 'DocVec'] logger = logging.getLogger('docarray') handler = logging.StreamHandler() formatter = logging.Formatter("%(levelname)s - %(name)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) def __getattr__(name: str): if name in ['Document', 'DocumentArray']: raise ImportError( f'Cannot import name \'{name}\' from \'{_get_path_from_docarray_root_level(__file__)}\'.\n' f'The object named \'{name}\' does not exist anymore in this version of docarray.\n' f'If you still want to use \'{name}\' please downgrade to version <=0.21.0 ' f'with: `pip install -U docarray==0.21.0`.' ) else: raise ImportError( f'cannot import name \'{name}\' from \'{_get_path_from_docarray_root_level(__file__)}\'' )
__version__ = '0.33.0' import logging from docarray.array import DocList, DocVec from docarray.base_doc.doc import BaseDoc from docarray.utils._internal.misc import _get_path_from_docarray_root_level __all__ = ['BaseDoc', 'DocList', 'DocVec'] logger = logging.getLogger('docarray') handler = logging.StreamHandler() formatter = logging.Formatter("%(levelname)s - %(name)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) def __getattr__(name: str): if name in ['Document', 'DocumentArray']: raise ImportError( f'Cannot import name \'{name}\' from \'{_get_path_from_docarray_root_level(__file__)}\'.\n' f'The object named \'{name}\' does not exist anymore in this version of docarray.\n' f'If you still want to use \'{name}\' please downgrade to version <=0.21.0 ' f'with: `pip install -U docarray==0.21.0`.' ) else: raise ImportError( f'cannot import name \'{name}\' from \'{_get_path_from_docarray_root_level(__file__)}\'' )
from typing import Any, Optional, Type, TypeVar, Union from docarray.base_document import BaseDocument from docarray.typing import TextUrl from docarray.typing.tensor.embedding import AnyEmbedding T = TypeVar('T', bound='Text') class Text(BaseDocument): """ Document for handling text. It can contain a TextUrl (`Text.url`), a str (`Text.text`), and an AnyEmbedding (`Text.embedding`). EXAMPLE USAGE: You can use this Document directly: .. code-block:: python from docarray.documents import Text # use it directly txt_doc = Text(url='http://www.jina.ai/') txt_doc.text = txt_doc.url.load() model = MyEmbeddingModel() txt_doc.embedding = model(txt_doc.text) You can initialize directly from a string: .. code-block:: python from docarray.documents import Text txt_doc = Text('hello world') You can extend this Document: .. code-block:: python from docarray.documents import Text from docarray.typing import AnyEmbedding from typing import Optional # extend it class MyText(Text): second_embedding: Optional[AnyEmbedding] txt_doc = MyText(url='http://www.jina.ai/') txt_doc.text = txt_doc.url.load() model = MyEmbeddingModel() txt_doc.embedding = model(txt_doc.text) txt_doc.second_embedding = model(txt_doc.text) You can use this Document for composition: .. code-block:: python from docarray import BaseDocument from docarray.documents import Image, Text # compose it class MultiModalDoc(BaseDocument): image_doc: Image text_doc: Text mmdoc = MultiModalDoc( image_doc=Image(url="http://www.jina.ai/image.jpg"), text_doc=Text(text="hello world, how are you doing?"), ) mmdoc.text_doc.text = mmdoc.text_doc.url.load() # or mmdoc.text_doc.bytes = mmdoc.text_doc.url.load_bytes() This Document can be compared against another Document of the same type or a string. When compared against another object of the same type, the pydantic BaseModel equality check will apply which checks the equality of every attribute, including `id`. When compared against a str, it will check the equality of the `text` attribute against the given string. .. code-block:: python from docarray.documents import Text doc = Text(text='This is the main text', url='exampleurl.com') doc2 = Text(text='This is the main text', url='exampleurl.com') doc == 'This is the main text' # True doc == doc2 # False, their ids are not equivalent """ text: Optional[str] = None url: Optional[TextUrl] = None embedding: Optional[AnyEmbedding] = None bytes: Optional[bytes] = None def __init__(self, text: Optional[str] = None, **kwargs): if 'text' not in kwargs: kwargs['text'] = text super().__init__(**kwargs) @classmethod def validate( cls: Type[T], value: Union[str, Any], ) -> T: if isinstance(value, str): value = cls(text=value) return super().validate(value) def __eq__(self, other: Any) -> bool: if isinstance(other, str): return self.text == other else: # BaseModel has a default equality return super().__eq__(other) def __contains__(self, item: str) -> bool: """ This method makes `Text` behave the same as an `str`. .. code-block:: python from docarray.documents import Text t = Text(text='this is my text document') assert 'text' in t assert 'docarray' not in t :param item: A string to be checked if is a substring of `text` attribute :return: A boolean determining the presence of `item` as a substring in `text` """ if self.text is not None: return self.text.__contains__(item) else: return False def _get_string_for_regex_filter(self): return self.text
from typing import Any, Optional, Type, TypeVar, Union from docarray.base_document import BaseDocument from docarray.typing import TextUrl from docarray.typing.tensor.embedding import AnyEmbedding T = TypeVar('T', bound='Text') class Text(BaseDocument): """ Document for handling text. It can contain a TextUrl (`Text.url`), a str (`Text.text`), and an AnyEmbedding (`Text.embedding`). EXAMPLE USAGE: You can use this Document directly: .. code-block:: python from docarray.documents import Text # use it directly txt_doc = Text(url='http://www.jina.ai/') txt_doc.text = txt_doc.url.load() model = MyEmbeddingModel() txt_doc.embedding = model(txt_doc.text) You can initialize directly from a string: .. code-block:: python from docarray.documents import Text txt_doc = Text('hello world') You can extend this Document: .. code-block:: python from docarray.documents import Text from docarray.typing import AnyEmbedding from typing import Optional # extend it class MyText(Text): second_embedding: Optional[AnyEmbedding] txt_doc = MyText(url='http://www.jina.ai/') txt_doc.text = txt_doc.url.load() model = MyEmbeddingModel() txt_doc.embedding = model(txt_doc.text) txt_doc.second_embedding = model(txt_doc.text) You can use this Document for composition: .. code-block:: python from docarray import BaseDocument from docarray.documents import Image, Text # compose it class MultiModalDoc(BaseDocument): image_doc: Image text_doc: Text mmdoc = MultiModalDoc( image_doc=Image(url="http://www.jina.ai/image.jpg"), text_doc=Text(text="hello world, how are you doing?"), ) mmdoc.text_doc.text = mmdoc.text_doc.url.load() #or mmdoc.text_doc.bytes = mmdoc.text_doc.url.load_bytes() This Document can be compared against another Document of the same type or a string. When compared against another object of the same type, the pydantic BaseModel equality check will apply which checks the equality of every attribute, including `id`. When compared against a str, it will check the equality of the `text` attribute against the given string. .. code-block:: python from docarray.documents Text doc = Text(text='This is the main text', url='exampleurl.com') doc2 = Text(text='This is the main text', url='exampleurl.com') doc == 'This is the main text' # True doc == doc2 # False, their ids are not equivalent """ text: Optional[str] = None url: Optional[TextUrl] = None embedding: Optional[AnyEmbedding] = None bytes: Optional[bytes] = None def __init__(self, text: Optional[str] = None, **kwargs): if 'text' not in kwargs: kwargs['text'] = text super().__init__(**kwargs) @classmethod def validate( cls: Type[T], value: Union[str, Any], ) -> T: if isinstance(value, str): value = cls(text=value) return super().validate(value) def __eq__(self, other: Any) -> bool: if isinstance(other, str): return self.text == other else: # BaseModel has a default equality return super().__eq__(other) def __contains__(self, item: str) -> bool: """ This method makes `Text` behave the same as an `str`. .. code-block:: python from docarray.documents import Text t = Text(text='this is my text document') assert 'text' in t assert 'docarray' not in t :param item: A string to be checked if is a substring of `text` attribute :return: A boolean determining the presence of `item` as a substring in `text` """ if self.text is not None: return self.text.__contains__(item) else: return False def _get_string_for_regex_filter(self): return self.text
""" Example of training with Dask on CPU ==================================== """ from dask import array as da from dask.distributed import Client, LocalCluster from xgboost import dask as dxgb from xgboost.dask import DaskDMatrix def main(client): # generate some random data for demonstration m = 100000 n = 100 rng = da.random.default_rng(1) X = rng.normal(size=(m, n)) y = X.sum(axis=1) # DaskDMatrix acts like normal DMatrix, works as a proxy for local # DMatrix scatter around workers. dtrain = DaskDMatrix(client, X, y) # Use train method from xgboost.dask instead of xgboost. This # distributed version of train returns a dictionary containing the # resulting booster and evaluation history obtained from # evaluation metrics. output = dxgb.train( client, {"verbosity": 1, "tree_method": "hist"}, dtrain, num_boost_round=4, evals=[(dtrain, "train")], ) bst = output["booster"] history = output["history"] # you can pass output directly into `predict` too. prediction = dxgb.predict(client, bst, dtrain) print("Evaluation history:", history) return prediction if __name__ == "__main__": # or use other clusters for scaling with LocalCluster(n_workers=7, threads_per_worker=4) as cluster: with Client(cluster) as client: main(client)
""" Example of training with Dask on CPU ==================================== """ from dask import array as da from dask.distributed import Client, LocalCluster from xgboost import dask as dxgb from xgboost.dask import DaskDMatrix def main(client): # generate some random data for demonstration m = 100000 n = 100 rng = da.random.default_rng(1) X = rng.normal(size=(m, n)) y = X.sum(axis=1) # DaskDMatrix acts like normal DMatrix, works as a proxy for local # DMatrix scatter around workers. dtrain = DaskDMatrix(client, X, y) # Use train method from xgboost.dask instead of xgboost. This # distributed version of train returns a dictionary containing the # resulting booster and evaluation history obtained from # evaluation metrics. output = dxgb.train( client, {"verbosity": 1, "tree_method": "hist"}, dtrain, num_boost_round=4, evals=[(dtrain, "train")], ) bst = output["booster"] history = output["history"] # you can pass output directly into `predict` too. prediction = dxgb.predict(client, bst, dtrain) print("Evaluation history:", history) return prediction if __name__ == "__main__": # or use other clusters for scaling with LocalCluster(n_workers=7, threads_per_worker=4) as cluster: with Client(cluster) as client: main(client)
End of preview. Expand in Data Studio
README.md exists but content is empty.
Downloads last month
79

Models trained or fine-tuned on Girinath11/aiml_code_debug_dataset