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..c3249ba28 --- /dev/null +++ b/EmotionDetection/emotion_detection.py @@ -0,0 +1,47 @@ +import nltk +from nltk.sentiment import SentimentIntensityAnalyzer + +nltk.download("vader_lexicon", quiet=True) # Descarga el léxico si no está disponible + + +def emotion_detector(text_to_analize): + """ + Detecta emociones en un texto utilizando NLTK VADER. + + Args: + text_to_analize (str): El texto para analizar. + + Returns: + dict: Un diccionario con las puntuaciones de emoción y la emoción dominante. + """ + if not text_to_analize: + return { + "anger": None, + "disgust": None, + "fear": None, + "joy": None, + "sadness": None, + "dominant_emotion": None, + } + + analyzer = SentimentIntensityAnalyzer() + scores = analyzer.polarity_scores(text_to_analize) + + emotion_scores = { + "anger": scores["neg"], + "disgust": scores["neg"] / 2, + "fear": scores["neg"] / 2, + "joy": scores["pos"], + "sadness": scores["neg"], + } + + dominant_emotion = max(emotion_scores, key=emotion_scores.get) + + return { + "anger": emotion_scores["anger"], + "disgust": emotion_scores["disgust"], + "fear": emotion_scores["fear"], + "joy": emotion_scores["joy"], + "sadness": emotion_scores["sadness"], + "dominant_emotion": dominant_emotion, + } diff --git a/config.py b/config.py new file mode 100644 index 000000000..3a9b91086 --- /dev/null +++ b/config.py @@ -0,0 +1,4 @@ +# config.py +IBM_API_KEY = "tu_clave_api" +IBM_SERVICE_URL = "tu_url_servicio" + OTHER_VAR = "otro_valor" diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 000000000..1dcd0b3d9 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,13 @@ +[project] +name = "oaqjp-final-project-emb-ai" +version = "0.1.0" +description = "Add your description here" +readme = "README.md" +requires-python = ">=3.13" +dependencies = [ + "flask>=3.1.0", + "ibm-watson>=9.0.0", + "nltk>=3.9.1", + "pylint>=3.3.6", + "requests>=2.32.3", +] diff --git a/server.py b/server.py new file mode 100644 index 000000000..becc58b08 --- /dev/null +++ b/server.py @@ -0,0 +1,46 @@ +""" +Flask application for emotion detection. +""" + +from flask import Flask, jsonify, render_template, request + +from EmotionDetection import emotion_detector + +app = Flask(__name__) + + +@app.route("/") +def index(): + """Renders the index.html template.""" + return render_template("index.html") + + +@app.route("/emotionDetector", methods=["POST"]) +def emotion_detector_endpoint(): + """ + Handles emotion detection requests and returns the result. + + Returns: + str: The emotion detection result as a string. + """ + text_to_analyze = request.form["text"] + result = emotion_detector(text_to_analyze) + + if result["dominant_emotion"] is None: + return "Invalid text! Please try again." + + if "error" in result: + return jsonify({"error": result["error"]}), 500 + + response_text = ( + f"For the given statement, the system response is 'anger': {result['anger']}, " + f"'disgust': {result['disgust']}, 'fear': {result['fear']}, " + f"'joy': {result['joy']} and 'sadness': {result['sadness']}. " + f"The dominant emotion is {result['dominant_emotion']}." + ) + + return response_text + + +if __name__ == "__main__": + app.run(debug=True) diff --git a/static/mywebscript.js b/static/mywebscript.js index 53d424977..69a971039 100644 --- a/static/mywebscript.js +++ b/static/mywebscript.js @@ -1,12 +1,13 @@ -let RunSentimentAnalysis = ()=>{ - textToAnalyze = document.getElementById("textToAnalyze").value; +let RunSentimentAnalysis = () => { + let textToAnalyze = document.getElementById("textToAnalyze").value; let xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { - document.getElementById("system_response").innerHTML = xhttp.responseText; + document.getElementById("system_response").innerHTML = this.responseText; } }; - xhttp.open("GET", "emotionDetector?textToAnalyze"+"="+textToAnalyze, true); - xhttp.send(); + xhttp.open("POST", "/emotionDetector", true); + xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + xhttp.send("text=" + encodeURIComponent(textToAnalyze)); } diff --git a/templates/index.html b/templates/index.html index 1feadd6be..9e90e7137 100644 --- a/templates/index.html +++ b/templates/index.html @@ -11,7 +11,7 @@