Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us
Notesclub

Introduction to Livebook — Lightning Talk

livebook_intro_lightning_talk.livemd

Introduction to Livebook — Lightning Talk

Mix.install(
  [
    {:kino, "~> 0.11.0"},
    {:kino_vega_lite, "~> 0.1.10"},
    {:req, "~> 0.4.4"},
    {:kino_bumblebee, "~> 0.4.0"},
    {:exla, ">= 0.0.0"}
  ],
  config: [nx: [default_backend: EXLA.Backend]]
)

Interactive code notebooks

  • You can use it in your computer (most common) or use it from a server
  • livebook.dev

Livebook vs Jupyter

a = 1
a = a + 1

Markdown and Mermaid diagrams

We can add Markdown and mermaid cells

graph TD;
  A-->B;
  A-->C;
  B-->D;
  C-->D;

Feed

Add and use any hex package:

Req.get("http://demo.doofinder.com")

Debug

1..10
|> Enum.map(&(&1 + 2))
|> Enum.map(&(&1 * 5))
|> Enum.sum()
1..10
|> Enum.map(&(&1 + 2))
|> Enum.map(&(&1 * 5))
|> Enum.sum()
|> dbg()
1..10
|> Stream.map(&(&1 + 2))
|> Stream.map(&(&1 * 5))
|> Enum.sum()
|> dbg()

Debug local/staging/production node (e.g. phoenix app)

Processes

processes =
  for pid <- Process.list() do
    info = Process.info(pid, [:reductions, :memory, :status])

    %{
      pid: inspect(pid),
      reductions: info[:reductions],
      memory: info[:memory],
      status: info[:status]
    }
  end
memory_eaters =
  processes
  |> Enum.sort_by(&amp;(-&amp;1[:memory]))
  |> Enum.take(5)

Smartcells — Charts

VegaLite.new()
|> VegaLite.data_from_values(memory_eaters, only: ["reductions", "memory", "pid"])
|> VegaLite.mark(:point)
|> VegaLite.encode_field(:x, "reductions", type: :quantitative)
|> VegaLite.encode_field(:y, "memory", type: :quantitative)
|> VegaLite.encode_field(:color, "pid", type: :nominal)

Smartcells - IA

{:ok, model_info} =
  Bumblebee.load_model({:hf, "finiteautomata/bertweet-base-sentiment-analysis"})

{:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "vinai/bertweet-base"})

serving =
  Bumblebee.Text.text_classification(model_info, tokenizer,
    compile: [batch_size: 1, sequence_length: 100],
    defn_options: [compiler: EXLA]
  )

text_input = Kino.Input.textarea("Text", default: "Cats are so cute")
form = Kino.Control.form([text: text_input], submit: "Run")
frame = Kino.Frame.new()

Kino.listen(form, fn %{data: %{text: text}} ->
  Kino.Frame.render(frame, Kino.Text.new("Running..."))
  output = Nx.Serving.run(serving, text)

  output.predictions
  |> Enum.map(&amp;{&amp;1.label, &amp;1.score})
  |> Kino.Bumblebee.ScoredList.new()
  |> then(&amp;Kino.Frame.render(frame, &amp;1))
end)

Kino.Layout.grid([form, frame], boxed: true, gap: 16)

Apps

Open app_example.livemd

Animations

Kino.animate(100, fn i ->
  Kino.Markdown.new("**Iteration: `#{i}`**")
end)

Browse and search public notebooks

notes.club — an open source site I created to discover notebooks

Try Livebook now without installing it

You can also install it in your computer (recommended): livebook.dev