Skip to content

Commit

Permalink
switch to using deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
cbh123 committed Sep 27, 2023
1 parent 4a8c243 commit 4fd12b7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 36 deletions.
48 changes: 15 additions & 33 deletions lib/emoji_web/live/home_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ defmodule EmojiWeb.HomeLive do

def handle_info({:image_generated, prediction, {:ok, r8_prediction}}, socket) do
# r2_url = save_r2("prediction-#{prediction.id}-emoji", r8_prediction.output |> List.first())
r8_prediction |> IO.inspect(label: "r8_prediction")

{:ok, prediction} =
Predictions.update_prediction(prediction, %{
emoji_output: r8_prediction["output"] |> List.first(),
uuid: r8_prediction["id"]
emoji_output: r8_prediction.output |> List.first(),
uuid: r8_prediction.id
})

start_task(fn -> {:background_removed, prediction, remove_bg(prediction.emoji_output)} end)
Expand Down Expand Up @@ -175,41 +176,22 @@ defmodule EmojiWeb.HomeLive do
end

defp gen_image(prompt) do
url = "https://api.replicate.com/v1/deployments/cbh123/sdxl-emoji/predictions"
headers = [Authorization: "Token #{System.get_env("REPLICATE_API_TOKEN")}"]

body =
Jason.encode!(%{
input: %{
prompt: prompt,
width: 512,
height: 512,
num_inference_steps: 30,
negative_prompt: "racist, xenophobic, antisemitic, islamophobic, bigoted"
}
})
{:ok, deployment} = Replicate.Deployments.get("cbh123/sdxl-emoji")

HTTPoison.post!(url, body, headers) |> Map.get(:body) |> Jason.decode() |> wait()
end

defp wait({:ok, %{"id" => id, "status" => status} = prediction}) do
cond do
status in ["starting", "processing"] ->
Process.sleep(500)

HTTPoison.get!("https://api.replicate.com/v1/predictions/#{id}",
Authorization: "Token #{System.get_env("REPLICATE_API_TOKEN")}"
)
|> Map.get(:body)
|> Jason.decode()
|> wait()
{:ok, prediction} =
Replicate.Deployments.create_prediction(deployment,
prompt: prompt,
width: 512,
height: 512,
num_inference_steps: 30,
negative_prompt: "racist, xenophobic, antisemitic, islamophobic, bigoted"
)
|> wait()

true ->
{:ok, prediction}
end
{:ok, prediction}
end

defp wait({:error, message}), do: {:error, message}
defp wait({:ok, prediction}), do: Replicate.Predictions.wait(prediction)

defp start_task(fun) do
pid = self()
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule Emoji.MixProject do
{:gettext, "~> 0.20"},
{:jason, "~> 1.2"},
{:plug_cowboy, "~> 2.5"},
{:replicate, "~> 1.0.1"},
{:replicate, "~> 1.1.1"},
{:ex_aws, "~> 2.1"},
{:ex_aws_s3, "~> 2.0"},
{:nx, "~> 0.4"},
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"finch": {:hex, :finch, "0.16.0", "40733f02c89f94a112518071c0a91fe86069560f5dbdb39f9150042f44dcfb1a", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6 or ~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f660174c4d519e5fec629016054d60edd822cdfe2b7270836739ac2f97735ec5"},
"floki": {:hex, :floki, "0.34.3", "5e2dcaec5d7c228ce5b1d3501502e308b2d79eb655e4191751a1fe491c37feac", [:mix], [], "hexpm", "9577440eea5b97924b4bf3c7ea55f7b8b6dce589f9b28b096cc294a8dc342341"},
"gettext": {:hex, :gettext, "0.23.1", "821e619a240e6000db2fc16a574ef68b3bd7fe0167ccc264a81563cc93e67a31", [:mix], [{:expo, "~> 0.4.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "19d744a36b809d810d610b57c27b934425859d158ebd56561bc41f7eeb8795db"},
"hackney": {:hex, :hackney, "1.18.2", "d7ff544ddae5e1cb49e9cf7fa4e356d7f41b283989a1c304bfc47a8cc1cf966f", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "af94d5c9f97857db257090a4a10e5426ecb6f4918aa5cc666798566ae14b65fd"},
"hackney": {:hex, :hackney, "1.19.1", "59de4716e985dd2b5cbd4954fa1ae187e2b610a9c4520ffcb0b1653c3d6e5559", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "8aa08234bdefc269995c63c2282cf3cd0e36febe3a6bfab11b610572fdd1cad0"},
"hnswlib": {:hex, :hnswlib, "0.1.2", "e0ec31e8fabcc79edc2f0ce237cfb3acf1a377da3c73b9b95a3da97e1c35c79e", [:make, :mix], [{:cc_precompiler, "~> 0.1.0", [hex: :cc_precompiler, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.7.0", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:nx, "~> 0.5", [hex: :nx, repo: "hexpm", optional: false]}], "hexpm", "67589aa6088668c48fdc8e8e30ae71161939fdc51d9064430ed52a84bacebb30"},
"hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"},
"httpoison": {:hex, :httpoison, "2.1.0", "655fd9a7b0b95ee3e9a3b535cf7ac8e08ef5229bab187fa86ac4208b122d934b", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "fc455cb4306b43827def4f57299b2d5ac8ac331cb23f517e734a4b78210a160c"},
Expand Down Expand Up @@ -55,7 +55,7 @@
"postgrex": {:hex, :postgrex, "0.17.3", "c92cda8de2033a7585dae8c61b1d420a1a1322421df84da9a82a6764580c503d", [:mix], [{:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "946cf46935a4fdca7a81448be76ba3503cff082df42c6ec1ff16a4bdfbfb098d"},
"progress_bar": {:hex, :progress_bar, "3.0.0", "f54ff038c2ac540cfbb4c2bfe97c75e7116ead044f3c2b10c9f212452194b5cd", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "6981c2b25ab24aecc91a2dc46623658e1399c21a2ae24db986b90d678530f2b7"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
"replicate": {:hex, :replicate, "1.0.3", "41eae66ee3d83d304b41c843ac91fe67882ddaeb8ea79ed0865459efbe59456d", [:mix], [{:httpoison, "~> 2.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "b7789d4a2f0280ecb1b2034208cbb41fcbe1cabe8269ad90fa24df842ebd2c03"},
"replicate": {:hex, :replicate, "1.1.1", "77f017b7bc0af2df6b3abfcf06a1136a3279823e881084a8b57e3d90c28eacff", [:mix], [{:httpoison, "~> 2.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "73ce49fc3185ec5988f25265ae8562f64c1e8bd0ac040eed1c62f9527759b1bb"},
"rustler_precompiled": {:hex, :rustler_precompiled, "0.6.3", "f838d94bc35e1844973ee7266127b156fdc962e9e8b7ff666c8fb4fed7964d23", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:rustler, "~> 0.23", [hex: :rustler, repo: "hexpm", optional: true]}], "hexpm", "e18ecca3669a7454b3a2be75ae6c3ef01d550bc9a8cf5fbddcfff843b881d7c6"},
"safetensors": {:hex, :safetensors, "0.1.1", "b5859a010fb56249ecfba4799d316e96b89152576af2db7657786c55dcf2f5b6", [:mix], [{:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:nx, "~> 0.5", [hex: :nx, repo: "hexpm", optional: false]}], "hexpm", "dfbb525bf3debb2e2d90f840728af70da5d55f6caa091cac4d0891a4eb4c52d5"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
Expand Down

0 comments on commit 4fd12b7

Please sign in to comment.