init commit
This commit is contained in:
9
lib/diffuser.ex
Normal file
9
lib/diffuser.ex
Normal file
@@ -0,0 +1,9 @@
|
||||
defmodule Diffuser do
|
||||
@moduledoc """
|
||||
Diffuser 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
|
37
lib/diffuser/application.ex
Normal file
37
lib/diffuser/application.ex
Normal file
@@ -0,0 +1,37 @@
|
||||
defmodule Diffuser.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 Ecto repository
|
||||
Diffuser.Repo,
|
||||
# Start the Telemetry supervisor
|
||||
DiffuserWeb.Telemetry,
|
||||
# Start the PubSub system
|
||||
{Phoenix.PubSub, name: Diffuser.PubSub},
|
||||
# Start the Endpoint (http/https)
|
||||
DiffuserWeb.Endpoint,
|
||||
# Start a worker by calling: Diffuser.Worker.start_link(arg)
|
||||
# {Diffuser.Worker, arg}
|
||||
{Task.Supervisor, name: Diffuser.Generator.PromptRequestSupervisor}
|
||||
]
|
||||
|
||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||
# for other strategies and supported options
|
||||
opts = [strategy: :one_for_one, name: Diffuser.Supervisor]
|
||||
Supervisor.start_link(children, opts)
|
||||
end
|
||||
|
||||
# Tell Phoenix to update the endpoint configuration
|
||||
# whenever the application is updated.
|
||||
@impl true
|
||||
def config_change(changed, _new, removed) do
|
||||
DiffuserWeb.Endpoint.config_change(changed, removed)
|
||||
:ok
|
||||
end
|
||||
end
|
125
lib/diffuser/generator.ex
Normal file
125
lib/diffuser/generator.ex
Normal file
@@ -0,0 +1,125 @@
|
||||
defmodule Diffuser.Generator do
|
||||
@moduledoc """
|
||||
The Generator context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias Diffuser.Repo
|
||||
|
||||
alias Diffuser.Generator.{PromptRequest, PromptRequestResult}
|
||||
|
||||
@doc """
|
||||
Returns the list of prompt_requests.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_prompt_requests()
|
||||
[%PromptRequest{}, ...]
|
||||
|
||||
"""
|
||||
def list_prompt_requests do
|
||||
Repo.all(PromptRequest)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single prompt_request.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Prompt request does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_prompt_request!(123)
|
||||
%PromptRequest{}
|
||||
|
||||
iex> get_prompt_request!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_prompt_request!(id),
|
||||
do: Repo.one!(from pr in PromptRequest, where: pr.id == ^id, preload: [:images])
|
||||
|
||||
@doc """
|
||||
Creates a prompt_request.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_prompt_request(%{field: value})
|
||||
{:ok, %PromptRequest{}}
|
||||
|
||||
iex> create_prompt_request(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_prompt_request(attrs \\ %{}) do
|
||||
%PromptRequest{}
|
||||
|> PromptRequest.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a prompt_request.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_prompt_request(prompt_request, %{field: new_value})
|
||||
{:ok, %PromptRequest{}}
|
||||
|
||||
iex> update_prompt_request(prompt_request, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_prompt_request(%PromptRequest{} = prompt_request, attrs) do
|
||||
prompt_request
|
||||
|> PromptRequest.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a prompt_request.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_prompt_request(prompt_request)
|
||||
{:ok, %PromptRequest{}}
|
||||
|
||||
iex> delete_prompt_request(prompt_request)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_prompt_request(%PromptRequest{} = prompt_request) do
|
||||
Repo.delete(prompt_request)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking prompt_request changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_prompt_request(prompt_request)
|
||||
%Ecto.Changeset{data: %PromptRequest{}}
|
||||
|
||||
"""
|
||||
def change_prompt_request(%PromptRequest{} = prompt_request, attrs \\ %{}) do
|
||||
PromptRequest.changeset(prompt_request, attrs)
|
||||
end
|
||||
|
||||
def create_prompt_request_result(prompt_request_id, image) do
|
||||
%PromptRequestResult{}
|
||||
|> PromptRequestResult.changeset(%{
|
||||
image: image,
|
||||
prompt_request_id: prompt_request_id
|
||||
})
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
def create_prompt_request_results(prompt_request_id, images) do
|
||||
Enum.each(images, fn %{file_name: file_name, binary: binary} ->
|
||||
create_prompt_request_result(prompt_request_id, %{
|
||||
file_name: file_name,
|
||||
filename: file_name,
|
||||
binary: binary,
|
||||
updated_at: DateTime.utc_now()
|
||||
})
|
||||
end)
|
||||
end
|
||||
end
|
23
lib/diffuser/generator/prompt_request.ex
Normal file
23
lib/diffuser/generator/prompt_request.ex
Normal file
@@ -0,0 +1,23 @@
|
||||
defmodule Diffuser.Generator.PromptRequest do
|
||||
use Ecto.Schema
|
||||
use Waffle.Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
alias Diffuser.Generator.PromptRequestResult
|
||||
|
||||
@primary_key {:id, Ecto.UUID, autogenerate: true}
|
||||
schema "prompt_requests" do
|
||||
field :prompt, :string
|
||||
field :status, :string, default: "queued"
|
||||
|
||||
has_many :images, PromptRequestResult, on_delete: :delete_all
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(prompt_request, attrs) do
|
||||
prompt_request
|
||||
|> cast(attrs, [:prompt, :status])
|
||||
|> validate_required([:prompt])
|
||||
end
|
||||
end
|
98
lib/diffuser/generator/prompt_request_genserver.ex
Normal file
98
lib/diffuser/generator/prompt_request_genserver.ex
Normal file
@@ -0,0 +1,98 @@
|
||||
defmodule Diffuser.Generator.PromptRequestGenserver do
|
||||
use GenServer
|
||||
alias Diffuser.Generator
|
||||
alias Diffuser.Generator.PromptRequest
|
||||
alias DiffuserWeb.Endpoint
|
||||
alias Diffuser.PythonHelper, as: Helper
|
||||
|
||||
@path 'lib/diffuser/python'
|
||||
|
||||
def new(%{prompt_request: %PromptRequest{} = prompt_request}) do
|
||||
GenServer.start_link(
|
||||
__MODULE__,
|
||||
%{prompt_request: prompt_request},
|
||||
name: name_for(prompt_request)
|
||||
)
|
||||
end
|
||||
|
||||
def name_for(%PromptRequest{id: prompt_request_id}),
|
||||
do: {:global, "prompt_request:#{prompt_request_id}"}
|
||||
|
||||
def init(%{prompt_request: %PromptRequest{} = prompt_request}) do
|
||||
send(self(), :start_prompt)
|
||||
|
||||
{:ok,
|
||||
%{
|
||||
prompt_request: prompt_request
|
||||
}}
|
||||
end
|
||||
|
||||
def handle_info(:start_prompt, %{prompt_request: prompt_request} = state) do
|
||||
with {:ok, %{prompt: prompt} = active_prompt} <-
|
||||
update_and_broadcast_progress(prompt_request, "in_progress"),
|
||||
:ok <- call_python(:test_script, :test_func, prompt),
|
||||
%PromptRequest{} = prompt_request_with_results <- write_and_save_images(active_prompt),
|
||||
{:ok, completed_prompt} <-
|
||||
update_and_broadcast_progress(prompt_request_with_results, "finished") do
|
||||
IO.inspect(completed_prompt)
|
||||
{:noreply, state}
|
||||
else
|
||||
nil ->
|
||||
raise("prompt not found")
|
||||
|
||||
{:error, message} ->
|
||||
raise(message)
|
||||
end
|
||||
end
|
||||
|
||||
defp update_and_broadcast_progress(%PromptRequest{id: id} = prompt_request, new_status) do
|
||||
{:ok, new_prompt} = Generator.update_prompt_request(prompt_request, %{status: new_status})
|
||||
:ok = Endpoint.broadcast("request:#{id}", "request", %{prompt_request: new_prompt})
|
||||
|
||||
{:ok, new_prompt}
|
||||
end
|
||||
|
||||
defp call_python(_module, _func, prompt) do
|
||||
Port.open(
|
||||
{:spawn, "python #{@path}/stable_diffusion.py --prompt #{prompt}"},
|
||||
[:binary, {:packet, 4}]
|
||||
)
|
||||
|
||||
# TODO: We will want to flush, and get the image data from the script
|
||||
# then write it to PromptResult
|
||||
|
||||
# pid = Helper.py_instance(Path.absname(@path))
|
||||
# :python.call(pid, module, func, args)
|
||||
|
||||
# pid
|
||||
# |> :python.stop()
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
defp write_and_save_images(%PromptRequest{id: id, prompt: prompt}) do
|
||||
height = :rand.uniform(512)
|
||||
width = :rand.uniform(512)
|
||||
IO.inspect(height)
|
||||
|
||||
{:ok, resp} =
|
||||
:httpc.request(
|
||||
:get,
|
||||
{'http://placekitten.com/#{height}/#{width}', []},
|
||||
[],
|
||||
body_format: :binary
|
||||
)
|
||||
|
||||
{{_, 200, 'OK'}, _headers, body} = resp
|
||||
|
||||
Generator.create_prompt_request_results(id, [
|
||||
%{
|
||||
file_name: "#{prompt}.jpg",
|
||||
filename: "#{prompt}.jpg",
|
||||
binary: body
|
||||
}
|
||||
])
|
||||
|
||||
Generator.get_prompt_request!(id)
|
||||
end
|
||||
end
|
22
lib/diffuser/generator/prompt_request_result.ex
Normal file
22
lib/diffuser/generator/prompt_request_result.ex
Normal file
@@ -0,0 +1,22 @@
|
||||
defmodule Diffuser.Generator.PromptRequestResult do
|
||||
use Ecto.Schema
|
||||
use Waffle.Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
alias Diffuser.Generator.PromptRequest
|
||||
|
||||
@primary_key {:id, Ecto.UUID, autogenerate: true}
|
||||
schema "prompt_request_results" do
|
||||
field :image, Diffuser.Uploaders.Image.Type
|
||||
belongs_to :prompt_request, PromptRequest, type: :binary_id
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(prompt_request, attrs) do
|
||||
prompt_request
|
||||
|> cast(attrs, [:prompt_request_id])
|
||||
|> cast_attachments(attrs, [:image])
|
||||
|> validate_required([:prompt_request_id, :image])
|
||||
end
|
||||
end
|
27
lib/diffuser/generator/prompt_request_supervisor.ex
Normal file
27
lib/diffuser/generator/prompt_request_supervisor.ex
Normal file
@@ -0,0 +1,27 @@
|
||||
defmodule Diffuser.Generator.PromptRequestSupervisor do
|
||||
use DynamicSupervisor
|
||||
alias Diffuser.Generator.PromptRequest
|
||||
|
||||
def start_link(init_arg) do
|
||||
DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(_init_arg) do
|
||||
DynamicSupervisor.init(strategy: :one_for_one)
|
||||
end
|
||||
|
||||
def start_prompt_request(%PromptRequest{} = prompt_request) do
|
||||
Task.Supervisor.start_child(
|
||||
__MODULE__,
|
||||
Diffuser.Generator.PromptRequestGenserver,
|
||||
:new,
|
||||
[
|
||||
%{
|
||||
prompt_request: prompt_request
|
||||
}
|
||||
],
|
||||
restart: :transient
|
||||
)
|
||||
end
|
||||
end
|
3
lib/diffuser/mailer.ex
Normal file
3
lib/diffuser/mailer.ex
Normal file
@@ -0,0 +1,3 @@
|
||||
defmodule Diffuser.Mailer do
|
||||
use Swoosh.Mailer, otp_app: :diffuser
|
||||
end
|
201
lib/diffuser/python/LICENSE
Normal file
201
lib/diffuser/python/LICENSE
Normal file
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
82
lib/diffuser/python/LICENSE_MODEL
Normal file
82
lib/diffuser/python/LICENSE_MODEL
Normal file
@@ -0,0 +1,82 @@
|
||||
Copyright (c) 2022 Robin Rombach and Patrick Esser and contributors
|
||||
|
||||
CreativeML Open RAIL-M
|
||||
dated August 22, 2022
|
||||
|
||||
Section I: PREAMBLE
|
||||
|
||||
Multimodal generative models are being widely adopted and used, and have the potential to transform the way artists, among other individuals, conceive and benefit from AI or ML technologies as a tool for content creation.
|
||||
|
||||
Notwithstanding the current and potential benefits that these artifacts can bring to society at large, there are also concerns about potential misuses of them, either due to their technical limitations or ethical considerations.
|
||||
|
||||
In short, this license strives for both the open and responsible downstream use of the accompanying model. When it comes to the open character, we took inspiration from open source permissive licenses regarding the grant of IP rights. Referring to the downstream responsible use, we added use-based restrictions not permitting the use of the Model in very specific scenarios, in order for the licensor to be able to enforce the license in case potential misuses of the Model may occur. At the same time, we strive to promote open and responsible research on generative models for art and content generation.
|
||||
|
||||
Even though downstream derivative versions of the model could be released under different licensing terms, the latter will always have to include - at minimum - the same use-based restrictions as the ones in the original license (this license). We believe in the intersection between open and responsible AI development; thus, this License aims to strike a balance between both in order to enable responsible open-science in the field of AI.
|
||||
|
||||
This License governs the use of the model (and its derivatives) and is informed by the model card associated with the model.
|
||||
|
||||
NOW THEREFORE, You and Licensor agree as follows:
|
||||
|
||||
1. Definitions
|
||||
|
||||
- "License" means the terms and conditions for use, reproduction, and Distribution as defined in this document.
|
||||
- "Data" means a collection of information and/or content extracted from the dataset used with the Model, including to train, pretrain, or otherwise evaluate the Model. The Data is not licensed under this License.
|
||||
- "Output" means the results of operating a Model as embodied in informational content resulting therefrom.
|
||||
- "Model" means any accompanying machine-learning based assemblies (including checkpoints), consisting of learnt weights, parameters (including optimizer states), corresponding to the model architecture as embodied in the Complementary Material, that have been trained or tuned, in whole or in part on the Data, using the Complementary Material.
|
||||
- "Derivatives of the Model" means all modifications to the Model, works based on the Model, or any other model which is created or initialized by transfer of patterns of the weights, parameters, activations or output of the Model, to the other model, in order to cause the other model to perform similarly to the Model, including - but not limited to - distillation methods entailing the use of intermediate data representations or methods based on the generation of synthetic data by the Model for training the other model.
|
||||
- "Complementary Material" means the accompanying source code and scripts used to define, run, load, benchmark or evaluate the Model, and used to prepare data for training or evaluation, if any. This includes any accompanying documentation, tutorials, examples, etc, if any.
|
||||
- "Distribution" means any transmission, reproduction, publication or other sharing of the Model or Derivatives of the Model to a third party, including providing the Model as a hosted service made available by electronic or other remote means - e.g. API-based or web access.
|
||||
- "Licensor" means the copyright owner or entity authorized by the copyright owner that is granting the License, including the persons or entities that may have rights in the Model and/or distributing the Model.
|
||||
- "You" (or "Your") means an individual or Legal Entity exercising permissions granted by this License and/or making use of the Model for whichever purpose and in any field of use, including usage of the Model in an end-use application - e.g. chatbot, translator, image generator.
|
||||
- "Third Parties" means individuals or legal entities that are not under common control with Licensor or You.
|
||||
- "Contribution" means any work of authorship, including the original version of the Model and any modifications or additions to that Model or Derivatives of the Model thereof, that is intentionally submitted to Licensor for inclusion in the Model by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Model, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
- "Contributor" means Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Model.
|
||||
|
||||
Section II: INTELLECTUAL PROPERTY RIGHTS
|
||||
|
||||
Both copyright and patent grants apply to the Model, Derivatives of the Model and Complementary Material. The Model and Derivatives of the Model are subject to additional terms as described in Section III.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare, publicly display, publicly perform, sublicense, and distribute the Complementary Material, the Model, and Derivatives of the Model.
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License and where and as applicable, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this paragraph) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Model and the Complementary Material, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Model to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Model and/or Complementary Material or a Contribution incorporated within the Model and/or Complementary Material constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for the Model and/or Work shall terminate as of the date such litigation is asserted or filed.
|
||||
|
||||
Section III: CONDITIONS OF USAGE, DISTRIBUTION AND REDISTRIBUTION
|
||||
|
||||
4. Distribution and Redistribution. You may host for Third Party remote access purposes (e.g. software-as-a-service), reproduce and distribute copies of the Model or Derivatives of the Model thereof in any medium, with or without modifications, provided that You meet the following conditions:
|
||||
Use-based restrictions as referenced in paragraph 5 MUST be included as an enforceable provision by You in any type of legal agreement (e.g. a license) governing the use and/or distribution of the Model or Derivatives of the Model, and You shall give notice to subsequent users You Distribute to, that the Model or Derivatives of the Model are subject to paragraph 5. This provision does not apply to the use of Complementary Material.
|
||||
You must give any Third Party recipients of the Model or Derivatives of the Model a copy of this License;
|
||||
You must cause any modified files to carry prominent notices stating that You changed the files;
|
||||
You must retain all copyright, patent, trademark, and attribution notices excluding those notices that do not pertain to any part of the Model, Derivatives of the Model.
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions - respecting paragraph 4.a. - for use, reproduction, or Distribution of Your modifications, or for any such Derivatives of the Model as a whole, provided Your use, reproduction, and Distribution of the Model otherwise complies with the conditions stated in this License.
|
||||
5. Use-based restrictions. The restrictions set forth in Attachment A are considered Use-based restrictions. Therefore You cannot use the Model and the Derivatives of the Model for the specified restricted uses. You may use the Model subject to this License, including only for lawful purposes and in accordance with the License. Use may include creating any content with, finetuning, updating, running, training, evaluating and/or reparametrizing the Model. You shall require all of Your users who use the Model or a Derivative of the Model to comply with the terms of this paragraph (paragraph 5).
|
||||
6. The Output You Generate. Except as set forth herein, Licensor claims no rights in the Output You generate using the Model. You are accountable for the Output you generate and its subsequent uses. No use of the output can contravene any provision as stated in the License.
|
||||
|
||||
Section IV: OTHER PROVISIONS
|
||||
|
||||
7. Updates and Runtime Restrictions. To the maximum extent permitted by law, Licensor reserves the right to restrict (remotely or otherwise) usage of the Model in violation of this License, update the Model through electronic means, or modify the Output of the Model based on updates. You shall undertake reasonable efforts to use the latest version of the Model.
|
||||
8. Trademarks and related. Nothing in this License permits You to make use of Licensors’ trademarks, trade names, logos or to otherwise suggest endorsement or misrepresent the relationship between the parties; and any rights not expressly granted herein are reserved by the Licensors.
|
||||
9. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Model and the Complementary Material (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Model, Derivatives of the Model, and the Complementary Material and assume any risks associated with Your exercise of permissions under this License.
|
||||
10. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Model and the Complementary Material (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
11. Accepting Warranty or Additional Liability. While redistributing the Model, Derivatives of the Model and the Complementary Material thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
12. If any provision of this License is held to be invalid, illegal or unenforceable, the remaining provisions shall be unaffected thereby and remain valid as if such provision had not been set forth herein.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
|
||||
|
||||
|
||||
Attachment A
|
||||
|
||||
Use Restrictions
|
||||
|
||||
You agree not to use the Model or Derivatives of the Model:
|
||||
- In any way that violates any applicable national, federal, state, local or international law or regulation;
|
||||
- For the purpose of exploiting, harming or attempting to exploit or harm minors in any way;
|
||||
- To generate or disseminate verifiably false information and/or content with the purpose of harming others;
|
||||
- To generate or disseminate personal identifiable information that can be used to harm an individual;
|
||||
- To defame, disparage or otherwise harass others;
|
||||
- For fully automated decision making that adversely impacts an individual’s legal rights or otherwise creates or modifies a binding, enforceable obligation;
|
||||
- For any use intended to or which has the effect of discriminating against or harming individuals or groups based on online or offline social behavior or known or predicted personal or personality characteristics;
|
||||
- To exploit any of the vulnerabilities of a specific group of persons based on their age, social, physical or mental characteristics, in order to materially distort the behavior of a person pertaining to that group in a manner that causes or is likely to cause that person or another person physical or psychological harm;
|
||||
- For any use intended to or which has the effect of discriminating against individuals or groups based on legally protected characteristics or categories;
|
||||
- To provide medical advice and medical results interpretation;
|
||||
- To generate or disseminate information for the purpose to be used for administration of justice, law enforcement, immigration or asylum processes, such as predicting an individual will commit fraud/crime commitment (e.g. by text profiling, drawing causal relationships between assertions made in documents, indiscriminate and arbitrarily-targeted use).
|
59
lib/diffuser/python/README.md
Normal file
59
lib/diffuser/python/README.md
Normal file
@@ -0,0 +1,59 @@
|
||||
# stable_diffusion.openvino
|
||||
|
||||
Implementation of Text-To-Image generation using Stable Diffusion on Intel CPU.
|
||||
<p align="center">
|
||||
<img src="data/title.png"/>
|
||||
</p>
|
||||
|
||||
## Requirements
|
||||
|
||||
* Linux, Windows, MacOS
|
||||
* Python 3.8.+
|
||||
* CPU compatible with OpenVINO.
|
||||
|
||||
## Install requirements
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## Generate image from text description
|
||||
|
||||
```bash
|
||||
usage: stable_diffusion.py [-h] [--models-cfg MODELS_CFG] [--beta-start BETA_START] [--beta-end BETA_END] [--beta-schedule BETA_SCHEDULE] [--num-inference-steps NUM_INFERENCE_STEPS]
|
||||
[--guidance-scale GUIDANCE_SCALE] [--eta ETA] [--tokenizer TOKENIZER] [--prompt PROMPT] [--output OUTPUT]
|
||||
|
||||
optional arguments:
|
||||
-h, --help show this help message and exit
|
||||
--models-cfg MODELS_CFG
|
||||
path to models config
|
||||
--beta-start BETA_START
|
||||
LMSDiscreteScheduler::beta_start
|
||||
--beta-end BETA_END LMSDiscreteScheduler::beta_end
|
||||
--beta-schedule BETA_SCHEDULE
|
||||
LMSDiscreteScheduler::beta_schedule
|
||||
--num-inference-steps NUM_INFERENCE_STEPS
|
||||
num inference steps
|
||||
--guidance-scale GUIDANCE_SCALE
|
||||
guidance scale
|
||||
--eta ETA eta
|
||||
--tokenizer TOKENIZER
|
||||
tokenizer
|
||||
--prompt PROMPT prompt
|
||||
--output OUTPUT output image name
|
||||
```
|
||||
|
||||
### Example
|
||||
```bash
|
||||
python stable_diffusion.py --prompt "Street-art painting of Emilia Clarke in style of Banksy, photorealism"
|
||||
```
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
* Original implementation of Stable Diffusion: https://github.com/CompVis/stable-diffusion
|
||||
* diffusers library: https://github.com/huggingface/diffusers
|
||||
|
||||
## Disclaimer
|
||||
|
||||
The authors are not responsible for the content generated using this project.
|
||||
Please, don't use this project to produce illegal, harmful, offensive etc. content.
|
0
lib/diffuser/python/__init__.py
Normal file
0
lib/diffuser/python/__init__.py
Normal file
26
lib/diffuser/python/data/models.json
Normal file
26
lib/diffuser/python/data/models.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"text_encoder.xml": {
|
||||
"url": "https://drive.google.com/uc?id=1VXPpeieEkFbSkxbmga32DebTsf8E8NN4",
|
||||
"md5": "d5c6dd1e3ea5b8412b4434863877dfc9"
|
||||
},
|
||||
"text_encoder.bin": {
|
||||
"url": "https://drive.google.com/uc?id=1r09rTco5FtIP0IPzJTompAN8YAu2doGG",
|
||||
"md5": "ba70816e82b49b33ca292d46a787975f"
|
||||
},
|
||||
"unet.xml": {
|
||||
"url": "https://drive.google.com/uc?id=1md8Eur0AUWyEciWO-NCQ5gqpsHC7p_gD",
|
||||
"md5": "2d9e95da71447a81e07559de8b6df6f3"
|
||||
},
|
||||
"unet.bin": {
|
||||
"url": "https://drive.google.com/uc?id=1aCEXRLPK7vjABaD4ZC-SGtpT5kgyTcMo",
|
||||
"md5": "b0ffbc118ed32532172ad60bb1cc2a71"
|
||||
},
|
||||
"vae.xml": {
|
||||
"url": "https://drive.google.com/uc?id=1MblgqTMooC6N5jq5s_69k02G4evRlST1",
|
||||
"md5": "281704d317c33b06e96bb54b2d546efe"
|
||||
},
|
||||
"vae.bin": {
|
||||
"url": "https://drive.google.com/uc?id=1g9cFqb0qcr4nEBrLk_mlWBl5Lu_ggIfA",
|
||||
"md5": "15c7db7f79059553b7d8381815347c03"
|
||||
}
|
||||
}
|
BIN
lib/diffuser/python/data/title.png
Normal file
BIN
lib/diffuser/python/data/title.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 484 KiB |
8
lib/diffuser/python/requirements.txt
Normal file
8
lib/diffuser/python/requirements.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
numpy==1.19.5
|
||||
opencv-python==4.5.5.64
|
||||
transformers==4.16.2
|
||||
diffusers==0.2.4
|
||||
tqdm==4.64.0
|
||||
openvino==2022.1.0
|
||||
huggingface_hub==0.9.0
|
||||
scipy==1.9.0
|
173
lib/diffuser/python/stable_diffusion.py
Normal file
173
lib/diffuser/python/stable_diffusion.py
Normal file
@@ -0,0 +1,173 @@
|
||||
import argparse
|
||||
import os
|
||||
import inspect
|
||||
import numpy as np
|
||||
# openvino
|
||||
from openvino.runtime import Core
|
||||
# tokenizer
|
||||
from transformers import CLIPTokenizer
|
||||
# scheduler
|
||||
from diffusers import LMSDiscreteScheduler
|
||||
# utils
|
||||
from tqdm import tqdm
|
||||
import cv2
|
||||
from huggingface_hub import hf_hub_download
|
||||
|
||||
|
||||
class StableDiffusion:
|
||||
def __init__(
|
||||
self,
|
||||
scheduler,
|
||||
model="bes-dev/stable-diffusion-v1-4-openvino",
|
||||
tokenizer="openai/clip-vit-large-patch14",
|
||||
device="CPU"
|
||||
):
|
||||
self.tokenizer = CLIPTokenizer.from_pretrained(tokenizer)
|
||||
self.scheduler = scheduler
|
||||
# models
|
||||
self.core = Core()
|
||||
# text features
|
||||
self._text_encoder = self.core.read_model(
|
||||
hf_hub_download(repo_id=model, filename="text_encoder.xml"),
|
||||
hf_hub_download(repo_id=model, filename="text_encoder.bin")
|
||||
)
|
||||
self.text_encoder = self.core.compile_model(self._text_encoder, device)
|
||||
# diffusion
|
||||
self._unet = self.core.read_model(
|
||||
hf_hub_download(repo_id=model, filename="unet.xml"),
|
||||
hf_hub_download(repo_id=model, filename="unet.bin")
|
||||
)
|
||||
self.unet = self.core.compile_model(self._unet, device)
|
||||
self.latent_shape = tuple(self._unet.inputs[0].shape)[1:]
|
||||
# decoder
|
||||
self._vae = self.core.read_model(
|
||||
hf_hub_download(repo_id=model, filename="vae.xml"),
|
||||
hf_hub_download(repo_id=model, filename="vae.bin")
|
||||
)
|
||||
self.vae = self.core.compile_model(self._vae, device)
|
||||
|
||||
def __call__(self, prompt, num_inference_steps = 32, guidance_scale = 7.5, eta = 0.0):
|
||||
result = lambda var: next(iter(var.values()))
|
||||
|
||||
# extract condition
|
||||
tokens = self.tokenizer(
|
||||
prompt,
|
||||
padding="max_length",
|
||||
max_length=self.tokenizer.model_max_length,
|
||||
truncation=True
|
||||
).input_ids
|
||||
text_embeddings = result(
|
||||
self.text_encoder.infer_new_request({"tokens": np.array([tokens])})
|
||||
)
|
||||
|
||||
# do classifier free guidance
|
||||
if guidance_scale > 1.0:
|
||||
tokens_uncond = self.tokenizer(
|
||||
"",
|
||||
padding="max_length",
|
||||
max_length=self.tokenizer.model_max_length,
|
||||
truncation=True
|
||||
).input_ids
|
||||
uncond_embeddings = result(
|
||||
self.text_encoder.infer_new_request({"tokens": np.array([tokens_uncond])})
|
||||
)
|
||||
text_embeddings = np.concatenate((uncond_embeddings, text_embeddings), axis=0)
|
||||
|
||||
# make noise
|
||||
latents = np.random.randn(*self.latent_shape)
|
||||
|
||||
# set timesteps
|
||||
accepts_offset = "offset" in set(inspect.signature(self.scheduler.set_timesteps).parameters.keys())
|
||||
extra_set_kwargs = {}
|
||||
if accepts_offset:
|
||||
extra_set_kwargs["offset"] = 1
|
||||
|
||||
self.scheduler.set_timesteps(num_inference_steps, **extra_set_kwargs)
|
||||
|
||||
# if we use LMSDiscreteScheduler, let's make sure latents are mulitplied by sigmas
|
||||
if isinstance(self.scheduler, LMSDiscreteScheduler):
|
||||
latents = latents * self.scheduler.sigmas[0]
|
||||
|
||||
# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
|
||||
# eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
|
||||
# eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
|
||||
# and should be between [0, 1]
|
||||
accepts_eta = "eta" in set(inspect.signature(self.scheduler.step).parameters.keys())
|
||||
extra_step_kwargs = {}
|
||||
if accepts_eta:
|
||||
extra_step_kwargs["eta"] = eta
|
||||
|
||||
for i, t in tqdm(enumerate(self.scheduler.timesteps)):
|
||||
# expand the latents if we are doing classifier free guidance
|
||||
latent_model_input = np.stack([latents, latents], 0) if guidance_scale > 1.0 else latents
|
||||
if isinstance(self.scheduler, LMSDiscreteScheduler):
|
||||
sigma = self.scheduler.sigmas[i]
|
||||
latent_model_input = latent_model_input / ((sigma**2 + 1) ** 0.5)
|
||||
|
||||
# predict the noise residual
|
||||
noise_pred = result(self.unet.infer_new_request({
|
||||
"latent_model_input": latent_model_input,
|
||||
"t": t,
|
||||
"encoder_hidden_states": text_embeddings
|
||||
}))
|
||||
|
||||
# perform guidance
|
||||
if guidance_scale > 1.0:
|
||||
noise_pred = noise_pred[0] + guidance_scale * (noise_pred[1] - noise_pred[0])
|
||||
|
||||
# compute the previous noisy sample x_t -> x_t-1
|
||||
if isinstance(self.scheduler, LMSDiscreteScheduler):
|
||||
latents = self.scheduler.step(noise_pred, i, latents, **extra_step_kwargs)["prev_sample"]
|
||||
else:
|
||||
latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs)["prev_sample"]
|
||||
|
||||
image = result(self.vae.infer_new_request({
|
||||
"latents": np.expand_dims(latents, 0)
|
||||
}))
|
||||
|
||||
# convert tensor to opencv's image format
|
||||
image = (image / 2 + 0.5).clip(0, 1)
|
||||
image = (image[0].transpose(1, 2, 0)[:, :, ::-1] * 255).astype(np.uint8)
|
||||
return image
|
||||
|
||||
def main(args):
|
||||
scheduler = LMSDiscreteScheduler(
|
||||
beta_start=args.beta_start,
|
||||
beta_end=args.beta_end,
|
||||
beta_schedule=args.beta_schedule,
|
||||
tensor_format="np"
|
||||
)
|
||||
stable_diffusion = StableDiffusion(
|
||||
model = args.model,
|
||||
scheduler = scheduler,
|
||||
tokenizer = args.tokenizer
|
||||
)
|
||||
image = stable_diffusion(
|
||||
prompt = args.prompt,
|
||||
num_inference_steps = args.num_inference_steps,
|
||||
guidance_scale = args.guidance_scale,
|
||||
eta = args.eta
|
||||
)
|
||||
cv2.imwrite(args.output, image)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
# pipeline configure
|
||||
parser.add_argument("--model", type=str, default="bes-dev/stable-diffusion-v1-4-openvino", help="model name")
|
||||
# scheduler params
|
||||
parser.add_argument("--beta-start", type=float, default=0.00085, help="LMSDiscreteScheduler::beta_start")
|
||||
parser.add_argument("--beta-end", type=float, default=0.012, help="LMSDiscreteScheduler::beta_end")
|
||||
parser.add_argument("--beta-schedule", type=str, default="scaled_linear", help="LMSDiscreteScheduler::beta_schedule")
|
||||
# diffusion params
|
||||
parser.add_argument("--num-inference-steps", type=int, default=32, help="num inference steps")
|
||||
parser.add_argument("--guidance-scale", type=float, default=7.5, help="guidance scale")
|
||||
parser.add_argument("--eta", type=float, default=0.0, help="eta")
|
||||
# tokenizer
|
||||
parser.add_argument("--tokenizer", type=str, default="openai/clip-vit-large-patch14", help="tokenizer")
|
||||
# prompt
|
||||
parser.add_argument("--prompt", type=str, default="Street-art painting of Emilia Clarke in style of Banksy, photorealism", help="prompt")
|
||||
# output name
|
||||
parser.add_argument("--output", type=str, default="output.png", help="output image name")
|
||||
args = parser.parse_args()
|
||||
main(args)
|
17
lib/diffuser/python/test_script.py
Normal file
17
lib/diffuser/python/test_script.py
Normal file
@@ -0,0 +1,17 @@
|
||||
import time
|
||||
import asyncio
|
||||
from stable_diffusion import StableDiffusion
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("starting")
|
||||
|
||||
def test_func(args):
|
||||
i = 0
|
||||
print("running diffuser")
|
||||
# asyncio.create_task(StableDiffusion(args))
|
||||
StableDiffusion(args)
|
||||
print("done running")
|
||||
# while i<3:
|
||||
# print("hi")
|
||||
# time.sleep(1)
|
||||
# i = i + 1
|
19
lib/diffuser/python_helper.ex
Normal file
19
lib/diffuser/python_helper.ex
Normal file
@@ -0,0 +1,19 @@
|
||||
defmodule Diffuser.PythonHelper do
|
||||
@moduledoc false
|
||||
|
||||
def py_instance(path) when is_binary(path) do
|
||||
# pybinpath = '/path/to/Anaconda/Anaconda3/bin/python'
|
||||
# :python.start([python_path: pypath, python: pybinpath])
|
||||
{:ok, pid} = :python.start([{:python_path, to_charlist(path)}])
|
||||
pid
|
||||
end
|
||||
|
||||
def py_call(pid, module, func, args \\ []) do
|
||||
pid
|
||||
|> :python.call(module, func, args)
|
||||
end
|
||||
|
||||
def py_stop(pid) do
|
||||
:python.stop(pid)
|
||||
end
|
||||
end
|
5
lib/diffuser/repo.ex
Normal file
5
lib/diffuser/repo.ex
Normal file
@@ -0,0 +1,5 @@
|
||||
defmodule Diffuser.Repo do
|
||||
use Ecto.Repo,
|
||||
otp_app: :diffuser,
|
||||
adapter: Ecto.Adapters.Postgres
|
||||
end
|
111
lib/diffuser_web.ex
Normal file
111
lib/diffuser_web.ex
Normal file
@@ -0,0 +1,111 @@
|
||||
defmodule DiffuserWeb do
|
||||
@moduledoc """
|
||||
The entrypoint for defining your web interface, such
|
||||
as controllers, views, channels and so on.
|
||||
|
||||
This can be used in your application as:
|
||||
|
||||
use DiffuserWeb, :controller
|
||||
use DiffuserWeb, :view
|
||||
|
||||
The definitions below will be executed for every view,
|
||||
controller, etc, so keep them short and clean, focused
|
||||
on imports, uses and aliases.
|
||||
|
||||
Do NOT define functions inside the quoted expressions
|
||||
below. Instead, define any helper function in modules
|
||||
and import those modules here.
|
||||
"""
|
||||
|
||||
def controller do
|
||||
quote do
|
||||
use Phoenix.Controller, namespace: DiffuserWeb
|
||||
|
||||
import Plug.Conn
|
||||
import DiffuserWeb.Gettext
|
||||
alias DiffuserWeb.Router.Helpers, as: Routes
|
||||
end
|
||||
end
|
||||
|
||||
def view do
|
||||
quote do
|
||||
use Phoenix.View,
|
||||
root: "lib/diffuser_web/templates",
|
||||
namespace: DiffuserWeb
|
||||
|
||||
# Import convenience functions from controllers
|
||||
import Phoenix.Controller,
|
||||
only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1]
|
||||
|
||||
# Include shared imports and aliases for views
|
||||
unquote(view_helpers())
|
||||
end
|
||||
end
|
||||
|
||||
def live_view do
|
||||
quote do
|
||||
use Phoenix.LiveView,
|
||||
layout: {DiffuserWeb.LayoutView, "live.html"}
|
||||
|
||||
unquote(view_helpers())
|
||||
end
|
||||
end
|
||||
|
||||
def live_component do
|
||||
quote do
|
||||
use Phoenix.LiveComponent
|
||||
|
||||
unquote(view_helpers())
|
||||
end
|
||||
end
|
||||
|
||||
def component do
|
||||
quote do
|
||||
use Phoenix.Component
|
||||
|
||||
unquote(view_helpers())
|
||||
end
|
||||
end
|
||||
|
||||
def router do
|
||||
quote do
|
||||
use Phoenix.Router
|
||||
|
||||
import Plug.Conn
|
||||
import Phoenix.Controller
|
||||
import Phoenix.LiveView.Router
|
||||
end
|
||||
end
|
||||
|
||||
def channel do
|
||||
quote do
|
||||
use Phoenix.Channel
|
||||
import DiffuserWeb.Gettext
|
||||
end
|
||||
end
|
||||
|
||||
defp view_helpers do
|
||||
quote do
|
||||
# Use all HTML functionality (forms, tags, etc)
|
||||
use Phoenix.HTML
|
||||
|
||||
# Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc)
|
||||
import Phoenix.LiveView.Helpers
|
||||
import DiffuserWeb.LiveHelpers
|
||||
|
||||
# Import basic rendering functionality (render, render_layout, etc)
|
||||
import Phoenix.View
|
||||
|
||||
import DiffuserWeb.ErrorHelpers
|
||||
import DiffuserWeb.Gettext
|
||||
alias DiffuserWeb.Router.Helpers, as: Routes
|
||||
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
|
8
lib/diffuser_web/channels/request_channel.ex
Normal file
8
lib/diffuser_web/channels/request_channel.ex
Normal file
@@ -0,0 +1,8 @@
|
||||
defmodule DiffuserWeb.RequestChannel do
|
||||
use Phoenix.Channel
|
||||
|
||||
def join("request:" <> room_id, _message, socket) do
|
||||
IO.inspect(room_id)
|
||||
{:ok, socket}
|
||||
end
|
||||
end
|
46
lib/diffuser_web/channels/request_socket.ex
Normal file
46
lib/diffuser_web/channels/request_socket.ex
Normal file
@@ -0,0 +1,46 @@
|
||||
defmodule DiffuserWeb.RequestSocket do
|
||||
use Phoenix.Socket
|
||||
|
||||
# A Socket handler
|
||||
#
|
||||
# It's possible to control the websocket connection and
|
||||
# assign values that can be accessed by your channel topics.
|
||||
|
||||
channel "request:*", DiffuserWeb.RequestChannel
|
||||
#
|
||||
# To create a channel file, use the mix task:
|
||||
#
|
||||
# mix phx.gen.channel Room
|
||||
#
|
||||
# See the [`Channels guide`](https://hexdocs.pm/phoenix/channels.html)
|
||||
# for further details.
|
||||
|
||||
# Socket params are passed from the client and can
|
||||
# be used to verify and authenticate a user. After
|
||||
# verification, you can put default assigns into
|
||||
# the socket that will be set for all channels, ie
|
||||
#
|
||||
# {:ok, assign(socket, :user_id, verified_user_id)}
|
||||
#
|
||||
# To deny connection, return `:error`.
|
||||
#
|
||||
# See `Phoenix.Token` documentation for examples in
|
||||
# performing token verification on connect.
|
||||
@impl true
|
||||
def connect(_params, socket, _connect_info) do
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
# Socket id's are topics that allow you to identify all sockets for a given user:
|
||||
#
|
||||
# def id(socket), do: "user_socket:#{socket.assigns.user_id}"
|
||||
#
|
||||
# Would allow you to broadcast a "disconnect" event and terminate
|
||||
# all active sockets and channels for a given user:
|
||||
#
|
||||
# Elixir.DiffuserWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
|
||||
#
|
||||
# Returning `nil` makes this socket anonymous.
|
||||
@impl true
|
||||
def id(_socket), do: nil
|
||||
end
|
7
lib/diffuser_web/controllers/page_controller.ex
Normal file
7
lib/diffuser_web/controllers/page_controller.ex
Normal file
@@ -0,0 +1,7 @@
|
||||
defmodule DiffuserWeb.PageController do
|
||||
use DiffuserWeb, :controller
|
||||
|
||||
def index(conn, _params) do
|
||||
render(conn, "index.html")
|
||||
end
|
||||
end
|
56
lib/diffuser_web/endpoint.ex
Normal file
56
lib/diffuser_web/endpoint.ex
Normal file
@@ -0,0 +1,56 @@
|
||||
defmodule DiffuserWeb.Endpoint do
|
||||
use Phoenix.Endpoint, otp_app: :diffuser
|
||||
|
||||
# The session will be stored in the cookie and signed,
|
||||
# this means its contents can be read but not tampered with.
|
||||
# Set :encryption_salt if you would also like to encrypt it.
|
||||
@session_options [
|
||||
store: :cookie,
|
||||
key: "_diffuser_key",
|
||||
signing_salt: "NZfusVTj"
|
||||
]
|
||||
|
||||
socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]
|
||||
|
||||
socket "/socket", DiffuserWeb.RequestSocket,
|
||||
websocket: true,
|
||||
longpoll: false
|
||||
|
||||
# Serve at "/" the static files from "priv/static" directory.
|
||||
#
|
||||
# You should set gzip to true if you are running phx.digest
|
||||
# when deploying your static files in production.
|
||||
plug Plug.Static,
|
||||
at: "/",
|
||||
from: :diffuser,
|
||||
gzip: false,
|
||||
only: ~w(assets fonts images favicon.ico robots.txt)
|
||||
|
||||
plug Plug.Static, at: "/uploads", from: Path.expand('./uploads'), gzip: false
|
||||
|
||||
# Code reloading can be explicitly enabled under the
|
||||
# :code_reloader configuration of your endpoint.
|
||||
if code_reloading? do
|
||||
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
|
||||
plug Phoenix.LiveReloader
|
||||
plug Phoenix.CodeReloader
|
||||
plug Phoenix.Ecto.CheckRepoStatus, otp_app: :diffuser
|
||||
end
|
||||
|
||||
plug Phoenix.LiveDashboard.RequestLogger,
|
||||
param_key: "request_logger",
|
||||
cookie_key: "request_logger"
|
||||
|
||||
plug Plug.RequestId
|
||||
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
|
||||
|
||||
plug Plug.Parsers,
|
||||
parsers: [:urlencoded, :multipart, :json],
|
||||
pass: ["*/*"],
|
||||
json_decoder: Phoenix.json_library()
|
||||
|
||||
plug Plug.MethodOverride
|
||||
plug Plug.Head
|
||||
plug Plug.Session, @session_options
|
||||
plug DiffuserWeb.Router
|
||||
end
|
24
lib/diffuser_web/gettext.ex
Normal file
24
lib/diffuser_web/gettext.ex
Normal file
@@ -0,0 +1,24 @@
|
||||
defmodule DiffuserWeb.Gettext do
|
||||
@moduledoc """
|
||||
A module providing Internationalization with a gettext-based API.
|
||||
|
||||
By using [Gettext](https://hexdocs.pm/gettext),
|
||||
your module gains a set of macros for translations, for example:
|
||||
|
||||
import DiffuserWeb.Gettext
|
||||
|
||||
# Simple translation
|
||||
gettext("Here is the string to translate")
|
||||
|
||||
# Plural translation
|
||||
ngettext("Here is the string to translate",
|
||||
"Here are the strings to translate",
|
||||
3)
|
||||
|
||||
# Domain-based translation
|
||||
dgettext("errors", "Here is the error message to translate")
|
||||
|
||||
See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
|
||||
"""
|
||||
use Gettext, otp_app: :diffuser
|
||||
end
|
60
lib/diffuser_web/live/live_helpers.ex
Normal file
60
lib/diffuser_web/live/live_helpers.ex
Normal file
@@ -0,0 +1,60 @@
|
||||
defmodule DiffuserWeb.LiveHelpers do
|
||||
import Phoenix.LiveView
|
||||
import Phoenix.LiveView.Helpers
|
||||
|
||||
alias Phoenix.LiveView.JS
|
||||
|
||||
@doc """
|
||||
Renders a live component inside a modal.
|
||||
|
||||
The rendered modal receives a `:return_to` option to properly update
|
||||
the URL when the modal is closed.
|
||||
|
||||
## Examples
|
||||
|
||||
<.modal return_to={Routes.prompt_request_index_path(@socket, :index)}>
|
||||
<.live_component
|
||||
module={DiffuserWeb.PromptRequestLive.FormComponent}
|
||||
id={@prompt_request.id || :new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
return_to={Routes.prompt_request_index_path(@socket, :index)}
|
||||
prompt_request: @prompt_request
|
||||
/>
|
||||
</.modal>
|
||||
"""
|
||||
def modal(assigns) do
|
||||
assigns = assign_new(assigns, :return_to, fn -> nil end)
|
||||
|
||||
~H"""
|
||||
<div id="modal" class="phx-modal fade-in" phx-remove={hide_modal()}>
|
||||
<div
|
||||
id="modal-content"
|
||||
class="phx-modal-content fade-in-scale"
|
||||
phx-click-away={JS.dispatch("click", to: "#close")}
|
||||
phx-window-keydown={JS.dispatch("click", to: "#close")}
|
||||
phx-key="escape"
|
||||
>
|
||||
<%= if @return_to do %>
|
||||
<%= live_patch "✖",
|
||||
to: @return_to,
|
||||
id: "close",
|
||||
class: "phx-modal-close",
|
||||
phx_click: hide_modal()
|
||||
%>
|
||||
<% else %>
|
||||
<a id="close" href="#" class="phx-modal-close" phx-click={hide_modal()}>✖</a>
|
||||
<% end %>
|
||||
|
||||
<%= render_slot(@inner_block) %>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
defp hide_modal(js \\ %JS{}) do
|
||||
js
|
||||
|> JS.hide(to: "#modal", transition: "fade-out")
|
||||
|> JS.hide(to: "#modal-content", transition: "fade-out-scale")
|
||||
end
|
||||
end
|
57
lib/diffuser_web/live/prompt_request_live/form_component.ex
Normal file
57
lib/diffuser_web/live/prompt_request_live/form_component.ex
Normal file
@@ -0,0 +1,57 @@
|
||||
defmodule DiffuserWeb.PromptRequestLive.FormComponent do
|
||||
use DiffuserWeb, :live_component
|
||||
|
||||
alias Diffuser.Generator
|
||||
alias Diffuser.Generator.PromptRequest
|
||||
|
||||
@impl true
|
||||
def update(%{prompt_request: prompt_request} = assigns, socket) do
|
||||
changeset = Generator.change_prompt_request(prompt_request)
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign(:changeset, changeset)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"prompt_request" => prompt_request_params}, socket) do
|
||||
changeset =
|
||||
socket.assigns.prompt_request
|
||||
|> Generator.change_prompt_request(prompt_request_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"prompt_request" => prompt_request_params}, socket) do
|
||||
save_prompt_request(socket, socket.assigns.action, prompt_request_params)
|
||||
end
|
||||
|
||||
defp save_prompt_request(socket, :edit, prompt_request_params) do
|
||||
case Generator.update_prompt_request(socket.assigns.prompt_request, prompt_request_params) do
|
||||
{:ok, %PromptRequest{} = prompt_request} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Prompt request updated successfully")
|
||||
|> push_redirect(to: Routes.prompt_request_show_path(socket, :show, prompt_request))}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_prompt_request(socket, :new, prompt_request_params) do
|
||||
with {:ok, prompt_request} <- Generator.create_prompt_request(prompt_request_params),
|
||||
{:ok, _pid} <-
|
||||
Diffuser.Generator.PromptRequestSupervisor.start_prompt_request(prompt_request) do
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Prompt request created successfully")
|
||||
|> push_redirect(to: Routes.prompt_request_show_path(socket, :show, prompt_request))}
|
||||
else
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, changeset: changeset)}
|
||||
end
|
||||
end
|
||||
end
|
@@ -0,0 +1,20 @@
|
||||
<div>
|
||||
<h2><%= @title %></h2>
|
||||
|
||||
<.form
|
||||
let={f}
|
||||
for={@changeset}
|
||||
id="prompt_request-form"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save">
|
||||
|
||||
<%= label f, :prompt %>
|
||||
<%= text_input f, :prompt %>
|
||||
<%= error_tag f, :prompt %>
|
||||
|
||||
<div>
|
||||
<%= submit "Save", phx_disable_with: "Saving..." %>
|
||||
</div>
|
||||
</.form>
|
||||
</div>
|
46
lib/diffuser_web/live/prompt_request_live/index.ex
Normal file
46
lib/diffuser_web/live/prompt_request_live/index.ex
Normal file
@@ -0,0 +1,46 @@
|
||||
defmodule DiffuserWeb.PromptRequestLive.Index do
|
||||
use DiffuserWeb, :live_view
|
||||
|
||||
alias Diffuser.Generator
|
||||
alias Diffuser.Generator.PromptRequest
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, assign(socket, :prompt_requests, list_prompt_requests())}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(params, _url, socket) do
|
||||
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
||||
end
|
||||
|
||||
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||
socket
|
||||
|> assign(:page_title, "Edit Prompt request")
|
||||
|> assign(:prompt_request, Generator.get_prompt_request!(id))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "New Prompt request")
|
||||
|> assign(:prompt_request, %PromptRequest{})
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Listing Prompt requests")
|
||||
|> assign(:prompt_request, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
prompt_request = Generator.get_prompt_request!(id)
|
||||
{:ok, _} = Generator.delete_prompt_request(prompt_request)
|
||||
|
||||
{:noreply, assign(socket, :prompt_requests, list_prompt_requests())}
|
||||
end
|
||||
|
||||
defp list_prompt_requests do
|
||||
Generator.list_prompt_requests()
|
||||
end
|
||||
end
|
38
lib/diffuser_web/live/prompt_request_live/index.html.heex
Normal file
38
lib/diffuser_web/live/prompt_request_live/index.html.heex
Normal file
@@ -0,0 +1,38 @@
|
||||
<h1>Listing Prompt requests</h1>
|
||||
|
||||
<%= if @live_action in [:new, :edit] do %>
|
||||
<.modal return_to={Routes.prompt_request_index_path(@socket, :index)}>
|
||||
<.live_component
|
||||
module={DiffuserWeb.PromptRequestLive.FormComponent}
|
||||
id={@prompt_request.id || :new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
prompt_request={@prompt_request}}
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Prompt</th>
|
||||
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="prompt_requests">
|
||||
<%= for prompt_request <- @prompt_requests do %>
|
||||
<tr id={"prompt_request-#{prompt_request.id}"}>
|
||||
<td><%= prompt_request.prompt %></td>
|
||||
|
||||
<td>
|
||||
<span><%= live_redirect "Show", to: Routes.prompt_request_show_path(@socket, :show, prompt_request) %></span>
|
||||
<span><%= live_patch "Edit", to: Routes.prompt_request_index_path(@socket, :edit, prompt_request) %></span>
|
||||
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: prompt_request.id, data: [confirm: "Are you sure?"] %></span>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<span><%= live_patch "New Prompt request", to: Routes.prompt_request_index_path(@socket, :new) %></span>
|
43
lib/diffuser_web/live/prompt_request_live/show.ex
Normal file
43
lib/diffuser_web/live/prompt_request_live/show.ex
Normal file
@@ -0,0 +1,43 @@
|
||||
defmodule DiffuserWeb.PromptRequestLive.Show do
|
||||
use DiffuserWeb, :live_view
|
||||
import Ecto.Query
|
||||
|
||||
alias Diffuser.Generator
|
||||
alias Diffuser.Generator.{Image, PromptRequest}
|
||||
alias Diffuser.Repo
|
||||
alias Phoenix.Socket.Broadcast
|
||||
alias DiffuserWeb.Endpoint
|
||||
|
||||
@impl true
|
||||
def mount(%{"id" => id}, _session, socket) do
|
||||
Endpoint.subscribe("request:#{id}")
|
||||
|
||||
{:ok, socket |> assign(:prompt_request, Generator.get_prompt_request!(id))}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, socket) do
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:page_title, page_title(socket.assigns.live_action))
|
||||
|> assign(:prompt_request, Generator.get_prompt_request!(id))}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Show Prompt request"
|
||||
defp page_title(:edit), do: "Edit Prompt request"
|
||||
|
||||
@impl true
|
||||
def handle_info(
|
||||
%Broadcast{
|
||||
topic: _,
|
||||
event: "request",
|
||||
payload: %{prompt_request: %PromptRequest{} = prompt_request}
|
||||
},
|
||||
socket
|
||||
),
|
||||
do:
|
||||
{:noreply,
|
||||
assign(socket, %{
|
||||
prompt_request: prompt_request
|
||||
})}
|
||||
end
|
38
lib/diffuser_web/live/prompt_request_live/show.html.heex
Normal file
38
lib/diffuser_web/live/prompt_request_live/show.html.heex
Normal file
@@ -0,0 +1,38 @@
|
||||
<h1>Show Prompt request</h1>
|
||||
|
||||
<%= if @live_action in [:edit] do %>
|
||||
<.modal return_to={Routes.prompt_request_show_path(@socket, :show, @prompt_request)}>
|
||||
<.live_component
|
||||
module={DiffuserWeb.PromptRequestLive.FormComponent}
|
||||
id={@prompt_request.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
prompt_request={@prompt_request}
|
||||
return_to={Routes.prompt_request_show_path(@socket, :show, @prompt_request)}
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
||||
<ul>
|
||||
|
||||
<li>
|
||||
<strong>Prompt:</strong>
|
||||
<%= @prompt_request.prompt %>
|
||||
</li>
|
||||
<li>
|
||||
<strong>Status:</strong>
|
||||
<%= @prompt_request.status %>
|
||||
</li>
|
||||
<li>
|
||||
<strong>Images:</strong>
|
||||
<%= if @prompt_request.images do %>
|
||||
<%= for result <- @prompt_request.images do %>
|
||||
<img src={"#{Diffuser.Uploaders.Image.url({result.image, result})}"} />
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<span><%= live_patch "Edit", to: Routes.prompt_request_show_path(@socket, :edit, @prompt_request), class: "button" %></span> |
|
||||
<span><%= live_redirect "Back", to: Routes.prompt_request_index_path(@socket, :index) %></span>
|
63
lib/diffuser_web/router.ex
Normal file
63
lib/diffuser_web/router.ex
Normal file
@@ -0,0 +1,63 @@
|
||||
defmodule DiffuserWeb.Router do
|
||||
use DiffuserWeb, :router
|
||||
|
||||
pipeline :browser do
|
||||
plug :accepts, ["html"]
|
||||
plug :fetch_session
|
||||
plug :fetch_live_flash
|
||||
plug :put_root_layout, {DiffuserWeb.LayoutView, :root}
|
||||
plug :protect_from_forgery
|
||||
plug :put_secure_browser_headers
|
||||
end
|
||||
|
||||
pipeline :api do
|
||||
plug :accepts, ["json"]
|
||||
end
|
||||
|
||||
scope "/", DiffuserWeb do
|
||||
pipe_through :browser
|
||||
|
||||
get "/", PageController, :index
|
||||
|
||||
live "/prompt_requests", PromptRequestLive.Index, :index
|
||||
live "/prompt_requests/new", PromptRequestLive.Index, :new
|
||||
live "/prompt_requests/:id/edit", PromptRequestLive.Index, :edit
|
||||
|
||||
live "/prompt_requests/:id", PromptRequestLive.Show, :show
|
||||
live "/prompt_requests/:id/show/edit", PromptRequestLive.Show, :edit
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
# scope "/api", DiffuserWeb do
|
||||
# pipe_through :api
|
||||
# end
|
||||
|
||||
# Enables LiveDashboard only for development
|
||||
#
|
||||
# If you want to use the LiveDashboard in production, you should put
|
||||
# it behind authentication and allow only admins to access it.
|
||||
# If your application does not have an admins-only section yet,
|
||||
# you can use Plug.BasicAuth to set up some basic authentication
|
||||
# as long as you are also using SSL (which you should anyway).
|
||||
if Mix.env() in [:dev, :test] do
|
||||
import Phoenix.LiveDashboard.Router
|
||||
|
||||
scope "/" do
|
||||
pipe_through :browser
|
||||
|
||||
live_dashboard "/dashboard", metrics: DiffuserWeb.Telemetry
|
||||
end
|
||||
end
|
||||
|
||||
# Enables the Swoosh mailbox preview in development.
|
||||
#
|
||||
# Note that preview only shows emails that were sent by the same
|
||||
# node running the Phoenix server.
|
||||
if Mix.env() == :dev do
|
||||
scope "/dev" do
|
||||
pipe_through :browser
|
||||
|
||||
forward "/mailbox", Plug.Swoosh.MailboxPreview
|
||||
end
|
||||
end
|
||||
end
|
71
lib/diffuser_web/telemetry.ex
Normal file
71
lib/diffuser_web/telemetry.ex
Normal file
@@ -0,0 +1,71 @@
|
||||
defmodule DiffuserWeb.Telemetry do
|
||||
use Supervisor
|
||||
import Telemetry.Metrics
|
||||
|
||||
def start_link(arg) do
|
||||
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(_arg) do
|
||||
children = [
|
||||
# Telemetry poller will execute the given period measurements
|
||||
# every 10_000ms. Learn more here: https://hexdocs.pm/telemetry_metrics
|
||||
{:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
|
||||
# Add reporters as children of your supervision tree.
|
||||
# {Telemetry.Metrics.ConsoleReporter, metrics: metrics()}
|
||||
]
|
||||
|
||||
Supervisor.init(children, strategy: :one_for_one)
|
||||
end
|
||||
|
||||
def metrics do
|
||||
[
|
||||
# Phoenix Metrics
|
||||
summary("phoenix.endpoint.stop.duration",
|
||||
unit: {:native, :millisecond}
|
||||
),
|
||||
summary("phoenix.router_dispatch.stop.duration",
|
||||
tags: [:route],
|
||||
unit: {:native, :millisecond}
|
||||
),
|
||||
|
||||
# Database Metrics
|
||||
summary("diffuser.repo.query.total_time",
|
||||
unit: {:native, :millisecond},
|
||||
description: "The sum of the other measurements"
|
||||
),
|
||||
summary("diffuser.repo.query.decode_time",
|
||||
unit: {:native, :millisecond},
|
||||
description: "The time spent decoding the data received from the database"
|
||||
),
|
||||
summary("diffuser.repo.query.query_time",
|
||||
unit: {:native, :millisecond},
|
||||
description: "The time spent executing the query"
|
||||
),
|
||||
summary("diffuser.repo.query.queue_time",
|
||||
unit: {:native, :millisecond},
|
||||
description: "The time spent waiting for a database connection"
|
||||
),
|
||||
summary("diffuser.repo.query.idle_time",
|
||||
unit: {:native, :millisecond},
|
||||
description:
|
||||
"The time the connection spent waiting before being checked out for the query"
|
||||
),
|
||||
|
||||
# VM Metrics
|
||||
summary("vm.memory.total", unit: {:byte, :kilobyte}),
|
||||
summary("vm.total_run_queue_lengths.total"),
|
||||
summary("vm.total_run_queue_lengths.cpu"),
|
||||
summary("vm.total_run_queue_lengths.io")
|
||||
]
|
||||
end
|
||||
|
||||
defp periodic_measurements do
|
||||
[
|
||||
# A module, function and arguments to be invoked periodically.
|
||||
# This function must call :telemetry.execute/3 and a metric must be added above.
|
||||
# {DiffuserWeb, :count_users, []}
|
||||
]
|
||||
end
|
||||
end
|
5
lib/diffuser_web/templates/layout/app.html.heex
Normal file
5
lib/diffuser_web/templates/layout/app.html.heex
Normal file
@@ -0,0 +1,5 @@
|
||||
<main class="container">
|
||||
<p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p>
|
||||
<p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p>
|
||||
<%= @inner_content %>
|
||||
</main>
|
11
lib/diffuser_web/templates/layout/live.html.heex
Normal file
11
lib/diffuser_web/templates/layout/live.html.heex
Normal file
@@ -0,0 +1,11 @@
|
||||
<main class="container">
|
||||
<p class="alert alert-info" role="alert"
|
||||
phx-click="lv:clear-flash"
|
||||
phx-value-key="info"><%= live_flash(@flash, :info) %></p>
|
||||
|
||||
<p class="alert alert-danger" role="alert"
|
||||
phx-click="lv:clear-flash"
|
||||
phx-value-key="error"><%= live_flash(@flash, :error) %></p>
|
||||
|
||||
<%= @inner_content %>
|
||||
</main>
|
30
lib/diffuser_web/templates/layout/root.html.heex
Normal file
30
lib/diffuser_web/templates/layout/root.html.heex
Normal file
@@ -0,0 +1,30 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<meta name="csrf-token" content={csrf_token_value()}>
|
||||
<%= live_title_tag assigns[:page_title] || "Diffuser", suffix: " · Phoenix Framework" %>
|
||||
<link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/assets/app.css")}/>
|
||||
<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js")}></script>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<section class="container">
|
||||
<nav>
|
||||
<ul>
|
||||
<li><a href="https://hexdocs.pm/phoenix/overview.html">Get Started</a></li>
|
||||
<%= if function_exported?(Routes, :live_dashboard_path, 2) do %>
|
||||
<li><%= link "LiveDashboard", to: Routes.live_dashboard_path(@conn, :home) %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</nav>
|
||||
<a href="https://phoenixframework.org/" class="phx-logo">
|
||||
<img src={Routes.static_path(@conn, "/images/phoenix.png")} alt="Phoenix Framework Logo"/>
|
||||
</a>
|
||||
</section>
|
||||
</header>
|
||||
<%= @inner_content %>
|
||||
</body>
|
||||
</html>
|
41
lib/diffuser_web/templates/page/index.html.heex
Normal file
41
lib/diffuser_web/templates/page/index.html.heex
Normal file
@@ -0,0 +1,41 @@
|
||||
<section class="phx-hero">
|
||||
<h1><%= gettext "Welcome to %{name}!", name: "Phoenix" %></h1>
|
||||
<p>Peace of mind from prototype to production</p>
|
||||
</section>
|
||||
|
||||
<section class="row">
|
||||
<article class="column">
|
||||
<h2>Resources</h2>
|
||||
<ul>
|
||||
<li>
|
||||
<a href="https://hexdocs.pm/phoenix/overview.html">Guides & Docs</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="https://github.com/phoenixframework/phoenix">Source</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="https://github.com/phoenixframework/phoenix/blob/v1.6/CHANGELOG.md">v1.6 Changelog</a>
|
||||
</li>
|
||||
</ul>
|
||||
</article>
|
||||
<article class="column">
|
||||
<h2>Help</h2>
|
||||
<ul>
|
||||
<li>
|
||||
<a href="https://elixirforum.com/c/phoenix-forum">Forum</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="https://web.libera.chat/#elixir">#elixir on Libera Chat (IRC)</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="https://twitter.com/elixirphoenix">Twitter @elixirphoenix</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="https://elixir-slackin.herokuapp.com/">Elixir on Slack</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="https://discord.gg/elixir">Elixir on Discord</a>
|
||||
</li>
|
||||
</ul>
|
||||
</article>
|
||||
</section>
|
60
lib/diffuser_web/uploaders/image.ex
Normal file
60
lib/diffuser_web/uploaders/image.ex
Normal file
@@ -0,0 +1,60 @@
|
||||
defmodule Diffuser.Uploaders.Image do
|
||||
use Waffle.Definition
|
||||
use Waffle.Ecto.Definition
|
||||
|
||||
# Include ecto support (requires package waffle_ecto installed):
|
||||
# use Waffle.Ecto.Definition
|
||||
|
||||
@versions [:original]
|
||||
|
||||
# To add a thumbnail version:
|
||||
# @versions [:original, :thumb]
|
||||
|
||||
# Override the bucket on a per definition basis:
|
||||
# def bucket do
|
||||
# :custom_bucket_name
|
||||
# end
|
||||
|
||||
# def bucket({_file, scope}) do
|
||||
# scope.bucket || bucket()
|
||||
# end
|
||||
|
||||
# Whitelist file extensions:
|
||||
# def validate({file, _}) do
|
||||
# file_extension = file.file_name |> Path.extname() |> String.downcase()
|
||||
#
|
||||
# case Enum.member?(~w(.jpg .jpeg .gif .png), file_extension) do
|
||||
# true -> :ok
|
||||
# false -> {:error, "invalid file type"}
|
||||
# end
|
||||
# end
|
||||
|
||||
# Define a thumbnail transformation:
|
||||
# def transform(:thumb, _) do
|
||||
# {:convert, "-strip -thumbnail 250x250^ -gravity center -extent 250x250 -format png", :png}
|
||||
# end
|
||||
|
||||
# Override the persisted filenames:
|
||||
# def filename(version, _) do
|
||||
# version
|
||||
# end
|
||||
|
||||
# Override the storage directory:
|
||||
# def storage_dir(version, {file, scope}) do
|
||||
# "uploads/user/avatars/#{scope.id}"
|
||||
# end
|
||||
|
||||
# Provide a default URL if there hasn't been a file uploaded
|
||||
# def default_url(version, scope) do
|
||||
# "/images/avatars/default_#{version}.png"
|
||||
# end
|
||||
|
||||
# Specify custom headers for s3 objects
|
||||
# Available options are [:cache_control, :content_disposition,
|
||||
# :content_encoding, :content_length, :content_type,
|
||||
# :expect, :expires, :storage_class, :website_redirect_location]
|
||||
#
|
||||
# def s3_object_headers(version, {file, scope}) do
|
||||
# [content_type: MIME.from_path(file.file_name)]
|
||||
# end
|
||||
end
|
47
lib/diffuser_web/views/error_helpers.ex
Normal file
47
lib/diffuser_web/views/error_helpers.ex
Normal file
@@ -0,0 +1,47 @@
|
||||
defmodule DiffuserWeb.ErrorHelpers do
|
||||
@moduledoc """
|
||||
Conveniences for translating and building error messages.
|
||||
"""
|
||||
|
||||
use Phoenix.HTML
|
||||
|
||||
@doc """
|
||||
Generates tag for inlined form input errors.
|
||||
"""
|
||||
def error_tag(form, field) do
|
||||
Enum.map(Keyword.get_values(form.errors, field), fn error ->
|
||||
content_tag(:span, translate_error(error),
|
||||
class: "invalid-feedback",
|
||||
phx_feedback_for: input_name(form, field)
|
||||
)
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Translates an error message using gettext.
|
||||
"""
|
||||
def translate_error({msg, opts}) do
|
||||
# When using gettext, we typically pass the strings we want
|
||||
# to translate as a static argument:
|
||||
#
|
||||
# # Translate "is invalid" in the "errors" domain
|
||||
# dgettext("errors", "is invalid")
|
||||
#
|
||||
# # Translate the number of files with plural rules
|
||||
# dngettext("errors", "1 file", "%{count} files", count)
|
||||
#
|
||||
# Because the error messages we show in our forms and APIs
|
||||
# are defined inside Ecto, we need to translate them dynamically.
|
||||
# This requires us to call the Gettext module passing our gettext
|
||||
# backend as first argument.
|
||||
#
|
||||
# Note we use the "errors" domain, which means translations
|
||||
# should be written to the errors.po file. The :count option is
|
||||
# set by Ecto and indicates we should also apply plural rules.
|
||||
if count = opts[:count] do
|
||||
Gettext.dngettext(DiffuserWeb.Gettext, "errors", msg, msg, count, opts)
|
||||
else
|
||||
Gettext.dgettext(DiffuserWeb.Gettext, "errors", msg, opts)
|
||||
end
|
||||
end
|
||||
end
|
16
lib/diffuser_web/views/error_view.ex
Normal file
16
lib/diffuser_web/views/error_view.ex
Normal file
@@ -0,0 +1,16 @@
|
||||
defmodule DiffuserWeb.ErrorView do
|
||||
use DiffuserWeb, :view
|
||||
|
||||
# If you want to customize a particular status code
|
||||
# for a certain format, you may uncomment below.
|
||||
# def render("500.html", _assigns) do
|
||||
# "Internal Server Error"
|
||||
# end
|
||||
|
||||
# By default, Phoenix returns the status message from
|
||||
# the template name. For example, "404.html" becomes
|
||||
# "Not Found".
|
||||
def template_not_found(template, _assigns) do
|
||||
Phoenix.Controller.status_message_from_template(template)
|
||||
end
|
||||
end
|
7
lib/diffuser_web/views/layout_view.ex
Normal file
7
lib/diffuser_web/views/layout_view.ex
Normal file
@@ -0,0 +1,7 @@
|
||||
defmodule DiffuserWeb.LayoutView do
|
||||
use DiffuserWeb, :view
|
||||
|
||||
# Phoenix LiveDashboard is available only in development by default,
|
||||
# so we instruct Elixir to not warn if the dashboard route is missing.
|
||||
@compile {:no_warn_undefined, {Routes, :live_dashboard_path, 2}}
|
||||
end
|
3
lib/diffuser_web/views/page_view.ex
Normal file
3
lib/diffuser_web/views/page_view.ex
Normal file
@@ -0,0 +1,3 @@
|
||||
defmodule DiffuserWeb.PageView do
|
||||
use DiffuserWeb, :view
|
||||
end
|
Reference in New Issue
Block a user