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

Master #12

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

def emotion_detector(text_to_analyse):
# Define the URL and headers for the API request
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"}

# Handle blank entries
if not text_to_analyse.strip():
return {
'anger': None,
'disgust': None,
'fear': None,
'joy': None,
'sadness': None,
'dominant_emotion': None
}

# Send a POST request to the API with the text and headers
response = requests.post(url, json=myobj, headers=header)

# Check for a 400 status code
if response.status_code == 400:
return {
'anger': None,
'disgust': None,
'fear': None,
'joy': None,
'sadness': None,
'dominant_emotion': None
}

# Convert the response text to a dictionary
response_dict = json.loads(response.text)

# Extract the required set of emotions and their scores
# Accessing the first element in emotionPredictions list and then the emotion dictionary
emotions = response_dict.get('emotionPredictions', [{}])[0].get('emotion', {})

# Extract relevant emotions
anger_score = emotions.get('anger', 0)
disgust_score = emotions.get('disgust', 0)
fear_score = emotions.get('fear', 0)
joy_score = emotions.get('joy', 0)
sadness_score = emotions.get('sadness', 0)

# Find the dominant emotion
relevant_emotions = {
'anger': anger_score,
'disgust': disgust_score,
'fear': fear_score,
'joy': joy_score,
'sadness': sadness_score
}
dominant_emotion = max(relevant_emotions, key=relevant_emotions.get)

# Prepare the output format
result = {
'anger': anger_score,
'disgust': disgust_score,
'fear': fear_score,
'joy': joy_score,
'sadness': sadness_score,
'dominant_emotion': dominant_emotion
}

return result
129 changes: 129 additions & 0 deletions final_project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
1 change: 1 addition & 0 deletions final_project/EmotionDetection/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import emotion_detection
47 changes: 47 additions & 0 deletions final_project/EmotionDetection/emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import requests
import json

def emotion_detector(text_to_analyse):
# Define the URL and headers for the API request
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"}

# Send a POST request to the API with the text and headers
response = requests.post(url, json=myobj, headers=header)

# Convert the response text to a dictionary
response_dict = json.loads(response.text)

# Extract the required set of emotions and their scores
# Accessing the first element in emotionPredictions list and then the emotion dictionary
emotions = response_dict.get('emotionPredictions', [{}])[0].get('emotion', {})

# Extract relevant emotions
anger_score = emotions.get('anger', 0)
disgust_score = emotions.get('disgust', 0)
fear_score = emotions.get('fear', 0)
joy_score = emotions.get('joy', 0)
sadness_score = emotions.get('sadness', 0)

# Find the dominant emotion
relevant_emotions = {
'anger': anger_score,
'disgust': disgust_score,
'fear': fear_score,
'joy': joy_score,
'sadness': sadness_score
}
dominant_emotion = max(relevant_emotions, key=relevant_emotions.get)

# Prepare the output format
result = {
'anger': anger_score,
'disgust': disgust_score,
'fear': fear_score,
'joy': joy_score,
'sadness': sadness_score,
'dominant_emotion': dominant_emotion
}

return result
Loading