Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project finished #27

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added CapturasPantalla.rar
Binary file not shown.
1 change: 1 addition & 0 deletions EmotionDetection/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import emotion_detection
43 changes: 43 additions & 0 deletions EmotionDetection/emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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"}

body = { "raw_document": { "text": text_to_analyze } }

response = requests.post(url, json=body, headers=headers)

if response.status_code == 400 or response.status_code == 500:
emotionsError = {
'anger': None,
'disgust': None,
'fear': None,
'joy': None,
'sadness': None,
'dominant_emotion' : None
}

return emotionsError

formatted_response = json.loads(response.text)

anger_score = formatted_response["emotionPredictions"][0]["emotion"]["anger"]
disgust_score = formatted_response["emotionPredictions"][0]["emotion"]["disgust"]
fear_score = formatted_response["emotionPredictions"][0]["emotion"]["fear"]
joy_score = formatted_response["emotionPredictions"][0]["emotion"]["joy"]
sadness_score = formatted_response["emotionPredictions"][0]["emotion"]["sadness"]

emotions = {
'anger': anger_score,
'disgust': disgust_score,
'fear': fear_score,
'joy': joy_score,
'sadness': sadness_score,
}

emotions['dominant_emotion'] = max(emotions, key = emotions.get)

return emotions
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Repository for final project
# Repository for final project "Emotion Detector"

![screeshot](screenshot.png)
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""That application runs an emotion detector."""
from flask import Flask, render_template, request
from EmotionDetection.emotion_detection import emotion_detector

app = Flask(__name__)


@app.route("/emotionDetector")
def emotion_detector_server():
"""Return the result of the emotions of the text in string format."""
text_to_analyze = request.args.get('textToAnalyze')

result = emotion_detector(text_to_analyze)

if result['dominant_emotion'] is None:
return "Invalid text! Please try again!."

string_result = "For the given statement, the system response is "
string_result += f"'anger': {result['anger']}, 'disgust': {result['disgust']}, "
string_result += f"'fear': {result['fear']}, 'joy': {result['joy']} and "
string_result += f"'sadness': {result['sadness']}. "
string_result += f"The dominant emotion is {result['dominant_emotion']}."

return string_result

@app.route("/")
def index():
"""Return the index page."""
return render_template('index.html')


if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
22 changes: 22 additions & 0 deletions test_emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import unittest
from EmotionDetection.emotion_detection import emotion_detector

class TestEmotion(unittest.TestCase):

def test_emotions(self):
joy = emotion_detector("I am glad this happened")["dominant_emotion"]
self.assertEqual(joy, "joy")

anger = emotion_detector("I am really mad about this")["dominant_emotion"]
self.assertEqual(anger, "anger")

disgust = emotion_detector("I feel disgusted just hearing about this")["dominant_emotion"]
self.assertEqual(disgust, "disgust")

sadness = emotion_detector("I am so sad about this")["dominant_emotion"]
self.assertEqual(sadness, "sadness")

fear = emotion_detector("I am really afraid that this will happen")["dominant_emotion"]
self.assertEqual(fear, "fear")

unittest.main()