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..17a579ad9 --- /dev/null +++ b/EmotionDetection/emotion_detection.py @@ -0,0 +1,48 @@ +import json +import requests + +def emotion_detector(text_to_analyse): + url = 'https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict' + myobj = {"raw_document": {"text": text_to_analyse}} + header = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"} + + response = requests.post(url, json=myobj, headers=header) + + # Error handling for bad request + if response.status_code == 400: + return { + 'anger': None, + 'disgust': None, + 'fear': None, + 'joy': None, + 'sadness': None, + 'dominant_emotion': None + } + + response_dict = json.loads(response.text) + emotions = response_dict['emotionPredictions'][0]['emotion'] + + anger = emotions['anger'] + disgust = emotions['disgust'] + fear = emotions['fear'] + joy = emotions['joy'] + sadness = emotions['sadness'] + + emotion_scores = { + 'anger': anger, + 'disgust': disgust, + 'fear': fear, + 'joy': joy, + 'sadness': sadness + } + + dominant_emotion = max(emotion_scores, key=emotion_scores.get) + + return { + 'anger': anger, + 'disgust': disgust, + 'fear': fear, + 'joy': joy, + 'sadness': sadness, + 'dominant_emotion': dominant_emotion + } diff --git a/server.py b/server.py new file mode 100644 index 000000000..0726e77b9 --- /dev/null +++ b/server.py @@ -0,0 +1,42 @@ +"""Flask server for Emotion Detection Application.""" + +from flask import Flask, render_template, request +from EmotionDetection.emotion_detection import emotion_detector + +# Initialize the Flask application +app = Flask("Emotion Detector") + +@app.route("/emotionDetector") +def emo_detector(): + """ + Endpoint to analyze the emotion in a given text. + Returns formatted emotion scores and dominant emotion, + or an error message for invalid input. + """ + text_to_analyze = request.args.get('textToAnalyze') + result = emotion_detector(text_to_analyze) + + # Handle blank input case + if result['dominant_emotion'] is None: + return "Invalid text! Please try again!" + + response_text = ( + f"For the given statement, the system response is " + f"'anger': {result['anger']}, 'disgust': {result['disgust']}, " + f"'fear': {result['fear']}, 'joy': {result['joy']} and 'sadness': {result['sadness']}. " + f"The dominant emotion is {result['dominant_emotion']}." + ) + + return response_text + +@app.route("/") +def render_index_page(): + """ + Serves the main HTML page. + """ + return render_template('index.html') + +if __name__ == "__main__": + # Run the Flask app on localhost at port 5000. + app.run(host="0.0.0.0", port=5000) + \ No newline at end of file diff --git a/test_emotion_detection.py b/test_emotion_detection.py new file mode 100644 index 000000000..dce38b2ba --- /dev/null +++ b/test_emotion_detection.py @@ -0,0 +1,25 @@ +from EmotionDetection.emotion_detection import emotion_detector +import unittest + +class TestEmotionDetector(unittest.TestCase): + def test_emotion_detector(self): + + result_1 = emotion_detector('I am glad this happened') + self.assertEqual(result_1['dominant_emotion'], 'joy') + + + result_2 = emotion_detector('I am really mad about this') + self.assertEqual(result_2['dominant_emotion'], 'anger') + + + result_3 = emotion_detector('I feel disgusted just hearing about this') + self.assertEqual(result_3['dominant_emotion'], 'disgust') + + + result_4 = emotion_detector('I am so sad about this') + self.assertEqual(result_4['dominant_emotion'], 'sadness') + + result_5 = emotion_detector('I am really afraid that this will happen') + self.assertEqual(result_5['dominant_emotion'], 'fear') + +unittest.main() \ No newline at end of file