Monday, 23 September 2019

django tensorflow

project urlhttps://chuanshuoge-movie-review.herokuapp.com/movie_review/
code linkhttps://github.com/chuanshuoge6/django-tensorflow-movieReview


fill in negative review, click predict

feed back is negative

paste in positive review, predict

prediction is positive

#movie_review/views

from django.shortcuts import render
from django.http import  HttpResponse
import tensorflow as td
from tensorflow import keras
import numpy as np

data = keras.datasets.imdb
#dictionary {word: index, word2, index2,...}
word_index = data.get_word_index()

def review_encode(s):
    #1 means 'start line'
    encoded = [1]

    for word in s:
         #translate human words to machine digits
        if word.lower() in word_index:
            encoded.append(word_index[word.lower()])
        else:
            #2 means 'word not found in dictionary'
            encoded.append(2)

    return encoded

def index(request):
    p = ''
    w = ''
    #model generated from machine learning
    model = keras.models.load_model('model.h5')
    # assume review is either negative or positive
    class_names = ['negative', 'positive']
    try:
        review_posted = request.POST['review']
    except Exception:
        pass
    else:
        nline = review_posted.replace(',', '').replace('(', '').replace(')', '').replace(':', '')\
            .replace("\"",'').strip().split(' ')
         #word -> digits
        encode = review_encode(nline)
        encode = keras.preprocessing.sequence.pad_sequences([encode], value=0, padding='post', maxlen=256)
        w = encode
        predict = model.predict(encode)
        print(review_posted)
        print(encode)
        result = class_names[int(round(predict[0][0]))]
        p = str(predict[0][0]) + ' ' + result
        print(predict[0], result)

    return render(request, 'index.html', {'prediction': p, 'wordMap': w})

--------------------------------------
#templates/index

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Movie Review</title>
</head>
<body>
    <h2>Predict whether movie review is positive or negative</h2>

    <form action="{% url 'movie_review:index'  %}" method="post">
        {% csrf_token %}
        <label>Copy and paste movie review in English to the box below</label><br/>
        <textarea rows="8" cols="50" name='review'></textarea><br/>
        <input type="submit" value="Predict"><br/><br/>
    </form>

    <label>Prediction (0 = most negative, 1 = most positive)</label><br/>
    {{prediction}}<br/><br/>

    <label>AI word mapping, (AI dictionary contains 88584 vocabulary). extra long reviews will be trimmed for prediction</label><br/>
    {{wordMap}}
</body>
</html>

-----------------------------------
#django_keras/settings

TEMPLATES = [
    {
...
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
    },
]

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR),
]

ALLOWED_HOSTS = ['chuanshuoge-movie-review.herokuapp.com', '127.0.0.1']

-------------------------------
#requirements

django==3.0a1
django-jsonview==1.2.0
django-watchman==0.18.0
tensorboard==1.14.0
tensorflow==1.14.0
tensorflow-estimator==1.14.0
numpy==1.16.4
numpydoc==0.9.1
astroid==2.1.0
autopep8==1.4.3
certifi==2019.6.16
chardet==3.0.4
colorama==0.4.1
defusedxml==0.6.0
gunicorn==19.9.0
idna==2.8
isort==4.3.4
lazy-object-proxy==1.3.1
mccabe==0.6.1
oauthlib==3.0.1
pycodestyle==2.4.0
pylint==2.2.2
python3-openid==3.1.0
pytz==2018.9
requests==2.22.0
requests-oauthlib==1.2.0
six==1.12.0
sqlparse==0.2.4
urllib3==1.25.3
wrapt==1.10.11
Keras==2.2.5
Keras-Applications==1.0.8
Keras-Preprocessing==1.1.0
whitenoise==4.1.3

-----------------------------------
#Procfile

release: python manage.py migrate
web: gunicorn django_keras.wsgi

------------------------------------------
#powershell heroku

git init
heroku git:remote -a chuanshuoge-movie-review
heroku buildpacks:set heroku/python
pip install whitenoise gunicorn
git add .
git commit -am "make it better"
git push heroku master

-------------------------------
heroku logs

PS C:\Users\bob\django_keras> git add .
PS C:\Users\bob\django_keras> git commit -am "make it better"
[master 6101daa] make it better
 1 file changed, 3 insertions(+), 1 deletion(-)
PS C:\Users\bob\django_keras> git push heroku master
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 397 bytes | 397.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing requirements with pip
remote:
remote: -----> $ python manage.py collectstatic --noinput
remote:        Using TensorFlow backend.
remote:        158 static files copied to '/tmp/build_060c487a42affc8a12459384682ed518/staticfiles', 463 post-processed.
remote:
remote: -----> Discovering process types
remote:        Procfile declares types -> release, web
remote:
remote: -----> Compressing...
remote:        Done: 295.3M
remote: -----> Launching...
remote:  !     Release command declared: this new release will not be available until the command succeeds.
remote:        Released v6
remote:        https://chuanshuoge-movie-review.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
remote: Running release command...
remote:
remote: Using TensorFlow backend.
remote: Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/imdb_word_index.json
remote: 1646592/1641221 [==============================] - 0s 0us/step
remote: Operations to perform:
remote:   Apply all migrations: admin, auth, contenttypes, sessions
remote: Running migrations:
remote:   No migrations to apply.
To https://git.heroku.com/chuanshuoge-movie-review.git
   084af5d..6101daa  master -> master

-------------------------------------
#powershell github
#delete .git first

git init
git add .
git commit -m '1'
git remote add origin https://github.com/chuanshuoge6/django-tensorflow-movieReview.git
git push -u origin master

------------------------------------
reference:
https://mc.ai/integrate-deep-learning-with-keras-in-django/
https://blog.slinto.sk/tensorflow-on-heroku-good-idea-3e6904105892
http://chuanshuoge2.blogspot.com/2019/09/neural-network-7.html

http://chuanshuoge2.blogspot.com/2019/03/django-13-form.html
http://chuanshuoge2.blogspot.com/2019/03/django-9-render-views.html
https://stackoverflow.com/questions/1926049/django-templatedoesnotexist

3 comments: