Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
numpy >= 1.11.1
sugartensor >= 0.0.2.4
lxml >= 3.6.4.
nltk >= 3.2.1.
regex
readchar
56 changes: 56 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from __future__ import print_function
import sugartensor as tf
import numpy as np
from prepro import *
from train import ModelGraph
import codecs
import readchar

def main():
g = ModelGraph(mode="test")

with tf.Session() as sess:
tf.sg_init(sess)

# restore parameters
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint('asset/train'))
print("Restored!")
mname = open('asset/train/checkpoint', 'r').read().split('"')[1] # model name

char2idx, idx2char = load_char_vocab()
word2idx, idx2word = load_word_vocab()


previous = [0]*50 # a stack for previous words
para = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
ctx = [0]*50

while True:
key = readchar.readkey().lower()

if key == readchar.key.BACKSPACE:
ctx.insert(0, previous.pop())
ctx.pop()
previous.insert(0, 0)

elif key == readchar.key.ESC:
break

else:
key_idx = char2idx[key]
ctx.append(key_idx)
ctx.pop(0)

logits = sess.run(g.logits, {g.x: np.expand_dims(ctx, 0)})
preds = logits.argsort()[0][-3:]
# pred = np.argmax(logits, -1)[0]
predword1, predword2, predword3 = [idx2word.get(pred) for pred in preds]
print(predword1, ' ', predword2, ' ', predword3)



if __name__ == '__main__':
main()
print("Done")

153 changes: 153 additions & 0 deletions scss/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
//Set up some color variables
$darkgray: #53565a;
$midgray: #888b8d;
$lightgray: #a7a8aa;
$yellow: #ffd100;

//Lots of stuff be flexin'
@mixin flexy {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-ms-flex-line-pack: center;
align-content: center;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}

* {
box-sizing: border-box;
}

body {
//Change this font-size value to resize the keyboard!
font-size: 8px;
@include flexy;
width: 100%;
margin: 0;
box-sizing: border-box;
font-family: Quicksand;
height: 100vh;
max-height: 100vh;
overflow: hidden;

//The keyboard containing div and all key elements
#keyboard, kbd {
@include flexy;
box-sizing: border-box;
border-radius: 4px;
background: #ccc;
border: .2em solid $midgray;
text-align: center;
font-family: Quicksand;
}

kbd {
flex: 1;
}

.keyboard-row {
@include flexy;
width: 100%;
}

//The box the typed text appears in
div#text {
@include flexy;
align-items: flex-end;
width: 20em;
height: 44vh;
max-height: 44vh;
font-size: 3em;
margin-bottom: 4vh;
text-align: center;
overflow: auto;
align-self: flex-end;
position: relative;
}

//The div containing the keys
#keyboard {
width: 56em;
padding: .4em .4em .8em;
box-shadow: 0 .4em 0 $midgray;

//The elements that make up the keys themselves
kbd {
line-height: 3.2em;
height: 3.2em;
width: 3.2em;
margin: .25em;
text-align: center;
color: #fff;
background-color: $darkgray;
transition: background, position, top, box-shadow .1s;
box-shadow: 0px 2px 0px $midgray;
}

//For slightly wider keys
kbd.long {
flex-grow: 1;
flex: 2;
}

//For much wider keys
kbd.longer {
flex-grow: 2;
flex: 3;
}

//For the widest key
kbd.spacebar {
flex-grow: 14;
flex: 6;
}

//Keep the keys that aren't letters, numbers and punctuation from going to uppercase when shift or capslock is pressed
.operationKey {
text-transform: none!important;
}

//Styles applied to a currently-pressed key
.pressed {
background: $yellow;
position: relative;
top: 2px;
box-shadow: none;
}
//Applied to the whole keyboard when shift or capslock is pressed
}
pre {
height: 3vh;
line-height: 3vh;
}
.uppercase kbd {
text-transform: uppercase;
}
}


//Make keyboard responsive, kinda
@media (min-width: 768px) {
body {
font-size: 10px;
}
}

@media (min-width: 960px) {
body {
font-size: 12px;
}
}

@media (min-width: 1080px) {
body {
font-size: 14px;
}
}
61 changes: 61 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-

from __future__ import print_function
import sys
import codecs
import readchar
import simplejson as json
from flask import Flask, render_template,request

import sugartensor as tf
import numpy as np
from prepro import *
from train import ModelGraph

app = Flask(__name__)


@app.route("/test")
def output():
return render_template("index.html")


@app.route('/output', methods=['GET'])
def worker():
#print(request, file=sys.stderr)
string = request.args.get('string').lower()
work = request.args.get('work')
words=string.split()
#print(words, file=sys.stderr)
n=len(words)

latest_50_chars = string[-50:]
para = "E"*(50 - len(latest_50_chars)) + latest_50_chars
ctx = [char2idx[char] for char in para]

logits = sess.run(g.logits, {g.x: np.expand_dims(ctx, 0)})
preds = logits.argsort()[0][-3:]

predword1, predword2, predword3 = [idx2word.get(pred) for pred in preds]

return json.dumps([(predword1, ), (predword2, ), (predword3, )])


if __name__=="__main__":
g = ModelGraph(mode="test")

with tf.Session() as sess:
tf.sg_init(sess)
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint('asset/train'))
print('Restored')

char2idx, idx2char = load_char_vocab()
word2idx, idx2word = load_word_vocab()

previous = [0]*50 # a stack for previous words
para = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
ctx = [0]*50


app.run(debug=True)
Binary file added static/.DS_Store
Binary file not shown.
Loading