Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2021-049
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2021-049
2021-049
Commits
aad0638c
Commit
aad0638c
authored
Nov 21, 2021
by
Hasitha Samarasekara
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add flask API to the GitLab
parent
5fdf3eef
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
193 additions
and
0 deletions
+193
-0
FlaskAPI/review_prediction/Procfile
FlaskAPI/review_prediction/Procfile
+1
-0
FlaskAPI/review_prediction/README.md
FlaskAPI/review_prediction/README.md
+13
-0
FlaskAPI/review_prediction/app/main.py
FlaskAPI/review_prediction/app/main.py
+168
-0
FlaskAPI/review_prediction/app/model.h5
FlaskAPI/review_prediction/app/model.h5
+0
-0
FlaskAPI/review_prediction/app/tokenizer.pickle
FlaskAPI/review_prediction/app/tokenizer.pickle
+0
-0
FlaskAPI/review_prediction/requirements.txt
FlaskAPI/review_prediction/requirements.txt
+6
-0
FlaskAPI/review_prediction/wsgi.py
FlaskAPI/review_prediction/wsgi.py
+5
-0
No files found.
FlaskAPI/review_prediction/Procfile
0 → 100644
View file @
aad0638c
web: gunicorn --bind 0.0.0.0:$PORT wsgi:app
\ No newline at end of file
FlaskAPI/review_prediction/README.md
0 → 100644
View file @
aad0638c
# 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`
FlaskAPI/review_prediction/app/main.py
0 → 100644
View file @
aad0638c
# 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
})
FlaskAPI/review_prediction/app/model.h5
0 → 100644
View file @
aad0638c
File added
FlaskAPI/review_prediction/app/tokenizer.pickle
0 → 100644
View file @
aad0638c
File added
FlaskAPI/review_prediction/requirements.txt
0 → 100644
View file @
aad0638c
Flask
numpy
tensorflow==2.0
Pillow
flask_cors
gunicorn
\ No newline at end of file
FlaskAPI/review_prediction/wsgi.py
0 → 100644
View file @
aad0638c
from
app.main
import
app
if
__name__
==
"__main__"
:
app
.
run
(
debug
=
True
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment