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

Master #24

Open
wants to merge 2 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
Empty file added EmotionDetection/__init__.py
Empty file.
54 changes: 54 additions & 0 deletions EmotionDetection/emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import requests
import json

def emotion_detector(text_to_analyze):
if not text_to_analyze:
return {
'anger': None,
'disgust': None,
'fear': None,
'joy': None,
'sadness': None,
'dominant_emotion': None
}

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"}
myobj = { "raw_document": { "text": text_to_analyze } }

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

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

formatted_response = json.loads(response.text)

emotion_data = formatted_response['emotionPredictions'][0]['emotion']

anger_score = emotion_data['anger']
disgust_score = emotion_data['disgust']
fear_score = emotion_data['fear']
joy_score = emotion_data['joy']
sadness_score = emotion_data['sadness']

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

dominant_emotion = max(scores, key=scores.get)

return {'anger': anger_score, 'disgust': disgust_score,'fear': fear_score,
'joy': joy_score, 'sadness': sadness_score,
'dominant_emotion': dominant_emotion}

31 changes: 31 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
""" Import flask, render_tempalate, request from flask
import emotion_detector from EmotionDetection.emotion_detection package
"""
from flask import Flask, render_template, request
from EmotionDetection.emotion_detection import emotion_detector
# Initiate the Flask app
app = Flask('Emotion Detection')

@app.route('/emotionDetector')
def analyzer():
""" Analysis the response and return the sentiment emotion and dominent emotion """
# Retrieve the text to analyze from the request arguments
text_to_analyze = request.args.get('textToAnalyze')
# Pass the text to the sentiment_analyzer function and store the response
response = emotion_detector(text_to_analyze)

if response['dominant_emotion'] is None:
return 'Invalid text! Please try again!.'
# Return a formatted string with the sentiment emotion and dominent emotion
return f"For the given statement, the system response is 'anger': {response['anger']},\
'disgust': {response['disgust']}, 'fear': {response['fear']}, 'joy': {response['joy']},\
and 'sadness': {response['sadness']}. The dominant emotion is {response['dominant_emotion']}."

@app.route('/')
def render_index_page():
""" Render an index.html file"""
return render_template('index.html')

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

class TestEmotionDetector(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()