Commit 473ef24d authored by Malith Anjana's avatar Malith Anjana

finalize

parent f1100075
from ast import Return
import json
from chatterbot import ChatBot
from flask import Flask, jsonify, request
......@@ -9,24 +10,53 @@ print('Loading Sentiment Analysis model')
sa = Sentiment()
app = Flask(__name__)
# bot = ChatBot('Chatterbot', storage_adapter="chatterbot.storage.SQLStorageAdapter")
# trainer = ChatterBotCorpusTrainer(bot)
# trainer.train('./chatterbot_training_dataset/ai.yml', './chatterbot_training_dataset/emotion.yml', './chatterbot_training_dataset/conversational.yml')
#trainer.train('./chatterbot_training_dataset/conversational.yml')
#trainer.train('./chatterbot_training_dataset/emotion.yml')
sentiment_dict = dict()
score_report = list()
@app.route('/test', methods=['POST'])
def index_test():
global sentiment_dict
global score_report
overall_output=dict()
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'])
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]
avg = json.dumps(pred)
##clear the dictionary
if "end" in js_text:
if js_text["end"]:
sentiment_dict=dict()
score_report=list()
#response from chatbot
# res = str(bot.get_response(js_text['text']))
return avg
return overal_res
app.run(debug=True, port=8080)
\ No newline at end of file
......@@ -148,7 +148,7 @@ class Sentiment:
def load_model_weights(self,model):
model.load_weights('./model_weights/sentiment_weights.h5')
model.load_weights('./model_weights/sentiment_weights3.h5')
return model
......
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