-
Notifications
You must be signed in to change notification settings - Fork 13k
Expand file tree
/
Copy pathemotion_detection.py
More file actions
27 lines (22 loc) · 1010 Bytes
/
emotion_detection.py
File metadata and controls
27 lines (22 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import json
import requests
def emotion_detector(text_to_analyze):
url = "https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict"
header = {"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=header, timeout=200)
status_code = response.status_code
emotions = {}
if status_code == 200:
formatted_response = json.loads(response.text)
emotions = formatted_response["emotionPredictions"][0]["emotion"]
dominant_emotion = max(emotions.items(), key=lambda x: x[1])
emotions["dominant_emotion"] = dominant_emotion[0]
elif status_code == 400:
emotions["anger"] = None
emotions["disgust"] = None
emotions["fear"] = None
emotions["joy"] = None
emotions["sadness"] = None
emotions["dominant_emotion"] = None
return emotions