Last active
June 21, 2018 21:56
-
-
Save DrustZ/2ed6c73cfb5e125e08130197edbe9058 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import example_helper | |
import json | |
import csv | |
import urllib | |
import numpy as np | |
from deepmoji.sentence_tokenizer import SentenceTokenizer | |
from deepmoji.model_def import deepmoji_emojis | |
from deepmoji.global_variables import PRETRAINED_PATH, VOCAB_PATH | |
import tornado.ioloop | |
import tornado.web | |
maxlen = 30 | |
with open(VOCAB_PATH, 'r') as f: | |
vocabulary = json.load(f) | |
st = SentenceTokenizer(vocabulary, maxlen) | |
model = deepmoji_emojis(maxlen, PRETRAINED_PATH) | |
model.summary() | |
def top_elements(array, k): | |
ind = np.argpartition(array, -k)[-k:] | |
return ind[np.argsort(array[ind])][::-1] | |
class MainHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.write("Hello, world") | |
def post(self): | |
# Doesn't do anything with posted data | |
post_data = urllib.unquote(self.get_argument("content"))#.decode('utf8') # <--- Gets the data itself | |
print "receive: "+post_data | |
response = post_data+"-splt-" | |
# post_data = post_data.decode("utf-8") | |
tokenized, _, _ = st.tokenize_sentences([post_data]) | |
if len(tokenized[0]) > 30: | |
tokenized[0] = tokenized[0][-30:] | |
prob = model.predict(tokenized)[0] | |
ind_top = top_elements(prob, 5) | |
response += ','.join(str(x) for x in ind_top) | |
response += '-splt-' + ','.join(str(x) for x in [prob[ind] for ind in ind_top]) | |
response = response.encode("utf-16") | |
print "response: "+response | |
self.write(response) | |
def make_app(): | |
return tornado.web.Application([ | |
(r"/", MainHandler), | |
]) | |
if __name__ == "__main__": | |
from sys import argv | |
port = 8888 | |
if len(argv) == 2: | |
port = int(argv[1]) | |
app = make_app() | |
app.listen(port) | |
print 'Starting httpd...' | |
tornado.ioloop.IOLoop.current().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment