Skip to content

Initial Commit #103

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

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

25 changes: 25 additions & 0 deletions EmotionDetection/emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import json
import requests


def emotion_detector(text_to_analyse):
url = 'https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict'
myobj = { "raw_document": { "text": text_to_analyse } }
header = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"}
response = requests.post(url, json = myobj, headers=header)
json_str = response.text
data = json.loads(json_str)
emotion_scores = data['emotionPredictions'][0]['emotion']
dominant_emotion = max(emotion_scores, key=emotion_scores.get)
emotion_scores['dominant_emotion'] = dominant_emotion

return(emotion_scores)









17 changes: 17 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from flask import Flask, request, render_template
from EmotionDetection.emotion_detection import emotion_detector

app = Flask(__name__)

@app.route('/emotionDetector', methods=['POST', 'GET']) # Changed route name here
def emotions_route():
if request.method == 'POST':
text = request.form.get('text')
if not text or text.strip() == '':
return render_template('index.html', error="Invalid text! Please try again!")
result = emotion_detector(text)
return render_template('index.html', result=result, input_text=text)
return render_template('index.html')

if __name__ == '__main__':
app.run(host='0.0.0.0', port=500, debug=True)
22 changes: 22 additions & 0 deletions test_emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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, 'joy')

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

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

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

result5 = emotion_detector("I am really afraid this will happen")
self.assertEqual(result5, 'fear')


unittest.main()