Giter Site home page Giter Site logo

wozehua / stickerbaker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cbh123/stickerbaker

0.0 0.0 0.0 13.7 MB

Let's bake some (AI) stickers!

Home Page: https://stickerbaker.com

Shell 0.24% JavaScript 6.20% Elixir 72.66% CSS 1.14% HTML 18.20% Batchfile 0.05% Dockerfile 1.51%

stickerbaker's Introduction

StickerBaker

Announcing StickerBaker!

Make stickers with AI. Powered by @replicate and @flydotio, and 100% open-source.https://t.co/8vucCsHtAd pic.twitter.com/tBhDyGrOx0

โ€” Charlie Holtz (@charliebholtz) February 26, 2024

How it works

Enter a prompt and generating a sticker using https://replicate.com/fofr/sticker-maker.

The home page is rendered in lib/sticker_web/home_live.ex. When the prompt form is submitted, this handle_event gets called:

  def handle_event("save", %{"prompt" => prompt}, socket) do
    user_id = socket.assigns.local_user_id

    {:ok, prediction} =
      Predictions.create_prediction(%{
        prompt: prompt,
        local_user_id: user_id
      })

    send(self(), {:kick_off, prediction})

    {:noreply,
     socket
     |> assign(form: to_form(%{"prompt" => ""}))
     |> stream_insert(:my_predictions, prediction, at: 0)}
  end

This sends a :kick_off message to the LiveView (so there is no lag) which calls Predictions.moderate/3 in lib/sticker/predictions.ex:

  @doc """
  Moderates a prediction.
  The logic in replicate_webhook_controller.ex handles
  the webhook. Once the moderation is complete, the webhook controller automatically
  called gen_image.
  """
  def moderate(prompt, user_id, prediction_id) do
    "fofr/prompt-classifier"
    |> Replicate.Models.get!()
    |> Replicate.Models.get_latest_version!()
    |> Replicate.Predictions.create(
      %{
        prompt: "[PROMPT] #{prompt} [/PROMPT] [SAFETY_RANKING]",
        max_new_tokens: 128,
        temperature: 0.2,
        top_p: 0.9,
        top_k: 50,
        stop_sequences: "[/SAFETY_RANKING]"
      },
      "#{Sticker.Utils.get_host()}/webhooks/replicate?user_id=#{user_id}&prediction_id=#{prediction_id}"
    )
  end

We pass a webhook to Replicate. All the logic for the webhook lives in lib/sticker_web/controllers/replicate_webhook_controller.ex. The nice thing about this webhook is that we can refresh the page or disconnect and Replicate still handles the prediction queue for us. Once the prediction is ready, we upload it to Tigris (Replicate doesn't save our data for us) and then the sticker gets broadcast back to our home_live.ex.

Importantly, because we're passing Replicate a webhook, for local dev you'll need ngrok running to tunnel your localhost to a URL. Once you install ngrok run it with ngrok http 4000 and paste the URL into your copied .env file.

Stack

StickerBaker runs on:

Dev

To start your Phoenix server:

  • Run mix setup to install and setup dependencies
  • Create an env file with cp .env.copy .env
    • Add your Replicate tokens
    • Add Tigris tokens
    • Start ngrok with ngrok http 4000 and add that to your env
  • Start Phoenix endpoint with mix phx.server or inside IEx with iex -S mix phx.server
  • Add a .env file with REPLICATE_API_TOKEN set to your Replicate token

Now you can visit localhost:4000 from your browser.

Prod

Update the url and check_origin origin in prod.exs Deploy with fly launch Make sure when you fly launch you set up a Postgres DB!

stickerbaker's People

Contributors

bfirsh avatar cbh123 avatar hassanrsiddiqi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.