Conversation
LeonardPuettmannKern
left a comment
There was a problem hiding this comment.
Needs some work. :)
| @@ -19,28 +19,31 @@ def get_config(): | |||
| "text_analysis", | |||
There was a problem hiding this comment.
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.
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.
@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.
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.
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: