18 KiB
18 KiB
In [ ]:
!pip install transformersIn [29]:
import numpy as np
from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
from transformers import pipeline[0;36m File [0;32m"<ipython-input-29-c3a037bd4c55>"[0;36m, line [0;32m5[0m [0;31m from transformers import pipeline[0m [0m ^[0m [0;31mSyntaxError[0m[0;31m:[0m from __future__ imports must occur at the beginning of the file
In [6]:
nlp_sentence_classif = pipeline('sentiment-analysis')
nlp_sentence_classif(['Such a nice weather outside !', 'This movie was kind of boring.'])Out [6]:
HBox(children=(FloatProgress(value=0.0, description='Downloading', max=230.0, style=ProgressStyle(description_…
[{'label': 'POSITIVE', 'score': 0.800251},
{'label': 'NEGATIVE', 'score': 1.2489903}]In [16]:
nlp_token_class = pipeline('ner')
nlp_token_class('Hugging Face is a French company based in New-York.')Out [16]:
HBox(children=(FloatProgress(value=0.0, description='Downloading', max=230.0, style=ProgressStyle(description_…
[{'word': 'Hu', 'score': 0.9970937967300415, 'entity': 'I-ORG'},
{'word': '##gging', 'score': 0.9345750212669373, 'entity': 'I-ORG'},
{'word': 'Face', 'score': 0.9787060022354126, 'entity': 'I-ORG'},
{'word': 'French', 'score': 0.9981995820999146, 'entity': 'I-MISC'},
{'word': 'New', 'score': 0.9983047246932983, 'entity': 'I-LOC'},
{'word': '-', 'score': 0.8913455009460449, 'entity': 'I-LOC'},
{'word': 'York', 'score': 0.9979523420333862, 'entity': 'I-LOC'}]In [18]:
nlp_qa = pipeline('question-answering')
nlp_qa(context='Hugging Face is a French company based in New-York.', question='Where is based Hugging Face ?')Out [18]:
HBox(children=(FloatProgress(value=0.0, description='Downloading', max=230.0, style=ProgressStyle(description_…
convert squad examples to features: 100%|██████████| 1/1 [00:00<00:00, 53.05it/s] add example index and unique id: 100%|██████████| 1/1 [00:00<00:00, 2673.23it/s]
{'score': 0.9632966867654424, 'start': 42, 'end': 50, 'answer': 'New-York.'}In [20]:
nlp_fill = pipeline('fill-mask')
nlp_fill('Hugging Face is a French company based in <mask>')Out [20]:
HBox(children=(FloatProgress(value=0.0, description='Downloading', max=230.0, style=ProgressStyle(description_…
[{'sequence': '<s> Hugging Face is a French company based in Paris</s>',
'score': 0.25288480520248413,
'token': 2201},
{'sequence': '<s> Hugging Face is a French company based in Lyon</s>',
'score': 0.07639515399932861,
'token': 12790},
{'sequence': '<s> Hugging Face is a French company based in Brussels</s>',
'score': 0.055500105023384094,
'token': 6497},
{'sequence': '<s> Hugging Face is a French company based in Geneva</s>',
'score': 0.04264815151691437,
'token': 11559},
{'sequence': '<s> Hugging Face is a French company based in France</s>',
'score': 0.03868963569402695,
'token': 1470}]In [32]:
import numpy as np
nlp_features = pipeline('feature-extraction')
output = nlp_features('Hugging Face is a French company based in Paris')
np.array(output).shape # (Samples, Tokens, Vector Size)
Out [32]:
HBox(children=(FloatProgress(value=0.0, description='Downloading', max=230.0, style=ProgressStyle(description_…
(1, 12, 768)
In [40]:
task = widgets.Dropdown(
options=['sentiment-analysis', 'ner', 'fill_mask'],
value='ner',
description='Task:',
disabled=False
)
input = widgets.Text(
value='',
placeholder='Enter something',
description='Your input:',
disabled=False
)
def forward(_):
if len(input.value) > 0:
if task.value == 'ner':
output = nlp_token_class(input.value)
elif task.value == 'sentiment-analysis':
output = nlp_sentence_classif(input.value)
else:
if input.value.find('<mask>') == -1:
output = nlp_fill(input.value + ' <mask>')
else:
output = nlp_fill(input.value)
print(output)
input.on_submit(forward)
display(task, input)Dropdown(description='Task:', index=1, options=('sentiment-analysis', 'ner', 'fill_mask'), value='ner')Text(value='', description='Your input:', placeholder='Enter something')
[{'word': 'Paris', 'score': 0.9991844296455383, 'entity': 'I-LOC'}]
[{'sequence': '<s> I\'m from Paris."</s>', 'score': 0.224044069647789, 'token': 72}, {'sequence': "<s> I'm from Paris.)</s>", 'score': 0.16959427297115326, 'token': 1592}, {'sequence': "<s> I'm from Paris.]</s>", 'score': 0.10994981974363327, 'token': 21838}, {'sequence': '<s> I\'m from Paris!"</s>', 'score': 0.0706234946846962, 'token': 2901}, {'sequence': "<s> I'm from Paris.</s>", 'score': 0.0698278620839119, 'token': 4}]
[{'sequence': "<s> I'm from Paris and London</s>", 'score': 0.12238534539937973, 'token': 928}, {'sequence': "<s> I'm from Paris and Brussels</s>", 'score': 0.07107886672019958, 'token': 6497}, {'sequence': "<s> I'm from Paris and Belgium</s>", 'score': 0.040912602096796036, 'token': 7320}, {'sequence': "<s> I'm from Paris and Berlin</s>", 'score': 0.039884064346551895, 'token': 5459}, {'sequence': "<s> I'm from Paris and Melbourne</s>", 'score': 0.038133684545755386, 'token': 5703}]
[{'sequence': '<s> I like go to sleep</s>', 'score': 0.08942786604166031, 'token': 3581}, {'sequence': '<s> I like go to bed</s>', 'score': 0.07789064943790436, 'token': 3267}, {'sequence': '<s> I like go to concerts</s>', 'score': 0.06356740742921829, 'token': 12858}, {'sequence': '<s> I like go to school</s>', 'score': 0.03660670667886734, 'token': 334}, {'sequence': '<s> I like go to dinner</s>', 'score': 0.032155368477106094, 'token': 3630}]
In [43]:
context = widgets.Textarea(
value='Einstein is famous for the general theory of relativity',
placeholder='Enter something',
description='Context:',
disabled=False
)
query = widgets.Text(
value='Why is Einstein famous for ?',
placeholder='Enter something',
description='Question:',
disabled=False
)
def forward(_):
if len(context.value) > 0 and len(query.value) > 0:
output = nlp_qa(question=query.value, context=context.value)
print(output)
query.on_submit(forward)
display(context, query)Textarea(value='Einstein is famous for the general theory of relativity', description='Context:', placeholder=…
Text(value='Why is Einstein famous for ?', description='Question:', placeholder='Enter something')
convert squad examples to features: 100%|██████████| 1/1 [00:00<00:00, 168.83it/s] add example index and unique id: 100%|██████████| 1/1 [00:00<00:00, 1919.59it/s]
{'score': 0.40340670623875496, 'start': 27, 'end': 54, 'answer': 'general theory of relativity'}