{"id":2172,"date":"2024-05-01T16:52:47","date_gmt":"2024-05-01T11:22:47","guid":{"rendered":"http:\/\/ramkulkarni.com\/blog\/?p=2172"},"modified":"2024-05-01T16:57:35","modified_gmt":"2024-05-01T11:27:35","slug":"setting-up-ubuntu-for-huggingface","status":"publish","type":"post","link":"http:\/\/ramkulkarni.com\/blog\/setting-up-ubuntu-for-huggingface\/","title":{"rendered":"Setting up Ubuntu for HuggingFace"},"content":{"rendered":"\n<p>Just wanted to document steps I took to successfully setup an Ubuntu machine for using models on <a rel=\"noreferrer noopener\" href=\"https:\/\/huggingface.co\" target=\"_blank\">HuggingFace<\/a>. Refer to <a rel=\"noreferrer noopener\" href=\"https:\/\/huggingface.co\/docs\/transformers\/installation\" target=\"_blank\">https:\/\/huggingface.co\/docs\/transformers\/installation<\/a> for installation instructions for using Transformers module.<\/p>\n\n\n\n<p>Ubuntu comes installed with Python3.  I started with creating a new environment for the project. I switched to the folder where I was going to create projects\/files for using HuggingFace<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating new Python Environment<\/h2>\n\n\n\n<ol>\n<li><em>python3 -m venv .env<\/em><\/li>\n\n\n\n<li><em>source .env\/bin\/activate<\/em><\/li>\n<\/ol>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Adding the new environment to Jupyter<\/h2>\n\n\n\n<p>Jupyter does not use the new environment created above. We need to add the environment to Jupyter. But before that we need to install ipykernel<\/p>\n\n\n\n<ol>\n<li><em>python3 -m pip install ipykernel<\/em><\/li>\n\n\n\n<li><em>python3 -m ipykernel install &#8211;user &#8211;name=myenv &#8211;display-name=\u201cHuggingFace Env\u201d<\/em><br>Here <em>name<\/em> can be anything, you don\u2019t have to crate the environment with that name.<\/li>\n\n\n\n<li>Launch \u201cjupyter notebook\u201d, create a notebook and switch kernel to \u201cHuggingFace Env\u201d as specified in the display-name above<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Installing PyTorch<\/h2>\n\n\n\n<p>Go to <a rel=\"noreferrer noopener\" href=\"https:\/\/pytorch.org\/get-started\/locally\/\" target=\"_blank\">https:\/\/pytorch.org\/get-started\/locally\/ <\/a>and select appropriate options in \u201cStart Locally\u201d section to see the command to be executed for installing PyTorch. I used the following command (for CPU only option on Linux &#8211; <\/p>\n\n\n\n<p><em>python3 -m pip install torch torchvision torchaudio &#8211;index-url https:\/\/download.pytorch.org\/whl\/cpu<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install Transformers<\/h2>\n\n\n\n<p><em>python3 -m pip install transformers<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify Setup<\/h2>\n\n\n\n<p>Run following code, either in Jupyter notebook or from a .py file &#8211; <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import torch<br>from transformers import pipeline<br><br>sentiment_analyzer = pipeline(\u201csentiment-analysis\u201d)<br>result = sentiment_analyzer(\u201cthis setup is working!\u201d)<br>print(result)<\/pre>\n\n\n\n<p>-Ram Kulkarni<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just wanted to document steps I took to successfully setup an Ubuntu machine for using models on HuggingFace. Refer to https:\/\/huggingface.co\/docs\/transformers\/installation for installation instructions for using Transformers module. Ubuntu comes installed with Python3. I started with creating a new environment for the project. I switched to the folder where I was going to create projects\/files &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/ramkulkarni.com\/blog\/setting-up-ubuntu-for-huggingface\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Setting up Ubuntu for HuggingFace&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"everybody","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":false,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[161,115],"tags":[160,159],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2g9O8-z2","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/2172"}],"collection":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/comments?post=2172"}],"version-history":[{"count":4,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/2172\/revisions"}],"predecessor-version":[{"id":2178,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/2172\/revisions\/2178"}],"wp:attachment":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/media?parent=2172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/categories?post=2172"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/tags?post=2172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}