-
Notifications
You must be signed in to change notification settings - Fork 23
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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", |
There was a problem hiding this comment.
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 = [] |
There was a problem hiding this comment.
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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" |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
|
||
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 |
There was a problem hiding this comment.
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:
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() |
PR checklist: