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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!-- livebook:{"app_settings":{"access_type":"private","output_type":"rich","slug":"80s-music"},"file_entries":[{"file":{"file_system_id":"local","file_system_type":"local","path":"80s_detailed.csv"},"name":"80s_detailed.csv","type":"file"}],"persist_outputs":true} -->
|
| 2 |
+
|
| 3 |
+
# 1980s Music Sentiment
|
| 4 |
+
|
| 5 |
+
```elixir
|
| 6 |
+
Mix.install(
|
| 7 |
+
[
|
| 8 |
+
{:bumblebee, "~> 0.4.2"},
|
| 9 |
+
{:nx, "~> 0.6.1"},
|
| 10 |
+
{:exla, "~> 0.6.1"},
|
| 11 |
+
{:axon, "~> 0.6.0"},
|
| 12 |
+
{:kino, "~> 0.12.3"},
|
| 13 |
+
{:kino_explorer, "~> 0.1.18"},
|
| 14 |
+
{:kino_vega_lite, "~> 0.1.10"}
|
| 15 |
+
],
|
| 16 |
+
config: [nx: [default_backend: EXLA.Backend]]
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
Nx.global_default_backend(EXLA.Backend)
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
## Introduction
|
| 23 |
+
|
| 24 |
+
Music of the 80s came in many flavours; rock, pop, punk, synth. In this livebook we will delve into the feelings that many of these songs evoke. To our help we have [Hugging Face](https://huggingface.co/) and its library of open source ML models.
|
| 25 |
+
|
| 26 |
+
The model we will use here is a derivative of `roberta` called [`roberta-base-go_emotions`](https://huggingface.co/SamLowe/roberta-base-go_emotions?text=White+man+came+across+the+sea%2C+He+brought+us+pain+and+misery.+He+killed+our+tribes+killed+our+creed%2C+He+took+our+game+for+his+own+need). This model comes with 28 emotion labels. We will be able to use it via [Bumblebee Text Classification](https://hexdocs.pm/bumblebee/Bumblebee.Text.html#text_classification/3).
|
| 27 |
+
|
| 28 |
+
The idea is to pass a 1980's lyric into the model and get a classification back from it.
|
| 29 |
+
|
| 30 |
+
## Load models
|
| 31 |
+
|
| 32 |
+
```elixir
|
| 33 |
+
{:ok, emotions} = Bumblebee.load_model({:hf, "SamLowe/roberta-base-go_emotions"})
|
| 34 |
+
{:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "SamLowe/roberta-base-go_emotions"})
|
| 35 |
+
|
| 36 |
+
:ok
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
<!-- livebook:{"output":true} -->
|
| 40 |
+
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
07:59:01.614 [info] TfrtCpuClient created.
|
| 44 |
+
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
<!-- livebook:{"output":true} -->
|
| 48 |
+
|
| 49 |
+
```
|
| 50 |
+
:ok
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
With the emotion sentiment model loaded, lets get some lyrics to analyze.
|
| 54 |
+
|
| 55 |
+
```elixir
|
| 56 |
+
text_input =
|
| 57 |
+
Kino.Input.textarea("Text",
|
| 58 |
+
default:
|
| 59 |
+
"White man came across the sea, He brought us pain and misery. He killed our tribes killed our creed, He took our game for his own need"
|
| 60 |
+
)
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
```elixir
|
| 64 |
+
text = Kino.Input.read(text_input)
|
| 65 |
+
serving = Bumblebee.Text.text_classification(emotions, tokenizer)
|
| 66 |
+
Nx.Serving.run(serving, text)
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
<!-- livebook:{"output":true} -->
|
| 70 |
+
|
| 71 |
+
```
|
| 72 |
+
%{
|
| 73 |
+
predictions: [
|
| 74 |
+
%{label: "neutral", score: 0.31277623772621155},
|
| 75 |
+
%{label: "optimism", score: 0.23790912330150604},
|
| 76 |
+
%{label: "caring", score: 0.2258605808019638},
|
| 77 |
+
%{label: "approval", score: 0.11500591784715652},
|
| 78 |
+
%{label: "desire", score: 0.030266664922237396}
|
| 79 |
+
]
|
| 80 |
+
}
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
### Load and parse a file
|
| 84 |
+
|
| 85 |
+
We have an attachment to this Livebook: `80s_detailed.csv`. We import it with Kino and use Kino Explorer to parse the csv into a dataframe, `df`.
|
| 86 |
+
|
| 87 |
+
```elixir
|
| 88 |
+
df =
|
| 89 |
+
Kino.FS.file_path("80s_detailed.csv")
|
| 90 |
+
|> Explorer.DataFrame.from_csv!()
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
```elixir
|
| 94 |
+
analysis = fn lyric ->
|
| 95 |
+
%{predictions: [%{label: label} | _tail]} = Nx.Serving.run(serving, lyric)
|
| 96 |
+
label
|
| 97 |
+
end
|
| 98 |
+
|
| 99 |
+
size = Explorer.Series.size(df["lyrics"])
|
| 100 |
+
|
| 101 |
+
lyrics_list = Explorer.Series.to_list(df["lyrics"])
|
| 102 |
+
|
| 103 |
+
take_num = 10
|
| 104 |
+
# last_songs = lyrics_list |> Enum.slice(-take_num..-1)
|
| 105 |
+
|
| 106 |
+
analysed_list_0_10 =
|
| 107 |
+
lyrics_list
|
| 108 |
+
|> Enum.take(take_num)
|
| 109 |
+
|> Enum.map(fn lyric -> analysis.(lyric) end)
|
| 110 |
+
|
| 111 |
+
# |> Enum.take(take_num)
|
| 112 |
+
|
| 113 |
+
# dummy_values =
|
| 114 |
+
# 1..(size - take_num)
|
| 115 |
+
# |> Enum.map(fn _ -> nil end)
|
| 116 |
+
|
| 117 |
+
# list = dummy_values ++ analysed_list
|
| 118 |
+
|
| 119 |
+
# updated_list = Explorer.DataFrame.put(
|
| 120 |
+
# df,
|
| 121 |
+
# :emotion,
|
| 122 |
+
# Explorer.Series.from_list(list)
|
| 123 |
+
# )
|
| 124 |
+
```
|
| 125 |
+
|
| 126 |
+
<!-- livebook:{"output":true} -->
|
| 127 |
+
|
| 128 |
+
```
|
| 129 |
+
["love", "desire", "neutral", "love", "neutral", "neutral", "neutral", "neutral", "curiosity",
|
| 130 |
+
"neutral"]
|
| 131 |
+
```
|
| 132 |
+
|
| 133 |
+
```elixir
|
| 134 |
+
analysed_list_121_130 =
|
| 135 |
+
lyrics_list
|
| 136 |
+
|> Enum.slice(121..130)
|
| 137 |
+
|> Enum.map(fn lyric -> analysis.(lyric) end)
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
```elixir
|
| 141 |
+
analysed_list_11_30 =
|
| 142 |
+
lyrics_list
|
| 143 |
+
|> Enum.slice(11..30)
|
| 144 |
+
|> Enum.map(fn lyric -> analysis.(lyric) end)
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
<!-- livebook:{"output":true} -->
|
| 148 |
+
|
| 149 |
+
```
|
| 150 |
+
["love", "neutral", "love", "love", "love", "disapproval", "joy", "neutral", "love", "love",
|
| 151 |
+
"neutral", "love", "neutral", "neutral", "love", "love", "desire", "love", "approval", "neutral"]
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
```elixir
|
| 155 |
+
analysed_list_31_60 =
|
| 156 |
+
lyrics_list
|
| 157 |
+
|> Enum.slice(31..60)
|
| 158 |
+
|> Enum.map(fn lyric -> analysis.(lyric) end)
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
<!-- livebook:{"output":true} -->
|
| 162 |
+
|
| 163 |
+
```
|
| 164 |
+
["neutral", "disapproval", "neutral", "love", "love", "neutral", "love", "neutral", "love", "love",
|
| 165 |
+
"neutral", "curiosity", "love", "neutral", "admiration", "neutral", "neutral", "neutral", "love",
|
| 166 |
+
"neutral", "neutral", "neutral", "neutral", "neutral", "neutral", "neutral", "neutral", "neutral",
|
| 167 |
+
"fear", "neutral"]
|
| 168 |
+
```
|
| 169 |
+
|
| 170 |
+
```elixir
|
| 171 |
+
analysed_list_61_90 =
|
| 172 |
+
lyrics_list
|
| 173 |
+
|> Enum.slice(61..90)
|
| 174 |
+
|> Enum.map(fn lyric -> analysis.(lyric) end)
|
| 175 |
+
```
|
| 176 |
+
|
| 177 |
+
<!-- livebook:{"output":true} -->
|
| 178 |
+
|
| 179 |
+
```
|
| 180 |
+
["joy", "neutral", "neutral", "love", "neutral", "neutral", "sadness", "love", "love", "neutral",
|
| 181 |
+
"neutral", "neutral", "amusement", "confusion", "sadness", "neutral", "love", "excitement",
|
| 182 |
+
"neutral", "neutral", "love", "desire", "neutral", "disappointment", "desire", "approval", "love",
|
| 183 |
+
"neutral", "love", "sadness"]
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
```elixir
|
| 187 |
+
analysed_list_91_120 =
|
| 188 |
+
lyrics_list
|
| 189 |
+
|> Enum.slice(91..120)
|
| 190 |
+
|> Enum.map(fn lyric -> analysis.(lyric) end)
|
| 191 |
+
```
|
| 192 |
+
|
| 193 |
+
<!-- livebook:{"output":true} -->
|
| 194 |
+
|
| 195 |
+
```
|
| 196 |
+
["love", "joy", "neutral", "neutral", "love", "love", "neutral", "neutral", "love", "neutral",
|
| 197 |
+
"love", "neutral", "neutral", "neutral", "neutral", "love", "neutral", "neutral", "love",
|
| 198 |
+
"neutral", "disapproval", "neutral", "neutral", "desire", "neutral", "neutral", "love", "optimism",
|
| 199 |
+
"desire", "love"]
|
| 200 |
+
```
|
| 201 |
+
|
| 202 |
+
```elixir
|
| 203 |
+
analysed_list_131_150 =
|
| 204 |
+
lyrics_list
|
| 205 |
+
|> Enum.slice(131..150)
|
| 206 |
+
|> Enum.map(fn lyric -> analysis.(lyric) end)
|
| 207 |
+
```
|