Skip to content

New #96

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

New #96

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_detector
54 changes: 54 additions & 0 deletions EmotionDetection/emotion_detection.py
Original file line number Diff line number Diff line change
@@ -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}
Binary file added ScreenShots/1_folder_structure.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/2a_application_creation.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/2b_application_creation.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/3a_output_formatting.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/3b_formatted_output_test.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/4a_packaging.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/4b_packaging_test.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/5a_unit_testing.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/5b_unit_testing_result.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/6a_server.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/6b_deployment_test.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/7a_error_handling_function.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/7b_error_handling_server.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/7c_error_handling_interface.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/8a_server_modified.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ScreenShots/8b_static_code_analysis.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions testchild.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## adding a new file
print("inside child branch")