From 25dff938cfeffda18d9645785f1d4d1337289760 Mon Sep 17 00:00:00 2001 From: Kenton Lee Date: Mon, 30 Oct 2017 19:24:11 -0700 Subject: [PATCH] Move demo out of github page. --- README.md | 2 +- demo.py | 44 ++++++++++++++++++-------------------------- docs/index.html | 52 ++-------------------------------------------------- docs/main.js | 124 ---------------------------------------------------------------------------------------------------------------------------- html/deploy.sh | 6 ++++++ html/index.html | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ html/main.js | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ html/query.cgi | 26 ++++++++++++++++++++++++++ 8 files changed, 249 insertions(+), 201 deletions(-) delete mode 100644 docs/main.js create mode 100755 html/deploy.sh create mode 100644 html/index.html create mode 100644 html/main.js create mode 100755 html/query.cgi diff --git a/README.md b/README.md index 6aa2239..d4073c3 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ This repository contains the code for replicating results from * [Kenton Lee](https://homes.cs.washington.edu/~kentonl), [Luheng He](https://homes.cs.washington.edu/~luheng), [Mike Lewis](https://research.fb.com/people/lewis-mike) and [Luke Zettlemoyer](https://www.cs.washington.edu/people/faculty/lsz) * In Proceedings of the Conference on Empirical Methods in Natural Language Process (EMNLP), 2017 -A demo of the code can be found here: https://kentonl.github.io/e2e-coref. +A demo of the code can be found here: http://www.kentonl.com/e2e-coref. ### Requirements * Python 2.7 diff --git a/demo.py b/demo.py index 5f02888..0208f2b 100755 --- a/demo.py +++ b/demo.py @@ -56,14 +56,6 @@ if __name__ == "__main__": else: port = None - if len(sys.argv) > 3: - # For https. See https://certbot.eff.org - keyfile = sys.argv[3] - certfile = sys.argv[4] - else: - keyfile = None - certfile = None - print "Running experiment: {}.".format(name) config = util.get_config("experiments.conf")[name] config["log_dir"] = util.mkdirs(os.path.join(config["log_root"], name)) @@ -80,29 +72,29 @@ if __name__ == "__main__": if port is not None: class CorefRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): - def do_GET(self): - idx = self.path.find("?") - if idx >= 0: - args = cgi.parse_qs(self.path[idx+1:]) - if "text" in args: - text_arg = args["text"] - if len(text_arg) == 1 and len(text_arg[0]) <= 10000: - text = text_arg[0].decode("utf-8") - print(u"Document text: {}".format(text)) - example = make_predictions(text, model) - print_predictions(example) - self.send_response(200) - self.send_header("Content-Type", "application/json") - self.end_headers() - self.wfile.write("jsonCallback({})".format(json.dumps(example))) - return + def do_POST(self): + form = cgi.FieldStorage( + fp=self.rfile, + headers=self.headers, + environ={"REQUEST_METHOD":"POST", + "CONTENT_TYPE":self.headers["Content-Type"] + }) + if "text" in form: + text = form["text"].value.decode("utf-8") + if len(text) <= 10000: + print(u"Document text: {}".format(text)) + example = make_predictions(text, model) + print_predictions(example) + self.send_response(200) + self.send_header("Content-Type", "application/json") + self.end_headers() + self.wfile.write(json.dumps(example)) + return self.send_response(400) self.send_header("Content-Type", "application/json") self.end_headers() server = BaseHTTPServer.HTTPServer(("", port), CorefRequestHandler) - if keyfile is not None: - server.socket = ssl.wrap_socket(server.socket, keyfile=keyfile, certfile=certfile, server_side=True) print("Running server at port {}".format(port)) server.serve_forever() else: diff --git a/docs/index.html b/docs/index.html index 8b920ab..74c901c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -5,7 +5,6 @@ - @@ -13,56 +12,9 @@

End-to-end Neural Coreference Resolution

-

This is the demo for the coreference resolution system described in:

-
- End-to-end Neural Coreference Resolution -
- Kenton Lee, - Luheng He, - Mike Lewis and - Luke Zettlemoyer -
- In Proceedings of the Conference on Empirical Methods in Natural Language Process (EMNLP), 2017. -
-

Submit an input document below. A simple example is provided.

-

On the right side, the highest-scoring spans are shown. On the left side, the predicted coreference cluster (mentions of the same entity) are shown.

-

Hover over the mentions on either side to highlight their location in the document and render the attention weights of the words.

-

The code is available here.

-
-
-
-
-
-
-

Input Document

- -
-
-
-
-
- -      - - Render attention weights on hover -
-
-
-
-
-
-
-

-
    -
    -
    -

    -

    -
    -
    -

    -
      +

      The demo has been moved to here.

      + diff --git a/docs/main.js b/docs/main.js deleted file mode 100644 index d5376eb..0000000 --- a/docs/main.js +++ /dev/null @@ -1,124 +0,0 @@ -function logsumexp(x) { - var a = Math.max.apply(null, x); - var total = 0.0; - for (var i = 0; i < x.length; i++) { - total += Math.exp(x[i] - a); - } - return Math.log(total) + a; -} - -function set_mention_callbacks(hover_callbacks, unhover_callbacks, head_scores, mention_start, mention_end) { - var scores = []; - for (var token = mention_start; token <= mention_end; token++) { - scores.push(head_scores[token]); - } - var log_norm = logsumexp(scores); - - hover_callbacks.add(function() { - var show_attention = $("#show-attention").is(":checked"); - for (var token = mention_start; token <= mention_end; token++) { - var token_span = $("#t" + token); - token_span.css("font-weight", "normal"); - if (show_attention) { - var p = Math.exp(head_scores[token] - log_norm); - var r = Math.round(-36 * p + 255); - var g = Math.round(-187 * p + 255); - var b = Math.round(-200 * p + 255); - var c = p > 0.75 ? "lightgray" : "black"; - token_span - .css("color", c) - .css("background-color", "rgb(" + r + "," + g + "," + b + ")"); - } - } - $("#lrb" + mention_start).text("("); - $("#rrb" + mention_end).text(")"); - }); - unhover_callbacks.add(function() { - for (var token = mention_start; token <= mention_end; token++) { - $("#t" + token) - .css("color", "black") - .css("font-weight", "300") - .css("background-color", "white"); - } - $("#lrb" + mention_start).text(""); - $("#rrb" + mention_end).text(""); - }); -} - -function render_predicted_clusters(clusters_div, clusters_data, text, head_scores) { - clusters_div.empty(); - $.each(clusters_data, function(i, cluster) { - var item = $("
    • ") - .addClass("list-group-item") - .appendTo(clusters_div); - - var hover_callbacks = $.Callbacks(); - var unhover_callbacks = $.Callbacks(); - $.each(cluster, function(j, mention) { - if (j != 0) { - $("").html(", ").appendTo(item); - } - var mention_start = mention[0]; - var mention_end = mention[1]; - set_mention_callbacks(hover_callbacks, unhover_callbacks, head_scores, mention_start, mention_end); - var mention_text = text.slice(mention_start, mention_end + 1).join(" "); - $("") - .html(mention_text) - .appendTo(item); - }); - - item.hover(hover_callbacks.fire, unhover_callbacks.fire); - }); -} - -function render_top_spans(top_spans_div, top_spans_data, text, head_scores) { - top_spans_div.empty(); - $.each(top_spans_data, function(i, mention) { - var item = $("
    • ") - .addClass("list-group-item") - .appendTo(top_spans_div); - - var hover_callbacks = $.Callbacks(); - var unhover_callbacks = $.Callbacks(); - var mention_start = mention[0]; - var mention_end = mention[1]; - set_mention_callbacks(hover_callbacks, unhover_callbacks, head_scores, mention_start, mention_end); - var mention_text = text.slice(mention_start, mention_end + 1).join(" "); - $("") - .html(mention_text) - .appendTo(item); - item.hover(hover_callbacks.fire, unhover_callbacks.fire); - }); -} - -function jsonCallback(data) { - var text_div = $("#text"); - text_div.empty(); - var text = [].concat.apply([], data.sentences); - $.each(text, function(i, token) { - var lrb = $("") - .attr("id", "lrb" + i) - .appendTo(text_div); - var text_span = $("") - .attr("id", "t" + i) - .html(token) - .appendTo(text_div); - var rrb = $("") - .attr("id", "rrb" + i) - .appendTo(text_div); - $("").text(" ").appendTo(text_div); - }); - render_predicted_clusters($("#predicted-clusters"), data.predicted_clusters, text, data.head_scores); - render_top_spans($("#top-spans"), data.top_spans, text, data.head_scores); - $("#top-spans-title").text("Top Spans"); - $("#text-title").text("Document"); - $("#predicted-clusters-title").text("Predicted Clusters"); -} - -$(document).ready(function() { - $("#input-form").submit(function(event){ - event.preventDefault(); - var text = $("#input-doc").val().replace("’", "'"); - $.getJSON("https://appositive.cs.washington.edu:8080?callback=?", {"text": text}); - }); -}); diff --git a/html/deploy.sh b/html/deploy.sh new file mode 100755 index 0000000..4f520d5 --- /dev/null +++ b/html/deploy.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +html_root=~/public_html +name=e2e-coref +mkdir -p $html_root/$name +cp index.html main.js query.cgi $html_root/$name diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..8b920ab --- /dev/null +++ b/html/index.html @@ -0,0 +1,68 @@ + + + + End-to-End Neural Coreference Resolution Demo + + + + + + + +
      +
      +
      +

      End-to-end Neural Coreference Resolution

      +

      This is the demo for the coreference resolution system described in:

      +
      + End-to-end Neural Coreference Resolution +
      + Kenton Lee, + Luheng He, + Mike Lewis and + Luke Zettlemoyer +
      + In Proceedings of the Conference on Empirical Methods in Natural Language Process (EMNLP), 2017. +
      +

      Submit an input document below. A simple example is provided.

      +

      On the right side, the highest-scoring spans are shown. On the left side, the predicted coreference cluster (mentions of the same entity) are shown.

      +

      Hover over the mentions on either side to highlight their location in the document and render the attention weights of the words.

      +

      The code is available here.

      +
      +
      +
      +
      +
      +
      +

      Input Document

      + +
      +
      +
      +
      +
      + +      + + Render attention weights on hover +
      +
      +
      +
      +
      +
      +
      +

      +
        +
        +
        +

        +

        +
        +
        +

        +
          +
          +
          + + diff --git a/html/main.js b/html/main.js new file mode 100644 index 0000000..b31e4cc --- /dev/null +++ b/html/main.js @@ -0,0 +1,128 @@ +function logsumexp(x) { + var a = Math.max.apply(null, x); + var total = 0.0; + for (var i = 0; i < x.length; i++) { + total += Math.exp(x[i] - a); + } + return Math.log(total) + a; +} + +function set_mention_callbacks(hover_callbacks, unhover_callbacks, head_scores, mention_start, mention_end) { + var scores = []; + for (var token = mention_start; token <= mention_end; token++) { + scores.push(head_scores[token]); + } + var log_norm = logsumexp(scores); + + hover_callbacks.add(function() { + var show_attention = $("#show-attention").is(":checked"); + for (var token = mention_start; token <= mention_end; token++) { + var token_span = $("#t" + token); + token_span.css("font-weight", "normal"); + if (show_attention) { + var p = Math.exp(head_scores[token] - log_norm); + var r = Math.round(-36 * p + 255); + var g = Math.round(-187 * p + 255); + var b = Math.round(-200 * p + 255); + var c = p > 0.75 ? "lightgray" : "black"; + token_span + .css("color", c) + .css("background-color", "rgb(" + r + "," + g + "," + b + ")"); + } + } + $("#lrb" + mention_start).text("("); + $("#rrb" + mention_end).text(")"); + }); + unhover_callbacks.add(function() { + for (var token = mention_start; token <= mention_end; token++) { + $("#t" + token) + .css("color", "black") + .css("font-weight", "300") + .css("background-color", "white"); + } + $("#lrb" + mention_start).text(""); + $("#rrb" + mention_end).text(""); + }); +} + +function render_predicted_clusters(clusters_div, clusters_data, text, head_scores) { + clusters_div.empty(); + $.each(clusters_data, function(i, cluster) { + var item = $("
        • ") + .addClass("list-group-item") + .appendTo(clusters_div); + + var hover_callbacks = $.Callbacks(); + var unhover_callbacks = $.Callbacks(); + $.each(cluster, function(j, mention) { + if (j != 0) { + $("").html(", ").appendTo(item); + } + var mention_start = mention[0]; + var mention_end = mention[1]; + set_mention_callbacks(hover_callbacks, unhover_callbacks, head_scores, mention_start, mention_end); + var mention_text = text.slice(mention_start, mention_end + 1).join(" "); + $("") + .html(mention_text) + .appendTo(item); + }); + + item.hover(hover_callbacks.fire, unhover_callbacks.fire); + }); +} + +function render_top_spans(top_spans_div, top_spans_data, text, head_scores) { + top_spans_div.empty(); + $.each(top_spans_data, function(i, mention) { + var item = $("
        • ") + .addClass("list-group-item") + .appendTo(top_spans_div); + + var hover_callbacks = $.Callbacks(); + var unhover_callbacks = $.Callbacks(); + var mention_start = mention[0]; + var mention_end = mention[1]; + set_mention_callbacks(hover_callbacks, unhover_callbacks, head_scores, mention_start, mention_end); + var mention_text = text.slice(mention_start, mention_end + 1).join(" "); + $("") + .html(mention_text) + .appendTo(item); + item.hover(hover_callbacks.fire, unhover_callbacks.fire); + }); +} + +$(document).ready(function() { + $("#input-form").submit(function(event){ + event.preventDefault(); + var text = $("#input-doc").val().replace("’", "'"); + $.ajax({ + dataType: "json", + url: "query.cgi", + data: { "text": text }, + timeout: 60000, + success: function(data, statusText, xhr) { + var text_div = $("#text"); + text_div.empty(); + var text = [].concat.apply([], data.sentences); + $.each(text, function(i, token) { + var lrb = $("") + .attr("id", "lrb" + i) + .appendTo(text_div); + var text_span = $("") + .attr("id", "t" + i) + .html(token) + .appendTo(text_div); + var rrb = $("") + .attr("id", "rrb" + i) + .appendTo(text_div); + $("").text(" ").appendTo(text_div); + }); + render_predicted_clusters($("#predicted-clusters"), data.predicted_clusters, text, data.head_scores); + render_top_spans($("#top-spans"), data.top_spans, text, data.head_scores); + $("#top-spans-title").text("Top Spans"); + $("#text-title").text("Document"); + $("#predicted-clusters-title").text("Predicted Clusters"); + } + }); + }); +}); diff --git a/html/query.cgi b/html/query.cgi new file mode 100755 index 0000000..9476f62 --- /dev/null +++ b/html/query.cgi @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +import sys +import json +import requests +import cgi, cgitb +cgitb.enable() + +SERVER = "http://localhost" +PORT = 10001 + +form = cgi.FieldStorage() +if "text" not in form: + print "Status: %d %s\n\n" % (400, "Bad Request") + sys.exit(0) +text = form.getvalue("text") +try: + post_response = requests.post(url="{}:{}".format(SERVER, PORT), data={"text":text}, timeout=180) +except requests.exceptions.ConnectionError as e: + print e + sys.exit(0) + +print "Content-Type: text/html" +print + +print post_response.text.encode("utf-8") -- libgit2 0.27.1