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

def emotion_detector(text_to_analyze):
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"}
input_json = {"raw_document": {"text": text_to_analyze}}

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

response_dict = json.loads(response.text)

emotions = response_dict['emotionPredictions'][0]['emotion']

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

emotion_scores = {
'anger': anger_score,
'disgust': disgust_score,
'fear': fear_score,
'joy': joy_score,
'sadness': sadness_score
}
dominant_emotion = max(emotion_scores, key=emotion_scores.get)

return {
'anger': anger_score,
'disgust': disgust_score,
'fear': fear_score,
'joy': joy_score,
'sadness': sadness_score,
'dominant_emotion': dominant_emotion
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Repository for final project
# Final project