gabrielmancini commited on
Commit
9fb11ce
1 Parent(s): bf629d8

Upload whisper-chat-app.livemd

Browse files
Files changed (1) hide show
  1. public-apps/whisper-chat-app.livemd +64 -0
public-apps/whisper-chat-app.livemd ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ Kino.Markdown.new("""
19
+ This chat is open to anyone, be polite and act responsibly. :) Note chat history has been disabled.
20
+ """)
21
+ ```
22
+
23
+ ```elixir
24
+ {:ok, model_info} =
25
+ Bumblebee.load_model({:hf, "openai/whisper-tiny"}, log_params_diff: false)
26
+
27
+ {:ok, featurizer} = Bumblebee.load_featurizer({:hf, "openai/whisper-tiny"})
28
+ {:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "openai/whisper-tiny"})
29
+
30
+ serving =
31
+ Bumblebee.Audio.speech_to_text(model_info, featurizer, tokenizer,
32
+ max_new_tokens: 100,
33
+ compile: [batch_size: 8],
34
+ defn_options: [compiler: EXLA]
35
+ )
36
+
37
+ Kino.start_child({Nx.Serving, serving: serving, name: WhisperChat})
38
+ ```
39
+
40
+ ```elixir
41
+ audio_input = Kino.Input.audio("Audio", sampling_rate: featurizer.sampling_rate)
42
+ name_input = Kino.Input.text("Name")
43
+ form = Kino.Control.form([name: name_input, audio: audio_input], submit: "Send")
44
+ frame = Kino.Frame.new()
45
+
46
+ Kino.async_listen(form, fn %{data: %{audio: audio, name: name}, origin: origin} ->
47
+ if audio && name != "" do
48
+ audio =
49
+ audio.data
50
+ |> Nx.from_binary(:f32)
51
+ |> Nx.reshape({:auto, audio.num_channels})
52
+ |> Nx.mean(axes: [1])
53
+
54
+ %{results: [%{text: generated_text}]} = Nx.Serving.batched_run(WhisperChat, audio)
55
+ content = Kino.Markdown.new("**#{name}**: #{generated_text}")
56
+ Kino.Frame.append(frame, content, temporary: true)
57
+ else
58
+ content = Kino.Markdown.new("*Error! Name and Audio are required*")
59
+ Kino.Frame.append(frame, content, to: origin)
60
+ end
61
+ end)
62
+
63
+ Kino.Layout.grid([frame, form], boxed: true, gap: 16)
64
+ ```