diff --git a/EmotionDetection/__init__.py b/EmotionDetection/__init__.py new file mode 100644 index 000000000..bbfca6ec8 --- /dev/null +++ b/EmotionDetection/__init__.py @@ -0,0 +1 @@ +#from . import emotion_detector \ No newline at end of file diff --git a/EmotionDetection/emotion_detection.py b/EmotionDetection/emotion_detection.py new file mode 100644 index 000000000..2ed7bddd6 --- /dev/null +++ b/EmotionDetection/emotion_detection.py @@ -0,0 +1,54 @@ +import requests # Import the requests library to handle HTTP requests +import json + +# +# Michael David 2025 +# +# + +def emotion_detector(text_to_analyse): # Define a function named sentiment_analyzer that takes a string input (text_to_analyse) + # URL of the sentiment analysis service + url = 'https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict' + # Constructing the request payload in the expected format + myobj = { "raw_document": { "text": text_to_analyse } } + + + # Custom header specifying the model ID for the sentiment analysis service + header = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"} + + + # Sending a POST request to the sentiment analysis API + response = requests.post(url, json=myobj, headers=header) + print(response) + if response.status_code == 400: + print("Invalid text! Please try again!") + results = { + 'anger': None, + 'disgust': None, + 'fear': None, + 'joy': None, + 'sadness': None, + 'dominant_emotion': None + } + elif response.status_code == 200: + + # Parsing the JSON response from the API + formatted_response = json.loads(response.text) + + #convert to a set of emotions + emotions = formatted_response["emotionPredictions"][0]["emotion"] + + # find emotion with highest score + highestEmotion = max(emotions, key = emotions.get) + + results = { + 'anger': emotions["anger"], + 'disgust': emotions["disgust"], + 'fear': emotions["fear"], + 'joy': emotions["joy"], + 'sadness': emotions["sadness"], + 'dominant_emotion': highestEmotion + } + + return(results) + #return {'label': label, 'score': score} \ No newline at end of file diff --git a/ScreenShots/1_folder_structure.PNG b/ScreenShots/1_folder_structure.PNG new file mode 100644 index 000000000..22a45c62b Binary files /dev/null and b/ScreenShots/1_folder_structure.PNG differ diff --git a/ScreenShots/2a_application_creation.PNG b/ScreenShots/2a_application_creation.PNG new file mode 100644 index 000000000..e0ea1e9c0 Binary files /dev/null and b/ScreenShots/2a_application_creation.PNG differ diff --git a/ScreenShots/2b_application_creation.PNG b/ScreenShots/2b_application_creation.PNG new file mode 100644 index 000000000..7ed8f92bb Binary files /dev/null and b/ScreenShots/2b_application_creation.PNG differ diff --git a/ScreenShots/3a_output_formatting.PNG b/ScreenShots/3a_output_formatting.PNG new file mode 100644 index 000000000..fc6fb7c2f Binary files /dev/null and b/ScreenShots/3a_output_formatting.PNG differ diff --git a/ScreenShots/3b_formatted_output_test.PNG b/ScreenShots/3b_formatted_output_test.PNG new file mode 100644 index 000000000..9c8ce0260 Binary files /dev/null and b/ScreenShots/3b_formatted_output_test.PNG differ diff --git a/ScreenShots/4a_packaging.PNG b/ScreenShots/4a_packaging.PNG new file mode 100644 index 000000000..3f7014b22 Binary files /dev/null and b/ScreenShots/4a_packaging.PNG differ diff --git a/ScreenShots/4b_packaging_test.PNG b/ScreenShots/4b_packaging_test.PNG new file mode 100644 index 000000000..a07b0a0f9 Binary files /dev/null and b/ScreenShots/4b_packaging_test.PNG differ diff --git a/ScreenShots/5a_unit_testing.PNG b/ScreenShots/5a_unit_testing.PNG new file mode 100644 index 000000000..d5ed6fb63 Binary files /dev/null and b/ScreenShots/5a_unit_testing.PNG differ diff --git a/ScreenShots/5b_unit_testing_result.PNG b/ScreenShots/5b_unit_testing_result.PNG new file mode 100644 index 000000000..854758161 Binary files /dev/null and b/ScreenShots/5b_unit_testing_result.PNG differ diff --git a/ScreenShots/6a_server.PNG b/ScreenShots/6a_server.PNG new file mode 100644 index 000000000..9c8993b18 Binary files /dev/null and b/ScreenShots/6a_server.PNG differ diff --git a/ScreenShots/6b_deployment_test.PNG b/ScreenShots/6b_deployment_test.PNG new file mode 100644 index 000000000..05df08d92 Binary files /dev/null and b/ScreenShots/6b_deployment_test.PNG differ diff --git a/ScreenShots/7a_error_handling_function.PNG b/ScreenShots/7a_error_handling_function.PNG new file mode 100644 index 000000000..0e656d887 Binary files /dev/null and b/ScreenShots/7a_error_handling_function.PNG differ diff --git a/ScreenShots/7b_error_handling_server.PNG b/ScreenShots/7b_error_handling_server.PNG new file mode 100644 index 000000000..f9b7a74d4 Binary files /dev/null and b/ScreenShots/7b_error_handling_server.PNG differ diff --git a/ScreenShots/7c_error_handling_interface.PNG b/ScreenShots/7c_error_handling_interface.PNG new file mode 100644 index 000000000..da1bafc9c Binary files /dev/null and b/ScreenShots/7c_error_handling_interface.PNG differ diff --git a/ScreenShots/8a_server_modified.PNG b/ScreenShots/8a_server_modified.PNG new file mode 100644 index 000000000..642ff1f96 Binary files /dev/null and b/ScreenShots/8a_server_modified.PNG differ diff --git a/ScreenShots/8b_static_code_analysis.PNG b/ScreenShots/8b_static_code_analysis.PNG new file mode 100644 index 000000000..b24babaaf Binary files /dev/null and b/ScreenShots/8b_static_code_analysis.PNG differ diff --git a/server.py b/server.py new file mode 100644 index 000000000..dc27fe776 --- /dev/null +++ b/server.py @@ -0,0 +1,43 @@ +""" +This module is part of my final project + +""" +from flask import Flask, render_template, request +from EmotionDetection.emotion_detection import emotion_detector + +app = Flask("Emotion Detector") + +@app.route('/') +def render_homepage(): + """ renders home page """ + return render_template("index.html") + +@app.route("/emotionDetector", methods = ["GET"]) +def sent_analyzer(): + """ calls emotion detector function and returns response to webpage """ + text = request.args.get('textToAnalyze') + + response = emotion_detector(text) + + if response["dominant_emotion"] is None: + result = "Invalid text! Please try again!" + else: + result = "For the given statement, the system response is" + + for key, value in response.items(): + # dominant emotion is mentioned separately as its own sentence. + if key != "dominant_emotion": + result += f" '{key}': {value}," + # Replace last comma with point. If index is -1, no commas found. + last_comma_index = result.rfind(",") + if last_comma_index != -1: + result = result[:last_comma_index] + '.' + result[last_comma_index + 1:] + + result += f" The dominant emotion is {response['dominant_emotion']}." + + return result + + + +if __name__ == "__main__": + app.run(debug=True,host="0.0.0.0", port=5010) diff --git a/testchild.py b/testchild.py new file mode 100644 index 000000000..383f07cd9 --- /dev/null +++ b/testchild.py @@ -0,0 +1,2 @@ +## adding a new file +print("inside child branch")