<jupyter_start><jupyter_text>Class 11: Prompting  Example from class11.md<jupyter_code>from transformers import pipeline

model_name = "distilbert-base-uncased" # a small version of BERT

nlp = pipeline("fill-mask", model_name) # create pipeline
sentence_to_classify = "I love working with NLP"

prompt = f"""
I hate my life. => negative
The movie I saw yesterday was great! => positive
{sentence_to_classify} => {nlp.tokenizer.mask_token}
"""

print(prompt)
print(nlp(prompt))<jupyter_output>I hate my life. => negative
The movie I saw yesterday was great! => positive
I love working with NLP => [MASK]

[{'sequence': 'i hate my life. = > negative the movie i saw yesterday was great! = > positive i love working with nlp = > positive', 'score': 0.30130624771118164, 'token': 3893, 'token_str': 'positive'}, {'sequence': 'i hate my life. = > negative the movie i saw yesterday was great! = > positive i love working with nlp = > |', 'score': 0.2728317677974701, 'token': 1064, 'token_str': '|'}, {'sequence': 'i hate my life. = > negative the movie i saw yesterday was great! = > positive i love working with nlp = > negative', 'score': 0.12607789039611816, 'token': 4997, 'token_str': 'negative'}, {'sequence': 'i hate my life. = > negative the movie i saw yesterday was great! = > positive i love working with nlp = >.', 'score': 0.03605766221880913, 'token': 1012, 'token_str': '.'}, {'sequence': 'i hate my life. = > negative the movie i saw yesterday was great! = > positive i love work[...]<jupyter_text>Using danish BERT to classify sentiment<jupyter_code>model_name = "flax-community/roberta-base-danish" # danish BERT

nlp = pipeline("fill-mask", model_name) # create pipeline
sentence_to_classify = "Jeg elsker at arbejde med NLP!"

prompt = f"""
Jeg hader mit liv => negativt
Den film jeg så i går var fantastisk! => positivt
{sentence_to_classify} => {nlp.tokenizer.mask_token}
"""

print(prompt)
print(nlp(prompt))
sentence_to_classify = "I dag er det torsdag"

arrow = '=>'

sentences = [('Jeg hader mit liv.', 'negativt'),
             ('Den film jeg så i går var fantastisk!', 'positivt'),
             ('Dette er en stol.', 'neutralt')]

sentence_to_classify = "Det er jeg ked af at høre."
sentence_to_classify = 'Du er sød! :)'

arrow = '=>'

sentences = [('Det har været en virkelig god dag!.', 'glæde'),
             ('Jeg hader dig.', 'vrede'),
             ('Dette er en stol.', 'neutralt'),
             ('Jeg er meget glad :)', 'glæde'),
             ('Det gør mig virkelig sur!', 'vrede')]
prompt = ''
for sent, label in sentences:
    prompt += f'{sent} {arrow} {label} \n'
prompt += f'{sentence_to_classify} {arrow} {nlp.tokenizer.mask_token}'

print(prompt)
print(*[(sent['token_str'], sent['score']) for sent in nlp(prompt)[:5]], sep='\n')<jupyter_output>Det har været en virkelig god dag!. => glæde 
Jeg hader dig. => vrede 
Dette er en stol. => neutralt 
Jeg er meget glad :) => glæde 
Det gør mig virkelig sur! => vrede 
Du er sød! :) => <mask>
(' glæde', 0.4528110921382904)
(' smil', 0.11764959990978241)
(' vrede', 0.08412950485944748)
(' glad', 0.026707587763667107)
(' glade', 0.018830718472599983)
{'fraction_non_alphanumeric': 0.15051020408163265, 'fraction_numerical': 0.05835459183673469, 'mean_word_length': 4.362393162393162, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '659293', 'license': 'MIT', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1089, 'n_words': 354, 'path': 'syllabus/classes/class11/class11_exercise.ipynb', 'repo_name': 'saraoe/NLP-E21', 'size': '3136'}