diff --git a/.gitignore b/.gitignore deleted file mode 100644 index b6e47617d..000000000 --- a/.gitignore +++ /dev/null @@ -1,129 +0,0 @@ -# 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/ diff --git a/EmotionDetection/__init__.py b/EmotionDetection/__init__.py new file mode 100644 index 000000000..d69073368 --- /dev/null +++ b/EmotionDetection/__init__.py @@ -0,0 +1 @@ +from . import emotion_detection \ No newline at end of file diff --git a/EmotionDetection/__pycache__/__init__.cpython-311.pyc b/EmotionDetection/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 000000000..f75f76cbf Binary files /dev/null and b/EmotionDetection/__pycache__/__init__.cpython-311.pyc differ diff --git a/EmotionDetection/__pycache__/emotion_detection.cpython-311.pyc b/EmotionDetection/__pycache__/emotion_detection.cpython-311.pyc new file mode 100644 index 000000000..cb3b1f5b4 Binary files /dev/null and b/EmotionDetection/__pycache__/emotion_detection.cpython-311.pyc differ diff --git a/EmotionDetection/emotion_detection.py b/EmotionDetection/emotion_detection.py new file mode 100644 index 000000000..7f9533eb6 --- /dev/null +++ b/EmotionDetection/emotion_detection.py @@ -0,0 +1,29 @@ +import requests +import json + +def emotion_detector(text_to_analyse): + 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_analyse}} + response = requests.post(URL, json = input_json, headers = header) + status_code = response.status_code + + emotions = {} + + if status_code == 200: + formatted_response = json.loads(response.text) + # Format output and inslude dominant emotion + emotions = formatted_response['emotionPredictions'][0]['emotion'] + dominant_emotion = max(emotions, key=emotions.get) + # Append to emotions dict + emotions['dominant_emotion'] = dominant_emotion + + 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 \ No newline at end of file diff --git a/server.py b/server.py new file mode 100644 index 000000000..361377c87 --- /dev/null +++ b/server.py @@ -0,0 +1,39 @@ +'''Deploy a Flask application that will allow the user to provide a text string +and return a breakdown of five emotions conveyed by the text +''' +from flask import Flask, request, render_template +from EmotionDetection.emotion_detection import emotion_detector + +app = Flask("Emotion Detector") + +@app.route('/emotionDetector') +def emotion_analyzer(): + '''Retrieves (get) the user input text string, pass the text into the + emotion_detector function inside of emotion_detection.py. Error handeling + ''' + text_to_analyse = request.args.get('textToAnalyze') + emotion_result = emotion_detector(text_to_analyse) + + if emotion_result['dominant_emotion'] is None: + return "Invalid text! Please try again" + + formatted_result = ( + f"For the given statement, the system response is " + f"'anger': {emotion_result['anger']}, " + f"'disgust': {emotion_result['disgust']}, " + f"'fear': {emotion_result['fear']}, " + f"'joy': {emotion_result['joy']} and " + f"'sadness': {emotion_result['sadness']}. " + f"The dominant emotion is {emotion_result['dominant_emotion']}." + ) + return formatted_result + +@app.route("/") +def render_index_page(): + '''Render the index page to the user. This is the page that prompts the user + to enter text into the box and emotion detection will be returned + ''' + return render_template('index.html') + +if __name__ == "__main__": + app.run(debug=True, host='0.0.0.0', port=5000) diff --git a/test_emotion_detection.py b/test_emotion_detection.py new file mode 100644 index 000000000..604505e5c --- /dev/null +++ b/test_emotion_detection.py @@ -0,0 +1,21 @@ +from EmotionDetection.emotion_detection import emotion_detector +import unittest + +class TestEmotionDetection(unittest.TestCase): + def test_emotion_detector(self): + result1 = emotion_detector('I am glad this happened') + self.assertEqual(result1['dominant_emotion'], 'joy') + + result2 = emotion_detector('I am really mad about this') + self.assertEqual(result2['dominant_emotion'], 'anger') + + result3 = emotion_detector('I feel disgusted just hearing about this') + self.assertEqual(result3['dominant_emotion'], 'disgust') + + result4 = emotion_detector('I am so sad about this') + self.assertEqual(result4['dominant_emotion'], 'sadness') + + result5 = emotion_detector('I am really afraid that this will happen ') + self.assertEqual(result5['dominant_emotion'], 'fear') + +unittest.main() \ No newline at end of file