medicode / livebooks /summarization.livemd
noahsettersten's picture
feat: Gather keywords from transcribed text
7ae16f7
raw
history blame
5.26 kB
# 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)
```