from transformers import pipeline
from tabulate import tabulate
text = ["Aww another lovely video! It’s always so lovely to watch you and Darcy, I know lots of people have said it but one day I would love to have a daughter and have a bond like you two, it’s so special! 💜🧡",
"Oh I love Claire so much 🥰 Such a happy girl 😂",
"It's funny how royalty would leave the kids when she wanted to shop for hours with her mom and she would take the big kids and leave the babies now she act like you can't manage weird but the kids is having a good time though and her house flooded so it's probably good they got out of that but still",
"Omg CJ so cool 😍 What an awesome video So much love coming from every angel. You and your baby brother mending was great 👍. It brought tears 😭 to my eyes. So happy to see all of the family together ❤️. And Lexi did a phenomenal job with the food 💕. Continue to heal and feel better 🙏 I love you 💘😘",
"Ok how did you get that beautiful awesome long shot? Drone? Helicopter? Wyze on steroids! Fantastic trip! Keep them coming!",
"The 2020 word has to b LOCKDOWN. Good vid. Xxxxxxx"]
hypothesis_template = "This comment includes {}"
classes_verbalized = ["the commenter addressing or talking about a person",
"the commenter being (not) able to understand the situation / motivation of another person",
"the commenter commenter relating something that another person does or says to a memory of a situation or other person in his life",
"the commenter making a comparison between themselves and another person",
"the commenter expressing positive or negative feelings towards another person",
"the commenter judging another person or another person’s actions (in a good or a bad way)"]
zeroshot_classifier = pipeline("zero-shot-classification", model="MoritzLaurer/deberta-v3-large-zeroshot-v2.0") # change the model identifier here
output = zeroshot_classifier(text, classes_verbalized, hypothesis_template=hypothesis_template, multi_label=True)
# print(output)
for item in output:
sequence = item['sequence']
classes = item['labels']
scores = item['scores']
print(sequence)
table_data = []
for cls, score in zip(classes, scores):
table_data.append([cls, score])
print(tabulate(table_data, headers=['Class', 'Score'], tablefmt="pretty"))
print()
Exercise 04
Computational Analysis of Communication — Spring Term 2024
CCS
Exercise
Data Science
Zero-/Few-Shot Learning
- https://huggingface.co/blog/setfit
- https://www.promptingguide.ai/techniques/fewshot
- https://huggingface.co/collections/MoritzLaurer/zeroshot-classifiers-6548b4ff407bb19ff5c3ad6f
Zero-Shot Example
Aww another lovely video! It’s always so lovely to watch you and Darcy, I know lots of people have said it but one day I would love to have a daughter and have a bond like you two, it’s so special! 💜🧡
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
| Class | Score |
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
| the commenter addressing or talking about a person | 0.9023832082748413 |
| the commenter expressing positive or negative feelings towards another person | 0.7629444599151611 |
| the commenter making a comparison between themselves and another person | 0.31739509105682373 |
| the commenter commenter relating something that another person does or says to a memory of a situation or other person in his life | 0.05873614922165871 |
| the commenter being (not) able to understand the situation / motivation of another person | 0.019225282594561577 |
| the commenter judging another person or another person’s actions (in a good or a bad way) | 0.0016927079996094108 |
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
Oh I love Claire so much 🥰 Such a happy girl 😂
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
| Class | Score |
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
| the commenter expressing positive or negative feelings towards another person | 0.9838107228279114 |
| the commenter addressing or talking about a person | 0.9701910614967346 |
| the commenter commenter relating something that another person does or says to a memory of a situation or other person in his life | 0.05141744390130043 |
| the commenter being (not) able to understand the situation / motivation of another person | 0.036288436502218246 |
| the commenter judging another person or another person’s actions (in a good or a bad way) | 0.002854443620890379 |
| the commenter making a comparison between themselves and another person | 0.0022960701026022434 |
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
It's funny how royalty would leave the kids when she wanted to shop for hours with her mom and she would take the big kids and leave the babies now she act like you can't manage weird but the kids is having a good time though and her house flooded so it's probably good they got out of that but still
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
| Class | Score |
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
| the commenter addressing or talking about a person | 0.741041898727417 |
| the commenter being (not) able to understand the situation / motivation of another person | 0.7371388673782349 |
| the commenter commenter relating something that another person does or says to a memory of a situation or other person in his life | 0.558375358581543 |
| the commenter making a comparison between themselves and another person | 0.0837913453578949 |
| the commenter judging another person or another person’s actions (in a good or a bad way) | 0.04128776863217354 |
| the commenter expressing positive or negative feelings towards another person | 0.0022759311832487583 |
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
Omg CJ so cool 😍 What an awesome video So much love coming from every angel. You and your baby brother mending was great 👍. It brought tears 😭 to my eyes. So happy to see all of the family together ❤️. And Lexi did a phenomenal job with the food 💕. Continue to heal and feel better 🙏 I love you 💘😘
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
| Class | Score |
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
| the commenter addressing or talking about a person | 0.984747052192688 |
| the commenter commenter relating something that another person does or says to a memory of a situation or other person in his life | 0.052711356431245804 |
| the commenter expressing positive or negative feelings towards another person | 0.016381816938519478 |
| the commenter being (not) able to understand the situation / motivation of another person | 0.0016682315617799759 |
| the commenter making a comparison between themselves and another person | 0.0009321392280980945 |
| the commenter judging another person or another person’s actions (in a good or a bad way) | 0.0008889816817827523 |
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
Ok how did you get that beautiful awesome long shot? Drone? Helicopter? Wyze on steroids! Fantastic trip! Keep them coming!
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
| Class | Score |
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
| the commenter addressing or talking about a person | 0.726194441318512 |
| the commenter being (not) able to understand the situation / motivation of another person | 0.01638122834265232 |
| the commenter commenter relating something that another person does or says to a memory of a situation or other person in his life | 0.010792856104671955 |
| the commenter expressing positive or negative feelings towards another person | 0.004388310015201569 |
| the commenter judging another person or another person’s actions (in a good or a bad way) | 0.0034894521813839674 |
| the commenter making a comparison between themselves and another person | 0.002462285803630948 |
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
The 2020 word has to b LOCKDOWN. Good vid. Xxxxxxx
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
| Class | Score |
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+
| the commenter being (not) able to understand the situation / motivation of another person | 0.060072656720876694 |
| the commenter addressing or talking about a person | 0.04575435072183609 |
| the commenter commenter relating something that another person does or says to a memory of a situation or other person in his life | 0.028105996549129486 |
| the commenter judging another person or another person’s actions (in a good or a bad way) | 0.011913832277059555 |
| the commenter expressing positive or negative feelings towards another person | 0.006551239173859358 |
| the commenter making a comparison between themselves and another person | 0.0012101518223062158 |
+------------------------------------------------------------------------------------------------------------------------------------+-----------------------+