Galleria
Playlist immagini
Gruppi
Unisciti a un gruppo
Acquisizione
Nuovo oggetto
Mappe concettuali
Gestione
Tabelle (CRUD generico)
Ricerca tag
Elaborazioni
Somma di prova
Sistema
Impostazioni
Esci
☰
Oggetti
LOGISTAR
LMP
ospite
⋮
Profilo utente
Preferenze colonne
Guida programma
Segnala anomalia
Cambia utente
⌂
💾 Salva
↻ Aggiorna
☑ Lista
🏷 Righe
Id
Descrizione
dta reg (aaaa-mm-gg)
Body
<h2>Using Hugging Face’s Transformers Library</h2> <p><strong>Hugging Face</strong> is a repository that provides open-source pre-trained language models ready to load and use for NLP tasks like text generation, translation, and sentiment analysis. It is powered by its centerpiece library: <em>Transformers</em>, which offers seamless integration with popular Python libraries like PyTorch, JAX, and TensorFlow. Best of all: they are free to use and require minimal setup, making AI development accessible to everyone.</p> <p>Let’s start this practical tour by installing the transformers library in a new notebook of your own:</p> <p> </p> <div id="urvanov-syntax-highlighter-6a55007fe886a723849851" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important; height: auto;" data-settings=" minimize scroll-mouseover disable-anim"> <div class="urvanov-syntax-highlighter-plain-wrap"> </div> <div class="urvanov-syntax-highlighter-main" style="position: relative; z-index: 1; overflow: hidden;"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size: 4; -o-tab-size: 4; -webkit-tab-size: 4; tab-size: 4;"> <div id="urvanov-syntax-highlighter-6a55007fe886a723849851-1" class="crayon-line"><span class="crayon-o">!</span><span class="crayon-e">pip </span><span class="crayon-e">install </span><span class="crayon-v">transformers</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p> </p> <p>We will now load a model from Hugging Face, specifically GPT-2 for text generation. When loading pre-trained models, we normally need to load not only the model itself, but also a compatible tokenizer responsible for splitting text inputs into logical language units called tokens (roughly equivalent to words in most cases) before being passed to the language model for processing it and generating follow-up text.</p> <p>The following code imports the necessary packages and initializes the language model and tokenizer.</p> <p> </p> <div id="urvanov-syntax-highlighter-6a55007fe8873048112977" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important; height: auto;" data-settings=" minimize scroll-mouseover disable-anim"> <div class="urvanov-syntax-highlighter-plain-wrap"> </div> <div class="urvanov-syntax-highlighter-main" style="position: relative; z-index: 1; overflow: hidden;"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size: 4; -o-tab-size: 4; -webkit-tab-size: 4; tab-size: 4;"> <div id="urvanov-syntax-highlighter-6a55007fe8873048112977-1" class="crayon-line"><span class="crayon-e">from </span><span class="crayon-e">transformers </span><span class="crayon-e">import </span><span class="crayon-v">GPT2LMHeadModel</span><span class="crayon-sy">,</span><span class="crayon-e">GPT2Tokenizer</span></div> <div id="urvanov-syntax-highlighter-6a55007fe8873048112977-2" class="crayon-line crayon-striped-line"> </div> <div id="urvanov-syntax-highlighter-6a55007fe8873048112977-3" class="crayon-line"><span class="crayon-v">model_name</span><span class="crayon-o">=</span><span class="crayon-s">"gpt2"</span></div> <div id="urvanov-syntax-highlighter-6a55007fe8873048112977-4" class="crayon-line crayon-striped-line"><span class="crayon-v">model</span><span class="crayon-o">=</span><span class="crayon-v">GPT2LMHeadModel</span><span class="crayon-sy">.</span><span class="crayon-e">from_pretrained</span><span class="crayon-sy">(</span><span class="crayon-v">model_name</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-6a55007fe8873048112977-5" class="crayon-line"><span class="crayon-v">tokenizer</span><span class="crayon-o">=</span><span class="crayon-v">GPT2Tokenizer</span><span class="crayon-sy">.</span><span class="crayon-e">from_pretrained</span><span class="crayon-sy">(</span><span class="crayon-v">model_name</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p> </p> <p>Next we define a prompt for the model, which is typically a query, question, or request to which the language model will try to generate a response. Some less sophisticated models limit themselves to generating follow-up words that continue the input sequence or prompt, for instance to continue a tale that starts with “Once upon a time”. In our case, we will try asking GPT-2 what is the capital of Japan and … fingers crossed.</p> <p> </p> <div id="urvanov-syntax-highlighter-6a55007fe8877490511473" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important; height: auto;" data-settings=" minimize scroll-mouseover disable-anim"> <div class="urvanov-syntax-highlighter-plain-wrap"> </div> <div class="urvanov-syntax-highlighter-main" style="position: relative; z-index: 1; overflow: hidden;"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size: 4; -o-tab-size: 4; -webkit-tab-size: 4; tab-size: 4;"> <div id="urvanov-syntax-highlighter-6a55007fe8877490511473-1" class="crayon-line"><span class="crayon-v">prompt</span><span class="crayon-o">=</span><span class="crayon-s">"What is the capital of Japan?"</span></div> <div id="urvanov-syntax-highlighter-6a55007fe8877490511473-2" class="crayon-line crayon-striped-line"> </div> <div id="urvanov-syntax-highlighter-6a55007fe8877490511473-3" class="crayon-line"><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">tokenizer</span><span class="crayon-sy">.</span><span class="crayon-e">encode</span><span class="crayon-sy">(</span><span class="crayon-v">prompt</span><span class="crayon-sy">,</span><span class="crayon-v">return_tensors</span><span class="crayon-o">=</span><span class="crayon-s">"pt"</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-6a55007fe8877490511473-4" class="crayon-line crayon-striped-line"><span class="crayon-v">output</span><span class="crayon-o">=</span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">generate</span><span class="crayon-sy">(</span><span class="crayon-v">inputs</span><span class="crayon-sy">,</span><span class="crayon-v">max_length</span><span class="crayon-o">=</span><span class="crayon-cn">50</span><span class="crayon-sy">,</span><span class="crayon-v">num_return_sequences</span><span class="crayon-o">=</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-6a55007fe8877490511473-5" class="crayon-line"> </div> <div id="urvanov-syntax-highlighter-6a55007fe8877490511473-6" class="crayon-line crayon-striped-line"><span class="crayon-v">response</span><span class="crayon-o">=</span><span class="crayon-v">tokenizer</span><span class="crayon-sy">.</span><span class="crayon-e">decode</span><span class="crayon-sy">(</span><span class="crayon-v">output</span><span class="crayon-sy">[</span><span class="crayon-cn">0</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-v">skip_special_tokens</span><span class="crayon-o">=</span><span class="crayon-t">True</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-6a55007fe8877490511473-7" class="crayon-line"><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-v">response</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p> </p> <p>Getting technical, in the above code you may have noticed that there are two processing steps taking place: encoding and decoding. The input sequence (the prompt) must be encoded into a numerical vector representation understandable by the model, and after it generates the raw (numerical) response, it is decoded back into text to make it understandable by us.</p> <p>This is the decoded response:</p> <p> </p> <div id="urvanov-syntax-highlighter-6a55007fe887d222368509" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important; height: auto;" data-settings=" minimize scroll-mouseover disable-anim"> <div class="urvanov-syntax-highlighter-plain-wrap"> </div> <div class="urvanov-syntax-highlighter-main" style="position: relative; z-index: 1; overflow: hidden;"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size: 4; -o-tab-size: 4; -webkit-tab-size: 4; tab-size: 4;"> <div id="urvanov-syntax-highlighter-6a55007fe887d222368509-1" class="crayon-line"><span class="crayon-e">The </span><span class="crayon-e">capital </span><span class="crayon-e">of </span><span class="crayon-e">Japan </span><span class="crayon-st">is</span><span class="crayon-v">Tokyo</span><span class="crayon-sy">.</span><span class="crayon-e">It </span><span class="crayon-st">is</span><span class="crayon-e">the </span><span class="crayon-e">capital </span><span class="crayon-e">of </span><span class="crayon-v">Japan</span><span class="crayon-sy">.</span><span class="crayon-e">It </span><span class="crayon-st">is</span><span class="crayon-e">the </span><span class="crayon-e">capital </span><span class="crayon-e">of </span><span class="crayon-v">Japan</span><span class="crayon-sy">.</span><span class="crayon-e">It </span><span class="crayon-st">is</span><span class="crayon-e">the </span><span class="crayon-e">capital </span><span class="crayon-e">of </span><span class="crayon-v">Japan</span><span class="crayon-sy">.</span><span class="crayon-e">It </span><span class="crayon-st">is</span><span class="crayon-e">the </span><span class="crayon-e">capital </span><span class="crayon-e">of </span><span class="crayon-v">Japan</span><span class="crayon-sy">.</span><span class="crayon-e">It </span><span class="crayon-st">is</span><span class="crayon-e">the </span><span class="crayon-e">capital </span><span class="crayon-e">of </span><span class="crayon-v">Japan</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p> </p> <p>Well, at least it gave the right answer! These pre-trained models are comparatively smaller, more manageable for testing environments, and not as absurdly powerful and high-performing as ChatGPT ones, hence it is not surprising they might be more limited in their text generation behavior. In particular, since we specified a maximum response length of 50 tokens, the model seems to prioritize the need to provide a response that closely matches that length, rather than keeping it short, simple, and logical.</p> <p>Time to look at another example and introduce another framework.</p> <div id="harzsgqwhvymy7ygigflny0yos73n73y0ky0y7ntb3s1" class="harzsgqwhvvy73igflnyivy7 harzsgqwhvvy73igflnyiqs73n73 harzsgqwhvvy73igflnyiqs73n73yik harzsgqwhvvy73igflnyivy7yiqzt" style="min-height: 250px;" data-harzsgqwhvcsscznyi85ng0yify7="CNzWrr6X05UDFTTCDQkd5IwFEA"> </div> <p> </p>
tipo ogg.
cat
subcat
proprietario
azienda
ute
selez1
op1
codart
codice
codclifor
motivo
url
email
tag
imgsrc
sunto_breve
sunto_medio
note
keyscrape
Lista tag dell'oggetto (oggettag)
cod_tag
value
api
5
deep learning
5
llm
5
llm_internal
5
wpg
5
Lista delle righe dell'oggetto (oggettir)
tip_pro
cod_pro
descrizione
val_pro
pes_pro
---.