Skip to content
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
129 changes: 0 additions & 129 deletions .gitignore

This file was deleted.

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
Binary file not shown.
Binary file not shown.
29 changes: 29 additions & 0 deletions EmotionDetection/emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import requests
import json

def emotion_detector(text_to_analyse):
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_analyse}}
response = requests.post(URL, json = input_json, headers = header)
status_code = response.status_code

emotions = {}

if status_code == 200:
formatted_response = json.loads(response.text)
# Format output and inslude dominant emotion
emotions = formatted_response['emotionPredictions'][0]['emotion']
dominant_emotion = max(emotions, key=emotions.get)
# Append to emotions dict
emotions['dominant_emotion'] = dominant_emotion

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
39 changes: 39 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'''Deploy a Flask application that will allow the user to provide a text string
and return a breakdown of five emotions conveyed by the text
'''
from flask import Flask, request, render_template
from EmotionDetection.emotion_detection import emotion_detector

app = Flask("Emotion Detector")

@app.route('/emotionDetector')
def emotion_analyzer():
'''Retrieves (get) the user input text string, pass the text into the
emotion_detector function inside of emotion_detection.py. Error handeling
'''
text_to_analyse = request.args.get('textToAnalyze')
emotion_result = emotion_detector(text_to_analyse)

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

formatted_result = (
f"For the given statement, the system response is "
f"'anger': {emotion_result['anger']}, "
f"'disgust': {emotion_result['disgust']}, "
f"'fear': {emotion_result['fear']}, "
f"'joy': {emotion_result['joy']} and "
f"'sadness': {emotion_result['sadness']}. "
f"The dominant emotion is {emotion_result['dominant_emotion']}."
)
return formatted_result

@app.route("/")
def render_index_page():
'''Render the index page to the user. This is the page that prompts the user
to enter text into the box and emotion detection will be returned
'''
return render_template('index.html')

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

class TestEmotionDetection(unittest.TestCase):
def test_emotion_detector(self):
result1 = emotion_detector('I am glad this happened')
self.assertEqual(result1['dominant_emotion'], 'joy')

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

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

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

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

unittest.main()