From 7acf0be1512a8f5fff728828ea3e899030b419a7 Mon Sep 17 00:00:00 2001 From: "rajarshi.r" Date: Mon, 18 Aug 2025 10:51:50 +0530 Subject: [PATCH] Changed made for project --- EmotionDetection/__init__.py | 1 + EmotionDetection/emotion_detection.py | 27 ++++++++++++++++ server.py | 46 +++++++++++++++++++++++++++ test_emotion_detection.py | 20 ++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 EmotionDetection/__init__.py create mode 100644 EmotionDetection/emotion_detection.py create mode 100644 server.py create mode 100644 test_emotion_detection.py 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..bb45058d4 --- /dev/null +++ b/EmotionDetection/emotion_detection.py @@ -0,0 +1,27 @@ +import json + +import requests + + +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, timeout=200) + status_code = response.status_code + + 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 diff --git a/server.py b/server.py new file mode 100644 index 000000000..f17458599 --- /dev/null +++ b/server.py @@ -0,0 +1,46 @@ +"""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, render_template, request + +from EmotionDetection.emotion_detection import emotion_detector + +app = Flask("Emotion Detector") + + +@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"] + 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(): + """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) diff --git a/test_emotion_detection.py b/test_emotion_detection.py new file mode 100644 index 000000000..07e75d7d9 --- /dev/null +++ b/test_emotion_detection.py @@ -0,0 +1,20 @@ +import unittest + +from EmotionDetection.emotion_detection import emotion_detector + + +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()