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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79e8ef4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
<!-- livebook:{"app_settings":{"access_type":"public","auto_shutdown_ms":5000,"multi_session":true,"output_type":"rich","show_existing_sessions":false,"slug":"mystery"},"autosave_interval_s":null,"file_entries":[{"file":{"file_system_id":"local","file_system_type":"local","path":"/data/messages.term"},"name":"messages.term","type":"file"}],"persist_outputs":true} -->

# Mysterious Crystal Ball

```elixir
Mix.install(
  [
    {:bumblebee, "~> 0.5.3"},
    {:nx, "~> 0.7.0"},
    {:exla, "~> 0.7.0"},
    {:axon, "~> 0.6.1"},
    {:kino, "~> 0.12.0"},
    {:req, "~> 0.4.11"},
    {:kino_bumblebee, "~> 0.5.0"},
    {:nimble_csv, "~> 1.2"}
  ],
  config: [nx: [default_backend: EXLA.Backend]]
)

Kino.Frame.new(placeholder: false)
```

## Section

```elixir
html =
  Kino.HTML.new("""
      <style>

          .truth-container {
              display: flex;
              flex-direction: column;
              justify-content: center;
              height: 38.5rem;
              padding: 0.5rem;
          }

          .collapse-x {
              transform: scaleX(0);
              transition: transform 0.8s;
          }

          .collapse-y {
              transform: scaley(0);
              transition: transform 0.8s;
          }

          .gone {
              display: none;
          }

          .exists {
              display: block;
          }

          #beyond-truth {
              width: 100%;

              display: flex;
              justify-content: center;
          }

          #beyond-truth.gone {
              margin: 0;
              padding: 0;
              height: 0;
          }

          #globe-canvas {
              margin: auto;
              max-width: 100%;
              width: 37.5rem;
              height: auto;
          }

          #globe-canvas.gone {
              height: 0;
          }

          .orb {
              width: 40rem;
              height: 40rem;
              border-radius: 50%;
              box-shadow: 0px 0px 16px rgba(81, 81, 231, 0.4); /* Subtle shadow */
          }

          .message {
              margin-top: 20px;
              font-family: monospace;
              color: rgba(81, 81, 231, 1);
          }

    </style>
    <div class="truth-container">
          <canvas id="globe-canvas" width="600" height="600" class="orb"></canvas>
          <div id="beyond-truth" class="message gone"></div >
          
      </div>

      <script>
          const canvas = document.getElementById('globe-canvas');

          canvas.addEventListener('click', shatterGlobe);
          let isCollapsing = false;
          let hasCollapsed = false;

          const context = canvas.getContext('2d');

          const tiltAngle = Math.PI / 228;
          let rotation = 90;
          const dots = [];
          const radius = 120;

          // Create dots on the globe
          for (let lat = 0; lat < Math.PI; lat += Math.PI / 30) {
              for (let lon = 0; lon < 2 * Math.PI; lon += Math.PI / 30) {
                  const x = radius * Math.sin(lat) * Math.cos(lon);
                  const y = radius * Math.sin(lat) * Math.sin(lon);
                  const z = radius * Math.cos(lat);
                  dots.push({x: x, y: y, z: z});
              }
          }

          function draw() {

              context.clearRect(0, 0, canvas.width, canvas.height);
              context.translate(canvas.width / 2, canvas.height / 2);

              context.rotate(tiltAngle);

              // Rotate the globe
              if (!hasCollapsed) {
                  rotation += 0.01;
              }

              dots.forEach(function(dot) {
                  const x = dot.x * Math.cos(rotation) - dot.z * Math.sin(rotation);
                  const y = dot.y;
                  const z = dot.z * Math.cos(rotation) + dot.x * Math.sin(rotation);

                  const brightness = 1 - (z / radius + 1.25) / 2;

                  const size = 2 - z / radius;
                  context.fillRect(x, y, size, size);
                  context.fillStyle = 'rgba(81, 81, 231, ' + brightness + ')';

              });
              context.rotate(-tiltAngle)

              if (isCollapsing) {
                  let collapsed = true;
                  dots.forEach(dot => {
                      dot.y = (dot.y + 0.001 )* 1.015; // Adjust the speed of falling

                      // Check for bottom boundary
                      if (Math.abs(dot.y) > canvas.height / 2) {
                          dot.y = canvas.height / 2; // Stop it at the 'ground'
                      } else {
                          collapsed = false;
                      }
                  });

                  if (collapsed) {
                      hasCollapsed = true;
                  }
              }

              context.translate(-canvas.width / 2, -canvas.height / 2);

              if (!hasCollapsed) {
                  requestAnimationFrame(draw);
              } else {
                  console.log({why: "Look what you have done to my precious orb!"});
                  canvas.classList.add("collapse-x");
                  setTimeout(() => {
                      canvas.classList.add("collapse-y");
                  }, 1000)
                  setTimeout(() => {
                      canvas.classList.add("gone");
                  }, 2000)
                  setTimeout(() => {
                      const beyondTruth = document.getElementById("beyond-truth");
                      beyondTruth.classList.remove("gone");

                      const subTitle = document.createElement("h3");
                      subTitle.innerText = "Now what is the secret of Today?";
                      beyondTruth.appendChild(subTitle);
                  }, 2100)
              }
          }

          function shatterGlobe() {
              isCollapsing = true;
          }
          draw();
      </script>

  """)

main_frame = Kino.Frame.new() |> Kino.render()
Kino.Frame.render(main_frame, html)

Kino.Frame.new(placeholder: false)
```

```elixir
message_frame = Kino.Frame.new(placeholder: false) |> Kino.render()

message_pid =
  spawn(fn ->
    receive_message = fn loop ->
      receive do
        {:message, message} ->
          Kino.Frame.render(
            message_frame,
            Kino.HTML.new("""
              <div class="message" style="color: rgba(81, 81, 231, 0.8); display: flex;">
              <pre style="margin: auto;">#{message}</pre>
              </div>
            """)
          )

          loop.(loop)

        _ ->
          {:error}
      end
    end

    receive_message.(receive_message)
  end)

Kino.Frame.new(placeholder: false)
```

```elixir
status_frame = Kino.Frame.new(placeholder: false) |> Kino.render()

status_pid =
  spawn(fn ->
    receive_messages = fn loop ->
      receive do
        {:status, status} ->
          Kino.Frame.render(
            status_frame,
            Kino.HTML.new("""
            <div style="position: absolue; bottom: 0px; font-size: 6px; color: rgba(81, 81, 231, 0.4)"><pre>Status: #{status}</pre></div>
            """)
          )

          loop.(loop)

        _ ->
          {:error}
      end
    end

    receive_messages.(receive_messages)
  end)

Kino.Frame.new(placeholder: false)
```

```elixir
file_name = "messages.term"

case File.exists?(file_name) do
  false -> File.touch!(file_name)
  _ -> :ok
end

binary = File.read!(file_name)

extract_today = fn binary ->
  case byte_size(binary) do
    0 ->
      [result | _] = :erlang.binary_to_term(binary)

      case result.date == Date.utc_today() do
        true -> {:ok, result.data}
        _ -> {:no, false}
      end

    _ ->
      {:no, false}
  end
end

loaded_today_data =
  case String.length(binary) == 0 do
    true -> {:no, false}
    _ -> extract_today.(binary)
  end

frame = Kino.Frame.new(placeholder: false)
```

```elixir
bumblebee = fn ->
  {:ok, cosmo} = Bumblebee.load_model({:hf, "HuggingFaceTB/cosmo-1b"})
  {:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "HuggingFaceTB/cosmo-1b"})
  {:ok, generation_config} = Bumblebee.load_generation_config({:hf, "HuggingFaceTB/cosmo-1b"})

  generation_config =
    Bumblebee.configure(generation_config,
      max_new_tokens: 50,
      strategy: %{type: :multinomial_sampling, top_p: 0.6}
    )

  serving =
    Bumblebee.Text.generation(cosmo, tokenizer, generation_config,
      compile: [batch_size: 1, sequence_length: 256],
      stream: true,
      defn_options: [compiler: EXLA]
    )

  Kino.start_child!({Nx.Serving, name: :cosmo, serving: serving})
  %{name: :cosmo, serving: serving}
end

crypticize = fn subject, i ->
  prompt =
    "<s> [INST]You will write a cryptic message in the form of a haiku in paragraph form about this headline: \"#{subject}\"[/INST]"

  tokens =
    Nx.Serving.batched_run(:cosmo, prompt)
    |> Stream.chunk_every(2)
    |> Enum.reduce([], fn token, acc -> acc ++ token end)

  send(status_pid, {:status, i})

  tokens
  |> Enum.join()
end

Kino.Frame.new(placeholder: false)
```

```elixir
themes = [
  "business",
  "technology",
  "science",
  "health",
  "politics",
  "world",
  "education",
  "entertainment",
  "games",
  "movie",
  "TV",
  "lifestyle",
  "opinion",
  "self-improvement",
  "positivism",
  "fun%20facts",
  "community",
  "wholesome",
  "human%20interest",
  "inspirational"
]

theme = themes |> Enum.random()

select_message = fn messages ->
  sanitize = fn text ->
    text
    |> String.replace(~r/\[\/INST\]/, "")
    |> String.replace(~r/\[INST\]/, "")
  end

  messages |> Enum.random() |> sanitize.()
end

news_loader = fn ->
  response =
    receive do
      {:fetched, data} -> data
      _ -> raise "Could not fetch data"
    end

  bumblebee.()

  results = response.body["results"]

  messages =
    results
    |> Enum.map(fn news_item -> news_item["title"] end)
    |> Enum.with_index()
    |> Enum.map(fn {subject, index} -> crypticize.(subject, index) end)

  date = Date.utc_today()

  data =
    messages
    |> Enum.join("{nextItem}")

  map = %{date: date, data: data, theme: theme}

  prev_data =
    case String.length(binary) == 0 do
      true -> []
      _ -> :erlang.binary_to_term(binary)
    end

  [map] ++ prev_data
end

handler_pid =
  case loaded_today_data do
    {:no, _} ->
      spawn(fn ->
        receive do
          {:fetched, data} -> File.write(file_name, :erlang.term_to_binary(data))
          _ -> raise "Could not analyze data"
        end
      end)

    _ ->
      false
  end

loader_pid =
  case loaded_today_data do
    {:no, _} ->
      spawn(fn ->
        data = news_loader.()

        if is_pid(handler_pid) do
          send(handler_pid, {:fetched, data})
        end

        [first_data | _] = data

        messages =
          first_data.data
          |> String.split("{nextItem}")

        Process.send_after(message_pid, {:message, select_message.(messages)}, 500)
      end)

    _ ->
      false
  end

fetcher_pid =
  case loaded_today_data do
    {:no, _} ->
      spawn(fn ->
        apikey = System.fetch_env!("LB_NEWSDATA_KEY")
        data = Req.get!("https://newsdata.io/api/1/news?apikey=#{apikey}&q=#{theme}")

        if is_pid(loader_pid) do
          send(loader_pid, {:fetched, data})
        end
      end)

    _ ->
      false
  end

get_first = fn binary ->
  case byte_size(binary) do
    0 ->
      false

    _ ->
      [head | _tail] = :erlang.binary_to_term(binary)
      head
  end
end

unpack_data = fn data ->
  {:ok, unpacked} = data
  unpacked
end

today_data =
  case loaded_today_data do
    {:no, _} ->
      case byte_size(binary) do
        0 ->
          ""

        _ ->
          first = get_first.(binary)
          first.data
      end

    _ ->
      unpack_data.(loaded_today_data)
  end

Kino.Frame.new(placeholder: false)
```

```elixir
random_message =
  today_data
  |> String.split("{nextItem}")
  |> select_message.()

send(message_pid, {:message, random_message})

Kino.Frame.new(placeholder: false)
```

<!-- livebook:{"offset":11969,"stamp":{"token":"XCP.iIVI4xv_1GYusasCFjEbGmeI2kB0YkVy75HklXNmtEKySH2FCP_-meZfeL92Wb1W-5nHUJ3wUoqecAcQmJ114Z8RvFGkAgF_nzU6m63EhEdNgGuQcLIfhOp__iGeqT5HBvk9","version":2}} -->