Commit aad0638c authored by Hasitha Samarasekara's avatar Hasitha Samarasekara

Add flask API to the GitLab

parent 5fdf3eef
web: gunicorn --bind 0.0.0.0:$PORT wsgi:app
\ No newline at end of file
# flask-api
* **Emotion Detection Flask API**
* Open the project emotion-recognition (Code -> flask-api -> emotion-recognition)
* Install the required libraries using `pip`.
* Flask - `pip install -U Flask`
* numpy - `pip install numpy`
* tenserflow - `pip install --user --upgrade tensorflow`
* Pillow - `pip install Pillow==2.2.1`
* Flask Cors - `pip install Flask-Cors`
* gunicorn - `pip install gunicorn`
* Download the pretrained model from : https://drive.google.com/file/d/1trPbUpfoVFMxssZxMPorqMbIekbnxhI4/view?usp=sharing
* Place the model inside the app folder. (Code -> flask-api -> emotion-recognition -> app)
* After successfully installing the dependencies run the API with `python wsgi.py`
# Importing the libraries
import time
import numpy as np
from tensorflow import keras
from flask import Flask, request, jsonify
from flask_cors import CORS
from keras.preprocessing.sequence import pad_sequences
from keras.preprocessing.text import Tokenizer
import pickle
# Load Tensorflow and Keras Model
print('Loading Model ...')
model = keras.models.load_model('app/model.h5')
print('Model Loaded')
# Enabling Cors
app = Flask(__name__)
SEQUENCE_LENGTH = 300
POSITIVE = "POSITIVE"
NEGATIVE = "NEGATIVE"
NEUTRAL = "NEUTRAL"
SENTIMENT_THRESHOLDS = (0.6, 0.8)
#tokenizer = Tokenizer()
def conbinenegativeWords(comment):
notWord = 'not'
conbine = 'false'
tempWord = []
FinalText = ''
x = '0'
isSet ='false'
tokens = []
for eachWord in comment.split():
if eachWord in notWord:
if x in '0':
eachWord = 'ABC'
x = '1'
isSet = 'true'
tokens.append(eachWord)
else:
if x in '1':
tempWord.append(eachWord)
eachWord = ''
x = '0'
tokens.append(eachWord)
else:
tokens.append(eachWord)
if isSet in 'true':
FinalText = " ".join(tokens)
for tem in tempWord:
FinalText = FinalText.replace('ABC','not'+tem)
else:
FinalText = comment
return FinalText
# loading
with open('app/tokenizer.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
def decode_sentiment(score, include_neutral=True):
if include_neutral:
label = NEUTRAL
if score <= SENTIMENT_THRESHOLDS[0]:
label = NEGATIVE
elif score >= SENTIMENT_THRESHOLDS[1]:
label = POSITIVE
return label
else:
return NEGATIVE if score < 0.5 else POSITIVE
#return label, score and Time
def predict(text, include_neutral=True):
start_at = time.time()
# Tokenize text
x_test = pad_sequences(tokenizer.texts_to_sequences([text]), maxlen=SEQUENCE_LENGTH)
# Predict
score = model.predict([x_test])[0]
# Decode sentiment
label = decode_sentiment(score, include_neutral=include_neutral)
return {"label": label, "score": float(score),
"elapsed_time": time.time()-start_at}
#Return Only score
def predict_OnlyAccuracy(text, include_neutral=True):
start_at = time.time()
# Tokenize text
x_test = pad_sequences(tokenizer.texts_to_sequences([text]), maxlen=SEQUENCE_LENGTH)
# Predict
score = model.predict([x_test])[0]
# Decode sentiment
label = decode_sentiment(score, include_neutral=include_neutral)
return float(score)
# Test Route
@app.route('/')
def index():
return "<h1>Review Prediction Flask API !!</h1>"
# Review Prediction Route
@app.route('/review_prediction',methods=['POST'])
def review_prediction():
'''
For direct API calls trought request
'''
data = request.get_json(force=True)
start = time.time()
reviewResultsList = []
for var in data['review']:
reviewResultsList.append(predict(conbinenegativeWords(var)));
# Getting the prediciton
#prediction_new = predict(conbinenegativeWords(data['review']));
end = time.time()
print('Prediction Time: ',end - start)
return jsonify({"data":data, "result":reviewResultsList})
# Review Prediction Route Get Final value to Tutor
@app.route('/review_prediction_for_tutor',methods=['POST'])
def review_prediction_for_tutor():
'''
For direct API calls trought request
'''
data = request.get_json(force=True)
start = time.time()
reviewResultsList = []
totalScore = 0
for var in data:
print(var);
reviewResultsList.append(predict_OnlyAccuracy(conbinenegativeWords(var)));
for item in reviewResultsList:
totalScore = totalScore + item
finalyPrecentage = (totalScore / len(reviewResultsList)) * 100
end = time.time()
print('Prediction Time: ',end - start)
return jsonify({"result":finalyPrecentage})
Flask
numpy
tensorflow==2.0
Pillow
flask_cors
gunicorn
\ No newline at end of file
from app.main import app
if __name__ == "__main__":
app.run(debug=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment