Julia Community 🟣

The Julia Geek
The Julia Geek

Posted on

How to port the Huggingface🤗 into Julia using PyCall?

Ready for a fast and concise delicious Julia thing? Read to the end, you'll be surprised.
First of all, I should mention that you can port any python package into Julia with zero overhead! This means you won't face performance reduction at all!

First, import the PyCall and connect it into an executable Python (I go for python3.10 since the huggingface🤗 isn't available for 3.11 so far):

julia> using PyCall

julia> ENV["PYTHON"] = raw"C:\Users\<USER>\AppData\Local\Programs\Python\Python310\python.exe";

julia> using Pkg

julia> Pkg.build("PyCall")
Enter fullscreen mode Exit fullscreen mode

After a successful build, exit from the Julia and re-run it. Then check your python environment is changed successfully:

julia> PyCall.python
"C:\\Users\\<USER>\\AppData\\Local\\Programs\\Python\\Python310\\python.exe"
Enter fullscreen mode Exit fullscreen mode

Good! I'm on Python3.10 now. Then, open cmd and install the huggingface on the same python path via pip install transformers[torch]. Then start a fresh Julia session and continue:

julia> using PyCall

julia> ts = pyimport("transformers");

julia> gen = ts.pipeline(task="text-generation")
Enter fullscreen mode Exit fullscreen mode

This leads to downloading the requirements (~550MB). Then let's get a text generator:

julia> gen("Hello from Julia, Julia is a lovely programming language that is fast as C in many cases, and readable as Python in everycases.")
Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.
1-element Vector{Dict{Any, Any}}:
 Dict("generated_text" => "Hello from Julia, Julia is a lovely programming language that is fast as C in many cases, and readable as Python in everycases. Julia is a great way of creating beautiful applications and helping you write beautiful code, and I love using it everyday!")
Enter fullscreen mode Exit fullscreen mode

Look how beautiful it continued my sentence:

"Julia is a great way of creating beautiful applications and helping you write beautiful code, and I love using it everyday!"

Yes OF COURSE I LOVE USING IT EVERYDAY god damn ya 😍😂
As you can see, we are good to continue further! Cheers 🍻

🟢🔴🟣 JuliaGeeks

Latest comments (1)

Collapse
 
mantzaris profile image
a.v.mantzaris

Very good to know. Would be great if the example could continue to explore the usage a bit further.