diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..e945e12 --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,5 @@ +[ + import_deps: [:phoenix], + plugins: [Phoenix.LiveView.HTMLFormatter], + inputs: ["*.{heex,ex,exs}", "{config,lib,test}/**/*.{heex,ex,exs}"] +] diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..00472fc --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,8 @@ +version: 2 +updates: +- package-ecosystem: mix + directory: "/" + schedule: + interval: daily + time: "07:00" + timezone: Europe/London \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..6b8979b --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,35 @@ +name: Elixir CI + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build: + name: Build and test + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up Elixir + uses: erlef/setup-beam@v1 + with: + elixir-version: '1.14.2' # Define the elixir version [required] + otp-version: '25.1.2' # Define the OTP version [required] + - name: Restore dependencies cache + uses: actions/cache@v2 + with: + path: deps + key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }} + restore-keys: ${{ runner.os }}-mix- + - name: Install dependencies + run: mix deps.get + - name: Run Tests + run: mix coveralls.json + env: + MIX_ENV: test + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v1 \ No newline at end of file diff --git a/.gitignore b/.gitignore index b263cd1..8073c0c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,37 @@ -/_build -/cover -/deps -/doc +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where 3rd-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. /.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). *.ez -*.beam -/config/*.secret.exs -.elixir_ls/ + +# Temporary files, for example, from tests. +/tmp/ + +# Ignore package tarball (built via "mix hex.build"). +app-*.tar + +# Ignore assets that are produced by build tools. +/priv/static/assets/ + +# Ignore digested assets cache. +/priv/static/cache_manifest.json + +# In case you use Node.js/npm, you want to ignore these. +npm-debug.log +/assets/node_modules/ + diff --git a/README.md b/README.md index f103a53..326ea5e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,1415 @@ -# image-classifier -Classify images and attempt to extract data from or describe their contents +
+ +
+ +We're ready to start building. + + +## 1. Installing initial dependencies + +Now that we're ready to go, +let's start by adding some dependencies. + +Head over to `mix.exs` +and add the following dependencies +to the `deps` section. + +```elixir +{:bumblebee, "~> 0.4.2"}, +{:exla, "~> 0.6.1"}, +{:nx, "~> 0.6.2"}, +{:vix, "~> 0.23.1"} +``` + +- [**`bumblebee`**](https://github.com/elixir-nx/bumblebee), +a framework that will allows us to integrate +[`Transformer Models`](https://huggingface.co/docs/transformers/index) in `Phoenix`. +`Transformers` (from [Hugging Face](https://huggingface.co/)) +are APIs that allow us to easily download and train +[pretrained models](https://blogs.nvidia.com/blog/2022/12/08/what-is-a-pretrained-ai-model). +`Bumblebee` aims to support all Transformer Models, +however some are lacking. +You may check which ones are supported by visiting +`Bumblebee`'s repository +or visiting https://jonatanklosko-bumblebee-tools.hf.space/apps/repository-inspector +and checking if the model is currently supported. + +- [**`EXLA`**](https://hexdocs.pm/exla/EXLA.html), +Elixir implementation of [Google's XLA](https://www.tensorflow.org/xla/), +a compiler that provides faster linear algebra calculations +with `TensorFlow` models. +This backend compiler is needed for [`Nx`](https://github.com/elixir-nx/nx), +a framework that allows support for tensors and numerical definitions +in Elixir. +We are installing `EXLA` because allows us to compile models +*just-in-time* and run them on CPU and/or GPU. + +- [**`Nx`**](https://hexdocs.pm/nx/Nx.html), +a library that allows us to work with +[`Numerical Elixir`](https://github.com/elixir-nx/), +Elixir's way of doing [numerical computing](https://www.hilarispublisher.com/open-access/introduction-to-numerical-computing-2168-9679-1000423.pdf). + +- [**`Vix`**](https://hexdocs.pm/vix/readme.html), +an image processing library. + +In `config/config.exs`, +let's add our `:nx` configuration +to use `EXLA`. + +```elixir +config :nx, default_backend: EXLA.Backend +``` + +## 2. Adding `LiveView` capabilities to our project + +As it stands, +our project is not using `LiveView`. +Let's fix this. + +In `lib/app_web/router.ex`, +change the `scope "/"` to the following. + +```elixir + scope "/", AppWeb do + pipe_through :browser + + live "/", PageLive + end +``` + +Instead of using the `PageController`, +we are going to be creating `PageLive`, +a `LiveView` file. + +Let's create our `LiveView` files. +Inside `lib/app_web`, +create a folder called `live` +and create the following file +`page_live.ex`. + +```elixir +defmodule AppWeb.PageLive do + use AppWeb, :live_view + + @impl true + def mount(_params, _session, socket) do + {:ok, socket} + end +end + +``` + +This is a simple `LiveView` controller. + +In the same `live` folder, +create a file called `page_live.html.heex` +and use the following code. + +```html +<.flash_group flash={@flash} /> ++ +
+ +This means we've successfully added `LiveView` +and changed our view! + + +## 3. Receiving image files + +Now, let's start by receiving some image files. +In order to classify them, we need to have access to begin with, +right? + +With `LiveView`, +we can easily do this by using +[`allow_upload/3`](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#allow_upload/3) +when mounting our `LiveView`. +With this function, we can easily accept +file uploads with progress. +We can define file types, max number of entries, +max file size, +validate the uploaded file and much more! + +Firstly, +let's make some changes to +`lib/app_web/live/page_live.html.heex`. + +```html +<.flash_group flash={@flash} /> ++ Do simple classification with this LiveView + demo, powered by Bumblebee. +
+ + +or drag and drop
+PNG, JPG, GIF up to 5MB
++ +
+ +You can and **should** try other models. +`ResNet-50` is just one of the many that are supported by `Bumblebee`. +You can see the supported models in https://github.com/elixir-nx/bumblebee#model-support. + + +## 4.6 Considerations on user images + +To keep the app as simple as possible, +we are receiving the image from the person as is. +Although we are processing the image, +we are doing it so **it is processable by the model**. + +We have to understand that: +- in most cases, **full-resolution images are not necessary**, +because neural networks work on much smaller inputs +(e.g. `ResNet-50` works with `224px x 224px` images). +This means that a lot of data is unnecessarily uploaded over the network, +increasing workload on the server to potentially downsize a large image. +- decoding an image requires an additional package, +meaning more work on the server. + +We can avoid both of these downsides by moving this work to the client. +We can leverage the +[`Canvas API` ](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) +to decode and downsize this image in the client-side, +reducing server workload. + +You can see an example implementation of this technique +in `Bumblebee`'s repository +at https://github.com/elixir-nx/bumblebee/blob/main/examples/phoenix/image_classification.exs + +However, since we are not using `JavaScript` for anything, +we can (and *should*!) properly downsize our images +so they better fit the training dataset of the model we use. +This will allow the model to process faster, +since larger images carry over more data that is ultimately unnecessary +for models to make predictions. + +Open +`lib/app_web/live/page_live.ex`, +find the `handle_progress/3` function +and change resize the image *before processing it*. + +```elixir + file_binary = File.read!(meta.path) + + # Get image and resize + # This is dependant on the resolution of the model's dataset. + # In our case, we want the width to be closer to 640, whilst maintaining aspect ratio. + width = 640 + {:ok, thumbnail_vimage} = Vix.Vips.Operation.thumbnail(meta.path, width, size: :VIPS_SIZE_DOWN) + + # Pre-process it + {:ok, tensor} = pre_process_image(thumbnail_vimage) + + #... +``` + +We are using +[`Vix.Vips.Operation.thumbnail/3`](https://hexdocs.pm/vix/Vix.Vips.Operation.html#thumbnail/3) +to resize our image to a fixed width +whilst maintaining aspect ratio. +The `width` variable can be dependent on the model that you use. +For example, `ResNet-50` is trained on `224px224` pictures, +so you may want to resize the image to this width. + +> **Note**: We are using the `thumbnail/3` function +> instead of `resize/3` because it's _much_ faster.+ +
+ + +## 5.3 Show image preview + +As of now, even though our app predicts the given images, +it is not showing a preview of the image the person submitted. +Let's fix this π οΈ. + +Let's add a new socket assign variable +pertaining to the [base64](https://en.wikipedia.org/wiki/Base64) representation +of the image in `lib/app_web/live_page/live.ex` + +```elixir + |> assign(label: nil, running: false, task_ref: nil, image_preview_base64: nil) +``` + +We've added `image_preview_base64` +as a new socket assign, +initializing it as `nil`. + +Next, we need to *read the file while consuming it*, +and properly update the socket assign +so we can show it to the person. + +In the same file, +change the `handle_progress/3` function to the following. + +```elixir + def handle_progress(:image_list, entry, socket) do + if entry.done? do + + # Consume the entry and get the tensor to feed to classifier + %{tensor: tensor, file_binary: file_binary} = consume_uploaded_entry(socket, entry, fn %{} = meta -> + file_binary = File.read!(meta.path) + + {:ok, vimage} = Vix.Vips.Image.new_from_file(meta.path) + {:ok, tensor} = pre_process_image(vimage) + {:ok, %{tensor: tensor, file_binary: file_binary}} + end) + + # Create an async task to classify the image + task = Task.Supervisor.async(App.TaskSupervisor, fn -> Nx.Serving.batched_run(ImageClassifier, tensor) end) + + # Encode the image to base64 + base64 = "data:image/png;base64, " <> Base.encode64(file_binary) + + # Update socket assigns to show spinner whilst task is running + {:noreply, assign(socket, running: true, task_ref: task.ref, image_preview_base64: base64)} + else + {:noreply, socket} + end + end +``` + +We're using [`File.read!/1`](https://hexdocs.pm/elixir/1.13/File.html#read/1) +to retrieve the binary representation of the image that was uploaded. +We use [`Base.encode64/2`](https://hexdocs.pm/elixir/1.12/Base.html#encode64/2) +to encode this file binary +and assign the newly created `image_preview_base64` socket assign +with this base64 representation of the image. + +Now, all that's left to do +is to *render the image on our view*. +In `lib/app_web/live/page_live.html.heex`, +locate the line: + +```html +or drag and drop
+PNG, JPG, GIF up to 5MB
+ <% end %> ++ +
+ + +# 6. What about other models? + +Maybe you weren't happy with the results from this model. + +That's fair. `ResNet-50` is a smaller, "older" model compared to other +image captioning/classification models. + +What if you wanted to use others? +Well, as we've mentioned before, +`Bumblebee` uses +[**Transformer models from `HuggingFace`**](https://huggingface.co/docs/transformers/index). +To know if one is supported +(as shown in [`Bumblebee`'s docs](https://github.com/elixir-nx/bumblebee#model-support)), +we need to check the `config.json` file +in the model repository +and copy the class name under `"architectures"` +and search it on `Bumblebee`'s codebase. + +For example, +here's one of the more popular image captioning models - +Salesforce's `BLIP` - +https://huggingface.co/Salesforce/blip-image-captioning-large/blob/main/config.json. + ++ + +
+ + +If you visit `Bumblebee`'s codebase +and search for the class name, +you'll find it is supported. + ++ +
+ +Awesome! +Now we can use it! + +If you dig around `Bumblebee`'s docs as well +(https://hexdocs.pm/bumblebee/Bumblebee.Vision.html#image_to_text/5), +you'll see that we've got to use `image_to_text/5` with this model. +It needs a `tokenizer`, `featurizer` and a `generation-config` +so we can use it. + +Let's do it! +Head over to `lib/app/application.ex`, +and change the `serving/0` function. + +```elixir + def serving do + {:ok, model_info} = Bumblebee.load_model({:hf, "Salesforce/blip-image-captioning-base"}) + {:ok, featurizer} = Bumblebee.load_featurizer({:hf, "Salesforce/blip-image-captioning-base"}) + {:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "Salesforce/blip-image-captioning-base"}) + {:ok, generation_config} = Bumblebee.load_generation_config({:hf, "Salesforce/blip-image-captioning-base"}) + + Bumblebee.Vision.image_to_text(model_info, featurizer, tokenizer, generation_config, + compile: [batch_size: 10], + defn_options: [compiler: EXLA] + ) + end +``` + +As you can see, we're using the repository name of `BLIP`'s model +from the HuggingFace website. + +If you run `mix phx.server`, +you'll see that it will download the new models, +tokenizers, featurizer and configs to run the model. + +```sh +|======================================================================| 100% (989.82 MB) +[info] TfrtCpuClient created. +|======================================================================| 100% (711.39 KB) +[info] Running AppWeb.Endpoint with cowboy 2.10.0 at 127.0.0.1:4000 (http) +[info] Access AppWeb.Endpoint at http://localhost:4000 +[watch] build finished, watching for changes... +``` + +You may think we're done here. +But we are not! β + +The **destructuring of the output of the model may not be the same**.+ +
+ +Awesome! π + +> **Note**: Be aware that `BLIP` +> is a _much_ larger model than `ResNet-50`. +> There are more accurate and even larger models out there +> (e.g: +> [`blip-image-captioning-large`](https://huggingface.co/Salesforce/blip-image-captioning-large), +> the larger version of the model we've just used). +> This is a balancing act: the larger the model, the longer a prediction may take +> and more resources your server will need to have to handle this heavier workload. + + +# _Please_ Star the repo! βοΈ + +If you find this package/repo useful, +please star on GitHub, so that we know! β + +Thank you! π \ No newline at end of file diff --git a/assets/css/app.css b/assets/css/app.css new file mode 100644 index 0000000..d001c18 --- /dev/null +++ b/assets/css/app.css @@ -0,0 +1,17 @@ +@import "tailwindcss/base"; +@import "tailwindcss/components"; +@import "tailwindcss/utilities"; + +/* This file is for your main application CSS */ +/* override browser default */ +html, +body { + margin: 0; + padding: 0; +} + +/* use viewport-relative units to cover page fully */ +body { + height: 100vh; + width: 100vw; +} diff --git a/assets/js/app.js b/assets/js/app.js new file mode 100644 index 0000000..df0cdd9 --- /dev/null +++ b/assets/js/app.js @@ -0,0 +1,41 @@ +// If you want to use Phoenix channels, run `mix help phx.gen.channel` +// to get started and then uncomment the line below. +// import "./user_socket.js" + +// You can include dependencies in two ways. +// +// The simplest option is to put them in assets/vendor and +// import them using relative paths: +// +// import "../vendor/some-package.js" +// +// Alternatively, you can `npm install some-package --prefix assets` and import +// them using a path starting with the package name: +// +// import "some-package" +// + +// Include phoenix_html to handle method=PUT/DELETE in forms and buttons. +import "phoenix_html" +// Establish Phoenix Socket and LiveView configuration. +import {Socket} from "phoenix" +import {LiveSocket} from "phoenix_live_view" +import topbar from "../vendor/topbar" + +let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content") +let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}}) + +// Show progress bar on live navigation and form submits +topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"}) +window.addEventListener("phx:page-loading-start", _info => topbar.show(300)) +window.addEventListener("phx:page-loading-stop", _info => topbar.hide()) + +// connect if there are any LiveViews on the page +liveSocket.connect() + +// expose liveSocket on window for web console debug logs and latency simulation: +// >> liveSocket.enableDebug() +// >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session +// >> liveSocket.disableLatencySim() +window.liveSocket = liveSocket + diff --git a/assets/tailwind.config.js b/assets/tailwind.config.js new file mode 100644 index 0000000..e3bf241 --- /dev/null +++ b/assets/tailwind.config.js @@ -0,0 +1,26 @@ +// See the Tailwind configuration guide for advanced usage +// https://tailwindcss.com/docs/configuration + +const plugin = require("tailwindcss/plugin") + +module.exports = { + content: [ + "./js/**/*.js", + "../lib/*_web.ex", + "../lib/*_web/**/*.*ex" + ], + theme: { + extend: { + colors: { + brand: "#FD4F00", + } + }, + }, + plugins: [ + require("@tailwindcss/forms"), + plugin(({addVariant}) => addVariant("phx-no-feedback", [".phx-no-feedback&", ".phx-no-feedback &"])), + plugin(({addVariant}) => addVariant("phx-click-loading", [".phx-click-loading&", ".phx-click-loading &"])), + plugin(({addVariant}) => addVariant("phx-submit-loading", [".phx-submit-loading&", ".phx-submit-loading &"])), + plugin(({addVariant}) => addVariant("phx-change-loading", [".phx-change-loading&", ".phx-change-loading &"])) + ] +} diff --git a/assets/vendor/topbar.js b/assets/vendor/topbar.js new file mode 100644 index 0000000..4195727 --- /dev/null +++ b/assets/vendor/topbar.js @@ -0,0 +1,165 @@ +/** + * @license MIT + * topbar 2.0.0, 2023-02-04 + * https://buunguyen.github.io/topbar + * Copyright (c) 2021 Buu Nguyen + */ +(function (window, document) { + "use strict"; + + // https://gist.github.com/paulirish/1579671 + (function () { + var lastTime = 0; + var vendors = ["ms", "moz", "webkit", "o"]; + for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { + window.requestAnimationFrame = + window[vendors[x] + "RequestAnimationFrame"]; + window.cancelAnimationFrame = + window[vendors[x] + "CancelAnimationFrame"] || + window[vendors[x] + "CancelRequestAnimationFrame"]; + } + if (!window.requestAnimationFrame) + window.requestAnimationFrame = function (callback, element) { + var currTime = new Date().getTime(); + var timeToCall = Math.max(0, 16 - (currTime - lastTime)); + var id = window.setTimeout(function () { + callback(currTime + timeToCall); + }, timeToCall); + lastTime = currTime + timeToCall; + return id; + }; + if (!window.cancelAnimationFrame) + window.cancelAnimationFrame = function (id) { + clearTimeout(id); + }; + })(); + + var canvas, + currentProgress, + showing, + progressTimerId = null, + fadeTimerId = null, + delayTimerId = null, + addEvent = function (elem, type, handler) { + if (elem.addEventListener) elem.addEventListener(type, handler, false); + else if (elem.attachEvent) elem.attachEvent("on" + type, handler); + else elem["on" + type] = handler; + }, + options = { + autoRun: true, + barThickness: 3, + barColors: { + 0: "rgba(26, 188, 156, .9)", + ".25": "rgba(52, 152, 219, .9)", + ".50": "rgba(241, 196, 15, .9)", + ".75": "rgba(230, 126, 34, .9)", + "1.0": "rgba(211, 84, 0, .9)", + }, + shadowBlur: 10, + shadowColor: "rgba(0, 0, 0, .6)", + className: null, + }, + repaint = function () { + canvas.width = window.innerWidth; + canvas.height = options.barThickness * 5; // need space for shadow + + var ctx = canvas.getContext("2d"); + ctx.shadowBlur = options.shadowBlur; + ctx.shadowColor = options.shadowColor; + + var lineGradient = ctx.createLinearGradient(0, 0, canvas.width, 0); + for (var stop in options.barColors) + lineGradient.addColorStop(stop, options.barColors[stop]); + ctx.lineWidth = options.barThickness; + ctx.beginPath(); + ctx.moveTo(0, options.barThickness / 2); + ctx.lineTo( + Math.ceil(currentProgress * canvas.width), + options.barThickness / 2 + ); + ctx.strokeStyle = lineGradient; + ctx.stroke(); + }, + createCanvas = function () { + canvas = document.createElement("canvas"); + var style = canvas.style; + style.position = "fixed"; + style.top = style.left = style.right = style.margin = style.padding = 0; + style.zIndex = 100001; + style.display = "none"; + if (options.className) canvas.classList.add(options.className); + document.body.appendChild(canvas); + addEvent(window, "resize", repaint); + }, + topbar = { + config: function (opts) { + for (var key in opts) + if (options.hasOwnProperty(key)) options[key] = opts[key]; + }, + show: function (delay) { + if (showing) return; + if (delay) { + if (delayTimerId) return; + delayTimerId = setTimeout(() => topbar.show(), delay); + } else { + showing = true; + if (fadeTimerId !== null) window.cancelAnimationFrame(fadeTimerId); + if (!canvas) createCanvas(); + canvas.style.opacity = 1; + canvas.style.display = "block"; + topbar.progress(0); + if (options.autoRun) { + (function loop() { + progressTimerId = window.requestAnimationFrame(loop); + topbar.progress( + "+" + 0.05 * Math.pow(1 - Math.sqrt(currentProgress), 2) + ); + })(); + } + } + }, + progress: function (to) { + if (typeof to === "undefined") return currentProgress; + if (typeof to === "string") { + to = + (to.indexOf("+") >= 0 || to.indexOf("-") >= 0 + ? currentProgress + : 0) + parseFloat(to); + } + currentProgress = to > 1 ? 1 : to; + repaint(); + return currentProgress; + }, + hide: function () { + clearTimeout(delayTimerId); + delayTimerId = null; + if (!showing) return; + showing = false; + if (progressTimerId != null) { + window.cancelAnimationFrame(progressTimerId); + progressTimerId = null; + } + (function loop() { + if (topbar.progress("+.1") >= 1) { + canvas.style.opacity -= 0.05; + if (canvas.style.opacity <= 0.05) { + canvas.style.display = "none"; + fadeTimerId = null; + return; + } + } + fadeTimerId = window.requestAnimationFrame(loop); + })(); + }, + }; + + if (typeof module === "object" && typeof module.exports === "object") { + module.exports = topbar; + } else if (typeof define === "function" && define.amd) { + define(function () { + return topbar; + }); + } else { + this.topbar = topbar; + } +}.call(this, window, document)); diff --git a/config/config.exs b/config/config.exs new file mode 100644 index 0000000..e3e1cfa --- /dev/null +++ b/config/config.exs @@ -0,0 +1,56 @@ +# This file is responsible for configuring your application +# and its dependencies with the aid of the Config module. +# +# This configuration file is loaded before any dependency and +# is restricted to this project. + +# General application configuration +import Config + +# Tells `NX` to use `EXLA` as backend +config :nx, default_backend: EXLA.Backend + +# Configures the endpoint +config :app, AppWeb.Endpoint, + url: [host: "localhost"], + render_errors: [ + formats: [html: AppWeb.ErrorHTML, json: AppWeb.ErrorJSON], + layout: false + ], + pubsub_server: App.PubSub, + live_view: [signing_salt: "euyclMQ2"] + +# Configure esbuild (the version is required) +config :esbuild, + version: "0.14.41", + default: [ + args: + ~w(js/app.js --bundle --target=es2017 --outdir=../priv/static/assets --external:/fonts/* --external:/images/*), + cd: Path.expand("../assets", __DIR__), + env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)} + ] + +# Configure tailwind (the version is required) +config :tailwind, + version: "3.2.4", + default: [ + args: ~w( + --config=tailwind.config.js + --input=css/app.css + --output=../priv/static/assets/app.css + ), + cd: Path.expand("../assets", __DIR__) + ] + +# Configures Elixir's Logger +config :logger, :console, + format: "$time $metadata[$level] $message\n", + metadata: [:request_id] + +# Use Jason for JSON parsing in Phoenix +config :phoenix, :json_library, Jason + + +# Import environment specific config. This must remain at the bottom +# of this file so it overrides the configuration defined above. +import_config "#{config_env()}.exs" diff --git a/config/dev.exs b/config/dev.exs new file mode 100644 index 0000000..e0be8f8 --- /dev/null +++ b/config/dev.exs @@ -0,0 +1,65 @@ +import Config + +# For development, we disable any cache and enable +# debugging and code reloading. +# +# The watchers configuration can be used to run external +# watchers to your application. For example, we use it +# with esbuild to bundle .js and .css sources. +config :app, AppWeb.Endpoint, + # Binding to loopback ipv4 address prevents access from other machines. + # Change to `ip: {0, 0, 0, 0}` to allow access from other machines. + http: [ip: {127, 0, 0, 1}, port: 4000], + check_origin: false, + code_reloader: true, + debug_errors: true, + secret_key_base: "btTsEy6WVagm+4u+ZrbwVg6F48ZfgpePZx70twE9SSyPZKkZHiaYa77bFUWV4Vw5", + watchers: [ + esbuild: {Esbuild, :install_and_run, [:default, ~w(--sourcemap=inline --watch)]}, + tailwind: {Tailwind, :install_and_run, [:default, ~w(--watch)]} + ] + +# ## SSL Support +# +# In order to use HTTPS in development, a self-signed +# certificate can be generated by running the following +# Mix task: +# +# mix phx.gen.cert +# +# Run `mix help phx.gen.cert` for more information. +# +# The `http:` config above can be replaced with: +# +# https: [ +# port: 4001, +# cipher_suite: :strong, +# keyfile: "priv/cert/selfsigned_key.pem", +# certfile: "priv/cert/selfsigned.pem" +# ], +# +# If desired, both `http:` and `https:` keys can be +# configured to run both http and https servers on +# different ports. + +# Watch static and templates for browser reloading. +config :app, AppWeb.Endpoint, + live_reload: [ + patterns: [ + ~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$", + ~r"lib/app_web/(controllers|live|components)/.*(ex|heex)$" + ] + ] + +# Enable dev routes for dashboard and mailbox +config :app, dev_routes: true + +# Do not include metadata nor timestamps in development logs +config :logger, :console, format: "[$level] $message\n" + +# Set a higher stacktrace during development. Avoid configuring such +# in production as building large stacktraces may be expensive. +config :phoenix, :stacktrace_depth, 20 + +# Initialize plugs at runtime for faster development compilation +config :phoenix, :plug_init_mode, :runtime diff --git a/config/prod.exs b/config/prod.exs new file mode 100644 index 0000000..608ef13 --- /dev/null +++ b/config/prod.exs @@ -0,0 +1,18 @@ +import Config + +# For production, don't forget to configure the url host +# to something meaningful, Phoenix uses this information +# when generating URLs. + +# Note we also include the path to a cache manifest +# containing the digested version of static files. This +# manifest is generated by the `mix phx.digest` task, +# which you should run after static files are built and +# before starting your production server. +config :app, AppWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json" + +# Do not print debug messages in production +config :logger, level: :info + +# Runtime production configuration, including reading +# of environment variables, is done on config/runtime.exs. diff --git a/config/runtime.exs b/config/runtime.exs new file mode 100644 index 0000000..894e9a3 --- /dev/null +++ b/config/runtime.exs @@ -0,0 +1,82 @@ +import Config + +# config/runtime.exs is executed for all environments, including +# during releases. It is executed after compilation and before the +# system starts, so it is typically used to load production configuration +# and secrets from environment variables or elsewhere. Do not define +# any compile-time configuration in here, as it won't be applied. +# The block below contains prod specific runtime configuration. + +# ## Using releases +# +# If you use `mix release`, you need to explicitly enable the server +# by passing the PHX_SERVER=true when you start it: +# +# PHX_SERVER=true bin/app start +# +# Alternatively, you can use `mix phx.gen.release` to generate a `bin/server` +# script that automatically sets the env var above. +if System.get_env("PHX_SERVER") do + config :app, AppWeb.Endpoint, server: true +end + +if config_env() == :prod do + # The secret key base is used to sign/encrypt cookies and other secrets. + # A default value is used in config/dev.exs and config/test.exs but you + # want to use a different value for prod and you most likely don't want + # to check this value into version control, so we use an environment + # variable instead. + secret_key_base = + System.get_env("SECRET_KEY_BASE") || + raise """ + environment variable SECRET_KEY_BASE is missing. + You can generate one by calling: mix phx.gen.secret + """ + + host = System.get_env("PHX_HOST") || "example.com" + port = String.to_integer(System.get_env("PORT") || "4000") + + config :app, AppWeb.Endpoint, + url: [host: host, port: 443, scheme: "https"], + http: [ + # Enable IPv6 and bind on all interfaces. + # Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access. + # See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html + # for details about using IPv6 vs IPv4 and loopback vs public addresses. + ip: {0, 0, 0, 0, 0, 0, 0, 0}, + port: port + ], + secret_key_base: secret_key_base + + # ## SSL Support + # + # To get SSL working, you will need to add the `https` key + # to your endpoint configuration: + # + # config :app, AppWeb.Endpoint, + # https: [ + # ..., + # port: 443, + # cipher_suite: :strong, + # keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"), + # certfile: System.get_env("SOME_APP_SSL_CERT_PATH") + # ] + # + # The `cipher_suite` is set to `:strong` to support only the + # latest and more secure SSL ciphers. This means old browsers + # and clients may not be supported. You can set it to + # `:compatible` for wider support. + # + # `:keyfile` and `:certfile` expect an absolute path to the key + # and cert in disk or a relative path inside priv, for example + # "priv/ssl/server.key". For all supported SSL configuration + # options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1 + # + # We also recommend setting `force_ssl` in your endpoint, ensuring + # no data is ever sent via http, always redirecting to https: + # + # config :app, AppWeb.Endpoint, + # force_ssl: [hsts: true] + # + # Check `Plug.SSL` for all available options in `force_ssl`. +end diff --git a/config/test.exs b/config/test.exs new file mode 100644 index 0000000..20a13c9 --- /dev/null +++ b/config/test.exs @@ -0,0 +1,14 @@ +import Config + +# We don't run a server during test. If one is required, +# you can enable the server option below. +config :app, AppWeb.Endpoint, + http: [ip: {127, 0, 0, 1}, port: 4002], + secret_key_base: "d422JqbVTXef5vPy90SakC4QcPN76fRi6wLm+pUnC09eFxWUjPbTKe0dVmpGpI5N", + server: false + +# Print only warnings and errors during test +config :logger, level: :warning + +# Initialize plugs at runtime for faster test compilation +config :phoenix, :plug_init_mode, :runtime diff --git a/coveralls.json b/coveralls.json new file mode 100644 index 0000000..b1550d0 --- /dev/null +++ b/coveralls.json @@ -0,0 +1,17 @@ +{ + "skip_files": [ + "test/", + "lib/app.ex", + "lib/app/application.ex", + "lib/app_web.ex", + "lib/app/repo.ex", + "lib/app/release.ex", + "lib/app_web/views/app_view.ex", + "lib/app_web/views/init_view.ex", + "lib/app_web/views/layout_view.ex", + "lib/app_web/views/error_helpers.ex", + "lib/app_web/components", + "lib/app_web/endpoint.ex", + "lib/app_web/telemetry.ex" + ] + } \ No newline at end of file diff --git a/lib/app.ex b/lib/app.ex new file mode 100644 index 0000000..a10dc06 --- /dev/null +++ b/lib/app.ex @@ -0,0 +1,9 @@ +defmodule App do + @moduledoc """ + App keeps the contexts that define your domain + and business logic. + + Contexts are also responsible for managing your data, regardless + if it comes from the database, an external API or others. + """ +end diff --git a/lib/app/application.ex b/lib/app/application.ex new file mode 100644 index 0000000..c5ecf1b --- /dev/null +++ b/lib/app/application.ex @@ -0,0 +1,50 @@ +defmodule App.Application do + # See https://hexdocs.pm/elixir/Application.html + # for more information on OTP Applications + @moduledoc false + + use Application + + @impl true + def start(_type, _args) do + children = [ + # Start the Telemetry supervisor + AppWeb.Telemetry, + # Start the PubSub system + {Phoenix.PubSub, name: App.PubSub}, + # Nx serving for image classifier + {Nx.Serving, serving: serving(), name: ImageClassifier}, + # Adding a supervisor + {Task.Supervisor, name: App.TaskSupervisor}, + # Start the Endpoint (http/https) + AppWeb.Endpoint + # Start a worker by calling: App.Worker.start_link(arg) + # {App.Worker, arg} + ] + + # See https://hexdocs.pm/elixir/Supervisor.html + # for other strategies and supported options + opts = [strategy: :one_for_one, name: App.Supervisor] + Supervisor.start_link(children, opts) + end + + def serving do + {:ok, model_info} = Bumblebee.load_model({:hf, "Salesforce/blip-image-captioning-base"}) + {:ok, featurizer} = Bumblebee.load_featurizer({:hf, "Salesforce/blip-image-captioning-base"}) + {:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "Salesforce/blip-image-captioning-base"}) + {:ok, generation_config} = Bumblebee.load_generation_config({:hf, "Salesforce/blip-image-captioning-base"}) + + Bumblebee.Vision.image_to_text(model_info, featurizer, tokenizer, generation_config, + compile: [batch_size: 10], + defn_options: [compiler: EXLA] + ) + end + + # Tell Phoenix to update the endpoint configuration + # whenever the application is updated. + @impl true + def config_change(changed, _new, removed) do + AppWeb.Endpoint.config_change(changed, removed) + :ok + end +end diff --git a/lib/app_web.ex b/lib/app_web.ex new file mode 100644 index 0000000..512ce55 --- /dev/null +++ b/lib/app_web.ex @@ -0,0 +1,111 @@ +defmodule AppWeb do + @moduledoc """ + The entrypoint for defining your web interface, such + as controllers, components, channels, and so on. + + This can be used in your application as: + + use AppWeb, :controller + use AppWeb, :html + + The definitions below will be executed for every controller, + component, etc, so keep them short and clean, focused + on imports, uses and aliases. + + Do NOT define functions inside the quoted expressions + below. Instead, define additional modules and import + those modules here. + """ + + def static_paths, do: ~w(assets fonts images favicon.ico robots.txt) + + def router do + quote do + use Phoenix.Router, helpers: false + + # Import common connection and controller functions to use in pipelines + import Plug.Conn + import Phoenix.Controller + import Phoenix.LiveView.Router + end + end + + def channel do + quote do + use Phoenix.Channel + end + end + + def controller do + quote do + use Phoenix.Controller, + formats: [:html, :json], + layouts: [html: AppWeb.Layouts] + + import Plug.Conn + + unquote(verified_routes()) + end + end + + def live_view do + quote do + use Phoenix.LiveView, + layout: {AppWeb.Layouts, :app} + + unquote(html_helpers()) + end + end + + def live_component do + quote do + use Phoenix.LiveComponent + + unquote(html_helpers()) + end + end + + def html do + quote do + use Phoenix.Component + + # Import convenience functions from controllers + import Phoenix.Controller, + only: [get_csrf_token: 0, view_module: 1, view_template: 1] + + # Include general helpers for rendering HTML + unquote(html_helpers()) + end + end + + defp html_helpers do + quote do + # HTML escaping functionality + import Phoenix.HTML + # Core UI components and translation + import AppWeb.CoreComponents + + # Shortcut for generating JS commands + alias Phoenix.LiveView.JS + + # Routes generation with the ~p sigil + unquote(verified_routes()) + end + end + + def verified_routes do + quote do + use Phoenix.VerifiedRoutes, + endpoint: AppWeb.Endpoint, + router: AppWeb.Router, + statics: AppWeb.static_paths() + end + end + + @doc """ + When used, dispatch to the appropriate controller/view/etc. + """ + defmacro __using__(which) when is_atom(which) do + apply(__MODULE__, which, []) + end +end diff --git a/lib/app_web/components/core_components.ex b/lib/app_web/components/core_components.ex new file mode 100644 index 0000000..dad6364 --- /dev/null +++ b/lib/app_web/components/core_components.ex @@ -0,0 +1,650 @@ +defmodule AppWeb.CoreComponents do + @moduledoc """ + Provides core UI components. + + The components in this module use Tailwind CSS, a utility-first CSS framework. + See the [Tailwind CSS documentation](https://tailwindcss.com) to learn how to + customize the generated components in this module. + + Icons are provided by [heroicons](https://heroicons.com), using the + [heroicons_elixir](https://github.com/mveytsman/heroicons_elixir) project. + """ + use Phoenix.Component + + alias Phoenix.LiveView.JS + + @doc """ + Renders a modal. + + ## Examples + + <.modal id="confirm-modal"> + Are you sure? + <:confirm>OK + <:cancel>Cancel + + + JS commands may be passed to the `:on_cancel` and `on_confirm` attributes + for the caller to react to each button press, for example: + + <.modal id="confirm" on_confirm={JS.push("delete")} on_cancel={JS.navigate(~p"/posts")}> + Are you sure you? + <:confirm>OK + <:cancel>Cancel + + """ + attr :id, :string, required: true + attr :show, :boolean, default: false + attr :on_cancel, JS, default: %JS{} + attr :on_confirm, JS, default: %JS{} + + slot :inner_block, required: true + slot :title + slot :subtitle + slot :confirm + slot :cancel + + def modal(assigns) do + ~H""" + + """ + end + + @doc """ + Renders flash notices. + + ## Examples + + <.flash kind={:info} flash={@flash} /> + <.flash kind={:info} phx-mounted={show("#flash")}>Welcome Back! + """ + attr :id, :string, default: "flash", doc: "the optional id of flash container" + attr :flash, :map, default: %{}, doc: "the map of flash messages to display" + attr :title, :string, default: nil + attr :kind, :atom, values: [:info, :error], doc: "used for styling and flash lookup" + attr :autoshow, :boolean, default: true, doc: "whether to auto show the flash on mount" + attr :close, :boolean, default: true, doc: "whether the flash can be closed" + attr :rest, :global, doc: "the arbitrary HTML attributes to add to the flash container" + + slot :inner_block, doc: "the optional inner block that renders the flash message" + + def flash(assigns) do + ~H""" +
+
<%= msg %>
+ +
+
+ <%= render_slot(@subtitle) %> +
+<%= col[:label] %> | +Actions | +
---|---|
+
+
+
+ <%= render_slot(col, @row_item.(row)) %>
+
+
+ |
+
+
+
+
+ <%= render_slot(action, @row_item.(row)) %>
+
+
+ |
+
+ Do simple classification with this LiveView + demo, powered by Bumblebee. +
+ + +or drag and drop
+PNG, JPG, GIF up to 5MB
+ <% end %> +