-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
62 lines (41 loc) · 1.67 KB
/
app.py
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
from flask import Flask, render_template, flash, request, url_for, redirect, session
import numpy as np
import re
import os
import tensorflow as tf
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.text import Tokenizer
import pickle
IMAGE_FOLDER = os.path.join('static', 'img_pool')
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = IMAGE_FOLDER
with open('tokenizer.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
def init():
global model,graph
graph = tf.Graph()
@app.route('/', methods=['GET', 'POST'])
def home():
return render_template("index.html")
@app.route('/sentiment_prediction', methods = ['POST', "GET"])
def sent_anly_prediction():
if request.method=='POST':
text = request.form['text']
tw = tokenizer.texts_to_sequences([text])
tw = sequence.pad_sequences(tw,maxlen=200)
with graph.as_default():
# load the pre-trained Keras model
model = load_model('sentiment_analysis.h5')
probability = model.predict(tw)[0][0]
prediction = int(model.predict(tw).round().item())
if prediction == 0:
sentiment = 'Negative'
img_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'sad.gif')
else:
sentiment = 'Positive'
img_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'happy.gif')
return render_template('index.html', text=text, sentiment=sentiment, probability=probability, image=img_filename)
init()
app.run(debug=True)