해당 글은 제로베이스데이터스쿨 학습자료를 참고하여 작성되었습니다
!pip install transformers
!pip install datasets
from transformers import pipeline
classifier = pipeline('sentiment-analysis')
classifier.model
classifier("I've been waiting for a HuggingFace course my whole life.")
-------------------------------------------------------
[{'label': 'POSITIVE', 'score': 0.9598049521446228}]
classifier = pipeline("zero-shot-classification") # Default Model = facebook/bart-large-mnli
classifier(
"This is a course about the transformers library", # 문제
candidate_labels = ["education", "politics", "business"], # 정답보기
)
-----------------------------------------------------------------------------------------------------------------
{'sequence': 'This is a course about the transformers library',
'labels': ['education', 'business', 'politics'],
'scores': [0.9192408919334412, 0.060778193175792694, 0.01998087950050831]}
classifier(
"This is a course about the transformers library", # 문제
candidate_labels = ["course", "library", "game", "This"], # 정답보기
)
----------------------------------------------------------------
{'sequence': 'This is a course about the transformers library',
'labels': ['course', 'library', 'This', 'game'],
'scores': [0.732907235622406,
0.19588284194469452,
0.06776876002550125,
0.003441136097535491]}
generator = pipeline("text-generation") # Default Model = gpt2
generator("In this course, we will teach you how to ")
----------------------------------------------------------------
[{'generated_text': 'In this course, we will teach you how to \xa0create simple, beautiful, dynamic design diagrams, and how to create them with a variety of basic software tools. We will make use of our favorite tools like Sketch, L.A. Sketch'}]
generator("In this course, we will teach you how to ", num_return_sequences=5, max_length=20)
---------------------------------------------------------------------
[{'generated_text': 'In this course, we will teach you how to \xa0communicate with fellow listeners.\n2'},
{'generated_text': 'In this course, we will teach you how to \xa0install the new web application for PHP 5'},
{'generated_text': 'In this course, we will teach you how to \xa0understand the most basic \xa0of'},
{'generated_text': 'In this course, we will teach you how to \xa0compete with the enemy. By taking'},
{'generated_text': 'In this course, we will teach you how to \xa0explicitly use the\xa0cargo'}]
list_ = ["In this course, we will teach you how to ", "This is a course about the transformers library"]
for sentence in list_:
print(generator(sentence, num_return_sequences=1, max_length=20))
--------------------------------------------------------------------------------------------
[{'generated_text': 'In this course, we will teach you how to \xa0help others get involved in social media.'}]
[{'generated_text': 'This is a course about the transformers library.\n\nThis is part of the Meej'}]
generator = pipeline("text-generation", model="huggingtweets/dril")
generator("My dream is ", num_return_sequences=5)
-------------------------------------------------------------------------------------------------
[{'generated_text': 'My dream is ive invented. ive invented what is basically the most popular movie ever made and I need over $10,000 to make it go away. Thank you.'},
{'generated_text': 'My dream is ive been to be the next "Powerball jack"'},
{'generated_text': 'My dream is ive come to the realization that i have the power of unlimited consciousness. I would get a brain if i could simply convince all the guys in the house where i live to stop smoking pot that i can still tell the difference between a man and a woman and I would become completely Normal'},
{'generated_text': "My dream is ive gotten over 1000 jobs. That's what my self believes. Ive fucked over 1000 people"},
{'generated_text': 'My dream is \ue001� That the \ue003� of humanity \ue006nh is To See The \ue006nht That Is As The \ue001� Of My Dream.'}]
<mask>
에 들어갈 단어 맞추기unmasker = pipeline("fill-mask") # Default Model = distilroberta-base
unmasker("This coures will teach you all about <mask> models", top_k=5)
---------------------------------------------------------------------------------------------------
[{'score': 0.040895454585552216,
'token': 745,
'token_str': ' building',
'sequence': 'This coures will teach you all about building models'},
{'score': 0.03127061203122139,
'token': 30412,
'token_str': ' mathematical',
'sequence': 'This coures will teach you all about mathematical models'},
{'score': 0.025371771305799484,
'token': 774,
'token_str': ' role',
'sequence': 'This coures will teach you all about role models'},
{'score': 0.01844116672873497,
'token': 265,
'token_str': ' business',
'sequence': 'This coures will teach you all about business models'},
{'score': 0.015211271122097969,
'token': 3034,
'token_str': ' computer',
'sequence': 'This coures will teach you all about computer models'}]
ner = pipeline("ner", grouped_entities=True) # Default Model = dbmdz/bert-large-cased-finetuned-conll03-english
ner("My name is Sylvain and I work at Hugging Face in Brooklyn.")
--------------------------------------------------------------------------------
[{'entity_group': 'PER',
'score': 0.9981694,
'word': 'Sylvain',
'start': 11,
'end': 18},
{'entity_group': 'ORG',
'score': 0.9796019,
'word': 'Hugging Face',
'start': 33,
'end': 45},
{'entity_group': 'LOC',
'score': 0.9932106,
'word': 'Brooklyn',
'start': 49,
'end': 57}]
question_answer = pipeline("question-answering") # Default Model = distilbert-base-cased-distilled-squad
question_answer(
question="what's my name?",
context="My name is Sylvain and I work at Hugging Face in Brooklyn.",
)
-------------------------------------------
{'score': 0.9988495111465454, 'start': 11, 'end': 18, 'answer': 'Sylvain'}
summarizer = pipeline("summarization") # Default Model = sshleifer/distilbart-cnn-12-6
summarizer(
"""
National Commercial Bank (NCB),
Saudi Arabia’s largest lender by assets,
agreed to buy rival Samba Financial Group for $15 billion in the biggest banking takeover this year.
NCB will pay 28.45 riyals ($7.58) for each Samba share, according to a statement on Sunday,
valuing it at about 55.7 billion riyals. NCB will offer 0.739 new shares for each Samba share,
at the lower end of the 0.736-0.787 ratio the banks set when they signed an initial framework
agreement in June.The offer is a 3.5% premium to Samba’s Oct. 8 closing price of 27.50 riyals and
about 24% higher than the level the shares traded at before the talks were made public. Bloomberg
News first reported the merger discussions.The new bank will have total assets of more than $220 billion,
creating the Gulf region’s third-largest lender. The entity’s $46 billion market capitalization nearly matches
that of Qatar National Bank QPSC, which is still the Middle East’s biggest lender with about $268 billion of
assets.
"""
)
---------------------------------------------------------------------------------------------------------------------------
[{'summary_text': " Saudi Arabia's largest lender National Commercial Bank agrees to buy rival Samba Financial Group for $15 billion . NCB will pay 28.45 riyals ($7.58) for each Samba share, valuing it at about 55.7 billion . The new bank will have total assets of more than $220 billion, creating the Gulf region’s third-largest lender ."}]