-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
64 lines (41 loc) · 1.57 KB
/
app.py
File metadata and controls
64 lines (41 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from flask import Flask, request, render_template
import numpy
import re
import pandas as pd
import numpy as np
import keras
import pickle
import os
import tensorflow as tf
from tensorflow import keras
from keras.layers import Embedding
from keras.callbacks import ModelCheckpoint
from keras.preprocessing.text import Tokenizer
app = Flask(__name__)
model = tf.keras.models.load_model('Humourous Phrases Generator.keras')
dataX = np.load('dataX.npy')
with open('tokenizer.pkl', 'rb') as f:
tokenizer = pickle.load(f)
reverse_word_map = dict(map(reversed, tokenizer.word_index.items()))
def next_tokens(input_str, n):
final_string = ''
for i in range(n):
token = tokenizer.texts_to_sequences([input_str])[0]
prediction = model.predict([token], verbose=0)
lista = list(range(0,9312))
word = reverse_word_map[numpy.random.choice(a = lista, p = prediction[0])]
final_string = final_string + word + ' '
input_str = input_str + ' ' + word
input_str = ' '.join(input_str.split(' ')[1:])
return final_string
@app.route('/')
def main():
return render_template('html.html', humor='')
@app.route('/', methods=['POST'])
def generate_clickbait():
if request.method == 'POST':
start = numpy.random.randint(0, len(dataX)-1)
pattern = dataX[start]
input_str = ' '.join([reverse_word_map[value] for value in pattern])
output = next_tokens(input_str, 50)
return render_template('html.html', humor=output)