diff --git a/EmotionDetection/__init__.py b/EmotionDetection/__init__.py new file mode 100644 index 000000000..b4042ae25 --- /dev/null +++ b/EmotionDetection/__init__.py @@ -0,0 +1,5 @@ + +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..2dd2ef2bd 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..6276b0127 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..e0cae637b --- /dev/null +++ b/EmotionDetection/emotion_detection.py @@ -0,0 +1,57 @@ +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 = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"} + payload = {"raw_document": {"text": text_to_analyze}} + + # Verificar si el texto está vacío + if not text_to_analyze.strip(): + return { + 'anger': None, + 'disgust': None, + 'fear': None, + 'joy': None, + 'sadness': None, + 'dominant_emotion': None + } + + response = requests.post(url, headers=headers, json=payload) + + # Manejo de error si el servidor responde con un código 400 + if response.status_code == 400: + return { + 'anger': None, + 'disgust': None, + 'fear': None, + 'joy': None, + 'sadness': None, + 'dominant_emotion': None + } + + response_data = response.json() + emotions = response_data.get("emotionPredictions", [])[0].get("emotion", {}) + + if not emotions: + return { + 'anger': None, + 'disgust': None, + 'fear': None, + 'joy': None, + 'sadness': None, + 'dominant_emotion': None + } + + # Encontrar la emoción dominante + dominant_emotion = max(emotions, key=emotions.get) + + return { + 'anger': emotions.get('anger'), + 'disgust': emotions.get('disgust'), + 'fear': emotions.get('fear'), + 'joy': emotions.get('joy'), + 'sadness': emotions.get('sadness'), + 'dominant_emotion': dominant_emotion + } + diff --git a/__pycache__/emotion_detection.cpython-311.pyc b/__pycache__/emotion_detection.cpython-311.pyc new file mode 100644 index 000000000..f90dd2483 Binary files /dev/null and b/__pycache__/emotion_detection.cpython-311.pyc differ diff --git a/oaqjp-final-project-emb-ai b/oaqjp-final-project-emb-ai new file mode 160000 index 000000000..3e04588b7 --- /dev/null +++ b/oaqjp-final-project-emb-ai @@ -0,0 +1 @@ +Subproject commit 3e04588b7b73640f1afb806d3e6bf3e2a41e8fd2 diff --git a/server.py b/server.py new file mode 100644 index 000000000..b8168ac20 --- /dev/null +++ b/server.py @@ -0,0 +1,45 @@ +from flask import Flask, request, jsonify, render_template +from EmotionDetection import emotion_detector # Import the function + +app = Flask(__name__) + +# Route to serve the web application +@app.route("/") +def home(): + return render_template("index.html") + +# Route to handle emotion detection +@app.route("/emotionDetector", methods=["POST"]) +def detect_emotion(): + try: + # Get text input from request + data = request.get_json() + text_to_analyze = data.get("text", "") + + if not text_to_analyze: + return jsonify({"error": "No text provided"}), 400 + + # Call the emotion detection function + result = emotion_detector(text_to_analyze) + + # Check if dominant_emotion is None + if result.get('dominant_emotion') is None: + return jsonify({"error": "Invalid text! Please try again!"}), 400 + + # Format the response as requested + 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 " + f"'sadness': {result['sadness']}. The dominant emotion is {result['dominant_emotion']}." + ) + + return jsonify({"message": response_text}) + + except Exception as e: + return jsonify({"error": str(e)}), 500 + +if __name__ == "__main__": + app.run(debug=True, host="127.0.0.1", port=5000) + + diff --git a/test_emotion_detection.py b/test_emotion_detection.py new file mode 100644 index 000000000..6ba894414 --- /dev/null +++ b/test_emotion_detection.py @@ -0,0 +1,40 @@ +import unittest +from EmotionDetection import emotion_detector + +class TestEmotionDetector(unittest.TestCase): + """Pruebas unitarias para la función emotion_detector.""" + + def test_joy(self): + """Prueba si la emoción dominante es 'joy'.""" + result = emotion_detector("I am glad this happened") + self.assertIn("dominant_emotion", result) + self.assertEqual(result["dominant_emotion"], "joy") + + def test_anger(self): + """Prueba si la emoción dominante es 'anger'.""" + result = emotion_detector("I am really mad about this") + self.assertIn("dominant_emotion", result) + self.assertEqual(result["dominant_emotion"], "anger") + + def test_disgust(self): + """Prueba si la emoción dominante es 'disgust'.""" + result = emotion_detector("I feel disgusted just hearing about this") + self.assertIn("dominant_emotion", result) + self.assertEqual(result["dominant_emotion"], "disgust") + + def test_sadness(self): + """Prueba si la emoción dominante es 'sadness'.""" + result = emotion_detector("I am so sad about this") + self.assertIn("dominant_emotion", result) + self.assertEqual(result["dominant_emotion"], "sadness") + + def test_fear(self): + """Prueba si la emoción dominante es 'fear'.""" + result = emotion_detector("I am really afraid that this will happen") + self.assertIn("dominant_emotion", result) + self.assertEqual(result["dominant_emotion"], "fear") + +if __name__ == "__main__": + unittest.main() + +