Skip to content

Commit

Permalink
adding flask app and deploying
Browse files Browse the repository at this point in the history
  • Loading branch information
roanakb committed Jun 21, 2020
1 parent 04facaf commit 37b385a
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 28 deletions.
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: gunicorn app:app
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,19 @@ Dependencies: nltk, numpy, torch
Required nltk downloads:<br/>
`nltk.download('averaged_perceptron_tagger')` <br/>
`nltk.download('wordnet')`

Examples: <br/>
`sentence = "I tightened the bolt to make sure it didn't fall apart"` <br/>
`word = "bolt"`

`sentence = "The fast guy ran by in a bolt"` <br/>
`word = "bolt"`

`sentence = "The bolt during the thunderstorm shocked me"` <br/>
`word = "bolt"`

`sentence = "The current was too strong to swim against"` <br/>
`word = "current"`

`sentence = "The high current on the wire shocked me"` <br/>
`word = "current"`
19 changes: 19 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from flask import Flask, request, render_template

from get_synonyms import get_words, get_best_words

app = Flask(__name__)


@app.route('/')
def index():
return render_template('index.html')


@app.route('/', methods=['POST'])
def index_post():
word = request.form['word']
sent = request.form['sent']
synsets = get_words(word)
best_words = get_best_words(word, sent, synsets)
return render_template('index.html', synsets=synsets, best_words=best_words, word=word, sentence=sent)
46 changes: 18 additions & 28 deletions get_synonyms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
# nltk.download('wordnet')
# nltk.download('punkt')

from model_init import load_model
print('loading model')
model = load_model()
print('model loaded')

sent1 = "The current from the wire hurt"
word1 = "current"
from model_init import model


def get_words(in_word):
Expand All @@ -34,36 +28,32 @@ def cosine(u, v):
return np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v))


def get_best_words(word, sent):
syn = get_words(word)
def get_best_words(word, sent, syn):
parts = re.split(f'{word}', sent)
oldSent = model.encode([sent])[0]
oldWord = word
old_sent = model.encode([sent])[0]

synMax = -float('inf')
bestWords = []
wordMax = -float('inf')
bestWord = ''
syn_max = -float('inf')
best_words = []
word_max = -float('inf')
best_word = ''

for synset in syn:
total = 0
for w in synset:
newSent = f'{w}'.join(parts)
sim = cosine(model.encode([newSent])[0], oldSent)
if sim > wordMax:
bestWord = w
wordMax = sim
new_sent = f'{w}'.join(parts)
sim = cosine(model.encode([new_sent])[0], old_sent)
if sim > word_max:
best_word = w
word_max = sim
total += sim
print(f'{w} and {sim}')
avg = total / len(synset)
print(f'{synset} and {avg}')
if avg > synMax:
bestWords = synset
synMax = avg

print(bestWords)

print(f'best word indiv: {bestWord}')
if avg > syn_max:
best_words = synset
syn_max = avg
print(best_words)

print(f'best word indiv: {best_word}')

get_best_words(word1, sent1)
return best_words
5 changes: 5 additions & 0 deletions model_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ def load_model():
model.build_vocab_k_words(K=100000)
print('done building vocab')
return model


print('loading model')
model = load_model()
print('model loaded')
102 changes: 102 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
absl-py==0.9.0
appnope==0.1.0
astor==0.8.1
astunparse==1.6.3
attrs==19.3.0
backcall==0.1.0
bleach==3.1.5
cachetools==4.1.0
certifi==2020.4.5.1
chardet==3.0.4
click==7.1.2
decorator==4.4.2
defusedxml==0.6.0
entrypoints==0.3
flake8==3.7.8
Flask==1.1.2
future==0.18.2
gast==0.3.3
google-auth==1.15.0
google-auth-oauthlib==0.4.1
google-pasta==0.2.0
grpcio==1.29.0
gunicorn==20.0.4
h5py==2.10.0
idna==2.9
importlib-metadata==1.6.0
ipykernel==5.3.0
ipython==7.14.0
ipython-genutils==0.2.0
ipywidgets==7.5.1
itsdangerous==1.1.0
jedi==0.17.0
Jinja2==2.11.2
joblib==0.15.1
jsonschema==3.2.0
jupyter==1.0.0
jupyter-client==6.1.3
jupyter-console==6.1.0
jupyter-core==4.6.3
Keras-Applications==1.0.8
Keras-Preprocessing==1.1.2
Mako==1.1.0
Markdown==3.1.1
MarkupSafe==1.1.1
mccabe==0.6.1
mistune==0.8.4
nbconvert==5.6.1
nbformat==5.0.6
nltk==3.5
notebook==6.0.3
numpy==1.17.2
oauthlib==3.1.0
opencv-python==4.2.0.34
opt-einsum==3.2.1
packaging==20.4
pandocfilters==1.4.2
parso==0.7.0
pdoc3==0.7.0
pexpect==4.8.0
pickleshare==0.7.5
Pillow==6.0.0
prometheus-client==0.8.0
prompt-toolkit==3.0.5
protobuf==3.12.2
ptyprocess==0.6.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycodestyle==2.5.0
pyflakes==2.1.1
Pygments==2.6.1
pyparsing==2.4.7
pyrsistent==0.16.0
python-dateutil==2.8.1
pyzmq==19.0.1
qtconsole==4.7.4
QtPy==1.9.0
regex==2020.6.8
requests==2.23.0
requests-oauthlib==1.3.0
rsa==4.0
scipy==1.4.1
Send2Trash==1.5.0
six==1.15.0
tensorboard==2.2.1
tensorboard-plugin-wit==1.6.0.post3
tensorflow==2.2.0
tensorflow-estimator==2.2.0
termcolor==1.1.0
terminado==0.8.3
testpath==0.4.4
torch==1.5.0
torchvision==0.6.0
tornado==6.0.4
tqdm==4.46.1
traitlets==4.3.3
urllib3==1.25.9
wcwidth==0.1.9
webencodings==0.5.1
Werkzeug==1.0.1
widgetsnbextension==3.5.1
wrapt==1.12.1
zipp==3.1.0
15 changes: 15 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<title>Contextual Thesaurus</title>
<form method="POST">
Word: <input name="word">
Sentence: <input name="sent">
<input type="submit">
</form>
{% if synsets %}
<div>Given Word: {{word}}</div>
<div>Given Sentence: {{sentence}}</div>
<div>Possible Synsets: {{synsets}}</div>
<div>Determined Synset: {{best_words}}</div>
{% else %}
<div>This doesnt really work that well</div>
{% endif %}
Empty file added templates/output.html
Empty file.

0 comments on commit 37b385a

Please sign in to comment.