Skip to content

Instantly share code, notes, and snippets.

@DrustZ
Created June 4, 2018 06:15
Show Gist options
  • Save DrustZ/b73d2f9ff351648efa49558131d26f03 to your computer and use it in GitHub Desktop.
Save DrustZ/b73d2f9ff351648efa49558131d26f03 to your computer and use it in GitHub Desktop.
Simple server with deepemoji
# -*- coding: utf-8 -*-
"""
Very simple HTTP server in python.
Usage::
./dummy-web-server.py [<port>]
Send a GET request::
curl http://localhost
Send a HEAD request::
curl -I http://localhost
Send a POST request::
curl -d "foo=bar&bin=baz" http://localhost
"""
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
from HTMLParser import HTMLParser
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
maxlen = 30
h = HTMLParser()
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 S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
self._set_headers()
self.wfile.write("<html><body><h1>hi!</h1></body></html>")
def do_HEAD(self):
self._set_headers()
def do_POST(self):
# Doesn't do anything with posted data
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = urllib.unquote(self.rfile.read(content_length).split("=")[-1]).decode('utf8') # <--- Gets the data itself
print "receive: "+post_data
self._set_headers()
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.wfile.write(response)
def run(server_class=HTTPServer, handler_class=S, port=80):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print 'Starting httpd...'
httpd.serve_forever()
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment