From df992e2a9d810de757f937df87e8e2b99bf405c5 Mon Sep 17 00:00:00 2001 From: bmaiko Date: Mon, 21 Jul 2025 16:31:47 -0400 Subject: [PATCH 1/8] task 2 finalized --- emotion_detection.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 emotion_detection.py diff --git a/emotion_detection.py b/emotion_detection.py new file mode 100644 index 000000000..ff600bcfa --- /dev/null +++ b/emotion_detection.py @@ -0,0 +1,16 @@ +import requests +import json + +def emotion_detector(text_to_analyse): + # URL of the sentiment analysis service + url = 'https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict' + + # Constructing the request payload in the expected format + myobj = { "raw_document": { "text": text_to_analyse } } + + # Custom header specifying the model ID for the sentiment analysis service + header = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"} + + # Sending a POST request to the sentiment analysis API + response = requests.post(url, json = myobj, headers=header) # Send a POST request to the API with the text and headers + return response.text # Return the response text from the API \ No newline at end of file From 8b72e0648cd87c0a208823348a6164ddc2bd510a Mon Sep 17 00:00:00 2001 From: bmaiko Date: Mon, 21 Jul 2025 17:29:28 -0400 Subject: [PATCH 2/8] task 3 ongoing --- emotion_detection.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/emotion_detection.py b/emotion_detection.py index ff600bcfa..f65ac1dbb 100644 --- a/emotion_detection.py +++ b/emotion_detection.py @@ -2,15 +2,28 @@ import json def emotion_detector(text_to_analyse): - # URL of the sentiment analysis service url = 'https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict' - - # Constructing the request payload in the expected format myobj = { "raw_document": { "text": text_to_analyse } } - - # Custom header specifying the model ID for the sentiment analysis service header = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"} - - # Sending a POST request to the sentiment analysis API - response = requests.post(url, json = myobj, headers=header) # Send a POST request to the API with the text and headers - return response.text # Return the response text from the API \ No newline at end of file + response = requests.post(url, json=myobj, headers=header) + formatted_response = json.loads(response.text) + if all(value is None for value in text_to_analyse.values()): + return detected_text + emotions = detected_text['emotionPredictions'][0]['emotion'] + dominant_emotion = max(emotions.items(), key=lambda x: x[1])[0] + anger = emotions['anger'] + disgust = emotions['disgust'] + fear = emotions['fear'] + joy = emotions['joy'] + sadness = emotions['sadness'] + max_emotion = max(emotions, key=emotions.get) + #max_emotion_score = emotions[max_emotion] + formatted_dict_emotions = { + 'anger': anger, + 'disgust': disgust, + 'fear': fear, + 'joy': joy, + 'sadness': sadness, + 'dominant_emotion': max_emotion + } + return formatted_dict_emotions \ No newline at end of file From 489b95b87b7b39cd1d15c86d3200c9b98f1bf45d Mon Sep 17 00:00:00 2001 From: Maiko Blaukopf Date: Fri, 25 Jul 2025 14:44:14 -0400 Subject: [PATCH 3/8] tryout2 --- emotion_detection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/emotion_detection.py b/emotion_detection.py index f65ac1dbb..390362417 100644 --- a/emotion_detection.py +++ b/emotion_detection.py @@ -1,7 +1,7 @@ import requests import json -def emotion_detector(text_to_analyse): +def emotion_detector(detected_text): 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"} @@ -26,4 +26,4 @@ def emotion_detector(text_to_analyse): 'sadness': sadness, 'dominant_emotion': max_emotion } - return formatted_dict_emotions \ No newline at end of file + return formatted_dict_emotions From ee274108c2977dd95c299b76a537ebdcbcf3813a Mon Sep 17 00:00:00 2001 From: Maiko Blaukopf Date: Fri, 25 Jul 2025 14:49:37 -0400 Subject: [PATCH 4/8] tryout3 --- emotion_detection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/emotion_detection.py b/emotion_detection.py index 390362417..2bd9432d5 100644 --- a/emotion_detection.py +++ b/emotion_detection.py @@ -3,11 +3,11 @@ def emotion_detector(detected_text): url = 'https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict' - myobj = { "raw_document": { "text": text_to_analyse } } + myobj = { "raw_document": { "text": detected_text } } header = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"} response = requests.post(url, json=myobj, headers=header) formatted_response = json.loads(response.text) - if all(value is None for value in text_to_analyse.values()): + if all(value is None for value in detected_text.values()): return detected_text emotions = detected_text['emotionPredictions'][0]['emotion'] dominant_emotion = max(emotions.items(), key=lambda x: x[1])[0] From a5fca7d694515401dcaa8a0d896e64264efe3391 Mon Sep 17 00:00:00 2001 From: Maiko Blaukopf Date: Fri, 25 Jul 2025 15:07:46 -0400 Subject: [PATCH 5/8] task 3 finalized --- emotion_detection.py | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/emotion_detection.py b/emotion_detection.py index 2bd9432d5..c22b6c065 100644 --- a/emotion_detection.py +++ b/emotion_detection.py @@ -1,29 +1,18 @@ import requests import json -def emotion_detector(detected_text): - url = 'https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict' - myobj = { "raw_document": { "text": detected_text } } +def emotion_detector(text_to_analyze): + 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"} - response = requests.post(url, json=myobj, headers=header) + input_json = { "raw_document": { "text": text_to_analyze } } + response = requests.post(url, json=input_json, headers=header) + + + emotions = {} + formatted_response = json.loads(response.text) - if all(value is None for value in detected_text.values()): - return detected_text - emotions = detected_text['emotionPredictions'][0]['emotion'] - dominant_emotion = max(emotions.items(), key=lambda x: x[1])[0] - anger = emotions['anger'] - disgust = emotions['disgust'] - fear = emotions['fear'] - joy = emotions['joy'] - sadness = emotions['sadness'] - max_emotion = max(emotions, key=emotions.get) - #max_emotion_score = emotions[max_emotion] - formatted_dict_emotions = { - 'anger': anger, - 'disgust': disgust, - 'fear': fear, - 'joy': joy, - 'sadness': sadness, - 'dominant_emotion': max_emotion - } - return formatted_dict_emotions + emotions = formatted_response['emotionPredictions'][0]['emotion'] + dominant_emotion = max(emotions.items(), key=lambda x: x[1]) + emotions['dominant_emotion'] = dominant_emotion[0] + + return emotions From 2ecdf94631084c03abc3f5ac21441fc311ede2df Mon Sep 17 00:00:00 2001 From: Maiko Blaukopf Date: Fri, 25 Jul 2025 15:14:32 -0400 Subject: [PATCH 6/8] task 4 ongoing --- EmotionDetection/__init__.py | 1 + EmotionDetection/emotion_detection.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 EmotionDetection/__init__.py create mode 100644 EmotionDetection/emotion_detection.py 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..c22b6c065 --- /dev/null +++ b/EmotionDetection/emotion_detection.py @@ -0,0 +1,18 @@ +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' + header = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"} + input_json = { "raw_document": { "text": text_to_analyze } } + response = requests.post(url, json=input_json, headers=header) + + + emotions = {} + + formatted_response = json.loads(response.text) + emotions = formatted_response['emotionPredictions'][0]['emotion'] + dominant_emotion = max(emotions.items(), key=lambda x: x[1]) + emotions['dominant_emotion'] = dominant_emotion[0] + + return emotions From 302163c5a782ca220558df5cbf0e9342de9202ad Mon Sep 17 00:00:00 2001 From: Maiko Blaukopf Date: Fri, 25 Jul 2025 15:56:55 -0400 Subject: [PATCH 7/8] task 7 done --- EmotionDetection/emotion_detection.py | 21 +++++++++++++------- emotion_detection.py | 18 ----------------- server.py | 28 +++++++++++++++++++++++++++ test_emotion_detection.py | 18 +++++++++++++++++ 4 files changed, 60 insertions(+), 25 deletions(-) delete mode 100644 emotion_detection.py create mode 100644 server.py create mode 100644 test_emotion_detection.py diff --git a/EmotionDetection/emotion_detection.py b/EmotionDetection/emotion_detection.py index c22b6c065..4b4b8548b 100644 --- a/EmotionDetection/emotion_detection.py +++ b/EmotionDetection/emotion_detection.py @@ -6,13 +6,20 @@ def emotion_detector(text_to_analyze): header = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"} input_json = { "raw_document": { "text": text_to_analyze } } response = requests.post(url, json=input_json, headers=header) - + status_code = response.status_code emotions = {} - formatted_response = json.loads(response.text) - emotions = formatted_response['emotionPredictions'][0]['emotion'] - dominant_emotion = max(emotions.items(), key=lambda x: x[1]) - emotions['dominant_emotion'] = dominant_emotion[0] - - return emotions + if status_code == 200: + formatted_response = json.loads(response.text) + emotions = formatted_response['emotionPredictions'][0]['emotion'] + dominant_emotion = max(emotions.items(), key=lambda x: x[1]) + emotions['dominant_emotion'] = dominant_emotion[0] + elif status_code == 400: + emotions['anger'] = None + emotions['disgust'] = None + emotions['fear'] = None + emotions['joy'] = None + emotions['sadness'] = None + emotions['dominant_emotion'] = None + return emotions \ No newline at end of file diff --git a/emotion_detection.py b/emotion_detection.py deleted file mode 100644 index c22b6c065..000000000 --- a/emotion_detection.py +++ /dev/null @@ -1,18 +0,0 @@ -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' - header = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"} - input_json = { "raw_document": { "text": text_to_analyze } } - response = requests.post(url, json=input_json, headers=header) - - - emotions = {} - - formatted_response = json.loads(response.text) - emotions = formatted_response['emotionPredictions'][0]['emotion'] - dominant_emotion = max(emotions.items(), key=lambda x: x[1]) - emotions['dominant_emotion'] = dominant_emotion[0] - - return emotions diff --git a/server.py b/server.py new file mode 100644 index 000000000..34dbc9243 --- /dev/null +++ b/server.py @@ -0,0 +1,28 @@ +from flask import Flask, request, render_template +from EmotionDetection.emotion_detection import emotion_detector + +app = Flask("Emotion Detector") + +@app.route("/emotionDetector") +def emotion_analyzer(): + + text_to_analyse = request.args.get('textToAnalyze') + emotion_result = emotion_detector(text_to_analyse) + anger = emotion_result['anger'] + disgust = emotion_result['disgust'] + fear = emotion_result['fear'] + joy = emotion_result['joy'] + sadness = emotion_result['sadness'] + dominant_emotion = emotion_result['dominant_emotion'] + + if dominant_emotion is None: + return "Invalid text! Please try again" + response_str = f"""For the given statement, the system response is + 'anger': {anger}, 'disgust': {disgust}, 'fear': {fear}, 'joy': {joy}, 'sadness': {sadness}. + The dominant emotion is {dominant_emotion}.""" + return response_str +@app.route("/") +def render_index_page(): + return render_template('index.html') +if __name__ == "__main__": + 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..e908c6e51 --- /dev/null +++ b/test_emotion_detection.py @@ -0,0 +1,18 @@ +from EmotionDetection.emotion_detection import emotion_detector +import unittest + + +class TestEmotionAnalyzer(unittest.TestCase): + def test_emotion_analyzer(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 From b9866af504cf0ebcb4df621c0739609a335b5dfe Mon Sep 17 00:00:00 2001 From: Maiko Blaukopf Date: Fri, 25 Jul 2025 16:08:14 -0400 Subject: [PATCH 8/8] final commit --- server.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/server.py b/server.py index 34dbc9243..f6a2816d4 100644 --- a/server.py +++ b/server.py @@ -1,3 +1,7 @@ +'''Deploy a Flask application that will allow a user to provide +a text string which will then be analyzed to determine which emotion amongst a set of 5 +is the most likely emotion being conveyed by the given text. +''' from flask import Flask, request, render_template from EmotionDetection.emotion_detection import emotion_detector @@ -5,7 +9,10 @@ @app.route("/emotionDetector") def emotion_analyzer(): - + '''Retrieve the provided text string from the user, then pass the text + to be analyzed by the emotion detector. Finally, return a response displaying + the confidence scores across all emotions and the dominant emotion. + ''' text_to_analyse = request.args.get('textToAnalyze') emotion_result = emotion_detector(text_to_analyse) anger = emotion_result['anger'] @@ -17,12 +24,19 @@ def emotion_analyzer(): if dominant_emotion is None: return "Invalid text! Please try again" + response_str = f"""For the given statement, the system response is 'anger': {anger}, 'disgust': {disgust}, 'fear': {fear}, 'joy': {joy}, 'sadness': {sadness}. The dominant emotion is {dominant_emotion}.""" return response_str + @app.route("/") def render_index_page(): + '''Render the index page to the user, this is where the text string to be + analyzed is provided and a response is displayed back to the user. + ''' return render_template('index.html') + if __name__ == "__main__": - app.run(host="0.0.0.0", port=5000) \ No newline at end of file + app.run(host="0.0.0.0", port=5000) + \ No newline at end of file