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/__pycache__/__init__.cpython-311.pyc b/EmotionDetection/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 000000000..b870c1304 Binary files /dev/null and b/EmotionDetection/__pycache__/__init__.cpython-311.pyc differ diff --git a/EmotionDetection/__pycache__/emotion_detection.cpython-311.pyc b/EmotionDetection/__pycache__/emotion_detection.cpython-311.pyc new file mode 100644 index 000000000..e58c19200 Binary files /dev/null and b/EmotionDetection/__pycache__/emotion_detection.cpython-311.pyc differ diff --git a/EmotionDetection/emotion_detection.py b/EmotionDetection/emotion_detection.py new file mode 100644 index 000000000..3ae4e0d00 --- /dev/null +++ b/EmotionDetection/emotion_detection.py @@ -0,0 +1,33 @@ +import requests +import json + +def emotion_detector(text_to_analyze): + """ + Sends a POST request to Watson NLP Emotion Detection API and returns the formatted response. + """ + 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"} + payload = {"raw_document": {"text": text_to_analyze}} + + response = requests.post(url, json=payload, headers=headers) + response_dict = json.loads(response.text) # Convert response to dictionary + + # Extract emotion scores + emotions = response_dict.get("emotionPredictions", [{}])[0].get("emotion", {}) + 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) + + # Determine the dominant emotion + dominant_emotion = max(emotions, key=emotions.get, default="unknown") + + return { + "anger": anger, + "disgust": disgust, + "fear": fear, + "joy": joy, + "sadness": sadness, + "dominant_emotion": dominant_emotion + } diff --git a/emotion_detection.py b/emotion_detection.py new file mode 100644 index 000000000..798dafc12 --- /dev/null +++ b/emotion_detection.py @@ -0,0 +1,30 @@ +import requests + +def emotion_detector(text_to_analyze): + """ + Diagnostic version of the emotion_detector function + that prints the entire JSON to identify its structure. + """ + 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", + "Content-Type": "application/json" + } + payload = { + "raw_document": { + "text": text_to_analyze + } + } + + response = requests.post(url, headers=headers, json=payload) + + if response.status_code == 200: + try: + response_json = response.json() + # Print the entire response JSON to see its structure + print("DEBUG: Entire Watson Response:", response_json) + return response_json + except ValueError: + return "Error parsing JSON response." + else: + return f"Error: {response.status_code}, {response.text}" diff --git a/server.py b/server.py new file mode 100644 index 000000000..9d0d70368 --- /dev/null +++ b/server.py @@ -0,0 +1,28 @@ +from flask import Flask, request, jsonify +from EmotionDetection import emotion_detector + +app = Flask(__name__, template_folder="templates") + +@app.route('/') +def home(): + return "Emotion Detection API is running! Use /emotionDetector endpoint to analyze text." + +@app.route('/emotionDetector', methods=['GET']) +def emotion_detector_endpoint(): + text_to_analyze = request.args.get('textToAnalyze', '') + + if not text_to_analyze: + return jsonify({"error": "No text provided for analysis."}), 400 + + response = emotion_detector(text_to_analyze) + + formatted_response = ( + f"For the given statement, the system response is 'anger': {response['anger']}, 'disgust': {response['disgust']}, " + f"'fear': {response['fear']}, 'joy': {response['joy']} and 'sadness': {response['sadness']}. " + f"The dominant emotion is {response['dominant_emotion']}." + ) + + return formatted_response + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000, debug=True) diff --git a/test_emotion_detection.py b/test_emotion_detection.py new file mode 100644 index 000000000..c589f38aa --- /dev/null +++ b/test_emotion_detection.py @@ -0,0 +1,28 @@ +import unittest +from EmotionDetection import emotion_detector + +class TestEmotionDetection(unittest.TestCase): + + def test_emotion_detector(self): + # Test for joy + response = emotion_detector("I am glad this happened") + self.assertEqual(response['dominant_emotion'], "joy") + + # Test for anger + response = emotion_detector("I am really mad about this") + self.assertEqual(response['dominant_emotion'], "anger") + + # Test for disgust + response = emotion_detector("I feel disgusted just hearing about this") + self.assertEqual(response['dominant_emotion'], "disgust") + + # Test for sadness + response = emotion_detector("I am so sad about this") + self.assertEqual(response['dominant_emotion'], "sadness") + + # Test for fear + response = emotion_detector("I am really afraid that this will happen") + self.assertEqual(response['dominant_emotion'], "fear") + +if __name__ == "__main__": + unittest.main()