diff --git a/EmotionDetection/__init__.py b/EmotionDetection/__init__.py new file mode 100644 index 000000000..0b7bf8661 --- /dev/null +++ b/EmotionDetection/__init__.py @@ -0,0 +1 @@ +from .emotion_detection import emotion_detector diff --git a/EmotionDetection/emotion_detection.py b/EmotionDetection/emotion_detection.py new file mode 100644 index 000000000..2151d9805 --- /dev/null +++ b/EmotionDetection/emotion_detection.py @@ -0,0 +1,38 @@ +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 = { + "Content-Type": "application/json", + "grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock" + } + input_json = { "raw_document": { "text": text_to_analyze } } + + response = requests.post(url, headers=headers, json=input_json) + + if response.status_code == 400: + return { + 'anger': None, + 'disgust': None, + 'fear': None, + 'joy': None, + 'sadness': None, + 'dominant_emotion': None + } + + result = json.loads(response.text) + emotions = result['emotionPredictions'][0]['emotion'] + + scores = { + 'anger': emotions.get('anger', 0), + 'disgust': emotions.get('disgust', 0), + 'fear': emotions.get('fear', 0), + 'joy': emotions.get('joy', 0), + 'sadness': emotions.get('sadness', 0) + } + + dominant_emotion = max(scores, key=scores.get) + + scores['dominant_emotion'] = dominant_emotion + return scores diff --git a/server.py b/server.py new file mode 100644 index 000000000..0f2acdcba --- /dev/null +++ b/server.py @@ -0,0 +1,38 @@ +"""Flask web server for emotion detection.""" + +from flask import Flask, request, render_template +from EmotionDetection import emotion_detector + +app = Flask(__name__) + +@app.route('/') +def index(): + """Render the main HTML page with input form.""" + return render_template('index.html') + +@app.route('/emotionDetector', methods=['GET']) +def detect_emotion(): + """Handle emotion detection logic and return formatted output or error.""" + text = request.args.get('textToAnalyze') # Get the input from the query string + result = emotion_detector(text) + + # Error handling for blank input or 400 status from Watson API + if result['dominant_emotion'] is None: + return "Invalid text! Please try again!" + + # Create response string for display + response_str = ( + f"For the given statement, the system response is " + f"'anger': {result['anger']}, " + f"'disgust': {result['disgust']}, " + f"'fear': {result['fear']}, " + f"'joy': {result['joy']} and " + f"'sadness': {result['sadness']}. " + f"The dominant emotion is {result['dominant_emotion']}." + ) + + return response_str + +if __name__ == '__main__': + app.run(debug=True) + diff --git a/test_emotion_detection.py b/test_emotion_detection.py new file mode 100644 index 000000000..2374f7caa --- /dev/null +++ b/test_emotion_detection.py @@ -0,0 +1,26 @@ +import unittest +from EmotionDetection import emotion_detector + +class TestEmotionDetection(unittest.TestCase): + def test_joy(self): + result = emotion_detector("I am glad this happened") + self.assertEqual(result['dominant_emotion'], 'joy') + + def test_anger(self): + result = emotion_detector("I am really mad about this") + self.assertEqual(result['dominant_emotion'], 'anger') + + def test_disgust(self): + result = emotion_detector("I feel disgusted just hearing about this") + self.assertEqual(result['dominant_emotion'], 'disgust') + + def test_sadness(self): + result = emotion_detector("I am so sad about this") + self.assertEqual(result['dominant_emotion'], 'sadness') + + def test_fear(self): + result = emotion_detector("I am really afraid that this will happen") + self.assertEqual(result['dominant_emotion'], 'fear') + +if __name__ == '__main__': + unittest.main()