Skip to content

Add blank server.py and Emotion processing package #92

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
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, test_emotion_detection
28 changes: 28 additions & 0 deletions EmotionDetection/emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

import requests
import json


def emotion_detector(text_to_analyze):
#def <function_name>(<input_args>):
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)
# return response.text
formatted_response = json.loads(response.text)
final_response = {
'anger': formatted_response["emotionPredictions"][0]['emotion']['anger'],
'fear': formatted_response["emotionPredictions"][0]['emotion']['fear'],
'joy': formatted_response["emotionPredictions"][0]['emotion']['joy'],
'sadness': formatted_response["emotionPredictions"][0]['emotion']['sadness'],
'disgust': formatted_response["emotionPredictions"][0]['emotion']['disgust'],
}
dominant_score, dominant_emotion = 0, ''
for key in final_response.keys():
if final_response[key] > dominant_score:
dominant_score = final_response[key]
dominant_emotion = key

final_response['dominant_emotion'] = dominant_emotion
return final_response
8 changes: 8 additions & 0 deletions EmotionDetection/test_emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from EmotionDetection import emotion_detection

def test_emotion_detector():
assert(emotion_detection.emotion_detector("I am glad this happened")['dominant_emotion'] == 'joy')
assert(emotion_detection.emotion_detector("I am really mad about this")['dominant_emotion'] == 'anger')
assert(emotion_detection.emotion_detector("I feel disgusted just hearing about this")['dominant_emotion'] == 'disgust')
assert(emotion_detection.emotion_detector("I am so sad about this")['dominant_emotion'] == 'sadness')
assert(emotion_detection.emotion_detector("I am really afraid that this will happen")['dominant_emotion'] == 'fear')
2 changes: 2 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from Flask import flask
from