Skip to content

Commit

Permalink
Merge pull request #587 from ufal/server_update
Browse files Browse the repository at this point in the history
Server update
  • Loading branch information
varisd authored Nov 16, 2017
2 parents 0d10355 + 214ea75 commit 8f2d663
Show file tree
Hide file tree
Showing 6 changed files with 620 additions and 13 deletions.
2 changes: 1 addition & 1 deletion bin/neuralmonkey-server
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3

from neuralmonkey.server import main
from neuralmonkey.server.server import main

if __name__ == "__main__":
main()
52 changes: 41 additions & 11 deletions neuralmonkey/server.py → neuralmonkey/server/server.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import argparse
import os
import json
import datetime

import flask
from flask import Flask, request
from flask import Flask, request, Response, render_template

from neuralmonkey.dataset import Dataset
from neuralmonkey.learning_utils import run_on_dataset
Expand All @@ -15,7 +16,44 @@
APP.config["args"] = None


def root_dir(): # pragma: no cover
return os.path.abspath(os.path.dirname(__file__))


def get_file(filename): # pragma: no cover
src = os.path.join(root_dir(), filename)
return open(src).read()


def run(data): # pragma: no cover
args = APP.config["args"]
dataset = Dataset("request", data, {})
# TODO check the dataset
# check_dataset_and_coders(dataset, args.encoders)

_, response_data = run_on_dataset(
args.tf_manager, args.runners,
dataset, args.postprocess, write_out=False)

return response_data


@APP.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
source_text = request.form["source"]
data = {"source": [source_text.split()]}
translation_response = run(data)
translation = " ".join(translation_response["target"][0])
else:
source_text = "enter tokenized soruce language text here ."
translation = ""

return render_template(
"server.html", translation=translation, source=source_text)


@APP.route("/run", methods=["POST"])
def post_request():
start_time = datetime.datetime.now()
request_data = request.get_json()
Expand All @@ -24,16 +62,8 @@ def post_request():
response_data = {"error": "No data were provided."}
code = 400
else:
args = APP.config["args"]

try:
dataset = Dataset("request", request_data, {})
# TODO check the dataset
# check_dataset_and_coders(dataset, args.encoders)

_, response_data = run_on_dataset(
args.tf_manager, args.runners,
dataset, args.postprocess, write_out=False)
response_data = run(request_data)
code = 200
# pylint: disable=broad-except
except Exception as exc:
Expand All @@ -55,7 +85,7 @@ def main() -> None:
description="Runs Neural Monkey as a web server.")
parser.add_argument("--port", type=int, default=5000)
parser.add_argument("--host", type=str, default="127.0.0.1")
parser.add_argument("--configuration", type=str)
parser.add_argument("--configuration", type=str, required=True)
cli_args = parser.parse_args()

print("")
Expand Down
43 changes: 43 additions & 0 deletions neuralmonkey/server/static/server.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.label {
height: 20px;
margin-bottom: 15px;
}

.label label {
font-size: 1.5em;
clear: both;
}

.control {
clear: both;
}

.clear {
clear: both;
}

#submit-div {
text-align: left;
}

#submit-div input {
width: 100px;
height: 40px;
font-size: 1.5em;
}

textarea {
width: 98%;
height: 200px;
font-size: 1.5em;
font-weight: bold;
}

.translation {
width: 98%;
}

.translation p {
font-size: 1.5em;
text-align: center;
}
Loading

0 comments on commit 8f2d663

Please sign in to comment.