File size: 5,261 Bytes
7ae16f7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# Text summarization
```elixir
Mix.install(
[
{:kino_bumblebee, "~> 0.4.0"},
{:exla, ">= 0.0.0"},
{:explorer, "~> 0.7.0"},
{:kino_explorer, "~> 0.1.11"}
],
config: [nx: [default_backend: EXLA.Backend]]
)
```
## Summarize an audio transcription
```elixir
text = """
This 55-year-old man with known coronary artery disease comes for a follow-up visit today.
Last month he was admitted to our hospital with unstable angina.
He underwent heart catheterization on November 15th, 2007.
At that time he was found to have a tight 99% proxmost enosis, total occlusion and collateralization
of the mid-circumflex, right coronary artery was normal.
Ventricularography was normal and his ejection fraction was 65%.
He underwants an uncomplicated placement of a cipher drug-eleuting
stent to his proximal lesion.
The attempted coronary intervention of the circumflex was unsuccessful,
as his lesion cannot be crossed. His post procedure was uncomplicated, and he was discharged on the day following his intervention.
He comes today indicating that he is feeling great.
His current medications include aspirin,
325 milligrams daily, lipatore, 40 milligrams daily,
and platvic, 75 milligrams daily.
"""
```
## Section
```elixir
{:ok, model_info} =
Bumblebee.load_model({:hf, "vblagoje/bert-english-uncased-finetuned-pos"})
{:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "bert-base-uncased"})
serving =
Bumblebee.Text.token_classification(model_info, tokenizer,
aggregation: :same,
compile: [batch_size: 1, sequence_length: 100],
defn_options: [compiler: EXLA]
)
```
```elixir
text = "This 55-year-old man with known coronary artery disease comes for a follow-up visit today.
Last month he was admitted to our hospital with unstable angina."
text = "Last month he was admitted to our hospital with unstable angina."
ignored = ["DET", "PUNCT", "ADP", "NUM", "AUX", "PRON"]
# ignored = []
output = Nx.Serving.run(serving, text)
output.entities
|> Enum.reduce([], fn entity, acc ->
if Enum.member?(ignored, entity.label) do
acc
else
# "VERB", "NOUN", and "ADJ"
next_phrase =
if entity.label == "ADJ" do
"#{entity.phrase} [CONTINUATION]"
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, "[CONTINUATION]") do
acc_without_last = Enum.take(acc, Enum.count(acc) - 1)
acc_without_last ++ [String.replace(previous, "[CONTINUATION]", next_phrase)]
else
acc ++ [next_phrase]
end
else
acc ++ [next_phrase]
end
end
end)
```
<!-- livebook:{"attrs":"eyJjb21waWxlciI6ImV4bGEiLCJsYWJlbHMiOiJsYXN0IG1vbnRoLCBhZG1pdHRlZCwgaG9zcGl0YWwsIHVuc3RhYmxlIGFuZ2luYSIsInNlcXVlbmNlX2xlbmd0aCI6MTAwLCJ0YXNrX2lkIjoiemVyb19zaG90X3RleHRfY2xhc3NpZmljYXRpb24iLCJ0b3BfayI6bnVsbCwidmFyaWFudF9pZCI6ImJhcnRfbGFyZ2VfbW5saSJ9","chunks":[[0,396],[398,509]],"kind":"Elixir.KinoBumblebee.TaskCell","livebook_object":"smart_cell"} -->
```elixir
{:ok, model_info} = Bumblebee.load_model({:hf, "facebook/bart-large-mnli"})
{:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "facebook/bart-large-mnli"})
labels = ["last month", "admitted", "hospital", "unstable angina"]
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("Text", default: "One day I will see the world.")
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(&{&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:{"attrs":"eyJjb21waWxlciI6ImV4bGEiLCJzZXF1ZW5jZV9sZW5ndGgiOjUwMCwidGFza19pZCI6InF1ZXN0aW9uX2Fuc3dlcmluZyIsInZhcmlhbnRfaWQiOiJkaXN0aWxiZXJ0X2Jhc2VfY2FzZWQifQ","chunks":[[0,344],[346,595]],"kind":"Elixir.KinoBumblebee.TaskCell","livebook_object":"smart_cell"} -->
```elixir
{:ok, model_info} = Bumblebee.load_model({:hf, "distilbert-base-cased-distilled-squad"})
{:ok, tokenizer} =
Bumblebee.load_tokenizer({:hf, "distilbert-base-cased-distilled-squad"})
serving =
Bumblebee.Text.question_answering(model_info, tokenizer,
compile: [batch_size: 1, sequence_length: 500],
defn_options: [compiler: EXLA]
)
inputs = [
question: Kino.Input.text("Question", default: "Where do I live?"),
context: Kino.Input.textarea("Context", default: "My name is Sarah and I live in London.")
]
form = Kino.Control.form(inputs, submit: "Run")
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)
```
|