예제 #1
0
def main(_):
    # Create the models we support:
    with open(FLAGS.configuration) as configuration_file:
        configuration = json.load(configuration_file)

    # Read in the set of query processors.
    processors = {}
    for processor_configuration in configuration["configuration"]:
        key = (processor_configuration["source_language"],
               processor_configuration["target_language"],
               processor_configuration["label"])

        processors[key] = transformer_model.TransformerModel(
            processor_configuration)

    # Read in the list of supported languages.
    languages = {}
    for language in configuration["language"]:
        languages[language["code"]] = {
            "code": language["code"],
            "name": language["name"],
        }

    # Create flask to serve all paths starting with '/polymer' from the static
    # path.  This is to served non-vulcanized components.
    app = Flask(__name__.split(".")[0],
                static_url_path="/polymer",
                static_folder=FLAGS.static_path)
    app.json_encoder = NumpySerializationFix

    # Disable static file caching.
    app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 0

    @app.route("/api/language_list/")
    def language_list():  # pylint: disable=unused-variable
        """Responds to /api/language_list with the supported languages.

    Returns:
      JSON for the languages.
    """
        return jsonify({"language": list(languages.values())})

    @app.route("/api/list_models/")
    def list_models():  # pylint: disable=unused-variable
        """Responds to /api/list_models with the supported modes.


    Returns:
      JSON for the supported models.
    """
        # pylint: disable=g-complex-comprehension
        configuration_list = [{
            "id": label,
            "source_language": languages[source_code],
            "target_language": languages[target_code],
        } for source_code, target_code, label in processors]
        return jsonify({"configuration": configuration_list})

    @app.route("/debug", methods=["GET"])
    def query():  # pylint: disable=unused-variable
        """Responds to /debug with processing results.

    Returns:
      JSON for the query's result.
    """
        query = request.args.get("source")
        source_language = request.args.get("sl")
        target_language = request.args.get("tl")
        model_name = request.args.get("id")
        processor = processors[(source_language, target_language, model_name)]
        return jsonify(processor.process(query))

    # Catchall for all other paths.  Any other path should get the basic index
    # page, the polymer side will determine what view to show and what REST calls
    # to make for data.
    @app.route("/", defaults={"path": ""})
    @app.route("/<path:path>")
    def root(path):  # pylint: disable=unused-variable
        """Responds to all other non-static paths with index.html.

    Args:
      path: Unused path.

    Returns:
      The landing page html text.
    """
        if (path == "index.js"
                or path == "webcomponentsjs/webcomponents-lite.js"):
            # Some vulcanizing methods bundle the javascript into a index.js file
            # paired with index.html but leave two important webcomponents js files
            # outside of the bundle.  If requesting those special files, fetch them
            # directly rather than from a /static sub-directory.
            return send_from_directory(FLAGS.static_path, path)
        # Everything else should redirect to the main landing page.  Since we
        # use a single page app, any initial url requests may include random
        # paths (that don't start with /api or /static) which all should be
        # served by the main landing page.
        return send_from_directory(FLAGS.static_path, "index.html")

    # Run the server.
    tf.logging.info("############# READY ##################")
    options = {
        "bind": ":8010",
        "timeout": 600,
        "workers": 4,
        "reload": True,
        "spew": True,
        "worker_class": "gevent",
    }
    DebugFrontendApplication(app, options).run()
예제 #2
0
def main(_):
    # Create the models we support:
    processors = {}
    transformer_key = ("en", "de", "transformers_wmt32k")
    # TODO(kstevens): Turn this into a text proto configuration that's read in on
    # startup.
    processors[transformer_key] = transformer_model.TransformerModel(
        FLAGS.t2t_data_dir, FLAGS.t2t_model_dir)

    # Create flask to serve all paths starting with '/static' from the static
    # path.
    app = Flask(__name__.split(".")[0],
                static_url_path="/static",
                static_folder=FLAGS.static_path)

    # Disable static file caching.
    app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 0

    @app.route("/api/language_list/")
    def language_list():  # pylint: disable=unused-variable
        """Responds to /api/language_list with the supported languages.

    Returns:
      JSON for the languages.
    """
        # TODO(kstevens): Figure this out automatically by processing the
        # configuration.
        result = {
            "language": [
                {
                    "code": "en",
                    "name": "English"
                },
                {
                    "code": "de",
                    "name": "German"
                },
            ],
        }
        return jsonify(result)

    @app.route("/api/list_models/")
    def list_models():  # pylint: disable=unused-variable
        """Responds to /api/list_models with the supported modes.


    Returns:
      JSON for the supported models.
    """
        # TODO(kstevens): Turn this into a configuration text proto that's read in
        # on startup.
        result = {
            "configuration": [
                {
                    "id": "transformers_wmt32k",
                    "source_language": {
                        "code": "en",
                        "name": "English",
                    },
                    "target_language": {
                        "code": "de",
                        "name": "German",
                    },
                },
            ],
        }
        return jsonify(result)

    @app.route("/debug", methods=["GET"])
    def query():  # pylint: disable=unused-variable
        """Responds to /debug with processing results.

    Returns:
      JSON for the query's result.
    """
        query = request.args.get("source")
        source_language = request.args.get("sl")
        target_language = request.args.get("tl")
        model_name = request.args.get("id")
        processor = processors[(source_language, target_language, model_name)]
        return jsonify(processor.process(query))

    # Catchall for all other paths.  Any other path should get the basic index
    # page, the polymer side will determine what view to show and what REST calls
    # to make for data.
    @app.route("/", defaults={"path": ""})
    @app.route("/<path:path>")
    def root(path):  # pylint: disable=unused-variable
        """Responds to all other non-static paths with index.html.

    Args:
      path: Unused path.

    Returns:
      The landing page html text.
    """
        del path
        return send_from_directory(FLAGS.static_path, "index.html")

    # Run the server.
    tf.logging.info("############# READY ##################")
    options = {
        "bind": ":8010",
        "timeout": 600,
        "workers": 4,
        "reload": True,
        "spew": True,
        "worker_class": "gevent",
    }
    DebugFrontendApplication(app, options).run()