Commit
•
bb7a581
0
Parent(s):
Duplicate from DockerTemplates/single_file_phx_bumblebee_ml
Browse filesCo-authored-by: Omar Sanseviero <[email protected]>
- .gitattributes +34 -0
- Dockerfile +71 -0
- README.md +18 -0
- run.exs +307 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
Dockerfile
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Find eligible builder and runner images on Docker Hub. We use Ubuntu/Debian instead of
|
2 |
+
# Alpine to avoid DNS resolution issues in production.
|
3 |
+
#
|
4 |
+
# https://hub.docker.com/r/hexpm/elixir/tags?page=1&name=ubuntu
|
5 |
+
# https://hub.docker.com/_/ubuntu?tab=tags
|
6 |
+
#
|
7 |
+
#
|
8 |
+
# This file is based on these images:
|
9 |
+
#
|
10 |
+
# - https://hub.docker.com/r/hexpm/elixir/tags - for the build image
|
11 |
+
# - https://hub.docker.com/_/debian?tab=tags&page=1&name=bullseye-20210902-slim - for the release image
|
12 |
+
# - https://pkgs.org/ - resource for finding needed packages
|
13 |
+
# - Ex: hexpm/elixir:1.13.4-erlang-24.0.1-debian-bullseye-20210902-slim
|
14 |
+
#
|
15 |
+
ARG ELIXIR_VERSION=1.14.2
|
16 |
+
ARG OTP_VERSION=25.1
|
17 |
+
ARG DEBIAN_VERSION=bullseye-20220801-slim
|
18 |
+
|
19 |
+
ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
|
20 |
+
ARG RUNNER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
|
21 |
+
|
22 |
+
FROM ${BUILDER_IMAGE} as builder
|
23 |
+
|
24 |
+
# install build dependencies
|
25 |
+
RUN apt-get update -y && apt-get install -y build-essential git curl \
|
26 |
+
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
|
27 |
+
|
28 |
+
# prepare build dir
|
29 |
+
WORKDIR /app
|
30 |
+
|
31 |
+
# set build ENV
|
32 |
+
ENV MIX_ENV="prod"
|
33 |
+
ENV MIX_HOME="/app/.mix"
|
34 |
+
ENV EXS_DRY_RUN="true"
|
35 |
+
ENV MIX_INSTALL_DIR="/app/.mix"
|
36 |
+
ENV BUMBLEBEE_CACHE_DIR="/app/.bumblebee"
|
37 |
+
|
38 |
+
# install hex + rebar
|
39 |
+
RUN mix local.hex --force && \
|
40 |
+
mix local.rebar --force
|
41 |
+
|
42 |
+
# install mix dependencies
|
43 |
+
COPY run.exs ./
|
44 |
+
RUN elixir ./run.exs
|
45 |
+
|
46 |
+
# start a new build stage so that the final image will only contain
|
47 |
+
# the compiled release and other runtime necessities
|
48 |
+
FROM ${RUNNER_IMAGE}
|
49 |
+
|
50 |
+
# install build dependencies
|
51 |
+
RUN apt-get update -y && apt-get install -y build-essential git curl \
|
52 |
+
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
|
53 |
+
|
54 |
+
WORKDIR "/app"
|
55 |
+
|
56 |
+
# set runner ENV
|
57 |
+
ENV MIX_ENV="prod"
|
58 |
+
ENV MIX_HOME="/app/.mix"
|
59 |
+
ENV MIX_INSTALL_DIR="/app/.mix"
|
60 |
+
ENV BUMBLEBEE_CACHE_DIR="/app/.bumblebee"
|
61 |
+
ENV SHELL=/bin/bash
|
62 |
+
ENV PORT=7860
|
63 |
+
|
64 |
+
EXPOSE 7860
|
65 |
+
|
66 |
+
# Only copy the final release from the build stage
|
67 |
+
COPY --from=builder --chown=nobody:root /app/.mix/ ./.mix
|
68 |
+
COPY --from=builder --chown=nobody:root /app/.bumblebee/ ./.bumblebee
|
69 |
+
COPY --from=builder --chown=nobody:root /app/run.exs ./
|
70 |
+
|
71 |
+
CMD ["elixir", "/app/run.exs"]
|
README.md
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Phoenix image classification in a single file
|
3 |
+
emoji: 📚
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: indigo
|
6 |
+
sdk: docker
|
7 |
+
fullWidth: true
|
8 |
+
pinned: false
|
9 |
+
duplicated_from: DockerTemplates/single_file_phx_bumblebee_ml
|
10 |
+
---
|
11 |
+
|
12 |
+
# Phoenix image classification in a single file
|
13 |
+
|
14 |
+
To deploy your own app, duplicate this Space and get started.
|
15 |
+
|
16 |
+
## Acknowledgments
|
17 |
+
|
18 |
+
This Space is based on the [single file Phoenix app for Fly.io](https://github.com/chrismccord/single_file_phx_bumblebee_ml) from @chrismccord.
|
run.exs
ADDED
@@ -0,0 +1,307 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
host =
|
2 |
+
if repo_name = System.get_env("SPACE_REPO_NAME") do
|
3 |
+
"#{System.get_env("SPACE_AUTHOR_NAME")}-#{repo_name}.hf.space"
|
4 |
+
|> String.replace("_", "-")
|
5 |
+
|> String.downcase(:ascii)
|
6 |
+
else
|
7 |
+
"localhost"
|
8 |
+
end
|
9 |
+
|
10 |
+
Application.put_env(:phoenix, :json_library, Jason)
|
11 |
+
|
12 |
+
Application.put_env(:phoenix_demo, PhoenixDemo.Endpoint,
|
13 |
+
url: [host: host],
|
14 |
+
http: [
|
15 |
+
ip: {0, 0, 0, 0, 0, 0, 0, 0},
|
16 |
+
port: String.to_integer(System.get_env("PORT") || "4000"),
|
17 |
+
transport_options: [socket_opts: [:inet6]]
|
18 |
+
],
|
19 |
+
server: true,
|
20 |
+
live_view: [signing_salt: :crypto.strong_rand_bytes(8) |> Base.encode16()],
|
21 |
+
secret_key_base: :crypto.strong_rand_bytes(32) |> Base.encode16(),
|
22 |
+
pubsub_server: PhoenixDemo.PubSub
|
23 |
+
)
|
24 |
+
|
25 |
+
Mix.install([
|
26 |
+
{:plug_cowboy, "~> 2.6"},
|
27 |
+
{:jason, "~> 1.4"},
|
28 |
+
{:phoenix, "~> 1.7.0-rc.0", override: true},
|
29 |
+
{:phoenix_live_view, "~> 0.18.3"},
|
30 |
+
{:bumblebee, "~> 0.1.0"},
|
31 |
+
{:nx, "~> 0.4.1"},
|
32 |
+
{:exla, "~> 0.4.1"}
|
33 |
+
])
|
34 |
+
|
35 |
+
Application.put_env(:nx, :default_backend, EXLA.Backend)
|
36 |
+
|
37 |
+
defmodule PhoenixDemo.Layouts do
|
38 |
+
use Phoenix.Component
|
39 |
+
|
40 |
+
def render("live.html", assigns) do
|
41 |
+
~H"""
|
42 |
+
<script src="//cdn.jsdelivr.net/npm/[email protected]/priv/static/phoenix.min.js"></script>
|
43 |
+
<script src="//cdn.jsdelivr.net/npm/[email protected]/priv/static/phoenix_live_view.min.js"></script>
|
44 |
+
<script>
|
45 |
+
const ImageInput = {
|
46 |
+
mounted(){
|
47 |
+
const DROP_CLASSES = ["bg-blue-100", "border-blue-300"]
|
48 |
+
this.boundHeight = parseInt(this.el.dataset.height)
|
49 |
+
this.boundWidth = parseInt(this.el.dataset.width)
|
50 |
+
this.inputEl = this.el.querySelector(`#${this.el.id}-input`)
|
51 |
+
this.previewEl = this.el.querySelector(`#${this.el.id}-preview`)
|
52 |
+
|
53 |
+
this.el.addEventListener("click", e => this.inputEl.click())
|
54 |
+
this.inputEl.addEventListener("change", e => this.loadFile(event.target.files))
|
55 |
+
this.el.addEventListener("dragover", e => {
|
56 |
+
e.stopPropagation()
|
57 |
+
e.preventDefault()
|
58 |
+
e.dataTransfer.dropEffect = "copy"
|
59 |
+
})
|
60 |
+
this.el.addEventListener("drop", e => {
|
61 |
+
e.stopPropagation()
|
62 |
+
e.preventDefault()
|
63 |
+
this.loadFile(e.dataTransfer.files)
|
64 |
+
})
|
65 |
+
this.el.addEventListener("dragenter", e => this.el.classList.add(...DROP_CLASSES))
|
66 |
+
this.el.addEventListener("drop", e => this.el.classList.remove(...DROP_CLASSES))
|
67 |
+
this.el.addEventListener("dragleave", e => {
|
68 |
+
if(!this.el.contains(e.relatedTarget)){ this.el.classList.remove(...DROP_CLASSES) }
|
69 |
+
})
|
70 |
+
},
|
71 |
+
|
72 |
+
loadFile(files){
|
73 |
+
const file = files && files[0]
|
74 |
+
if(!file){ return }
|
75 |
+
const reader = new FileReader()
|
76 |
+
reader.onload = (readerEvent) => {
|
77 |
+
const imgEl = document.createElement("img")
|
78 |
+
imgEl.addEventListener("load", (loadEvent) => {
|
79 |
+
this.setPreview(imgEl)
|
80 |
+
const blob = this.canvasToBlob(this.toCanvas(imgEl))
|
81 |
+
this.upload("image", [blob])
|
82 |
+
})
|
83 |
+
imgEl.src = readerEvent.target.result
|
84 |
+
}
|
85 |
+
reader.readAsDataURL(file)
|
86 |
+
},
|
87 |
+
|
88 |
+
setPreview(imgEl){
|
89 |
+
const previewImgEl = imgEl.cloneNode()
|
90 |
+
previewImgEl.style.maxHeight = "100%"
|
91 |
+
this.previewEl.replaceChildren(previewImgEl)
|
92 |
+
},
|
93 |
+
|
94 |
+
toCanvas(imgEl){
|
95 |
+
// We resize the image, such that it fits in the configured height x width, but
|
96 |
+
// keep the aspect ratio. We could also easily crop, pad or squash the image, if desired
|
97 |
+
const canvas = document.createElement("canvas")
|
98 |
+
const ctx = canvas.getContext("2d")
|
99 |
+
const widthScale = this.boundWidth / imgEl.width
|
100 |
+
const heightScale = this.boundHeight / imgEl.height
|
101 |
+
const scale = Math.min(widthScale, heightScale)
|
102 |
+
canvas.width = Math.round(imgEl.width * scale)
|
103 |
+
canvas.height = Math.round(imgEl.height * scale)
|
104 |
+
ctx.drawImage(imgEl, 0, 0, imgEl.width, imgEl.height, 0, 0, canvas.width, canvas.height)
|
105 |
+
return canvas
|
106 |
+
},
|
107 |
+
|
108 |
+
canvasToBlob(canvas){
|
109 |
+
const imageData = canvas.getContext("2d").getImageData(0, 0, canvas.width, canvas.height)
|
110 |
+
const buffer = this.imageDataToRGBBuffer(imageData)
|
111 |
+
const meta = new ArrayBuffer(8)
|
112 |
+
const view = new DataView(meta)
|
113 |
+
view.setUint32(0, canvas.height, false)
|
114 |
+
view.setUint32(4, canvas.width, false)
|
115 |
+
return new Blob([meta, buffer], {type: "application/octet-stream"})
|
116 |
+
},
|
117 |
+
|
118 |
+
imageDataToRGBBuffer(imageData){
|
119 |
+
const pixelCount = imageData.width * imageData.height
|
120 |
+
const bytes = new Uint8ClampedArray(pixelCount * 3)
|
121 |
+
for(let i = 0; i < pixelCount; i++) {
|
122 |
+
bytes[i * 3] = imageData.data[i * 4]
|
123 |
+
bytes[i * 3 + 1] = imageData.data[i * 4 + 1]
|
124 |
+
bytes[i * 3 + 2] = imageData.data[i * 4 + 2]
|
125 |
+
}
|
126 |
+
return bytes.buffer
|
127 |
+
}
|
128 |
+
}
|
129 |
+
const liveSocket = new LiveView.LiveSocket("/live", Phoenix.Socket, {hooks: {ImageInput}})
|
130 |
+
liveSocket.connect()
|
131 |
+
</script>
|
132 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
133 |
+
<%= @inner_content %>
|
134 |
+
"""
|
135 |
+
end
|
136 |
+
end
|
137 |
+
|
138 |
+
defmodule PhoenixDemo.ErrorView do
|
139 |
+
def render(_, _), do: "error"
|
140 |
+
end
|
141 |
+
|
142 |
+
defmodule PhoenixDemo.SampleLive do
|
143 |
+
use Phoenix.LiveView, layout: {PhoenixDemo.Layouts, :live}
|
144 |
+
|
145 |
+
def mount(_params, _session, socket) do
|
146 |
+
{:ok,
|
147 |
+
socket
|
148 |
+
|> assign(label: nil, running: false, task_ref: nil)
|
149 |
+
|> allow_upload(:image,
|
150 |
+
accept: :any,
|
151 |
+
max_entries: 1,
|
152 |
+
max_file_size: 300_000,
|
153 |
+
progress: &handle_progress/3,
|
154 |
+
auto_upload: true
|
155 |
+
)}
|
156 |
+
end
|
157 |
+
|
158 |
+
def render(assigns) do
|
159 |
+
~H"""
|
160 |
+
<div class="h-screen w-screen flex items-center justify-center antialiased bg-gray-100">
|
161 |
+
<div class="flex flex-col items-center w-1/2">
|
162 |
+
<h1 class="text-slate-900 font-extrabold text-3xl tracking-tight text-center">Elixir image classification demo</h1>
|
163 |
+
<p class="mt-6 text-lg text-slate-600 text-center max-w-3xl mx-auto">
|
164 |
+
Powered by <a href="https://github.com/elixir-nx/bumblebee" target="_blank" class="font-mono font-medium text-sky-500">Bumblebee</a>,
|
165 |
+
an Nx/Axon library for pre-trained and transformer NN models with <a href="https://huggingface.co">🤗</a> integration.
|
166 |
+
Deployed on <a href="https://huggingface.co/" target="_blank" class="font-mono font-medium text-sky-500">Hugging Face</a> CPU Basic (2vCPU · 16 GiB RAM)
|
167 |
+
</p>
|
168 |
+
<form class="m-0 flex flex-col items-center space-y-2 mt-8" phx-change="noop" phx-submit="noop">
|
169 |
+
<.image_input id="image" upload={@uploads.image} height={224} width={224} />
|
170 |
+
</form>
|
171 |
+
<div class="mt-6 flex space-x-1.5 items-center text-gray-600 text-xl">
|
172 |
+
<span>Label:</span>
|
173 |
+
<%= if @running do %>
|
174 |
+
<.spinner />
|
175 |
+
<% else %>
|
176 |
+
<span class="text-gray-900 font-medium"><%= @label || "?" %></span>
|
177 |
+
<% end %>
|
178 |
+
</div>
|
179 |
+
<p class="text-lg text-center max-w-3xl mx-auto fixed top-2 right-2">
|
180 |
+
<a href="https://huggingface.co/spaces/DockerTemplates/single_file_phx_bumblebee_ml/tree/main" target="_blank" class="ml-6 text-sky-500 hover:text-sky-700 font-mono font-medium">
|
181 |
+
View the source
|
182 |
+
<span class="sr-only">view source on Hugging Face</span>
|
183 |
+
<img alt="Hugging Face's logo" class="md:mr-2 w-7 inline" src="https://huggingface.co/front/assets/huggingface_logo-noborder.svg">
|
184 |
+
</a>
|
185 |
+
</p>
|
186 |
+
</div>
|
187 |
+
</div>
|
188 |
+
"""
|
189 |
+
end
|
190 |
+
|
191 |
+
defp image_input(assigns) do
|
192 |
+
~H"""
|
193 |
+
<div
|
194 |
+
id={@id}
|
195 |
+
class="inline-flex p-4 border-2 border-dashed border-gray-200 rounded-lg cursor-pointer bg-white"
|
196 |
+
phx-hook="ImageInput"
|
197 |
+
data-height={@height}
|
198 |
+
data-width={@width}
|
199 |
+
>
|
200 |
+
<.live_file_input upload={@upload} class="hidden" />
|
201 |
+
<input id={"#{@id}-input"} type="file" class="hidden" />
|
202 |
+
<div
|
203 |
+
class="h-[300px] w-[300px] flex items-center justify-center"
|
204 |
+
id={"#{@id}-preview"}
|
205 |
+
phx-update="ignore"
|
206 |
+
>
|
207 |
+
<div class="text-gray-500 text-center">
|
208 |
+
Drag an image file here or click to open file browser
|
209 |
+
</div>
|
210 |
+
</div>
|
211 |
+
</div>
|
212 |
+
"""
|
213 |
+
end
|
214 |
+
|
215 |
+
defp spinner(assigns) do
|
216 |
+
~H"""
|
217 |
+
<svg phx-no-format class="inline mr-2 w-4 h-4 text-gray-200 animate-spin fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
|
218 |
+
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor" />
|
219 |
+
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill" />
|
220 |
+
</svg>
|
221 |
+
"""
|
222 |
+
end
|
223 |
+
|
224 |
+
def handle_progress(:image, entry, socket) do
|
225 |
+
if entry.done? do
|
226 |
+
socket
|
227 |
+
|> consume_uploaded_entries(:image, fn meta, _ -> {:ok, File.read!(meta.path)} end)
|
228 |
+
|> case do
|
229 |
+
[binary] ->
|
230 |
+
image = decode_as_tensor(binary)
|
231 |
+
task = Task.async(fn -> Nx.Serving.batched_run(PhoenixDemo.Serving, image) end)
|
232 |
+
{:noreply, assign(socket, running: true, task_ref: task.ref)}
|
233 |
+
|
234 |
+
[] ->
|
235 |
+
{:noreply, socket}
|
236 |
+
end
|
237 |
+
else
|
238 |
+
{:noreply, socket}
|
239 |
+
end
|
240 |
+
end
|
241 |
+
|
242 |
+
defp decode_as_tensor(<<height::32-integer, width::32-integer, data::binary>>) do
|
243 |
+
data |> Nx.from_binary(:u8) |> Nx.reshape({height, width, 3})
|
244 |
+
end
|
245 |
+
|
246 |
+
# We need phx-change and phx-submit on the form for live uploads
|
247 |
+
def handle_event("noop", %{}, socket) do
|
248 |
+
{:noreply, socket}
|
249 |
+
end
|
250 |
+
|
251 |
+
def handle_info({ref, result}, %{assigns: %{task_ref: ref}} = socket) do
|
252 |
+
Process.demonitor(ref, [:flush])
|
253 |
+
%{predictions: [%{label: label}]} = result
|
254 |
+
{:noreply, assign(socket, label: label, running: false)}
|
255 |
+
end
|
256 |
+
end
|
257 |
+
|
258 |
+
defmodule PhoenixDemo.Router do
|
259 |
+
use Phoenix.Router
|
260 |
+
import Phoenix.LiveView.Router
|
261 |
+
|
262 |
+
pipeline :browser do
|
263 |
+
plug(:accepts, ["html"])
|
264 |
+
end
|
265 |
+
|
266 |
+
scope "/", PhoenixDemo do
|
267 |
+
pipe_through(:browser)
|
268 |
+
|
269 |
+
live("/", SampleLive, :index)
|
270 |
+
end
|
271 |
+
end
|
272 |
+
|
273 |
+
defmodule PhoenixDemo.Endpoint do
|
274 |
+
use Phoenix.Endpoint, otp_app: :phoenix_demo
|
275 |
+
|
276 |
+
socket("/live", Phoenix.LiveView.Socket)
|
277 |
+
plug(PhoenixDemo.Router)
|
278 |
+
end
|
279 |
+
|
280 |
+
# Application startup
|
281 |
+
|
282 |
+
{:ok, model_info} = Bumblebee.load_model({:hf, "microsoft/resnet-50"})
|
283 |
+
{:ok, featurizer} = Bumblebee.load_featurizer({:hf, "microsoft/resnet-50"})
|
284 |
+
|
285 |
+
serving =
|
286 |
+
Bumblebee.Vision.image_classification(model_info, featurizer,
|
287 |
+
top_k: 1,
|
288 |
+
compile: [batch_size: 10],
|
289 |
+
defn_options: [compiler: EXLA]
|
290 |
+
)
|
291 |
+
|
292 |
+
# Dry run for copying cached mix install from builder to runner
|
293 |
+
if System.get_env("EXS_DRY_RUN") == "true" do
|
294 |
+
System.halt(0)
|
295 |
+
else
|
296 |
+
{:ok, _} =
|
297 |
+
Supervisor.start_link(
|
298 |
+
[
|
299 |
+
{Phoenix.PubSub, name: PhoenixDemo.PubSub},
|
300 |
+
PhoenixDemo.Endpoint,
|
301 |
+
{Nx.Serving, serving: serving, name: PhoenixDemo.Serving, batch_timeout: 100}
|
302 |
+
],
|
303 |
+
strategy: :one_for_one
|
304 |
+
)
|
305 |
+
|
306 |
+
Process.sleep(:infinity)
|
307 |
+
end
|