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

finished all tasks #26

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

def emotion_detector(text):
if not text.strip(): # Check if input is blank or only whitespace
return {
'anger': None,
'disgust': None,
'fear': None,
'joy': None,
'sadness': None,
'dominant_emotion': None
}

# Assuming you have the existing code to detect emotions here
# For demonstration, here's a simplified dictionary:
response = {
'anger': 0.004,
'disgust': 0.001,
'fear': 0.003,
'joy': 0.990,
'sadness': 0.002,
}

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

return response
45 changes: 45 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
server.py

This module sets up a Flask web server with a single endpoint `/emotionDetector` that
accepts POST requests containing text data. It uses the `emotion_detector` function
from the `EmotionDetection` package to analyze the emotions in the text and returns
the results in a formatted response.

The endpoint responds with the emotion scores and the dominant emotion.
"""

from flask import Flask, request, jsonify
from EmotionDetection import emotion_detector

app = Flask(__name__)

@app.route('/emotionDetector', methods=['POST'])
def detect_emotion():
"""
Endpoint to receive a POST request with text data and return emotion analysis.

The endpoint expects JSON data with a 'text' field. It returns the emotion scores
and the dominant emotion as a formatted response.

Returns:
Response object with JSON data containing the emotion analysis or an error message.
"""
data = request.get_json()
text = data.get('text', '').strip()

response = emotion_detector(text)

if response['dominant_emotion'] is None:
return jsonify({"response": "Invalid text! Please try again!"}), 400

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

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

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

def test_anger(self):
self.assertEqual(emotion_detector("I am really mad about this")['dominant_emotion'], 'anger')

def test_disgust(self):
self.assertEqual(emotion_detector("I feel disgusted just hearing about this")['dominant_emotion'], 'disgust')

def test_sadness(self):
self.assertEqual(emotion_detector("I am so sad about this")['dominant_emotion'], 'sadness')

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

if __name__ == '__main__':
unittest.main()