Spaces:
Paused
Paused
Murat commited on
Commit ·
aa12eca
1
Parent(s): 806cc56
Upload whisper_tests.livemd
Browse files
public-apps/whisper_tests.livemd
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!-- livebook:{"app_settings":{"access_type":"public","auto_shutdown_ms":3600000,"multi_session":true,"output_type":"rich","slug":"whisper-tests"}} -->
|
| 2 |
+
|
| 3 |
+
# Whisper Tests
|
| 4 |
+
|
| 5 |
+
```elixir
|
| 6 |
+
Mix.install(
|
| 7 |
+
[
|
| 8 |
+
{:bumblebee, github: "elixir-nx/bumblebee"},
|
| 9 |
+
{:kino_bumblebee, github: "livebook-dev/kino_bumblebee"},
|
| 10 |
+
{:kino, github: "livebook-dev/kino", override: true},
|
| 11 |
+
{:exla, "~> 0.6.1"}
|
| 12 |
+
],
|
| 13 |
+
config: [nx: [default_backend: EXLA.Backend]]
|
| 14 |
+
)
|
| 15 |
+
```
|
| 16 |
+
|
| 17 |
+
## Section
|
| 18 |
+
|
| 19 |
+
```elixir
|
| 20 |
+
language = Kino.Input.select("Select Language", en: "English", tr: "Turkish")
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
<!-- livebook:{"reevaluate_automatically":true} -->
|
| 24 |
+
|
| 25 |
+
```elixir
|
| 26 |
+
{:ok, model_info} = Bumblebee.load_model({:hf, "openai/whisper-base"})
|
| 27 |
+
{:ok, featurizer} = Bumblebee.load_featurizer({:hf, "openai/whisper-base"})
|
| 28 |
+
{:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "openai/whisper-base"})
|
| 29 |
+
|
| 30 |
+
{:ok, generation_config} =
|
| 31 |
+
Bumblebee.load_generation_config({:hf, "openai/whisper-base"})
|
| 32 |
+
|
| 33 |
+
generation_config = Bumblebee.configure(generation_config, max_new_tokens: 100)
|
| 34 |
+
|
| 35 |
+
# Kino.listen(language_form, fn event ->
|
| 36 |
+
# lang = Atom.to_string(event.data.name)
|
| 37 |
+
# # IO.puts(event.data.name)
|
| 38 |
+
|
| 39 |
+
# end)
|
| 40 |
+
|
| 41 |
+
lang = Kino.Input.read(language)
|
| 42 |
+
|
| 43 |
+
serving =
|
| 44 |
+
Bumblebee.Audio.speech_to_text_whisper(model_info, featurizer, tokenizer, generation_config,
|
| 45 |
+
compile: [batch_size: 30],
|
| 46 |
+
defn_options: [compiler: EXLA],
|
| 47 |
+
chunk_num_seconds: 30,
|
| 48 |
+
language: Atom.to_string(lang)
|
| 49 |
+
)
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
<!-- livebook:{"reevaluate_automatically":true} -->
|
| 53 |
+
|
| 54 |
+
```elixir
|
| 55 |
+
audio_input = Kino.Input.audio("Audio", sampling_rate: featurizer.sampling_rate)
|
| 56 |
+
|
| 57 |
+
form = Kino.Control.form([audio: audio_input], submit: "Run")
|
| 58 |
+
frame = Kino.Frame.new()
|
| 59 |
+
|
| 60 |
+
Kino.listen(form, fn %{data: %{audio: audio}} ->
|
| 61 |
+
if audio do
|
| 62 |
+
Kino.Frame.render(frame, Kino.Text.new("Running..."))
|
| 63 |
+
|
| 64 |
+
audio =
|
| 65 |
+
audio.data
|
| 66 |
+
|> Nx.from_binary(:f32)
|
| 67 |
+
|> Nx.reshape({:auto, audio.num_channels})
|
| 68 |
+
|> Nx.mean(axes: [1])
|
| 69 |
+
|
| 70 |
+
output = Nx.Serving.run(serving, audio)
|
| 71 |
+
# %{chunks: [%{text: generated_text}]}
|
| 72 |
+
text = output.chunks |> Enum.map_join(& &1.text) |> String.trim()
|
| 73 |
+
Kino.Frame.render(frame, Kino.Text.new(text))
|
| 74 |
+
end
|
| 75 |
+
end)
|
| 76 |
+
|
| 77 |
+
Kino.Layout.grid([form, frame], boxed: true, gap: 16)
|
| 78 |
+
```
|