Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kartikay1999 committed Jan 26, 2021
1 parent d5f5157 commit 74b2d84
Show file tree
Hide file tree
Showing 89 changed files with 69,195 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Poetry-Generation-Using-Bidirectional-lstm
# python-flask-blog
A flask based blog with automated smtp emailer, pagination, file uploader and sql databse connected
Binary file added __pycache__/model_functs.cpython-38.pyc
Binary file not shown.
166 changes: 166 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
from flask import Flask,render_template,request,session,redirect,jsonify
from flask_sqlalchemy import SQLAlchemy
import json
from datetime import datetime
import os
from werkzeug.utils import secure_filename
import math
from flask_mail import Mail
from model_functs import generate_text,suggestions
from keras.models import load_model
import re
import os

export_path = os.path.join(os.getcwd(), 'models', 'shakespeare_bi_400_ud_loss_0.66')
shake_model=load_model(export_path)

def suggest(seed,model):
suggested=[]
suggested.append(suggestions(seed,model,346).replace('\n','\\n'))
suggested.append(suggestions(seed,model,346//2).replace('\n','\\n'))
suggested.append(suggestions(seed,model,346//4).replace('\n','\\n'))
return suggested






with open('config.json','r') as c:
params=json.load(c)['params']
local_server=True

app=Flask(__name__)
app.secret_key='super-secret-key'
app.config.update(
MAIL_SERVER='smtp.gmail.com',
MAIL_PORT='465',
MAIL_USE_SSL='True',
MAIL_USERNAME=params['gmail_user'],
MAIL_PASSWORD=params['gmail_pwd']
)
app.config['UPLOAD_FOLDER']=params['upload_location']
mail=Mail(app)

if (local_server==True):
app.config['SQLALCHEMY_DATABASE_URI'] = params['local_uri']
else:
app.config['SQLALCHEMY_DATABASE_URI'] = params['prod_uri']
db=SQLAlchemy(app)

class Posts(db.Model):
sno = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50), unique=False, nullable=False)
slug=db.Column(db.String(21))
content = db.Column(db.String(120), unique=False, nullable=False)
date=db.Column(db.String(50))
img_file=db.Column(db.String(12))
tagline=db.Column(db.String(50))


class Contacts(db.Model):
sno = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=False, nullable=False)
phone_no=db.Column(db.String(50))
email = db.Column(db.String(50), unique=False, nullable=False)
msg=db.Column(db.String(50))

@app.route('/',methods=['POST','GET'])
def home():
#PAGINATION LOGIC
text=''
return render_template('index.html',params=params,text=text)

@app.route('/generate_poem',methods=['POST','GET'])
def generate_poem():
#PAGINATION LOGIC
text=''
if(request.method=='POST'):
print('successfully')
author=request.form.get('author')
len_text=request.form.get('len_text')
seed=request.form.get('seed')
print(request.form)
if(author=='Shakespeare'):
text=generate_text(seed, int(len_text), shake_model, 344)
text=text.split('\n')
if len(text[-2])//2>len(text[-1]):
text='\n'.join(text[:-1])
else:
text='\n'.join(text)
return render_template('generative.html',params=params,text=text)




@app.route('/about')
def about():
return render_template('about.html',params=params)

@app.route('/contact',methods=['GET','POST'])
def contact():
if (request.method=='POST'):
name=request.form.get('name')
email=request.form.get('email')
msg=request.form.get('msg')
phone=request.form.get('phone')
entry=Contacts(name=name,phone_no=phone,email=email,msg=msg)
db.session.add(entry)
db.session.commit()
mail.send_message('Send message from '+name,
sender=email,recipients=[params['gmail_user']],
body=msg+phone)
return render_template('contact.html',params=params)


@app.route('/autocomplete', methods=['POST'])
def autocomplete():
if request.method=='POST':
results=[]
seed = request.form.get('seed')
print(seed)
print(type(seed))
results=suggest(seed, shake_model)
return jsonify(results)


@app.route('/dashboard',methods=['GET','POST'])
def dashboard():
if('user' in session and session['user']==params['admin_user']):
posts=Posts.query.all()
return render_template('dashboard.html',params=params,posts=posts)
if(request.method=='POST'):
u_name=request.form.get('uname')
pwd=request.form.get('pwd')
if(u_name==params['admin_user'] and pwd==params['userpass']):
#set the session variable
session['user']=u_name
posts=Posts.query.all()
return render_template('dashboard.html',params=params,posts=posts)
else:
return render_template('login.html',params=params)


@app.route('/post/<string:post_slug>',methods=['GET'])
def post_route(post_slug):
post=Posts.query.filter_by(slug=post_slug).first()

return render_template('post.html',params=params,post=post)
@app.route('/suggestive', methods=['POST','GET'])
def suggestive():
if request.method=='POST':
seed = request.form.get('seed')
print(type(seed))
result=suggest(seed, shake_model)
seed=seed.replace('\n','\\n')

return render_template('post.html',params=params,results=list(set(result)),seed_text=seed)
else:
return render_template('post.html',params=params,results=['','',''],seed_text='')



if __name__ == "__main__":
app.run(debug=True)
#,host='0.0.0.0',port=int(os.environ.get('PORT',8080))
# app.run(debug=False)
19 changes: 19 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{

"params":
{
"local_server":"True",
"local_uri":"mysql://root:@localhost/flaskblog",
"prod_uri":"mysql://root:@localhost/flaskblog",
"fb_url":"https://www.facebook.com/codingthunder",
"blog_name":"Poetry Paradise",
"tag_line":"A rythemic creation of words.",
"gmail_user":"[email protected]",
"gmail_pwd":"PrekshNKV",
"about_text":"Our project is based on using Bi-directional LSTM (Long Short Term Memory), variant of RNN, to generate poetry in English language by feeding it a huge corpus of poems of renowned poets compiled specifically for the research. Our model has two layers with 256 units in each. We are able to analyse the overfitting nature of LSTM through our research.",
"no_of_posts":2,
"admin_user":"qwerty@q",
"userpass":"qwerty",
"upload_location":"C:\\Users\\kartikay\\Desktop\\PY files\\Flask\\static"
}
}
Binary file added download.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions model_functs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from keras.preprocessing.sequence import pad_sequences
import pickle
from tqdm import tqdm
import re


with open('tokenizers/shakes_tokenizer.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)


def generate_text(seed_text, next_words, model, max_sequence_len):
for _ in tqdm(range(next_words)):
token_list = tokenizer.texts_to_sequences([seed_text])[0]
token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
predicted = model.predict_classes(token_list, verbose=0)

output_word = ""
for word,index in tokenizer.word_index.items():
if index == predicted:
output_word = word
break
seed_text += " "+output_word
return seed_text.title()


def suggestions(seed_text, model, max_sequence_len):
generated=''
while True:
token_list = tokenizer.texts_to_sequences([seed_text])[0]
token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
predicted = model.predict_classes(token_list, verbose=0)

output_word = ""
for word,index in tokenizer.word_index.items():
if index == predicted:
output_word = word
break
seed_text += " "+output_word
generated+=' '+output_word
if(len(generated.split(' '))>5):
if re.search('[^a-zA-Z|^\s]',generated)!=None:
break

return generated





6 changes: 6 additions & 0 deletions models/.ipynb_checkpoints/Untitled-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}
63 changes: 63 additions & 0 deletions models/Untitled.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"converter=tf.lite.TFLiteConverter.from_saved_model('shakespeare_bi_400_ud_loss_0.66/')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tflite_model = converter.convert()\n",
"\n",
"# Save the model.\n",
"with open('model.tflite', 'wb') as f:\n",
" f.write(tflite_model)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Keras==2.4.3
Flask==1.1.2
Werkzeug==1.0.1
Flask_SQLAlchemy==2.4.4
Flask_Mail==0.9.1
tqdm==4.47.0
gunicorn==19.0.0
tensorflow-cpu==2.1.0
pickle5==0.0.11
Loading

0 comments on commit 74b2d84

Please sign in to comment.