Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
Better you
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
2022-301
Better you
Commits
473ef24d
Commit
473ef24d
authored
May 24, 2022
by
Malith Anjana
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finalize
parent
f1100075
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
12 deletions
+42
-12
__pycache__/sentiment_analysis.cpython-37.pyc
__pycache__/sentiment_analysis.cpython-37.pyc
+0
-0
app.py
app.py
+41
-11
sentiment_analysis.py
sentiment_analysis.py
+1
-1
No files found.
__pycache__/sentiment_analysis.cpython-37.pyc
View file @
473ef24d
No preview for this file type
app.py
View file @
473ef24d
from
ast
import
Return
import
json
import
json
from
chatterbot
import
ChatBot
from
chatterbot
import
ChatBot
from
flask
import
Flask
,
jsonify
,
request
from
flask
import
Flask
,
jsonify
,
request
...
@@ -9,24 +10,53 @@ print('Loading Sentiment Analysis model')
...
@@ -9,24 +10,53 @@ print('Loading Sentiment Analysis model')
sa
=
Sentiment
()
sa
=
Sentiment
()
app
=
Flask
(
__name__
)
app
=
Flask
(
__name__
)
# bot = ChatBot('Chatterbot', storage_adapter="chatterbot.storage.SQLStorageAdapter")
# trainer = ChatterBotCorpusTrainer(bot
)
sentiment_dict
=
dict
(
)
# trainer.train('./chatterbot_training_dataset/ai.yml', './chatterbot_training_dataset/emotion.yml', './chatterbot_training_dataset/conversational.yml')
#trainer.train('./chatterbot_training_dataset/conversational.yml'
)
score_report
=
list
(
)
#trainer.train('./chatterbot_training_dataset/emotion.yml')
@
app
.
route
(
'/test'
,
methods
=
[
'POST'
])
@
app
.
route
(
'/test'
,
methods
=
[
'POST'
])
def
index_test
():
def
index_test
():
global
sentiment_dict
global
score_report
overall_output
=
dict
()
js_text
=
request
.
get_json
()
js_text
=
request
.
get_json
()
#Initialize the dictionary
if
"start"
in
js_text
:
if
js_text
[
"start"
]:
sentiment_dict
=
dict
()
score_report
=
list
()
else
:
return
jsonify
({
"error"
:
"You have to define the start in the payload"
})
pred
=
sa
.
get_sentiment_prediction
(
js_text
[
'text'
])
pred
=
sa
.
get_sentiment_prediction
(
js_text
[
'text'
])
new_ma_val
=
max
(
pred
.
items
(),
key
=
operator
.
itemgetter
(
1
))[
0
]
max_emotion
=
max
(
pred
.
items
(),
key
=
operator
.
itemgetter
(
1
))[
0
]
max_value
=
max
(
pred
.
items
(),
key
=
operator
.
itemgetter
(
1
))[
1
]
output
=
{
"text"
:
js_text
[
'text'
],
"matchedEmotion"
:
max_emotion
,
"maximumValue"
:
max_value
,
"emotionWithAverage"
:
pred
}
score_report
.
append
(
output
)
if
max_emotion
in
sentiment_dict
:
sentiment_dict
[
max_emotion
]
+=
max_value
else
:
sentiment_dict
[
max_emotion
]
=
max_value
sorted_values
=
{
k
:
v
for
k
,
v
in
sorted
(
sentiment_dict
.
items
(),
key
=
lambda
x
:
x
[
1
],
reverse
=
True
)}
overall_output
=
{
"overalEmotion"
:
list
(
sorted_values
.
keys
())[
0
],
"overalScore"
:
list
(
sorted_values
.
values
())[
0
],
"otherScores"
:
dict
(
zip
(
list
(
sorted_values
.
keys
())[
1
:],
list
(
sorted_values
.
values
())[
1
:]))}
overal_output
=
{
"overall"
:
overall_output
,
"individual"
:
score_report
}
overal_res
=
json
.
dumps
(
overal_output
)
#arr = [ {'emotion' : i, 'value' : pred[i]} for i in pred]
##clear the dictionary
avg
=
json
.
dumps
(
pred
)
if
"end"
in
js_text
:
if
js_text
[
"end"
]:
sentiment_dict
=
dict
()
score_report
=
list
()
#response from chatbot
return
overal_res
# res = str(bot.get_response(js_text['text']))
return
avg
app
.
run
(
debug
=
True
,
port
=
8080
)
app
.
run
(
debug
=
True
,
port
=
8080
)
\ No newline at end of file
sentiment_analysis.py
View file @
473ef24d
...
@@ -148,7 +148,7 @@ class Sentiment:
...
@@ -148,7 +148,7 @@ class Sentiment:
def
load_model_weights
(
self
,
model
):
def
load_model_weights
(
self
,
model
):
model
.
load_weights
(
'./model_weights/sentiment_weights.h5'
)
model
.
load_weights
(
'./model_weights/sentiment_weights
3
.h5'
)
return
model
return
model
...
...
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