diff --git a/EmotionDetection/__init__.py b/EmotionDetection/__init__.py new file mode 100644 index 000000000..d69073368 --- /dev/null +++ b/EmotionDetection/__init__.py @@ -0,0 +1 @@ +from . import emotion_detection \ No newline at end of file diff --git a/EmotionDetection/emotion_detection.py b/EmotionDetection/emotion_detection.py new file mode 100644 index 000000000..16eeeda59 --- /dev/null +++ b/EmotionDetection/emotion_detection.py @@ -0,0 +1,16 @@ +import requests +import json + +def emotion_detector(text_to_analyze): + # Get emotion detect details + url = 'https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict' + header = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"} + myobj = { "raw_document": { "text": text_to_analyze } } + response = requests.post(url, json = myobj, headers = header) + + # Get Emotion Scores + emotions = json.loads(response.text)['emotionPredictions'][0]['emotion'] + + # Get Dominant Emotion + emotions['dominant_emotion'] = max(emotions, key=emotions.get) + return emotions diff --git a/server.py b/server.py new file mode 100644 index 000000000..1dc091cb6 --- /dev/null +++ b/server.py @@ -0,0 +1,24 @@ +from flask import Flask, request, render_template, url_for, redirect +from EmotionDetection.emotion_detection import emotion_detector + +app = Flask(__name__) + +@app.route('/emotionDetector', methods = ['GET']) +def emotion_detect(): + text_to_analyze = request.args.get('textToAnalyze', '') + if not text_to_analyze: + return render_template('index.html') + + emotions = emotion_detector(text_to_analyze) + response = f"For the given statement, the system response is " + response += f"'anger': {emotions.get('anger')}, " + response += f"'disgust': {emotions.get('disgust')}, " + response += f"'fear': {emotions.get('fear')}, " + response += f"'joy': {emotions.get('joy')}, " + response += f"'sadness': {emotions.get('sadness')}. " + response += f"The dominant emotion is {emotions.get('dominant_emotion')}" + + return response + +if __name__ == "__main__": + app.run(debug=True) \ No newline at end of file diff --git a/test_emotion_detection.py b/test_emotion_detection.py new file mode 100644 index 000000000..f8705a7ef --- /dev/null +++ b/test_emotion_detection.py @@ -0,0 +1,25 @@ +import unittest +from EmotionDetection.emotion_detection import emotion_detector + +class TestEmotionDetector(unittest.TestCase): + def test1(self): + results = emotion_detector('I am glad this happened') + self.assertEqual(results['dominant_emotion'], 'joy') + + def test2(self): + results = emotion_detector('I am really mad about this') + self.assertEqual(results['dominant_emotion'], 'anger') + + def test3(self): + results = emotion_detector('I feel disgusted just hearing about this') + self.assertEqual(results['dominant_emotion'], 'disgust') + + def test4(self): + results = emotion_detector('I am so sad about this') + self.assertEqual(results['dominant_emotion'], 'sadness') + + def test5(self): + results = emotion_detector('I am really afraid that this will happen') + self.assertEqual(results['dominant_emotion'], 'fear') + +unittest.main() \ No newline at end of file