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

Emotionallity Detection update #334

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Emotionallity Detection update #334

wants to merge 4 commits into from

Conversation

SvenjaKern
Copy link
Contributor

@SvenjaKern SvenjaKern commented Aug 16, 2023

PR checklist:

  • Tested by creator on localhost:8000/docs
  • Tested by creator on refinery
  • Tested by reviewer on localhost:8000/docs
  • Tested by reviewer on refinery
  • (If added) common code tested in notebook/ script
  • Conforms with the agreed upon code pattern

Copy link
Contributor

@LeonardPuettmannKern LeonardPuettmannKern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs some work. :)

@@ -19,28 +19,31 @@ def get_config():
"text_analysis",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would add this to the "llm" category as well as this is using a roberta model from HuggingFace.

try:
response = requests.post("https://api-inference.huggingface.co/models/j-hartmann/emotion-english-distilroberta-base", headers=headers, json=data)
response_json = response.json()
ner_positions = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is supposed to be a classification brick and not an extraction one. This code tries to extract entities from a text and yield them, which is not how this model works. The output of the model returns a list containing a dict like this: [[{'label': 'disgust', 'score': 0.8062547445297241}, {'label': 'sadness', 'score': 0.107185959815979}, {'label': 'anger', 'score': 0.06451956927776337}, {'label': 'fear', 'score': 0.014504954218864441}, {'label': 'neutral', 'score': 0.0030063888989388943}, {'label': 'joy', 'score': 0.0026550842449069023}, {'label': 'surprise', 'score': 0.0018732782918959856}]]

Doing NER won't work.

for text in texts:
print(f"\"{text}\" has emotion: {emotionality_detection(text)}")
def emotionality_detection():
hf_api_key = "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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SvenjaKern Please watch out to never include API keys in your git commits. Please deactivate this API key and generate a new one. This is a public repository, which are often scanned for keys like this which can then be abused.

hf_api_key = "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"
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 = emotion_detection(text, api_key=hf_api_key)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name of the function call doesn't match up to the actual function.

Comment on lines 13 to 26

nlp = spacy.load("en_core_web_sm")
doc = nlp(text)

for item in response_json:
start = item["start"]
end = item["end"]
span = doc.char_span(start, end, alignment_mode="expand")
ner_positions.append((item["entity_group"], span.start, span.end))
return ner_positions
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Common code should be like this:

Suggested change
nlp = spacy.load("en_core_web_sm")
doc = nlp(text)
for item in response_json:
start = item["start"]
end = item["end"]
span = doc.char_span(start, end, alignment_mode="expand")
ner_positions.append((item["entity_group"], span.start, span.end))
return ner_positions
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
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():
hf_api_key = "<API_KEY_GOES_HERE>"
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()

@LeonardPuettmannKern LeonardPuettmannKern changed the title New Branch for Emotionallity Detection Emotionallity Detection update Sep 25, 2023
@FelixKirschKern FelixKirschKern marked this pull request as draft October 18, 2023 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants