diff --git a/classifiers/text_analysis/emotionality_detection/__init__.py b/classifiers/text_analysis/emotionality_detection/__init__.py index d9b5d286..ff1a1abb 100644 --- a/classifiers/text_analysis/emotionality_detection/__init__.py +++ b/classifiers/text_analysis/emotionality_detection/__init__.py @@ -1,35 +1,36 @@ +import requests +from extractors.util.spacy import SpacySingleton from pydantic import BaseModel -from LeXmo import LeXmo INPUT_EXAMPLE = { - "text": """As Harry went inside the Chamber of Secrets, he discovered the Basilisk's layer. Before him stood Tom - Riddle, with his wand. Harry was numb for a second as if he had seen a ghost. Moments later the giant - snake attacked Harry but fortunately, Harry dodged and ran into one of the sewer lines while the serpent - followed. The Basilisk couldn't be killed with bare hands but only with a worthy weapon.""" + "text": "I did not know that you were coming! I am very glad to see you. If you would tell me sooner, I would have baked some cookies, though.", + "apiKey": "" } - class EmotionalityDetectionModel(BaseModel): - text: str - - class Config: - schema_example = {"example": INPUT_EXAMPLE} - - -def emotionality_detection(request: EmotionalityDetectionModel): - """Fetches emotions from a given text""" - - text = request.text - try: - emo = LeXmo.LeXmo(text) - del emo["text"] - del emo["positive"] - del emo["negative"] - unique = dict(zip(emo.values(), emo.keys())) - if len(unique) == 1: - return "Cannot determine emotion" - else: - emo = max(emo, key=emo.get) - return {"emotion": emo} - except ValueError: - return "Valid text required" + apiKey: str + text: str + + class Config: + schema_example = {"example": INPUT_EXAMPLE} + +def emotionality_detection(req: EmotionalityDetectionModel): + """huggingface model for emotion detection""" + headers = {"Authorization": f"Bearer {req.apiKey}"} + data = {"inputs": req.text} + try: + response = requests.post("https://api-inference.huggingface.co/models/j-hartmann/emotion-english-distilroberta-base", headers=headers, json=data) + response_json = response.json() + + # flatten the list of lists + flat_list = [item for sublist in response_json for item in sublist] + + # find the item with the highest score + max_item = max(flat_list, key=lambda x: x["score"]) + + # retrieve the label of the item with the highest score + max_label = max_item["label"] + + return max_label + except Exception as e: + return f"That didn't work. Did you provide a valid API key? Go error: {e} and message: {response_json}" \ No newline at end of file diff --git a/classifiers/text_analysis/emotionality_detection/code_snippet_common.md b/classifiers/text_analysis/emotionality_detection/code_snippet_common.md index 6b9a8153..54274eec 100644 --- a/classifiers/text_analysis/emotionality_detection/code_snippet_common.md +++ b/classifiers/text_analysis/emotionality_detection/code_snippet_common.md @@ -1,26 +1,37 @@ ```python -from LeXmo import LeXmo - -def emotionality_detection(text:str) -> str: - """ - @param text: text to check - @return: either 'anger', 'fear', 'anticipation', 'trust', 'surprise', 'sadness', 'joy' or 'disgust' depending on the score - """ - emo = LeXmo.LeXmo(text) - del emo["text"] - del emo["positive"] - del emo["negative"] - emo = max(emo, key=emo.get) - return emo - + +import requests + +def emotionality_detection(text, api_key): + headers = {"Authorization": f"Bearer {api_key}"} + data = {"inputs": text} + try: + response = requests.post("https://api-inference.huggingface.co/models/j-hartmann/emotion-english-distilroberta-base", headers=headers, json=data) + response_json = response.json() + + # flatten the list of lists + flat_list = [item for sublist in response_json for item in sublist] + + # find the item with the highest score + max_item = max(flat_list, key=lambda x: x["score"]) + + # retrieve the label of the item with the highest score + max_label = max_item["label"] + + return max_label + except Exception as e: + return f"That didn't work. Did you provide a valid API key? Go error: {e} and message: {response_json}" + # ↑ necessary bricks function # ----------------------------------------------------------------------------------------- # ↓ example implementation def example_integration(): - texts = ["I am scared to continue.", "Oh my goodness it was the best evening ever, hype!"] - for text in texts: - print(f"\"{text}\" has emotion: {emotionality_detection(text)}") + hf_api_key = "" + texts = ["What a great day to go to the beach.", "Sorry to hear that. CAn I help you?", "Why the hell would you do that?"] + for text in texts: + output = emotionality_detection(text, api_key=hf_api_key) + print(output) example_integration() ``` \ No newline at end of file diff --git a/classifiers/text_analysis/emotionality_detection/code_snippet_refinery.md b/classifiers/text_analysis/emotionality_detection/code_snippet_refinery.md index 6fa2e458..31148b97 100644 --- a/classifiers/text_analysis/emotionality_detection/code_snippet_refinery.md +++ b/classifiers/text_analysis/emotionality_detection/code_snippet_refinery.md @@ -1,16 +1,19 @@ ```python -#expects labeling task to have labels ["anger", "fear", "anticipation", "trust", "surprise", "sadness", "joy", "disgust"] -from LeXmo import LeXmo +import requests -ATTRIBUTE: str = "text" # only text attributes +ATTRIBUTE: str = "text" +API_KEY: str = "" -def emotionality_detection(record): - text = record[ATTRIBUTE].text # SpaCy document, hence we need to call .text to get the string - emo = LeXmo.LeXmo(text) - del emo["text"] - del emo["positive"] - del emo["negative"] - emo = max(emo, key=emo.get) - - return emo +def emotionality_detection(text, api_key): + headers = {"Authorization": f"Bearer {API_KEY}"} + data = {"inputs": record[ATTRIBUTE].text} + try: + response = requests.post("https://api-inference.huggingface.co/models/j-hartmann/emotion-english-distilroberta-base", headers=headers, json=data) + response_json = response.json() + flat_list = [item for sublist in response_json for item in sublist] + max_item = max(flat_list, key=lambda x: x["score"]) + max_label = max_item["label"] + return max_label + except Exception as e: + return f"That didn't work. Did you provide a valid API key? Go error: {e} and message: {response_json}" ``` \ No newline at end of file diff --git a/classifiers/text_analysis/emotionality_detection/config.py b/classifiers/text_analysis/emotionality_detection/config.py index a7b4571b..c9247b9e 100644 --- a/classifiers/text_analysis/emotionality_detection/config.py +++ b/classifiers/text_analysis/emotionality_detection/config.py @@ -19,28 +19,31 @@ def get_config(): "text_analysis", ], # first entry should be parent directory # bricks integrator information - integrator_inputs={ + integrator_inputs={ "name": "emotionality_detection", "refineryDataType": RefineryDataType.TEXT.value, - "globalComment": "Only for english text.\nWorks best with longer texts since scores for each word are accumulated.", "outputs": [ - "anger", - "fear", - "anticipation", - "trust", - "surprise", - "sadness", - "joy", - "disgust", + "anger", + "disgust", + "fear", + "joy", + "sadness", + "neutral", + "suprise" ], "variables": { - "ATTRIBUTE": { - "selectionType": SelectionType.CHOICE.value, - "addInfo": [ + "API_KEY": { + "selectionType": SelectionType.STRING.value, + "defaultValue": "", + "addInfo": [BricksVariableType.GENERIC_STRING.value], + }, + "ATTRIBUTE": { + "selectionType": SelectionType.CHOICE.value, + "addInfo": [ BricksVariableType.ATTRIBUTE.value, BricksVariableType.GENERIC_STRING.value, - ], - } + ], + }, }, - }, - ) + }, +) \ No newline at end of file