Skip to content
This repository was archived by the owner on Jul 7, 2023. It is now read-only.

Commit a9cca95

Browse files
haukurbafrozenator
authored andcommitted
Fixed wrong configuration.json example in README.md, fixed missing d3 error, fixed json serialization error (#929)
1 parent 458b86a commit a9cca95

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

tensor2tensor/insights/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ Start guide, a sample configuration would be:
4242
"hparams": "",
4343
"hparams_set": "transformer_base_single_gpu",
4444
"problem": "translate_ende_wmt32k"
45-
},
46-
}]
45+
}
46+
}],
4747
"language": [{
4848
"code": "en",
49-
"name": "English",
49+
"name": "English"
5050
},{
5151
"code": "de",
52-
"name": "German",
52+
"name": "German"
5353
}]
5454
}
5555
```

tensor2tensor/insights/polymer/explore_view/explore-view.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,6 @@ <h4>Rapid Response</h4>
150150
on-response="handleTranslationResponse_">
151151
</iron-ajax>
152152
</template>
153+
<script src="../d3/d3.js"></script>
153154
<script src="explore-view.js"></script>
154155
</dom-module>

tensor2tensor/insights/server.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from flask import jsonify
2222
from flask import request
2323
from flask import send_from_directory
24+
from flask.json import JSONEncoder
25+
import numpy as np
2426
from gunicorn.app.base import BaseApplication
2527
from gunicorn.six import iteritems
2628
from tensor2tensor.insights import transformer_model
@@ -37,6 +39,23 @@
3739
"Path to static javascript and html files to serve.")
3840

3941

42+
_NUMPY_INT_DTYPES = [
43+
np.int8, np.int16, np.int32, np.int64
44+
]
45+
_NUMPY_FP_DTYPES = [
46+
np.float16, np.float32, np.float64
47+
]
48+
class NumpySerializationFix(JSONEncoder):
49+
"""json module cannot serialize numpy datatypes, reinterpret them first"""
50+
def default(self, obj):
51+
obj_type = type(obj)
52+
if obj_type in _NUMPY_INT_DTYPES:
53+
return int(obj)
54+
if obj_type in _NUMPY_FP_DTYPES:
55+
return float(obj)
56+
return json.JSONEncoder.default(self, obj)
57+
58+
4059
class DebugFrontendApplication(BaseApplication):
4160
"""A local custom application for GUnicorns.
4261
@@ -101,6 +120,7 @@ def main(_):
101120
__name__.split(".")[0],
102121
static_url_path="/polymer",
103122
static_folder=FLAGS.static_path)
123+
app.json_encoder = NumpySerializationFix
104124

105125
# Disable static file caching.
106126
app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 0
@@ -113,7 +133,7 @@ def language_list(): # pylint: disable=unused-variable
113133
JSON for the languages.
114134
"""
115135
return jsonify({
116-
"language": languages.values()
136+
"language": list(languages.values())
117137
})
118138

119139
@app.route("/api/list_models/")

0 commit comments

Comments
 (0)