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

Add files via upload #23

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

def emotion_detector(text_to_analyze):
# Check if the input text is blank
if not text_to_analyze.strip():
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'
myobj = {"raw_document": {"text": text_to_analyze}}
header = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"}

# Make the POST request
response = requests.post(url, json=myobj, headers=header)

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

# Parse the JSON response if the request was successful
formatted_response = json.loads(response.text)

# Extract emotion scores
emotions = formatted_response['emotionPredictions'][0]['emotion']

# Determine the dominant emotion
dominant_emotion = max(emotions, key=emotions.get)

# Format the output
output = {
'anger': emotions['anger'],
'disgust': emotions['disgust'],
'fear': emotions['fear'],
'joy': emotions['joy'],
'sadness': emotions['sadness'],
'dominant_emotion': dominant_emotion
}

return output
45 changes: 45 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Flask application for emotion detection.
"""

from flask import Flask, render_template, request
from EmotionDetection.emotion_detection import emotion_detector

app = Flask(__name__)

@app.route("/emotionDetector")
def sent_detector():
"""
Handle emotion detection requests and return a formatted response.
"""
text_to_analyze = request.args.get('textToAnalyze')
response = emotion_detector(text_to_analyze)

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

anger = response['anger']
disgust = response['disgust']
fear = response['fear']
joy = response['joy']
sadness = response['sadness']
dominant_emotion = response['dominant_emotion']

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

return formatted_response

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

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


class TestEmotionDetector(unittest.TestCase): # Corrected the inheritance here
def test_emotion_detector(self):
result_1 = emotion_detector('I am glad this happened')
self.assertEqual(result_1['dominant_emotion'], 'joy') # Corrected the key to 'dominant_emotion'

result_2 = emotion_detector('I am really mad about this')
self.assertEqual(result_2['dominant_emotion'], 'anger') # Corrected the key to 'dominant_emotion'

result_3 = emotion_detector('I feel disgusted just hearing about this')
self.assertEqual(result_3['dominant_emotion'], 'disgust') # Corrected the key to 'dominant_emotion'

result_4 = emotion_detector('I am so sad about this')
self.assertEqual(result_4['dominant_emotion'], 'sadness') # Corrected the key to 'dominant_emotion'

result_5 = emotion_detector('I am really afraid that this will happen')
self.assertEqual(result_5['dominant_emotion'], 'fear') # Corrected the key to 'dominant_emotion'


if __name__ == '__main__': # Ensures the test runs only if this file is executed directly
unittest.main()