diff --git a/CapturasPantalla.rar b/CapturasPantalla.rar new file mode 100644 index 00000000..c6f8f522 Binary files /dev/null and b/CapturasPantalla.rar differ diff --git a/EmotionDetection/__init__.py b/EmotionDetection/__init__.py new file mode 100644 index 00000000..d6907336 --- /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 00000000..652a5fc3 --- /dev/null +++ b/EmotionDetection/emotion_detection.py @@ -0,0 +1,43 @@ +import requests +import json + +def emotion_detector(text_to_analyze): + url = "https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict" + + headers = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"} + + body = { "raw_document": { "text": text_to_analyze } } + + response = requests.post(url, json=body, headers=headers) + + if response.status_code == 400 or response.status_code == 500: + emotionsError = { + 'anger': None, + 'disgust': None, + 'fear': None, + 'joy': None, + 'sadness': None, + 'dominant_emotion' : None + } + + return emotionsError + + formatted_response = json.loads(response.text) + + anger_score = formatted_response["emotionPredictions"][0]["emotion"]["anger"] + disgust_score = formatted_response["emotionPredictions"][0]["emotion"]["disgust"] + fear_score = formatted_response["emotionPredictions"][0]["emotion"]["fear"] + joy_score = formatted_response["emotionPredictions"][0]["emotion"]["joy"] + sadness_score = formatted_response["emotionPredictions"][0]["emotion"]["sadness"] + + emotions = { + 'anger': anger_score, + 'disgust': disgust_score, + 'fear': fear_score, + 'joy': joy_score, + 'sadness': sadness_score, + } + + emotions['dominant_emotion'] = max(emotions, key = emotions.get) + + return emotions \ No newline at end of file diff --git a/README.md b/README.md index 7f22b432..660f3da0 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -# Repository for final project +# Repository for final project "Emotion Detector" + +![screeshot](screenshot.png) diff --git a/screenshot.png b/screenshot.png new file mode 100644 index 00000000..0728db66 Binary files /dev/null and b/screenshot.png differ diff --git a/server.py b/server.py new file mode 100644 index 00000000..e05775c2 --- /dev/null +++ b/server.py @@ -0,0 +1,33 @@ +"""That application runs an emotion detector.""" +from flask import Flask, render_template, request +from EmotionDetection.emotion_detection import emotion_detector + +app = Flask(__name__) + + +@app.route("/emotionDetector") +def emotion_detector_server(): + """Return the result of the emotions of the text in string format.""" + text_to_analyze = request.args.get('textToAnalyze') + + result = emotion_detector(text_to_analyze) + + if result['dominant_emotion'] is None: + return "Invalid text! Please try again!." + + string_result = "For the given statement, the system response is " + string_result += f"'anger': {result['anger']}, 'disgust': {result['disgust']}, " + string_result += f"'fear': {result['fear']}, 'joy': {result['joy']} and " + string_result += f"'sadness': {result['sadness']}. " + string_result += f"The dominant emotion is {result['dominant_emotion']}." + + return string_result + +@app.route("/") +def index(): + """Return the index page.""" + return render_template('index.html') + + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000) diff --git a/test_emotion_detection.py b/test_emotion_detection.py new file mode 100644 index 00000000..915d80d6 --- /dev/null +++ b/test_emotion_detection.py @@ -0,0 +1,22 @@ +import unittest +from EmotionDetection.emotion_detection import emotion_detector + +class TestEmotion(unittest.TestCase): + + def test_emotions(self): + joy = emotion_detector("I am glad this happened")["dominant_emotion"] + self.assertEqual(joy, "joy") + + anger = emotion_detector("I am really mad about this")["dominant_emotion"] + self.assertEqual(anger, "anger") + + disgust = emotion_detector("I feel disgusted just hearing about this")["dominant_emotion"] + self.assertEqual(disgust, "disgust") + + sadness = emotion_detector("I am so sad about this")["dominant_emotion"] + self.assertEqual(sadness, "sadness") + + fear = emotion_detector("I am really afraid that this will happen")["dominant_emotion"] + self.assertEqual(fear, "fear") + +unittest.main() \ No newline at end of file