Commit 1f37582e by Kenton Lee

Made visualization for long documents more readable.

parent 25dff938
...@@ -7,7 +7,7 @@ import json ...@@ -7,7 +7,7 @@ import json
import numpy as np import numpy as np
import cgi import cgi
import BaseHTTPServer from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import ssl import ssl
import tensorflow as tf import tensorflow as tf
...@@ -18,6 +18,30 @@ import nltk ...@@ -18,6 +18,30 @@ import nltk
nltk.download("punkt") nltk.download("punkt")
from nltk.tokenize import sent_tokenize, word_tokenize from nltk.tokenize import sent_tokenize, word_tokenize
class CorefRequestHandler(BaseHTTPRequestHandler):
model = None
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, self.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()
def create_example(text): def create_example(text):
raw_sentences = sent_tokenize(text) raw_sentences = sent_tokenize(text)
sentences = [word_tokenize(s) for s in raw_sentences] sentences = [word_tokenize(s) for s in raw_sentences]
...@@ -71,30 +95,8 @@ if __name__ == "__main__": ...@@ -71,30 +95,8 @@ if __name__ == "__main__":
saver.restore(session, checkpoint_path) saver.restore(session, checkpoint_path)
if port is not None: if port is not None:
class CorefRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): CorefRequestHandler.model = model
def do_POST(self): server = HTTPServer(("", port), CorefRequestHandler)
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)
print("Running server at port {}".format(port)) print("Running server at port {}".format(port))
server.serve_forever() server.serve_forever()
else: else:
......
...@@ -3,4 +3,4 @@ ...@@ -3,4 +3,4 @@
html_root=~/public_html html_root=~/public_html
name=e2e-coref name=e2e-coref
mkdir -p $html_root/$name mkdir -p $html_root/$name
cp index.html main.js query.cgi $html_root/$name cp index.html main.js main.css query.cgi $html_root/$name
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="main.js"></script> <script src="main.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="main.css">
</head> </head>
<body> <body>
<div class="container"> <div class="container">
...@@ -53,15 +54,15 @@ ...@@ -53,15 +54,15 @@
<div class="row"> <div class="row">
<div class="col-md-3"> <div class="col-md-3">
<h4 id="predicted-clusters-title"></h4> <h4 id="predicted-clusters-title"></h4>
<ul id="predicted-clusters" class="list-group"></ul> <ul id="predicted-clusters" class="list-group scrollable"></ul>
</div> </div>
<div class="col-md-6"> <div class="col-md-6">
<h4 id="text-title"></h4> <h4 id="text-title"></h4>
<p id="text" class="lead"></p> <p id="text" class="lead scrollable"></p>
</div> </div>
<div class="col-md-3"> <div class="col-md-3">
<h4 id="top-spans-title"></h4> <h4 id="top-spans-title"></h4>
<ul id="top-spans" class="list-group"></ul> <ul id="top-spans" class="list-group scrollable"></ul>
</div> </div>
</div> </div>
</body> </body>
......
.scrollable {
max-height: 800px;
overflow-y: auto;
}
...@@ -9,18 +9,22 @@ cgitb.enable() ...@@ -9,18 +9,22 @@ cgitb.enable()
SERVER = "http://localhost" SERVER = "http://localhost"
PORT = 10001 PORT = 10001
def print_header():
print"Content-Type: text/html"
print
def print_error(errno, message):
print "Status: {} {}".format(errno, message)
print_header()
sys.exit()
form = cgi.FieldStorage() form = cgi.FieldStorage()
if "text" not in form: if "text" not in form:
print "Status: %d %s\n\n" % (400, "Bad Request") print_error(400, "Bad Request")
sys.exit(0)
text = form.getvalue("text") text = form.getvalue("text")
try: try:
post_response = requests.post(url="{}:{}".format(SERVER, PORT), data={"text":text}, timeout=180) post_response = requests.post(url="{}:{}".format(SERVER, PORT), data={"text":text}, timeout=180)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
print e print_error(503, "Service Unavailable")
sys.exit(0) print_header()
print "Content-Type: text/html"
print
print post_response.text.encode("utf-8") print post_response.text.encode("utf-8")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment