|
# Text summarization |
|
|
|
```elixir |
|
Mix.install( |
|
[ |
|
{:kino_bumblebee, }, |
|
{:exla, }, |
|
{:explorer, }, |
|
{:kino_explorer, } |
|
], |
|
config: [nx: [default_backend: EXLA.Backend]] |
|
) |
|
``` |
|
|
|
## Summarize an audio transcription |
|
|
|
```elixir |
|
text = |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
``` |
|
|
|
## Section |
|
|
|
```elixir |
|
{:ok, model_info} = |
|
Bumblebee.load_model({:hf, }) |
|
|
|
{:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, }) |
|
|
|
serving = |
|
Bumblebee.Text.token_classification(model_info, tokenizer, |
|
aggregation: :same, |
|
compile: [batch_size: 1, sequence_length: 100], |
|
defn_options: [compiler: EXLA] |
|
) |
|
``` |
|
|
|
```elixir |
|
text = |
|
|
|
|
|
text = |
|
|
|
ignored = [, , , , , ] |
|
# ignored = [] |
|
|
|
output = Nx.Serving.run(serving, text) |
|
|
|
output.entities |
|
|> Enum.reduce([], fn entity, acc -> |
|
if Enum.member?(ignored, entity.label) do |
|
acc |
|
else |
|
# , , and |
|
next_phrase = |
|
if entity.label == do |
|
|
|
else |
|
entity.phrase |
|
end |
|
|
|
if Enum.count(acc) > 0 do |
|
previous = Enum.at(acc, -1) |
|
|
|
# First, check if the previous phrase ends with a continuation token. |
|
if String.ends_with?(previous, ) do |
|
acc_without_last = Enum.take(acc, Enum.count(acc) - 1) |
|
acc_without_last ++ [String.replace(previous, , next_phrase)] |
|
else |
|
acc ++ [next_phrase] |
|
end |
|
else |
|
acc ++ [next_phrase] |
|
end |
|
end |
|
end) |
|
``` |
|
|
|
<!-- livebook:{:,:[[0,396],[398,509]],:,:} --> |
|
|
|
```elixir |
|
{:ok, model_info} = Bumblebee.load_model({:hf, }) |
|
{:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, }) |
|
labels = [, , , ] |
|
|
|
serving = |
|
Bumblebee.Text.zero_shot_classification(model_info, tokenizer, labels, |
|
compile: [batch_size: 1, sequence_length: 100], |
|
defn_options: [compiler: EXLA] |
|
) |
|
|
|
text_input = Kino.Input.textarea(, default: ) |
|
form = Kino.Control.form([text: text_input], submit: ) |
|
frame = Kino.Frame.new() |
|
|
|
Kino.listen(form, fn %{data: %{text: text}} -> |
|
Kino.Frame.render(frame, Kino.Text.new()) |
|
output = Nx.Serving.run(serving, text) |
|
|
|
output.predictions |
|
|> Enum.map(&{&1.label, &1.score}) |
|
|> Kino.Bumblebee.ScoredList.new() |
|
|> then(&Kino.Frame.render(frame, &1)) |
|
end) |
|
|
|
Kino.Layout.grid([form, frame], boxed: true, gap: 16) |
|
``` |
|
|
|
## Question answering |
|
|
|
<!-- livebook:{:,:[[0,344],[346,595]],:,:} --> |
|
|
|
```elixir |
|
{:ok, model_info} = Bumblebee.load_model({:hf, }) |
|
|
|
{:ok, tokenizer} = |
|
Bumblebee.load_tokenizer({:hf, }) |
|
|
|
serving = |
|
Bumblebee.Text.question_answering(model_info, tokenizer, |
|
compile: [batch_size: 1, sequence_length: 500], |
|
defn_options: [compiler: EXLA] |
|
) |
|
|
|
inputs = [ |
|
question: Kino.Input.text(, default: ), |
|
context: Kino.Input.textarea(, default: ) |
|
] |
|
|
|
form = Kino.Control.form(inputs, submit: ) |
|
frame = Kino.Frame.new() |
|
|
|
Kino.listen(form, fn %{data: %{question: question, context: context}} -> |
|
output = Nx.Serving.run(serving, %{question: question, context: context}) |
|
|
|
output.results |
|
|> Enum.map(&{&1.text, &1.score}) |
|
|> Kino.Bumblebee.ScoredList.new() |
|
|> then(&Kino.Frame.render(frame, &1)) |
|
end) |
|
|
|
Kino.Layout.grid([form, frame], boxed: true, gap: 16) |
|
``` |
|
|