josevalim commited on
Commit
8619305
1 Parent(s): 739349d

Upload whisper-chat-app.livemd

Browse files
Files changed (1) hide show
  1. public-apps/whisper-chat-app.livemd +62 -0
public-apps/whisper-chat-app.livemd ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!-- livebook:{"app_settings":{"access_type":"public","slug":"whisper-chat"}} -->
2
+
3
+ # Whisper chat
4
+
5
+ ```elixir
6
+ Mix.install(
7
+ [
8
+ {:kino_bumblebee, "~> 0.2.1"},
9
+ {:exla, "~> 0.5.1"}
10
+ ],
11
+ config: [nx: [default_backend: EXLA.Backend]]
12
+ )
13
+ ```
14
+
15
+ ## Section
16
+
17
+ ```elixir
18
+
19
+ ```
20
+
21
+ ```elixir
22
+ {:ok, model_info} =
23
+ Bumblebee.load_model({:hf, "openai/whisper-tiny"}, log_params_diff: false)
24
+
25
+ {:ok, featurizer} = Bumblebee.load_featurizer({:hf, "openai/whisper-tiny"})
26
+ {:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "openai/whisper-tiny"})
27
+
28
+ serving =
29
+ Bumblebee.Audio.speech_to_text(model_info, featurizer, tokenizer,
30
+ max_new_tokens: 100,
31
+ compile: [batch_size: 8],
32
+ defn_options: [compiler: EXLA]
33
+ )
34
+
35
+ Kino.start_child({Nx.Serving, serving: serving, name: WhisperChat})
36
+ ```
37
+
38
+ ```elixir
39
+ audio_input = Kino.Input.audio("Audio", sampling_rate: featurizer.sampling_rate)
40
+ name_input = Kino.Input.text("Name")
41
+ form = Kino.Control.form([name: name_input, audio: audio_input], submit: "Send")
42
+ frame = Kino.Frame.new()
43
+
44
+ Kino.async_listen(form, fn %{data: %{audio: audio, name: name}, origin: origin} ->
45
+ if audio && name != "" do
46
+ audio =
47
+ audio.data
48
+ |> Nx.from_binary(:f32)
49
+ |> Nx.reshape({:auto, audio.num_channels})
50
+ |> Nx.mean(axes: [1])
51
+
52
+ %{results: [%{text: generated_text}]} = Nx.Serving.batched_run(WhisperChat, audio)
53
+ content = Kino.Markdown.new("**#{name}**: #{generated_text}")
54
+ Kino.Frame.append(frame, content)
55
+ else
56
+ content = Kino.Markdown.new("*Error! Name and Audio are required*")
57
+ Kino.Frame.append(frame, content, to: origin)
58
+ end
59
+ end)
60
+
61
+ Kino.Layout.grid([frame, form], boxed: true, gap: 16)
62
+ ```