Beispiel #1
0
    def api_config() -> Response:  # pylint: disable=unused-variable
        """
        There are basically two things that can happen here.
        If this method is called with a ``Registrable`` class (e.g. ``Model``),
        it should return the list of possible ``Model`` subclasses.
        If it is called with an instantiable subclass (e.g. ``CrfTagger``),
        is should return the config for that subclass.

        This is complicated by the fact that some Registrable base classes
        (e.g. Vocabulary, Trainer) are _themselves_ instantiable.

        We handle this in two ways: first, we insist that the first case
        include an extra ``get_choices`` parameter. That is, if you call
        this method for ``Trainer`` with get_choices=true, you get the list
        of Trainer subclasses. If you call it without that extra flag, you
        get the config for the class itself.

        There are basically two UX situations in which this API is called.
        The first is when you have a dropdown list of choices (e.g. Model types)
        and you select one. Such an API request is made *without* the get_choices flag,
        which means that the config is returned *even if the class in question
        is a Registrable class that has subclass choices*.

        The second is when you click a "Configure" button, which configures
        a class that may (e.g. ``Model``) or may not (e.g. ``FeedForward``)
        have registrable subclasses. In this case the API request is made
        with the "get_choices" flag, but will return the corresponding config
        object if no choices are available (e.g. in the ``FeedForward``) case.

        This is not elegant, but it works.
        """
        class_name = request.args.get('class', '')
        get_choices = request.args.get('get_choices', None)

        # Get the configuration for this class name
        config = configure(class_name)
        try:
            # May not have choices
            choice5 = choices(class_name)
        except ValueError:
            choice5 = []

        if get_choices and choice5:
            return jsonify({
                    "className": class_name,
                    "choices": choice5
            })
        else:
            return jsonify({
                    "className": class_name,
                    "config": config.to_json()
            })
    def api_config() -> Response:
        """
        There are basically two things that can happen here.
        If this method is called with a ``Registrable`` class (e.g. ``Model``),
        it should return the list of possible ``Model`` subclasses.
        If it is called with an instantiable subclass (e.g. ``CrfTagger``),
        is should return the config for that subclass.

        This is complicated by the fact that some Registrable base classes
        (e.g. Vocabulary, Trainer) are _themselves_ instantiable.

        We handle this in two ways: first, we insist that the first case
        include an extra ``get_choices`` parameter. That is, if you call
        this method for ``Trainer`` with get_choices=true, you get the list
        of Trainer subclasses. If you call it without that extra flag, you
        get the config for the class itself.

        There are basically two UX situations in which this API is called.
        The first is when you have a dropdown list of choices (e.g. Model types)
        and you select one. Such an API request is made *without* the get_choices flag,
        which means that the config is returned *even if the class in question
        is a Registrable class that has subclass choices*.

        The second is when you click a "Configure" button, which configures
        a class that may (e.g. ``Model``) or may not (e.g. ``FeedForward``)
        have registrable subclasses. In this case the API request is made
        with the "get_choices" flag, but will return the corresponding config
        object if no choices are available (e.g. in the ``FeedForward``) case.

        This is not elegant, but it works.
        """
        class_name = request.args.get("class", "")
        get_choices = request.args.get("get_choices", None)

        # Get the configuration for this class name
        config = configure(class_name)
        try:
            # May not have choices
            choice5 = choices(class_name)
        except ValueError:
            choice5 = []

        if get_choices and choice5:
            return jsonify({"className": class_name, "choices": choice5})
        else:
            return jsonify({
                "className": class_name,
                "config": config.to_json()
            })
Beispiel #3
0
    def test_abstract_base_class(self):
        config = choices(
            'allennlp.data.dataset_readers.dataset_reader.DatasetReader')

        assert isinstance(config, list)
        assert 'allennlp.data.dataset_readers.snli.SnliReader' in config