Add local background removal and mask tools
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -10,6 +10,9 @@ lerna-debug.log*
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
|
||||
# Self-hosted @imgly/background-removal model + wasm assets (fetched on predev/prebuild)
|
||||
public/imgly
|
||||
*.local
|
||||
.env
|
||||
.env.*
|
||||
|
||||
@@ -4,14 +4,19 @@ FROM node:22-alpine AS deps
|
||||
WORKDIR /app
|
||||
RUN corepack enable && corepack prepare pnpm@10.33.2 --activate
|
||||
COPY package.json pnpm-lock.yaml ./
|
||||
RUN pnpm install --frozen-lockfile
|
||||
RUN --mount=type=cache,id=plimi-pnpm-store,target=/root/.local/share/pnpm/store,sharing=locked \
|
||||
pnpm install --frozen-lockfile
|
||||
|
||||
FROM node:22-alpine AS build
|
||||
WORKDIR /app
|
||||
RUN corepack enable && corepack prepare pnpm@10.33.2 --activate
|
||||
COPY --from=deps /app/node_modules ./node_modules
|
||||
COPY . .
|
||||
RUN pnpm build
|
||||
# The IMG.LY model bundle is generated into public/imgly by prebuild. Keeping
|
||||
# that directory in a BuildKit cache avoids downloading ~300 MB after every
|
||||
# source-only redeploy, while Vite still copies the assets into dist/imgly.
|
||||
RUN --mount=type=cache,id=plimi-imgly-assets,target=/app/public/imgly,sharing=locked \
|
||||
pnpm build
|
||||
|
||||
FROM nginx:1.27-alpine AS runtime
|
||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||
|
||||
661
LICENSE
Normal file
661
LICENSE
Normal file
@@ -0,0 +1,661 @@
|
||||
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
Version 3, 19 November 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU Affero General Public License is a free, copyleft license for
|
||||
software and other kinds of works, specifically designed to ensure
|
||||
cooperation with the community in the case of network server software.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
our General Public Licenses are intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
Developers that use our General Public Licenses protect your rights
|
||||
with two steps: (1) assert copyright on the software, and (2) offer
|
||||
you this License which gives you legal permission to copy, distribute
|
||||
and/or modify the software.
|
||||
|
||||
A secondary benefit of defending all users' freedom is that
|
||||
improvements made in alternate versions of the program, if they
|
||||
receive widespread use, become available for other developers to
|
||||
incorporate. Many developers of free software are heartened and
|
||||
encouraged by the resulting cooperation. However, in the case of
|
||||
software used on network servers, this result may fail to come about.
|
||||
The GNU General Public License permits making a modified version and
|
||||
letting the public access it on a server without ever releasing its
|
||||
source code to the public.
|
||||
|
||||
The GNU Affero General Public License is designed specifically to
|
||||
ensure that, in such cases, the modified source code becomes available
|
||||
to the community. It requires the operator of a network server to
|
||||
provide the source code of the modified version running there to the
|
||||
users of that server. Therefore, public use of a modified version, on
|
||||
a publicly accessible server, gives the public access to the source
|
||||
code of the modified version.
|
||||
|
||||
An older license, called the Affero General Public License and
|
||||
published by Affero, was designed to accomplish similar goals. This is
|
||||
a different license, not a version of the Affero GPL, but Affero has
|
||||
released a new version of the Affero GPL which permits relicensing under
|
||||
this license.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, if you modify the
|
||||
Program, your modified version must prominently offer all users
|
||||
interacting with it remotely through a computer network (if your version
|
||||
supports such interaction) an opportunity to receive the Corresponding
|
||||
Source of your version by providing access to the Corresponding Source
|
||||
from a network server at no charge, through some standard or customary
|
||||
means of facilitating copying of software. This Corresponding Source
|
||||
shall include the Corresponding Source for any work covered by version 3
|
||||
of the GNU General Public License that is incorporated pursuant to the
|
||||
following paragraph.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the work with which it is combined will remain governed by version
|
||||
3 of the GNU General Public License.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU Affero General Public License from time to time. Such new versions
|
||||
will be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU Affero General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU Affero General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU Affero General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If your software can interact with users remotely through a computer
|
||||
network, you should also make sure that it provides a way for users to
|
||||
get its source. For example, if your program is a web application, its
|
||||
interface could display a "Source" link that leads users to an archive
|
||||
of the code. There are many ways you could offer source, and different
|
||||
solutions will be better for different programs; see section 13 for the
|
||||
specific requirements.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU AGPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
@@ -8,6 +8,11 @@ server {
|
||||
server_tokens off;
|
||||
access_log off;
|
||||
|
||||
# Enable SharedArrayBuffer so ONNX Runtime can use multithreaded WASM when
|
||||
# WebGPU is unavailable or falls back to CPU execution.
|
||||
add_header Cross-Origin-Opener-Policy "same-origin" always;
|
||||
add_header Cross-Origin-Embedder-Policy "require-corp" always;
|
||||
|
||||
gzip on;
|
||||
gzip_comp_level 6;
|
||||
gzip_min_length 1024;
|
||||
|
||||
@@ -3,8 +3,13 @@
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"license": "AGPL-3.0-only",
|
||||
"author": "Achraf Achkari",
|
||||
"scripts": {
|
||||
"fetch:imgly": "node scripts/fetch-imgly-assets.mjs",
|
||||
"predev": "node scripts/fetch-imgly-assets.mjs",
|
||||
"dev": "vite --host",
|
||||
"prebuild": "node scripts/fetch-imgly-assets.mjs",
|
||||
"build": "tsc -b && vite build",
|
||||
"preview": "vite preview",
|
||||
"test": "vitest",
|
||||
@@ -13,8 +18,10 @@
|
||||
"format": "prettier --write ."
|
||||
},
|
||||
"dependencies": {
|
||||
"@imgly/background-removal": "^1.7.0",
|
||||
"@tailwindcss/vite": "^4.3.0",
|
||||
"fabric": "^7.4.0",
|
||||
"onnxruntime-web": "1.21.0",
|
||||
"pdf-lib": "^1.17.1",
|
||||
"react": "^19.2.6",
|
||||
"react-dom": "^19.2.6",
|
||||
|
||||
186
pnpm-lock.yaml
generated
186
pnpm-lock.yaml
generated
@@ -8,12 +8,21 @@ importers:
|
||||
|
||||
.:
|
||||
dependencies:
|
||||
'@imgly/background-removal':
|
||||
specifier: ^1.7.0
|
||||
version: 1.7.0(onnxruntime-web@1.21.0)
|
||||
'@imgly/background-removal-data':
|
||||
specifier: ^1.4.5
|
||||
version: 1.4.5
|
||||
'@tailwindcss/vite':
|
||||
specifier: ^4.3.0
|
||||
version: 4.3.0(vite@8.0.12(@types/node@24.12.4)(jiti@2.7.0))
|
||||
fabric:
|
||||
specifier: ^7.4.0
|
||||
version: 7.4.0
|
||||
onnxruntime-web:
|
||||
specifier: 1.21.0
|
||||
version: 1.21.0
|
||||
pdf-lib:
|
||||
specifier: ^1.17.1
|
||||
version: 1.17.1
|
||||
@@ -247,6 +256,14 @@ packages:
|
||||
resolution: {integrity: sha512-bV0Tgo9K4hfPCek+aMAn81RppFKv2ySDQeMoSZuvTASywNTnVJCArCZE2FWqpvIatKu7VMRLWlR1EazvVhDyhQ==}
|
||||
engines: {node: '>=18.18'}
|
||||
|
||||
'@imgly/background-removal-data@1.4.5':
|
||||
resolution: {integrity: sha512-iRXZpPd7YirbJ4eOZ3SlvbdJkAc/LxVjzWLMHOezmRrVThPJPdPc+Y2kHEAVFEVvpDIuvBDyOfTTFBSJT2QR5w==}
|
||||
|
||||
'@imgly/background-removal@1.7.0':
|
||||
resolution: {integrity: sha512-/1ZryrMYg2ckIvJKoTu5Np50JfYMVffDMlVmppw/BdbN3pBTN7e6stI5/7E/LVh9DDzz6J588s7sWqul3fy5wA==}
|
||||
peerDependencies:
|
||||
onnxruntime-web: 1.21.0
|
||||
|
||||
'@jridgewell/gen-mapping@0.3.13':
|
||||
resolution: {integrity: sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==}
|
||||
|
||||
@@ -283,6 +300,36 @@ packages:
|
||||
engines: {node: '>=18'}
|
||||
hasBin: true
|
||||
|
||||
'@protobufjs/aspromise@1.1.2':
|
||||
resolution: {integrity: sha512-j+gKExEuLmKwvz3OgROXtrJ2UG2x8Ch2YZUxahh+s1F2HZ+wAceUNLkvy6zKCPVRkU++ZWQrdxsUeQXmcg4uoQ==}
|
||||
|
||||
'@protobufjs/base64@1.1.2':
|
||||
resolution: {integrity: sha512-AZkcAA5vnN/v4PDqKyMR5lx7hZttPDgClv83E//FMNhR2TMcLUhfRUBHCmSl0oi9zMgDDqRUJkSxO3wm85+XLg==}
|
||||
|
||||
'@protobufjs/codegen@2.0.5':
|
||||
resolution: {integrity: sha512-zgXFLzW3Ap33e6d0Wlj4MGIm6Ce8O89n/apUaGNB/jx+hw+ruWEp7EwGUshdLKVRCxZW12fp9r40E1mQrf/34g==}
|
||||
|
||||
'@protobufjs/eventemitter@1.1.1':
|
||||
resolution: {integrity: sha512-vW1GmwMZNnL+gMRaovlh9yZX74kc+TTU3FObkkurpMaRtBfLP3ldjS9KQWlwZgraRE0+dheEEoAxdzcJQ8eXZg==}
|
||||
|
||||
'@protobufjs/fetch@1.1.1':
|
||||
resolution: {integrity: sha512-GpptLrs57adMSuHi3VNj0mAF8dwh36LMaYF6XyJ6JMWlVsc+t42tm1HSEDmOs3A8fC9yyeisgLhsTVQokOZ0zw==}
|
||||
|
||||
'@protobufjs/float@1.0.2':
|
||||
resolution: {integrity: sha512-Ddb+kVXlXst9d+R9PfTIxh1EdNkgoRe5tOX6t01f1lYWOvJnSPDBlG241QLzcyPdoNTsblLUdujGSE4RzrTZGQ==}
|
||||
|
||||
'@protobufjs/inquire@1.1.2':
|
||||
resolution: {integrity: sha512-pa0vFRuws4wkvaXKK1uXZMAwAX4/t8ANaJo45iw/oQHNQ9q5xUzwgFmVJGXiga2BeN+zpX7Vf9vmsiIa2J+MUw==}
|
||||
|
||||
'@protobufjs/path@1.1.2':
|
||||
resolution: {integrity: sha512-6JOcJ5Tm08dOHAbdR3GrvP+yUUfkjG5ePsHYczMFLq3ZmMkAD98cDgcT2iA1lJ9NVwFd4tH/iSSoe44YWkltEA==}
|
||||
|
||||
'@protobufjs/pool@1.1.0':
|
||||
resolution: {integrity: sha512-0kELaGSIDBKvcgS4zkjz1PeddatrjYcmMWOlAuAPwAeccUrPHdUqo/J6LiymHHEiJT5NrF1UVwxY14f+fy4WQw==}
|
||||
|
||||
'@protobufjs/utf8@1.1.1':
|
||||
resolution: {integrity: sha512-oOAWABowe8EAbMyWKM0tYDKi8Yaox52D+HWZhAIJqQXbqe0xI/GV7FhLWqlEKreMkfDjshR5FKgi3mnle0h6Eg==}
|
||||
|
||||
'@rolldown/binding-android-arm64@1.0.0':
|
||||
resolution: {integrity: sha512-TWMZnRLMe63C2Lhyicviu7ZHaU4kxa6PS3rofvc9GmcvptzNN11BcfQ4Sl7MwTOsisQoa2keB/EBdNCAnUo8vA==}
|
||||
engines: {node: ^20.19.0 || >=22.12.0}
|
||||
@@ -848,6 +895,12 @@ packages:
|
||||
resolution: {integrity: sha512-f7ccFPK3SXFHpx15UIGyRJ/FJQctuKZ0zVuN3frBo4HnK3cay9VEW0R6yPYFHC0AgqhukPzKjq22t5DmAyqGyw==}
|
||||
engines: {node: '>=16'}
|
||||
|
||||
flatbuffers@1.12.0:
|
||||
resolution: {integrity: sha512-c7CZADjRcl6j0PlvFy0ZqXQ67qSEZfrVPynmnL+2zPc+NtMvrF8Y0QceMo7QqnSPc7+uWjUIAbvCQ5WIKlMVdQ==}
|
||||
|
||||
flatbuffers@25.9.23:
|
||||
resolution: {integrity: sha512-MI1qs7Lo4Syw0EOzUl0xjs2lsoeqFku44KpngfIduHBYvzm8h2+7K8YMQh1JtVVVrUvhLpNwqVi4DERegUJhPQ==}
|
||||
|
||||
flatted@3.4.2:
|
||||
resolution: {integrity: sha512-PjDse7RzhcPkIJwy5t7KPWQSZ9cAbzQXcafsetQoD7sOJRQlGikNbx7yZp2OotDnJyrDcbyRq3Ttb18iYOqkxA==}
|
||||
|
||||
@@ -882,6 +935,9 @@ packages:
|
||||
graceful-fs@4.2.11:
|
||||
resolution: {integrity: sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==}
|
||||
|
||||
guid-typescript@1.0.9:
|
||||
resolution: {integrity: sha512-Y8T4vYhEfwJOTbouREvG+3XDsjr8E3kIr7uf+JZ0BYloFsttiHU0WfvANVsR7TxNUJa/WpCnw/Ino/p+DeBhBQ==}
|
||||
|
||||
hermes-estree@0.25.1:
|
||||
resolution: {integrity: sha512-0wUoCcLp+5Ev5pDW2OriHC2MJCbwLwuRx+gAqMTOkGKJJiBCLjtrvy4PWUGn6MIVefecRpzoOZ/UV6iGdOr+Cw==}
|
||||
|
||||
@@ -925,6 +981,12 @@ packages:
|
||||
ini@1.3.8:
|
||||
resolution: {integrity: sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==}
|
||||
|
||||
iota-array@1.0.0:
|
||||
resolution: {integrity: sha512-pZ2xT+LOHckCatGQ3DcG/a+QuEqvoxqkiL7tvE8nn3uuu+f6i1TtpB5/FtWFbxUuVr5PZCx8KskuGatbJDXOWA==}
|
||||
|
||||
is-buffer@1.1.6:
|
||||
resolution: {integrity: sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==}
|
||||
|
||||
is-extglob@2.1.1:
|
||||
resolution: {integrity: sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==}
|
||||
engines: {node: '>=0.10.0'}
|
||||
@@ -1059,6 +1121,12 @@ packages:
|
||||
resolution: {integrity: sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==}
|
||||
engines: {node: '>=10'}
|
||||
|
||||
lodash-es@4.18.1:
|
||||
resolution: {integrity: sha512-J8xewKD/Gk22OZbhpOVSwcs60zhd95ESDwezOFuA3/099925PdHJ7OFHNTGtajL3AlZkykD32HykiMo+BIBI8A==}
|
||||
|
||||
long@5.3.2:
|
||||
resolution: {integrity: sha512-mNAgZ1GmyNhD7AuqnTG3/VQ26o760+ZYBPKjPvugO8+nLbYfX6TVpJPseBvopbdY+qpZ/lKUnmEc1LeZYS3QAA==}
|
||||
|
||||
lru-cache@10.4.3:
|
||||
resolution: {integrity: sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==}
|
||||
|
||||
@@ -1096,6 +1164,9 @@ packages:
|
||||
natural-compare@1.4.0:
|
||||
resolution: {integrity: sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==}
|
||||
|
||||
ndarray@1.0.19:
|
||||
resolution: {integrity: sha512-B4JHA4vdyZU30ELBw3g7/p9bZupyew5a7tX1Y/gGeF2hafrPaQZhgrGQfsvgfYbgdFZjYwuEcnaobeM/WMW+HQ==}
|
||||
|
||||
node-abi@3.92.0:
|
||||
resolution: {integrity: sha512-KdHvFWZjEKDf0cakgFjebl371GPsISX2oZHcuyKqM7DtogIsHrqKeLTo8wBHxaXRAQlY2PsPlZmfo+9ZCxEREQ==}
|
||||
engines: {node: '>=10'}
|
||||
@@ -1115,6 +1186,18 @@ packages:
|
||||
once@1.4.0:
|
||||
resolution: {integrity: sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==}
|
||||
|
||||
onnxruntime-common@1.17.3:
|
||||
resolution: {integrity: sha512-IkbaDelNVX8cBfHFgsNADRIq2TlXMFWW+nG55mwWvQT4i0NZb32Jf35Pf6h9yjrnK78RjcnlNYaI37w394ovMw==}
|
||||
|
||||
onnxruntime-common@1.21.0:
|
||||
resolution: {integrity: sha512-Q632iLLrtCAVOTO65dh2+mNbQir/QNTVBG3h/QdZBpns7mZ0RYbLRBgGABPbpU9351AgYy7SJf1WaeVwMrBFPQ==}
|
||||
|
||||
onnxruntime-web@1.17.3:
|
||||
resolution: {integrity: sha512-MSDrNUWgc1biP0YzY488OJ9n/jTMS9EXysgm9Aw4CUj2A836ALbO2J1sgzguWJeVUHTlM6p7tRzo8IGAgaXWKw==}
|
||||
|
||||
onnxruntime-web@1.21.0:
|
||||
resolution: {integrity: sha512-adzOe+7uI7lKz6pQNbAsLMQd2Fq5Jhmoxd8LZjJr8m3KvbFyiYyRxRiC57/XXD+jb18voppjeGAjoZmskXG+7A==}
|
||||
|
||||
optionator@0.9.4:
|
||||
resolution: {integrity: sha512-6IpQ7mKUxRcZNLIObR0hz7lxsapSSIYNZJwXPGeF0mTVqGKFIXj1DQcMoT22S3ROcLyY/rz0PWaWZ9ayWmad9g==}
|
||||
engines: {node: '>= 0.8.0'}
|
||||
@@ -1154,6 +1237,9 @@ packages:
|
||||
resolution: {integrity: sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==}
|
||||
engines: {node: '>=12'}
|
||||
|
||||
platform@1.3.6:
|
||||
resolution: {integrity: sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg==}
|
||||
|
||||
playwright-core@1.60.0:
|
||||
resolution: {integrity: sha512-9bW6zvX/m0lEbgTKJ6YppOKx8H3VOPBMOCFh2irXFOT4BbHgrx5hPjwJYLT40Lu+4qtD36qKc/Hn56StUW57IA==}
|
||||
engines: {node: '>=18'}
|
||||
@@ -1183,6 +1269,10 @@ packages:
|
||||
engines: {node: '>=14'}
|
||||
hasBin: true
|
||||
|
||||
protobufjs@7.6.2:
|
||||
resolution: {integrity: sha512-N9EiLovGEQOJSPF26Ij7qUGvahfEnq0eeYZ02aigIedkmz1qZSwjnP9SBITHJuF/6MYbIW4HDN8zdYjsjqJKXQ==}
|
||||
engines: {node: '>=12.0.0'}
|
||||
|
||||
pump@3.0.4:
|
||||
resolution: {integrity: sha512-VS7sjc6KR7e1ukRFhQSY5LM2uBWAUPiOPa/A3mkKmiMwSmRFUITt0xuj+/lesgnCv+dPIEYlkzrcyXgquIHMcA==}
|
||||
|
||||
@@ -1546,6 +1636,9 @@ packages:
|
||||
peerDependencies:
|
||||
zod: ^3.25.0 || ^4.0.0
|
||||
|
||||
zod@3.25.76:
|
||||
resolution: {integrity: sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==}
|
||||
|
||||
zod@4.4.3:
|
||||
resolution: {integrity: sha512-ytENFjIJFl2UwYglde2jchW2Hwm4GJFLDiSXWdTrJQBIN9Fcyp7n4DhxJEiWNAJMV1/BqWfW/kkg71UDcHJyTQ==}
|
||||
|
||||
@@ -1756,6 +1849,17 @@ snapshots:
|
||||
|
||||
'@humanwhocodes/retry@0.4.3': {}
|
||||
|
||||
'@imgly/background-removal-data@1.4.5':
|
||||
dependencies:
|
||||
onnxruntime-web: 1.17.3
|
||||
|
||||
'@imgly/background-removal@1.7.0(onnxruntime-web@1.21.0)':
|
||||
dependencies:
|
||||
lodash-es: 4.18.1
|
||||
ndarray: 1.0.19
|
||||
onnxruntime-web: 1.21.0
|
||||
zod: 3.25.76
|
||||
|
||||
'@jridgewell/gen-mapping@0.3.13':
|
||||
dependencies:
|
||||
'@jridgewell/sourcemap-codec': 1.5.5
|
||||
@@ -1796,6 +1900,28 @@ snapshots:
|
||||
dependencies:
|
||||
playwright: 1.60.0
|
||||
|
||||
'@protobufjs/aspromise@1.1.2': {}
|
||||
|
||||
'@protobufjs/base64@1.1.2': {}
|
||||
|
||||
'@protobufjs/codegen@2.0.5': {}
|
||||
|
||||
'@protobufjs/eventemitter@1.1.1': {}
|
||||
|
||||
'@protobufjs/fetch@1.1.1':
|
||||
dependencies:
|
||||
'@protobufjs/aspromise': 1.1.2
|
||||
|
||||
'@protobufjs/float@1.0.2': {}
|
||||
|
||||
'@protobufjs/inquire@1.1.2': {}
|
||||
|
||||
'@protobufjs/path@1.1.2': {}
|
||||
|
||||
'@protobufjs/pool@1.1.0': {}
|
||||
|
||||
'@protobufjs/utf8@1.1.1': {}
|
||||
|
||||
'@rolldown/binding-android-arm64@1.0.0':
|
||||
optional: true
|
||||
|
||||
@@ -2338,6 +2464,10 @@ snapshots:
|
||||
flatted: 3.4.2
|
||||
keyv: 4.5.4
|
||||
|
||||
flatbuffers@1.12.0: {}
|
||||
|
||||
flatbuffers@25.9.23: {}
|
||||
|
||||
flatted@3.4.2: {}
|
||||
|
||||
fs-constants@1.0.0:
|
||||
@@ -2362,6 +2492,8 @@ snapshots:
|
||||
|
||||
graceful-fs@4.2.11: {}
|
||||
|
||||
guid-typescript@1.0.9: {}
|
||||
|
||||
hermes-estree@0.25.1: {}
|
||||
|
||||
hermes-parser@0.25.1:
|
||||
@@ -2409,6 +2541,10 @@ snapshots:
|
||||
ini@1.3.8:
|
||||
optional: true
|
||||
|
||||
iota-array@1.0.0: {}
|
||||
|
||||
is-buffer@1.1.6: {}
|
||||
|
||||
is-extglob@2.1.1: {}
|
||||
|
||||
is-glob@4.0.3:
|
||||
@@ -2526,6 +2662,10 @@ snapshots:
|
||||
dependencies:
|
||||
p-locate: 5.0.0
|
||||
|
||||
lodash-es@4.18.1: {}
|
||||
|
||||
long@5.3.2: {}
|
||||
|
||||
lru-cache@10.4.3:
|
||||
optional: true
|
||||
|
||||
@@ -2559,6 +2699,11 @@ snapshots:
|
||||
|
||||
natural-compare@1.4.0: {}
|
||||
|
||||
ndarray@1.0.19:
|
||||
dependencies:
|
||||
iota-array: 1.0.0
|
||||
is-buffer: 1.1.6
|
||||
|
||||
node-abi@3.92.0:
|
||||
dependencies:
|
||||
semver: 7.8.0
|
||||
@@ -2579,6 +2724,28 @@ snapshots:
|
||||
wrappy: 1.0.2
|
||||
optional: true
|
||||
|
||||
onnxruntime-common@1.17.3: {}
|
||||
|
||||
onnxruntime-common@1.21.0: {}
|
||||
|
||||
onnxruntime-web@1.17.3:
|
||||
dependencies:
|
||||
flatbuffers: 1.12.0
|
||||
guid-typescript: 1.0.9
|
||||
long: 5.3.2
|
||||
onnxruntime-common: 1.17.3
|
||||
platform: 1.3.6
|
||||
protobufjs: 7.6.2
|
||||
|
||||
onnxruntime-web@1.21.0:
|
||||
dependencies:
|
||||
flatbuffers: 25.9.23
|
||||
guid-typescript: 1.0.9
|
||||
long: 5.3.2
|
||||
onnxruntime-common: 1.21.0
|
||||
platform: 1.3.6
|
||||
protobufjs: 7.6.2
|
||||
|
||||
optionator@0.9.4:
|
||||
dependencies:
|
||||
deep-is: 0.1.4
|
||||
@@ -2620,6 +2787,8 @@ snapshots:
|
||||
|
||||
picomatch@4.0.4: {}
|
||||
|
||||
platform@1.3.6: {}
|
||||
|
||||
playwright-core@1.60.0: {}
|
||||
|
||||
playwright@1.60.0:
|
||||
@@ -2654,6 +2823,21 @@ snapshots:
|
||||
|
||||
prettier@3.8.3: {}
|
||||
|
||||
protobufjs@7.6.2:
|
||||
dependencies:
|
||||
'@protobufjs/aspromise': 1.1.2
|
||||
'@protobufjs/base64': 1.1.2
|
||||
'@protobufjs/codegen': 2.0.5
|
||||
'@protobufjs/eventemitter': 1.1.1
|
||||
'@protobufjs/fetch': 1.1.1
|
||||
'@protobufjs/float': 1.0.2
|
||||
'@protobufjs/inquire': 1.1.2
|
||||
'@protobufjs/path': 1.1.2
|
||||
'@protobufjs/pool': 1.1.0
|
||||
'@protobufjs/utf8': 1.1.1
|
||||
'@types/node': 24.12.4
|
||||
long: 5.3.2
|
||||
|
||||
pump@3.0.4:
|
||||
dependencies:
|
||||
end-of-stream: 1.4.5
|
||||
@@ -2971,6 +3155,8 @@ snapshots:
|
||||
dependencies:
|
||||
zod: 4.4.3
|
||||
|
||||
zod@3.25.76: {}
|
||||
|
||||
zod@4.4.3: {}
|
||||
|
||||
zxing-wasm@3.1.0(@types/emscripten@1.41.5):
|
||||
|
||||
88
scripts/fetch-imgly-assets.mjs
Normal file
88
scripts/fetch-imgly-assets.mjs
Normal file
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* Self-host the @imgly/background-removal model + wasm assets.
|
||||
*
|
||||
* @imgly/background-removal is AGPL-3.0. By default it streams its ONNX model
|
||||
* and onnxruntime-web wasm binaries from staticimgly.com at runtime. To keep
|
||||
* Plimi's privacy promise (nothing leaves the user's browser, no third-party
|
||||
* fetches), we mirror the *version-matched* asset bundle into `public/imgly/`
|
||||
* and point the library at our own origin via `config.publicPath`.
|
||||
*
|
||||
* The assets are large (~300MB across all model variants) so they are NOT
|
||||
* committed to git — this script is run on `predev` / `prebuild` and is
|
||||
* idempotent: it only downloads chunks that are missing.
|
||||
*/
|
||||
import { mkdir, readFile, writeFile, stat } from "node:fs/promises";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import path from "node:path";
|
||||
|
||||
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const OUT_DIR = path.join(ROOT, "public", "imgly");
|
||||
|
||||
// Keep the data version in lock-step with the installed library version,
|
||||
// otherwise the resources.json format / model keys won't match the runtime.
|
||||
const { version } = JSON.parse(
|
||||
await readFile(
|
||||
path.join(ROOT, "node_modules/@imgly/background-removal/package.json"),
|
||||
"utf8"
|
||||
)
|
||||
);
|
||||
const BASE = `https://staticimgly.com/@imgly/background-removal-data/${version}/dist/`;
|
||||
|
||||
async function exists(p) {
|
||||
try {
|
||||
await stat(p);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function download(url, dest) {
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to fetch ${url}: ${res.status} ${res.statusText}`);
|
||||
}
|
||||
const buf = Buffer.from(await res.arrayBuffer());
|
||||
await writeFile(dest, buf);
|
||||
return buf.length;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await mkdir(OUT_DIR, { recursive: true });
|
||||
|
||||
console.log(`[imgly] Mirroring background-removal assets v${version}`);
|
||||
const resourcesPath = path.join(OUT_DIR, "resources.json");
|
||||
await download(new URL("resources.json", BASE).toString(), resourcesPath);
|
||||
|
||||
const resources = JSON.parse(await readFile(resourcesPath, "utf8"));
|
||||
const chunkNames = new Set();
|
||||
for (const entry of Object.values(resources)) {
|
||||
for (const chunk of entry.chunks ?? []) chunkNames.add(chunk.name);
|
||||
}
|
||||
|
||||
let downloaded = 0;
|
||||
let skipped = 0;
|
||||
let bytes = 0;
|
||||
for (const name of chunkNames) {
|
||||
const dest = path.join(OUT_DIR, name);
|
||||
if (await exists(dest)) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
bytes += await download(new URL(name, BASE).toString(), dest);
|
||||
downloaded++;
|
||||
if (downloaded % 10 === 0) {
|
||||
console.log(`[imgly] ${downloaded} chunks downloaded...`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(
|
||||
`[imgly] Done: ${downloaded} downloaded, ${skipped} cached ` +
|
||||
`(${(bytes / 1024 / 1024).toFixed(1)} MB new) → public/imgly/`
|
||||
);
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error("[imgly] Asset mirror failed:", err);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -1,16 +1,38 @@
|
||||
import { Link } from "react-router-dom";
|
||||
import { useAnalyticsConsent } from "../../core/privacy/analytics-consent-context";
|
||||
import { runtimeConfigValue } from "../../core/config/runtime-config";
|
||||
|
||||
export function Footer() {
|
||||
const { choice, openSettings } = useAnalyticsConsent();
|
||||
const repoUrl = runtimeConfigValue(
|
||||
"VITE_PLIMI_REPO_URL",
|
||||
"https://github.com/achrafachkari/plimi"
|
||||
);
|
||||
|
||||
return (
|
||||
<footer className="border-t border-[var(--p-border)] px-6 py-6 md:px-14">
|
||||
<div className="mx-auto flex max-w-[1200px] flex-col gap-4 text-sm text-[var(--p-muted)] sm:flex-row sm:items-center sm:justify-between">
|
||||
<span>
|
||||
© {new Date().getFullYear()} Achraf Achkari. Plimi processes tool inputs locally in your browser.
|
||||
© {new Date().getFullYear()} Achraf Achkari. Plimi is free software under the{" "}
|
||||
<a
|
||||
href={repoUrl}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="text-[var(--p-muted)] underline hover:text-[var(--p-text)]"
|
||||
>
|
||||
AGPL-3.0
|
||||
</a>
|
||||
.
|
||||
</span>
|
||||
<nav className="flex flex-wrap items-center gap-x-5 gap-y-2">
|
||||
<a
|
||||
href={repoUrl}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="text-[var(--p-muted)] no-underline hover:text-[var(--p-text)]"
|
||||
>
|
||||
Source code
|
||||
</a>
|
||||
<Link
|
||||
to="/privacy"
|
||||
className="text-[var(--p-muted)] no-underline hover:text-[var(--p-text)]"
|
||||
|
||||
@@ -5,6 +5,7 @@ import type {
|
||||
UnknownPlimiPlugin,
|
||||
} from "../../core/plugins/plugin-types";
|
||||
import { describeToolPermissions } from "../../core/plugins/plugin-permissions";
|
||||
import { normalizeToolOptions } from "../../core/plugins/plugin-validation";
|
||||
import { ToolInputPanel } from "./ToolInputPanel";
|
||||
import { ToolOptionsPanel } from "./ToolOptionsPanel";
|
||||
import { ToolResultPanel } from "./ToolResultPanel";
|
||||
@@ -87,6 +88,7 @@ export function ToolShell({
|
||||
}) {
|
||||
const [input, setInput] = useState<ToolInput>({});
|
||||
const [options, setOptions] = useState<ToolOptionsValue>({});
|
||||
const [runWarning, setRunWarning] = useState<string | null>(null);
|
||||
const { run, cancel, result, isExecuting, progress, error } =
|
||||
useToolExecution(plugin);
|
||||
|
||||
@@ -96,8 +98,22 @@ export function ToolShell({
|
||||
const primaryExample = examples[0];
|
||||
|
||||
const handleRun = useCallback(async () => {
|
||||
await run(input, options);
|
||||
}, [input, options, run]);
|
||||
const normalizedOptions = normalizeToolOptions(plugin.optionsSchema, options);
|
||||
if (plugin.getRunWarning) {
|
||||
const warning = await plugin.getRunWarning(input, normalizedOptions);
|
||||
if (warning) {
|
||||
setRunWarning(warning);
|
||||
return;
|
||||
}
|
||||
}
|
||||
await run(input, normalizedOptions);
|
||||
}, [input, options, run, plugin]);
|
||||
|
||||
const handleConfirmRun = useCallback(async () => {
|
||||
setRunWarning(null);
|
||||
const normalizedOptions = normalizeToolOptions(plugin.optionsSchema, options);
|
||||
await run(input, normalizedOptions);
|
||||
}, [input, options, run, plugin]);
|
||||
|
||||
const handleApplyExample = useCallback(() => {
|
||||
if (!primaryExample) return;
|
||||
@@ -232,6 +248,48 @@ export function ToolShell({
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{runWarning && (
|
||||
<div
|
||||
role="presentation"
|
||||
onClick={() => setRunWarning(null)}
|
||||
className="fixed inset-0 z-[60] flex items-center justify-center p-4 animate-plimi-fade"
|
||||
style={{
|
||||
background: "color-mix(in oklab, var(--p-bg) 60%, transparent)",
|
||||
backdropFilter: "blur(4px)",
|
||||
}}
|
||||
>
|
||||
<div
|
||||
role="alertdialog"
|
||||
aria-modal="true"
|
||||
aria-label="Confirm download"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
className="w-full max-w-[420px] rounded-[18px] border-[1.5px] border-[var(--p-border)] bg-[var(--p-surface)] p-[22px] animate-plimi-slide"
|
||||
style={{ boxShadow: "0 40px 80px -30px var(--p-shadow-soft)" }}
|
||||
>
|
||||
<div className="font-sans text-[16px] font-semibold tracking-tight text-[var(--p-text)]">
|
||||
One-time download
|
||||
</div>
|
||||
<p className="mt-2 mb-5 font-sans text-[13px] leading-relaxed text-[var(--p-muted)]">
|
||||
{runWarning}
|
||||
</p>
|
||||
<div className="flex justify-end gap-2">
|
||||
<button
|
||||
onClick={() => setRunWarning(null)}
|
||||
className="cursor-pointer rounded-[10px] bg-[var(--p-chip)] px-[16px] py-[9px] font-sans text-[13px] font-medium text-[var(--p-muted)]"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
onClick={handleConfirmRun}
|
||||
className="cursor-pointer rounded-[10px] bg-[var(--p-accent)] px-[16px] py-[9px] font-sans text-[13px] font-semibold text-[var(--p-accent-ink)]"
|
||||
>
|
||||
Download & run
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import { qrCodeGeneratorPlugin } from "../../tools/qr-code-generator";
|
||||
import { passwordGeneratorPlugin } from "../../tools/password-generator";
|
||||
import { fileChecksumVerifierPlugin } from "../../tools/file-checksum-verifier";
|
||||
import { svgViewerPlugin } from "../../tools/svg-viewer";
|
||||
import { backgroundRemoverPlugin } from "../../tools/background-remover";
|
||||
|
||||
function erasePlugin<TOptions>(plugin: PlimiPlugin<TOptions>): UnknownPlimiPlugin {
|
||||
return plugin as unknown as UnknownPlimiPlugin;
|
||||
@@ -61,6 +62,7 @@ export const pluginRegistry: UnknownPlimiPlugin[] = [
|
||||
erasePlugin(passwordGeneratorPlugin),
|
||||
erasePlugin(fileChecksumVerifierPlugin),
|
||||
erasePlugin(svgViewerPlugin),
|
||||
erasePlugin(backgroundRemoverPlugin),
|
||||
];
|
||||
|
||||
export function getAllPlugins(): UnknownPlimiPlugin[] {
|
||||
|
||||
86
src/core/plugins/plugin-runner.test.ts
Normal file
86
src/core/plugins/plugin-runner.test.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { ToolResult } from "../io/output-types";
|
||||
import type { PlimiPlugin, ToolContext } from "./plugin-types";
|
||||
import { disposePersistentWorker, runPlugin } from "./plugin-runner";
|
||||
|
||||
class FakeWorker extends EventTarget {
|
||||
terminate = vi.fn();
|
||||
postMessage = vi.fn((request: { id: string }) => {
|
||||
queueMicrotask(() => {
|
||||
const result: ToolResult = { type: "text", value: "done" };
|
||||
this.dispatchEvent(
|
||||
new MessageEvent("message", {
|
||||
data: { type: "success", id: request.id, result },
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function makePlugin(
|
||||
id: string,
|
||||
persistentWorker: boolean,
|
||||
workerFactory: () => Worker
|
||||
): PlimiPlugin<Record<string, never>> {
|
||||
return {
|
||||
manifest: {
|
||||
id,
|
||||
name: id,
|
||||
description: id,
|
||||
category: "image",
|
||||
version: "1.0.0",
|
||||
input: { type: "text" },
|
||||
output: { type: "text" },
|
||||
offlineReady: true,
|
||||
},
|
||||
capabilities: { worker: true, persistentWorker },
|
||||
worker: workerFactory,
|
||||
};
|
||||
}
|
||||
|
||||
function makeContext(): ToolContext {
|
||||
return {
|
||||
signal: new AbortController().signal,
|
||||
reportProgress: vi.fn(),
|
||||
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },
|
||||
};
|
||||
}
|
||||
|
||||
describe("runPlugin worker lifecycle", () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("reuses a persistent worker between runs", async () => {
|
||||
const worker = new FakeWorker();
|
||||
const factory = vi.fn(() => worker as unknown as Worker);
|
||||
const plugin = makePlugin("persistent-test", true, factory);
|
||||
|
||||
await runPlugin(plugin, { text: "one" }, {}, makeContext());
|
||||
await runPlugin(plugin, { text: "two" }, {}, makeContext());
|
||||
|
||||
expect(factory).toHaveBeenCalledTimes(1);
|
||||
expect(worker.postMessage).toHaveBeenCalledTimes(2);
|
||||
expect(worker.terminate).not.toHaveBeenCalled();
|
||||
|
||||
expect(disposePersistentWorker("persistent-test")).toBe(true);
|
||||
expect(worker.terminate).toHaveBeenCalledOnce();
|
||||
|
||||
await runPlugin(plugin, { text: "three" }, {}, makeContext());
|
||||
expect(factory).toHaveBeenCalledTimes(2);
|
||||
disposePersistentWorker("persistent-test");
|
||||
});
|
||||
|
||||
it("terminates a non-persistent worker after success", async () => {
|
||||
const worker = new FakeWorker();
|
||||
const plugin = makePlugin(
|
||||
"temporary-test",
|
||||
false,
|
||||
() => worker as unknown as Worker
|
||||
);
|
||||
|
||||
await runPlugin(plugin, { text: "one" }, {}, makeContext());
|
||||
|
||||
expect(worker.terminate).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
@@ -4,6 +4,44 @@ import type { ToolResult } from "../io/output-types";
|
||||
import type { ToolWorkerRequest, ToolWorkerResponse } from "./worker-protocol";
|
||||
import { normalizeToolOptions, validateToolInput } from "./plugin-validation";
|
||||
|
||||
const persistentWorkers = new Map<string, Worker>();
|
||||
|
||||
export function disposePersistentWorker(pluginId: string): boolean {
|
||||
const worker = persistentWorkers.get(pluginId);
|
||||
if (!worker) return false;
|
||||
persistentWorkers.delete(pluginId);
|
||||
worker.terminate();
|
||||
return true;
|
||||
}
|
||||
|
||||
function createPluginWorker<TOptions>(plugin: PlimiPlugin<TOptions>): Worker {
|
||||
if (!plugin.worker) {
|
||||
throw new Error("Worker is not defined for this plugin");
|
||||
}
|
||||
|
||||
if (!plugin.capabilities?.persistentWorker) {
|
||||
return plugin.worker();
|
||||
}
|
||||
|
||||
const pluginId = plugin.manifest.id;
|
||||
const existing = persistentWorkers.get(pluginId);
|
||||
if (existing) return existing;
|
||||
|
||||
const worker = plugin.worker();
|
||||
persistentWorkers.set(pluginId, worker);
|
||||
return worker;
|
||||
}
|
||||
|
||||
function discardPluginWorker<TOptions>(
|
||||
plugin: PlimiPlugin<TOptions>,
|
||||
worker: Worker
|
||||
): void {
|
||||
if (persistentWorkers.get(plugin.manifest.id) === worker) {
|
||||
persistentWorkers.delete(plugin.manifest.id);
|
||||
}
|
||||
worker.terminate();
|
||||
}
|
||||
|
||||
export async function runPlugin<TOptions>(
|
||||
plugin: PlimiPlugin<TOptions>,
|
||||
input: ToolInput,
|
||||
@@ -33,21 +71,28 @@ function runPluginInWorker<TOptions>(
|
||||
context: ToolContext
|
||||
): Promise<ToolResult> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!plugin.worker) {
|
||||
return reject(new Error("Worker is not defined for this plugin"));
|
||||
let worker: Worker;
|
||||
try {
|
||||
worker = createPluginWorker(plugin);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
const worker = plugin.worker();
|
||||
const jobId = Math.random().toString(36).substring(7);
|
||||
|
||||
// Handle cancellation
|
||||
const cleanup = () => {
|
||||
context.signal.removeEventListener("abort", onAbort);
|
||||
worker.removeEventListener("message", onMessage);
|
||||
worker.removeEventListener("error", onError);
|
||||
};
|
||||
|
||||
const onAbort = () => {
|
||||
worker.terminate();
|
||||
cleanup();
|
||||
discardPluginWorker(plugin, worker);
|
||||
reject(new DOMException("Operation cancelled by user.", "AbortError"));
|
||||
};
|
||||
context.signal.addEventListener("abort", onAbort);
|
||||
|
||||
worker.onmessage = (e: MessageEvent<ToolWorkerResponse>) => {
|
||||
const onMessage = (e: MessageEvent<ToolWorkerResponse>) => {
|
||||
const { type, id } = e.data;
|
||||
|
||||
if (id !== jobId) return;
|
||||
@@ -55,22 +100,28 @@ function runPluginInWorker<TOptions>(
|
||||
if (type === "progress") {
|
||||
context.reportProgress(e.data.progress);
|
||||
} else if (type === "success") {
|
||||
context.signal.removeEventListener("abort", onAbort);
|
||||
cleanup();
|
||||
if (!plugin.capabilities?.persistentWorker) {
|
||||
worker.terminate();
|
||||
}
|
||||
resolve(e.data.result);
|
||||
} else if (type === "error") {
|
||||
context.signal.removeEventListener("abort", onAbort);
|
||||
worker.terminate();
|
||||
cleanup();
|
||||
discardPluginWorker(plugin, worker);
|
||||
reject(new Error(e.data.error));
|
||||
}
|
||||
};
|
||||
|
||||
worker.onerror = (e) => {
|
||||
context.signal.removeEventListener("abort", onAbort);
|
||||
worker.terminate();
|
||||
const onError = (e: ErrorEvent) => {
|
||||
cleanup();
|
||||
discardPluginWorker(plugin, worker);
|
||||
reject(new Error("Worker error: " + e.message));
|
||||
};
|
||||
|
||||
context.signal.addEventListener("abort", onAbort);
|
||||
worker.addEventListener("message", onMessage);
|
||||
worker.addEventListener("error", onError);
|
||||
|
||||
// Serialize input payload (can't send DOM elements or complex functions)
|
||||
const request: ToolWorkerRequest<TOptions> = {
|
||||
type: "run",
|
||||
|
||||
@@ -80,6 +80,8 @@ export type ToolOptionsValue = Record<string, ToolOptionValue>;
|
||||
export interface ToolCapabilities {
|
||||
batch?: boolean;
|
||||
worker?: boolean;
|
||||
/** Keep the worker alive between runs so it can retain expensive state. */
|
||||
persistentWorker?: boolean;
|
||||
wasm?: boolean;
|
||||
preview?: boolean;
|
||||
streaming?: boolean;
|
||||
@@ -138,6 +140,16 @@ export interface PlimiPlugin<TOptions = unknown> {
|
||||
|
||||
worker?: () => Worker;
|
||||
|
||||
/**
|
||||
* Optional pre-run check. Return a message to surface a confirmation dialog
|
||||
* before the tool runs (e.g. "this will download 44 MB"); return null to run
|
||||
* immediately. Runs on the main thread, so it can inspect caches/storage.
|
||||
*/
|
||||
getRunWarning?: (
|
||||
input: ToolInput,
|
||||
options: TOptions
|
||||
) => Promise<string | null> | string | null;
|
||||
|
||||
customUi?: React.LazyExoticComponent<
|
||||
React.ComponentType<ToolUiProps<TOptions>>
|
||||
>;
|
||||
@@ -145,10 +157,14 @@ export interface PlimiPlugin<TOptions = unknown> {
|
||||
|
||||
export type UnknownPlimiPlugin = Omit<
|
||||
PlimiPlugin<never>,
|
||||
"run" | "customUi"
|
||||
"run" | "customUi" | "getRunWarning"
|
||||
> & {
|
||||
run?: ToolRunner<ToolOptionsValue>;
|
||||
examples?: ToolExample<ToolOptionsValue>[];
|
||||
getRunWarning?: (
|
||||
input: ToolInput,
|
||||
options: ToolOptionsValue
|
||||
) => Promise<string | null> | string | null;
|
||||
customUi?: React.LazyExoticComponent<
|
||||
React.ComponentType<ToolUiProps<ToolOptionsValue>>
|
||||
>;
|
||||
|
||||
@@ -134,6 +134,14 @@ export function PrivacyPage() {
|
||||
settings. Plimi does not use analytics cookies, advertising cookies,
|
||||
or marketing pixels.
|
||||
</p>
|
||||
<p className="m-0">
|
||||
If you use automatic background removal, Plimi stores the selected AI
|
||||
model and its ONNX runtime files in your browser's Cache Storage.
|
||||
This avoids downloading the same large files for every run. The cache
|
||||
contains application assets only, not your uploaded images, masks, or
|
||||
generated results. You can delete it at any time with the
|
||||
"Clear models" button in the Background Remover.
|
||||
</p>
|
||||
</Section>
|
||||
|
||||
<Section title="Recipients and transfers">
|
||||
|
||||
757
src/tools/background-remover/BackgroundRemoverUi.tsx
Normal file
757
src/tools/background-remover/BackgroundRemoverUi.tsx
Normal file
@@ -0,0 +1,757 @@
|
||||
import {
|
||||
useCallback,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
type PointerEvent as ReactPointerEvent,
|
||||
} from "react";
|
||||
import type { ToolUiProps } from "../../core/plugins/plugin-types";
|
||||
import { Button } from "../../components/ui/Button";
|
||||
import { Card } from "../../components/ui/Card";
|
||||
import { Dropzone } from "../../components/ui/Dropzone";
|
||||
import { Select } from "../../components/ui/Select";
|
||||
import { Slider } from "../../components/ui/Slider";
|
||||
import { ToolProgress } from "../../components/tool/ToolProgress";
|
||||
import { useToolExecution } from "../../components/tool/useToolExecution";
|
||||
import { disposePersistentWorker } from "../../core/plugins/plugin-runner";
|
||||
import { clearImglyCache } from "./imgly-cache";
|
||||
import type { BackgroundRemoverOptions } from "./remove";
|
||||
|
||||
type EditMode = "automatic" | "select-area";
|
||||
type EditorAction = "sample" | "remove" | "restore";
|
||||
|
||||
interface Point {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
interface MaskStroke {
|
||||
type: "stroke";
|
||||
action: Exclude<EditorAction, "sample">;
|
||||
points: Point[];
|
||||
size: number;
|
||||
}
|
||||
|
||||
interface SampleSelection {
|
||||
type: "sample";
|
||||
point: Point;
|
||||
tolerance: number;
|
||||
}
|
||||
|
||||
type MaskOperation = MaskStroke | SampleSelection;
|
||||
|
||||
function drawStrokePath(
|
||||
context: CanvasRenderingContext2D,
|
||||
stroke: MaskStroke,
|
||||
width: number,
|
||||
height: number
|
||||
): void {
|
||||
const points = stroke.points;
|
||||
if (points.length === 0) return;
|
||||
|
||||
const scale = Math.min(width, height);
|
||||
context.lineWidth = stroke.size * scale;
|
||||
context.lineCap = "round";
|
||||
context.lineJoin = "round";
|
||||
context.beginPath();
|
||||
context.moveTo(points[0].x * width, points[0].y * height);
|
||||
|
||||
if (points.length === 1) {
|
||||
context.lineTo(points[0].x * width + 0.01, points[0].y * height);
|
||||
} else {
|
||||
for (const point of points.slice(1)) {
|
||||
context.lineTo(point.x * width, point.y * height);
|
||||
}
|
||||
}
|
||||
context.stroke();
|
||||
}
|
||||
|
||||
function applyStroke(
|
||||
output: HTMLCanvasElement,
|
||||
source: HTMLCanvasElement,
|
||||
scratch: HTMLCanvasElement,
|
||||
stroke: MaskStroke
|
||||
): void {
|
||||
const outputContext = output.getContext("2d");
|
||||
const scratchContext = scratch.getContext("2d");
|
||||
if (!outputContext || !scratchContext) return;
|
||||
|
||||
if (stroke.action === "remove") {
|
||||
outputContext.save();
|
||||
outputContext.globalCompositeOperation = "destination-out";
|
||||
outputContext.strokeStyle = "#000";
|
||||
drawStrokePath(outputContext, stroke, output.width, output.height);
|
||||
outputContext.restore();
|
||||
return;
|
||||
}
|
||||
|
||||
scratchContext.clearRect(0, 0, scratch.width, scratch.height);
|
||||
scratchContext.globalCompositeOperation = "source-over";
|
||||
scratchContext.drawImage(source, 0, 0);
|
||||
scratchContext.globalCompositeOperation = "destination-in";
|
||||
scratchContext.strokeStyle = "#000";
|
||||
drawStrokePath(scratchContext, stroke, scratch.width, scratch.height);
|
||||
scratchContext.globalCompositeOperation = "source-over";
|
||||
|
||||
outputContext.drawImage(scratch, 0, 0);
|
||||
}
|
||||
|
||||
function applySampleSelection(
|
||||
output: HTMLCanvasElement,
|
||||
source: HTMLCanvasElement,
|
||||
selection: SampleSelection
|
||||
): void {
|
||||
const outputContext = output.getContext("2d", { willReadFrequently: true });
|
||||
const sourceContext = source.getContext("2d", { willReadFrequently: true });
|
||||
if (!outputContext || !sourceContext) return;
|
||||
|
||||
const { width, height } = output;
|
||||
const sourceData = sourceContext.getImageData(0, 0, width, height);
|
||||
const outputData = outputContext.getImageData(0, 0, width, height);
|
||||
const startX = Math.min(width - 1, Math.floor(selection.point.x * width));
|
||||
const startY = Math.min(height - 1, Math.floor(selection.point.y * height));
|
||||
const startIndex = startY * width + startX;
|
||||
const startOffset = startIndex * 4;
|
||||
const targetR = sourceData.data[startOffset];
|
||||
const targetG = sourceData.data[startOffset + 1];
|
||||
const targetB = sourceData.data[startOffset + 2];
|
||||
const threshold = (selection.tolerance / 100) * Math.sqrt(3 * 255 * 255);
|
||||
const thresholdSquared = threshold * threshold;
|
||||
const visited = new Uint8Array(width * height);
|
||||
const stack = new Uint32Array(width * height);
|
||||
let stackSize = 0;
|
||||
visited[startIndex] = 1;
|
||||
stack[stackSize++] = startIndex;
|
||||
|
||||
while (stackSize > 0) {
|
||||
const index = stack[--stackSize];
|
||||
const offset = index * 4;
|
||||
const deltaR = sourceData.data[offset] - targetR;
|
||||
const deltaG = sourceData.data[offset + 1] - targetG;
|
||||
const deltaB = sourceData.data[offset + 2] - targetB;
|
||||
const distanceSquared =
|
||||
deltaR * deltaR + deltaG * deltaG + deltaB * deltaB;
|
||||
if (distanceSquared > thresholdSquared) continue;
|
||||
|
||||
outputData.data[offset + 3] = 0;
|
||||
const x = index % width;
|
||||
const y = Math.floor(index / width);
|
||||
const neighbors = [
|
||||
x > 0 ? index - 1 : -1,
|
||||
x + 1 < width ? index + 1 : -1,
|
||||
y > 0 ? index - width : -1,
|
||||
y + 1 < height ? index + width : -1,
|
||||
];
|
||||
for (const neighbor of neighbors) {
|
||||
if (neighbor >= 0 && !visited[neighbor]) {
|
||||
visited[neighbor] = 1;
|
||||
stack[stackSize++] = neighbor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
outputContext.putImageData(outputData, 0, 0);
|
||||
}
|
||||
|
||||
function downloadBlob(blob: Blob, name: string): void {
|
||||
const url = URL.createObjectURL(blob);
|
||||
const anchor = document.createElement("a");
|
||||
anchor.href = url;
|
||||
anchor.download = name;
|
||||
document.body.appendChild(anchor);
|
||||
anchor.click();
|
||||
anchor.remove();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
export default function BackgroundRemoverUi({
|
||||
plugin,
|
||||
}: ToolUiProps<BackgroundRemoverOptions>) {
|
||||
const [files, setFiles] = useState<File[]>([]);
|
||||
const [sourceUrl, setSourceUrl] = useState("");
|
||||
const [resultUrl, setResultUrl] = useState("");
|
||||
const [editMode, setEditMode] = useState<EditMode>("automatic");
|
||||
const [quality, setQuality] =
|
||||
useState<BackgroundRemoverOptions["quality"]>("small");
|
||||
const [useGpu, setUseGpu] = useState(true);
|
||||
const [editorAction, setEditorAction] = useState<EditorAction>("sample");
|
||||
const [tolerance, setTolerance] = useState(14);
|
||||
const [brushSize, setBrushSize] = useState(4);
|
||||
const [operations, setOperations] = useState<MaskOperation[]>([]);
|
||||
const [isDrawing, setIsDrawing] = useState(false);
|
||||
const [warning, setWarning] = useState<string | null>(null);
|
||||
const [pendingOptions, setPendingOptions] =
|
||||
useState<BackgroundRemoverOptions | null>(null);
|
||||
const [cacheMessage, setCacheMessage] = useState("");
|
||||
const [isClearingCache, setIsClearingCache] = useState(false);
|
||||
|
||||
const canvasRef = useRef<HTMLCanvasElement>(null);
|
||||
const sourceCanvasRef = useRef<HTMLCanvasElement | null>(null);
|
||||
const baseCanvasRef = useRef<HTMLCanvasElement | null>(null);
|
||||
const scratchCanvasRef = useRef<HTMLCanvasElement | null>(null);
|
||||
const activeStrokeRef = useRef<MaskStroke | null>(null);
|
||||
const resultUrlRef = useRef("");
|
||||
|
||||
const { run, result, isExecuting, progress, error, reset } =
|
||||
useToolExecution(plugin);
|
||||
|
||||
const resultFile =
|
||||
result?.type === "files" && result.files.length > 0
|
||||
? result.files[0]
|
||||
: null;
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (sourceUrl) URL.revokeObjectURL(sourceUrl);
|
||||
};
|
||||
}, [sourceUrl]);
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
if (resultUrlRef.current) URL.revokeObjectURL(resultUrlRef.current);
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
const renderOperations = useCallback((nextOperations: MaskOperation[]) => {
|
||||
const output = canvasRef.current;
|
||||
const base = baseCanvasRef.current;
|
||||
const source = sourceCanvasRef.current;
|
||||
const scratch = scratchCanvasRef.current;
|
||||
if (!output || !base || !source || !scratch) return;
|
||||
|
||||
const context = output.getContext("2d");
|
||||
if (!context) return;
|
||||
context.clearRect(0, 0, output.width, output.height);
|
||||
context.drawImage(base, 0, 0);
|
||||
for (const operation of nextOperations) {
|
||||
if (operation.type === "sample") {
|
||||
applySampleSelection(output, source, operation);
|
||||
} else {
|
||||
applyStroke(output, source, scratch, operation);
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (editMode !== "select-area" || files.length === 0) return;
|
||||
|
||||
let cancelled = false;
|
||||
const initialize = async () => {
|
||||
const sourceBitmap = await createImageBitmap(files[0]);
|
||||
if (cancelled) {
|
||||
sourceBitmap.close();
|
||||
return;
|
||||
}
|
||||
|
||||
const width = sourceBitmap.width;
|
||||
const height = sourceBitmap.height;
|
||||
const sourceCanvas = document.createElement("canvas");
|
||||
const baseCanvas = document.createElement("canvas");
|
||||
const scratchCanvas = document.createElement("canvas");
|
||||
for (const canvas of [sourceCanvas, baseCanvas, scratchCanvas]) {
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
}
|
||||
|
||||
sourceCanvas.getContext("2d")?.drawImage(sourceBitmap, 0, 0, width, height);
|
||||
baseCanvas.getContext("2d")?.drawImage(sourceBitmap, 0, 0, width, height);
|
||||
sourceCanvasRef.current = sourceCanvas;
|
||||
baseCanvasRef.current = baseCanvas;
|
||||
scratchCanvasRef.current = scratchCanvas;
|
||||
|
||||
const output = canvasRef.current;
|
||||
if (output) {
|
||||
output.width = width;
|
||||
output.height = height;
|
||||
output.getContext("2d")?.drawImage(baseCanvas, 0, 0);
|
||||
}
|
||||
|
||||
sourceBitmap.close();
|
||||
setOperations([]);
|
||||
};
|
||||
|
||||
void initialize();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [editMode, files]);
|
||||
|
||||
const clearResult = useCallback(() => {
|
||||
if (resultUrlRef.current) {
|
||||
URL.revokeObjectURL(resultUrlRef.current);
|
||||
resultUrlRef.current = "";
|
||||
}
|
||||
setResultUrl("");
|
||||
setOperations([]);
|
||||
activeStrokeRef.current = null;
|
||||
reset();
|
||||
}, [reset]);
|
||||
|
||||
const handleFilesDrop = useCallback(
|
||||
(nextFiles: File[]) => {
|
||||
setFiles(nextFiles);
|
||||
setSourceUrl(
|
||||
nextFiles.length > 0 ? URL.createObjectURL(nextFiles[0]) : ""
|
||||
);
|
||||
clearResult();
|
||||
},
|
||||
[clearResult]
|
||||
);
|
||||
|
||||
const execute = useCallback(
|
||||
async (options: BackgroundRemoverOptions) => {
|
||||
if (files.length === 0) return;
|
||||
const nextResult = await run({ files }, options);
|
||||
if (nextResult?.type === "files" && nextResult.files.length > 0) {
|
||||
if (resultUrlRef.current) URL.revokeObjectURL(resultUrlRef.current);
|
||||
const url = URL.createObjectURL(nextResult.files[0].blob);
|
||||
resultUrlRef.current = url;
|
||||
setResultUrl(url);
|
||||
}
|
||||
},
|
||||
[files, run]
|
||||
);
|
||||
|
||||
const handleRun = useCallback(async () => {
|
||||
const options = { quality, useGpu };
|
||||
if (plugin.getRunWarning) {
|
||||
const nextWarning = await plugin.getRunWarning({ files }, options);
|
||||
if (nextWarning) {
|
||||
setPendingOptions(options);
|
||||
setWarning(nextWarning);
|
||||
return;
|
||||
}
|
||||
}
|
||||
await execute(options);
|
||||
}, [execute, files, plugin, quality, useGpu]);
|
||||
|
||||
const pointFromEvent = (
|
||||
event: ReactPointerEvent<HTMLCanvasElement>
|
||||
): Point => {
|
||||
const rect = event.currentTarget.getBoundingClientRect();
|
||||
return {
|
||||
x: Math.max(0, Math.min(1, (event.clientX - rect.left) / rect.width)),
|
||||
y: Math.max(0, Math.min(1, (event.clientY - rect.top) / rect.height)),
|
||||
};
|
||||
};
|
||||
|
||||
const handlePointerDown = (event: ReactPointerEvent<HTMLCanvasElement>) => {
|
||||
if (!canvasRef.current || !sourceCanvasRef.current || !scratchCanvasRef.current) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
if (editorAction === "sample") {
|
||||
const selection: SampleSelection = {
|
||||
type: "sample",
|
||||
point: pointFromEvent(event),
|
||||
tolerance,
|
||||
};
|
||||
applySampleSelection(
|
||||
canvasRef.current,
|
||||
sourceCanvasRef.current,
|
||||
selection
|
||||
);
|
||||
setOperations((current) => [...current, selection]);
|
||||
return;
|
||||
}
|
||||
|
||||
event.currentTarget.setPointerCapture(event.pointerId);
|
||||
const stroke: MaskStroke = {
|
||||
type: "stroke",
|
||||
action: editorAction,
|
||||
points: [pointFromEvent(event)],
|
||||
size: brushSize / 100,
|
||||
};
|
||||
activeStrokeRef.current = stroke;
|
||||
setIsDrawing(true);
|
||||
applyStroke(
|
||||
canvasRef.current,
|
||||
sourceCanvasRef.current,
|
||||
scratchCanvasRef.current,
|
||||
stroke
|
||||
);
|
||||
};
|
||||
|
||||
const handlePointerMove = (event: ReactPointerEvent<HTMLCanvasElement>) => {
|
||||
const active = activeStrokeRef.current;
|
||||
const output = canvasRef.current;
|
||||
const source = sourceCanvasRef.current;
|
||||
const scratch = scratchCanvasRef.current;
|
||||
if (!isDrawing || !active || !output || !source || !scratch) return;
|
||||
|
||||
const point = pointFromEvent(event);
|
||||
const segment: MaskStroke = {
|
||||
...active,
|
||||
points: [active.points.at(-1) ?? point, point],
|
||||
};
|
||||
active.points.push(point);
|
||||
applyStroke(output, source, scratch, segment);
|
||||
};
|
||||
|
||||
const finishStroke = (event: ReactPointerEvent<HTMLCanvasElement>) => {
|
||||
const active = activeStrokeRef.current;
|
||||
if (!active) return;
|
||||
if (event.currentTarget.hasPointerCapture(event.pointerId)) {
|
||||
event.currentTarget.releasePointerCapture(event.pointerId);
|
||||
}
|
||||
activeStrokeRef.current = null;
|
||||
setIsDrawing(false);
|
||||
setOperations((current) => [...current, active]);
|
||||
};
|
||||
|
||||
const handleUndo = () => {
|
||||
setOperations((current) => {
|
||||
const next = current.slice(0, -1);
|
||||
renderOperations(next);
|
||||
return next;
|
||||
});
|
||||
};
|
||||
|
||||
const handleResetMask = () => {
|
||||
setOperations([]);
|
||||
renderOperations([]);
|
||||
};
|
||||
|
||||
const handleDownloadEdited = async () => {
|
||||
const canvas = canvasRef.current;
|
||||
if (!canvas || files.length === 0) return;
|
||||
const blob = await new Promise<Blob | null>((resolve) =>
|
||||
canvas.toBlob(resolve, "image/png")
|
||||
);
|
||||
if (blob) {
|
||||
const name =
|
||||
files[0].name.replace(/\.[^./\\]+$/, "") + "_manual-no-bg.png";
|
||||
downloadBlob(blob, name);
|
||||
}
|
||||
};
|
||||
|
||||
const handleModeChange = (nextMode: EditMode) => {
|
||||
setEditMode(nextMode);
|
||||
clearResult();
|
||||
};
|
||||
|
||||
const handleClearModels = async () => {
|
||||
setIsClearingCache(true);
|
||||
setCacheMessage("");
|
||||
try {
|
||||
const deleted = await clearImglyCache();
|
||||
const released = disposePersistentWorker(plugin.manifest.id);
|
||||
setCacheMessage(
|
||||
deleted || released
|
||||
? "Cached models cleared. Automatic mode will download them again."
|
||||
: "No cached models were found."
|
||||
);
|
||||
} catch {
|
||||
setCacheMessage("Could not clear cached models.");
|
||||
} finally {
|
||||
setIsClearingCache(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="mx-auto max-w-6xl p-[18px] md:p-[28px]">
|
||||
<div className="grid grid-cols-1 gap-6 lg:grid-cols-[minmax(0,1fr)_300px]">
|
||||
<div className="space-y-6">
|
||||
{files.length === 0 ? (
|
||||
<Card title="Upload Image">
|
||||
<Dropzone
|
||||
accept="image/jpeg,image/png,image/webp"
|
||||
multiple={false}
|
||||
maxSizeMb={25}
|
||||
onFilesDrop={handleFilesDrop}
|
||||
/>
|
||||
</Card>
|
||||
) : (
|
||||
<Card
|
||||
title={
|
||||
editMode === "select-area"
|
||||
? "Select Background"
|
||||
: resultFile
|
||||
? "Result"
|
||||
: "Source Image"
|
||||
}
|
||||
>
|
||||
<div
|
||||
className="flex min-h-[320px] items-center justify-center overflow-hidden rounded-xl border border-[var(--p-border)]"
|
||||
style={{
|
||||
backgroundColor: "#fff",
|
||||
backgroundImage:
|
||||
"linear-gradient(45deg,#ddd 25%,transparent 25%),linear-gradient(-45deg,#ddd 25%,transparent 25%),linear-gradient(45deg,transparent 75%,#ddd 75%),linear-gradient(-45deg,transparent 75%,#ddd 75%)",
|
||||
backgroundPosition: "0 0,0 8px,8px -8px,-8px 0",
|
||||
backgroundSize: "16px 16px",
|
||||
}}
|
||||
>
|
||||
{editMode === "select-area" ? (
|
||||
<canvas
|
||||
ref={canvasRef}
|
||||
onPointerDown={handlePointerDown}
|
||||
onPointerMove={handlePointerMove}
|
||||
onPointerUp={finishStroke}
|
||||
onPointerCancel={finishStroke}
|
||||
className="max-h-[65vh] max-w-full cursor-crosshair object-contain"
|
||||
style={{ touchAction: "none" }}
|
||||
aria-label="Background mask editor"
|
||||
/>
|
||||
) : (
|
||||
<img
|
||||
src={resultUrl || sourceUrl}
|
||||
alt={resultFile ? "Background removed" : "Source"}
|
||||
className="max-h-[65vh] max-w-full object-contain"
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
{editMode === "select-area" && (
|
||||
<p className="mb-0 mt-3 text-sm text-[var(--p-muted)]">
|
||||
Choose Select background and click a representative point.
|
||||
Only the connected region with a similar color is removed.
|
||||
</p>
|
||||
)}
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{isExecuting && (
|
||||
<ToolProgress
|
||||
percentage={progress.percentage ?? 0}
|
||||
message={progress.message}
|
||||
/>
|
||||
)}
|
||||
|
||||
{error && (
|
||||
<Card className="border-red-300">
|
||||
<p className="m-0 text-sm text-red-700">{error}</p>
|
||||
</Card>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="space-y-6">
|
||||
<Card title="Mode">
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<Button
|
||||
variant={editMode === "automatic" ? "primary" : "secondary"}
|
||||
onClick={() => handleModeChange("automatic")}
|
||||
disabled={isExecuting}
|
||||
>
|
||||
Automatic
|
||||
</Button>
|
||||
<Button
|
||||
variant={editMode === "select-area" ? "primary" : "secondary"}
|
||||
onClick={() => handleModeChange("select-area")}
|
||||
disabled={isExecuting}
|
||||
>
|
||||
Select area
|
||||
</Button>
|
||||
</div>
|
||||
<p className="mb-0 mt-3 text-xs leading-relaxed text-[var(--p-muted)]">
|
||||
Select area uses a sampled background color and does not run the
|
||||
AI model.
|
||||
</p>
|
||||
</Card>
|
||||
|
||||
<Card title="Settings">
|
||||
<div className="space-y-5">
|
||||
{editMode === "automatic" ? (
|
||||
<>
|
||||
<Select
|
||||
label="Model"
|
||||
value={quality}
|
||||
disabled={isExecuting}
|
||||
onChange={(event) => {
|
||||
setQuality(
|
||||
event.target.value as BackgroundRemoverOptions["quality"]
|
||||
);
|
||||
clearResult();
|
||||
}}
|
||||
options={[
|
||||
{ label: "Smallest download (quantized)", value: "small" },
|
||||
{ label: "Balanced (FP16)", value: "medium" },
|
||||
{ label: "Best quality (FP32)", value: "large" },
|
||||
]}
|
||||
/>
|
||||
|
||||
<label className="flex items-center gap-2 text-sm text-[var(--p-text)]">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={useGpu}
|
||||
disabled={isExecuting}
|
||||
onChange={(event) => {
|
||||
setUseGpu(event.target.checked);
|
||||
clearResult();
|
||||
}}
|
||||
className="h-4 w-4 accent-[var(--p-accent)]"
|
||||
/>
|
||||
Use GPU acceleration
|
||||
</label>
|
||||
</>
|
||||
) : (
|
||||
<div className="space-y-4">
|
||||
<div className="grid grid-cols-3 gap-2">
|
||||
<Button
|
||||
variant={editorAction === "sample" ? "primary" : "secondary"}
|
||||
onClick={() => setEditorAction("sample")}
|
||||
>
|
||||
Select
|
||||
</Button>
|
||||
<Button
|
||||
variant={editorAction === "remove" ? "primary" : "secondary"}
|
||||
onClick={() => setEditorAction("remove")}
|
||||
>
|
||||
Remove
|
||||
</Button>
|
||||
<Button
|
||||
variant={editorAction === "restore" ? "primary" : "secondary"}
|
||||
onClick={() => setEditorAction("restore")}
|
||||
>
|
||||
Restore
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{editorAction === "sample" ? (
|
||||
<Slider
|
||||
label={`Color tolerance: ${tolerance}%`}
|
||||
min={1}
|
||||
max={40}
|
||||
value={tolerance}
|
||||
onChange={(event) =>
|
||||
setTolerance(Number(event.target.value))
|
||||
}
|
||||
/>
|
||||
) : (
|
||||
<Slider
|
||||
label={`Brush size: ${brushSize}%`}
|
||||
min={1}
|
||||
max={20}
|
||||
value={brushSize}
|
||||
onChange={(event) =>
|
||||
setBrushSize(Number(event.target.value))
|
||||
}
|
||||
/>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<Button
|
||||
variant="secondary"
|
||||
disabled={operations.length === 0}
|
||||
onClick={handleUndo}
|
||||
>
|
||||
Undo
|
||||
</Button>
|
||||
<Button
|
||||
variant="secondary"
|
||||
disabled={operations.length === 0}
|
||||
onClick={handleResetMask}
|
||||
>
|
||||
Reset
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="space-y-2 border-t border-[var(--p-border)] pt-4">
|
||||
{editMode === "select-area" ? (
|
||||
<Button
|
||||
className="w-full"
|
||||
disabled={files.length === 0}
|
||||
onClick={handleDownloadEdited}
|
||||
>
|
||||
Download selected result
|
||||
</Button>
|
||||
) : !resultFile ? (
|
||||
<Button
|
||||
className="w-full"
|
||||
disabled={files.length === 0 || isExecuting}
|
||||
onClick={handleRun}
|
||||
>
|
||||
{isExecuting ? "Processing..." : "Remove background"}
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
className="w-full"
|
||||
onClick={() => downloadBlob(resultFile.blob, resultFile.name)}
|
||||
>
|
||||
Download PNG
|
||||
</Button>
|
||||
)}
|
||||
{files.length > 0 && (
|
||||
<Button
|
||||
variant="secondary"
|
||||
className="w-full"
|
||||
disabled={isExecuting}
|
||||
onClick={() => {
|
||||
setFiles([]);
|
||||
setSourceUrl("");
|
||||
clearResult();
|
||||
}}
|
||||
>
|
||||
Choose another image
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2 border-t border-[var(--p-border)] pt-4">
|
||||
<Button
|
||||
variant="danger"
|
||||
className="w-full"
|
||||
disabled={isExecuting || isClearingCache}
|
||||
onClick={handleClearModels}
|
||||
>
|
||||
{isClearingCache ? "Clearing..." : "Clear models"}
|
||||
</Button>
|
||||
<p className="m-0 text-xs leading-relaxed text-[var(--p-muted)]">
|
||||
Removes downloaded AI models and ONNX runtime files from this
|
||||
browser.
|
||||
</p>
|
||||
{cacheMessage && (
|
||||
<p
|
||||
role="status"
|
||||
className="m-0 text-xs font-medium text-[var(--p-text)]"
|
||||
>
|
||||
{cacheMessage}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{warning && pendingOptions && (
|
||||
<div className="fixed inset-0 z-[70] flex items-center justify-center bg-black/50 p-4">
|
||||
<div
|
||||
role="alertdialog"
|
||||
aria-modal="true"
|
||||
className="w-full max-w-md rounded-2xl border border-[var(--p-border)] bg-[var(--p-surface)] p-6"
|
||||
>
|
||||
<h2 className="m-0 text-lg font-semibold text-[var(--p-text)]">
|
||||
One-time download
|
||||
</h2>
|
||||
<p className="my-4 text-sm leading-relaxed text-[var(--p-muted)]">
|
||||
{warning}
|
||||
</p>
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button
|
||||
variant="secondary"
|
||||
onClick={() => {
|
||||
setWarning(null);
|
||||
setPendingOptions(null);
|
||||
}}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
const options = pendingOptions;
|
||||
setWarning(null);
|
||||
setPendingOptions(null);
|
||||
void execute(options);
|
||||
}}
|
||||
>
|
||||
Download & run
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
91
src/tools/background-remover/imgly-cache.ts
Normal file
91
src/tools/background-remover/imgly-cache.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Persistent caching for the self-hosted @imgly model + wasm assets.
|
||||
*
|
||||
* @imgly/background-removal fetches its assets with plain `fetch`, so whether
|
||||
* they persist between runs is at the mercy of the server's Cache-Control
|
||||
* headers. The Vite dev server (and some hosts) send `no-store`/`no-cache`,
|
||||
* which makes the model re-download on every run.
|
||||
*
|
||||
* To make caching deterministic and host-independent, we intercept same-origin
|
||||
* `/imgly/` requests and back them with the Cache Storage API, which we control
|
||||
* explicitly. CacheStorage is shared across the origin, so an asset cached by
|
||||
* the worker (during a run) is visible to the main thread (for the pre-run
|
||||
* download estimate) and vice-versa.
|
||||
*/
|
||||
export const IMGLY_CACHE_NAME = "plimi-imgly-v1";
|
||||
|
||||
type FetchInput = Parameters<typeof fetch>[0];
|
||||
|
||||
function urlOf(input: FetchInput): string {
|
||||
if (typeof input === "string") return input;
|
||||
if (input instanceof URL) return input.toString();
|
||||
return input.url;
|
||||
}
|
||||
|
||||
function isImglyUrl(url: string): boolean {
|
||||
return url.includes("/imgly/");
|
||||
}
|
||||
|
||||
let installed = false;
|
||||
|
||||
/** Route `/imgly/` fetches through Cache Storage. Safe to call multiple times. */
|
||||
export function installImglyCache(): void {
|
||||
if (installed) return;
|
||||
installed = true;
|
||||
|
||||
const scope = self as unknown as {
|
||||
fetch: typeof fetch;
|
||||
caches?: CacheStorage;
|
||||
};
|
||||
if (!scope.caches || typeof scope.fetch !== "function") return;
|
||||
|
||||
const original = scope.fetch.bind(scope);
|
||||
scope.fetch = async (input: FetchInput, init?: RequestInit) => {
|
||||
try {
|
||||
const url = urlOf(input);
|
||||
if (isImglyUrl(url)) {
|
||||
const cache = await scope.caches!.open(IMGLY_CACHE_NAME);
|
||||
const hit = await cache.match(url);
|
||||
if (hit) return hit;
|
||||
const res = await original(input, init);
|
||||
if (res.ok) {
|
||||
try {
|
||||
await cache.put(url, res.clone());
|
||||
} catch {
|
||||
// Quota or opaque response — fall through with the live response.
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
} catch {
|
||||
// Any cache failure: fall back to the network below.
|
||||
}
|
||||
return original(input, init);
|
||||
};
|
||||
}
|
||||
|
||||
/** Whether an `/imgly/` asset is already stored locally (no download needed). */
|
||||
export async function isImglyAssetCached(url: string): Promise<boolean> {
|
||||
const scope = self as unknown as { caches?: CacheStorage };
|
||||
if (scope.caches) {
|
||||
try {
|
||||
const cache = await scope.caches.open(IMGLY_CACHE_NAME);
|
||||
return Boolean(await cache.match(url));
|
||||
} catch {
|
||||
// fall through to HTTP-cache probe
|
||||
}
|
||||
}
|
||||
try {
|
||||
const res = await fetch(url, { cache: "only-if-cached", mode: "same-origin" });
|
||||
return res.ok;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Delete all persisted IMG.LY model and runtime assets for this origin. */
|
||||
export async function clearImglyCache(): Promise<boolean> {
|
||||
const scope = globalThis as typeof globalThis & { caches?: CacheStorage };
|
||||
if (!scope.caches) return false;
|
||||
return scope.caches.delete(IMGLY_CACHE_NAME);
|
||||
}
|
||||
86
src/tools/background-remover/index.ts
Normal file
86
src/tools/background-remover/index.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { lazy } from "react";
|
||||
import type { PlimiPlugin } from "../../core/plugins/plugin-types";
|
||||
import { runBackgroundRemover } from "./run";
|
||||
import {
|
||||
estimateUncachedDownloadBytes,
|
||||
type BackgroundRemoverOptions,
|
||||
} from "./remove";
|
||||
import BgWorker from "./worker?worker";
|
||||
|
||||
export type { BackgroundRemoverOptions } from "./remove";
|
||||
|
||||
const BackgroundRemoverUi = lazy(() => import("./BackgroundRemoverUi"));
|
||||
|
||||
function formatMb(bytes: number): string {
|
||||
return `${(bytes / 1024 / 1024).toFixed(0)} MB`;
|
||||
}
|
||||
|
||||
export const backgroundRemoverPlugin: PlimiPlugin<BackgroundRemoverOptions> = {
|
||||
manifest: {
|
||||
id: "background-remover",
|
||||
name: "Background Remover",
|
||||
description:
|
||||
"Remove the background from any image with AI, entirely in your browser. Your photo is never uploaded.",
|
||||
category: "image",
|
||||
version: "1.0.0",
|
||||
tags: ["image", "background", "remove", "ai", "transparent", "cutout"],
|
||||
input: {
|
||||
type: "files",
|
||||
accept: ["image/png", "image/jpeg", "image/webp"],
|
||||
multiple: false,
|
||||
},
|
||||
output: { type: "files" },
|
||||
offlineReady: true,
|
||||
},
|
||||
|
||||
optionsSchema: {
|
||||
fields: [
|
||||
{
|
||||
type: "select",
|
||||
key: "quality",
|
||||
label: "Quality",
|
||||
defaultValue: "small",
|
||||
options: [
|
||||
{ label: "Smallest download (quantized)", value: "small" },
|
||||
{ label: "Balanced (FP16)", value: "medium" },
|
||||
{ label: "Best quality (FP32)", value: "large" },
|
||||
],
|
||||
},
|
||||
{
|
||||
type: "boolean",
|
||||
key: "useGpu",
|
||||
label: "Use GPU (WebGPU) when available",
|
||||
defaultValue: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
capabilities: {
|
||||
cancelable: false,
|
||||
worker: true,
|
||||
persistentWorker: true,
|
||||
wasm: true,
|
||||
customUi: true,
|
||||
preview: true,
|
||||
},
|
||||
|
||||
getRunWarning: async (_input, options) => {
|
||||
const bytes = await estimateUncachedDownloadBytes(options, self.location.origin);
|
||||
if (bytes <= 0) return null;
|
||||
const label =
|
||||
options.quality === "small"
|
||||
? "Smallest download"
|
||||
: options.quality === "medium"
|
||||
? "Balanced"
|
||||
: "Best quality";
|
||||
return (
|
||||
`Running the “${label}” model needs a one-time download of about ` +
|
||||
`${formatMb(bytes)} to your device. It is cached afterwards, so future ` +
|
||||
`runs are instant and fully offline. Continue?`
|
||||
);
|
||||
},
|
||||
|
||||
run: runBackgroundRemover,
|
||||
worker: () => new BgWorker(),
|
||||
customUi: BackgroundRemoverUi,
|
||||
};
|
||||
42
src/tools/background-remover/remove.test.ts
Normal file
42
src/tools/background-remover/remove.test.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createBackgroundRemovalProgress } from "./remove";
|
||||
|
||||
describe("background remover progress", () => {
|
||||
it("aggregates resource downloads without resetting the percentage", () => {
|
||||
const report = vi.fn();
|
||||
const progress = createBackgroundRemovalProgress(
|
||||
new Map([
|
||||
["/models/isnet_quint8", 80],
|
||||
["/onnxruntime-web/ort.wasm", 20],
|
||||
]),
|
||||
report
|
||||
);
|
||||
|
||||
progress("fetch:/models/isnet_quint8", 40, 80);
|
||||
progress("fetch:/models/isnet_quint8", 80, 80);
|
||||
progress("fetch:/onnxruntime-web/ort.wasm", 10, 20);
|
||||
progress("compute:decode", 0, 4);
|
||||
progress("compute:inference", 1, 4);
|
||||
progress("compute:encode", 4, 4);
|
||||
|
||||
const percentages = report.mock.calls.map(
|
||||
([value]) => value.percentage as number
|
||||
);
|
||||
expect(percentages).toEqual([...percentages].sort((a, b) => a - b));
|
||||
expect(percentages.at(-1)).toBe(100);
|
||||
});
|
||||
|
||||
it("uses the full progress range when the model session is already loaded", () => {
|
||||
const report = vi.fn();
|
||||
const progress = createBackgroundRemovalProgress(new Map(), report);
|
||||
|
||||
progress("compute:decode", 0, 4);
|
||||
progress("compute:inference", 1, 4);
|
||||
progress("compute:mask", 2, 4);
|
||||
progress("compute:encode", 4, 4);
|
||||
|
||||
expect(report.mock.calls.map(([value]) => value.percentage)).toEqual([
|
||||
0, 25, 50, 100,
|
||||
]);
|
||||
});
|
||||
});
|
||||
194
src/tools/background-remover/remove.ts
Normal file
194
src/tools/background-remover/remove.ts
Normal file
@@ -0,0 +1,194 @@
|
||||
import { removeBackground } from "@imgly/background-removal";
|
||||
import type { ToolResult } from "../../core/io/output-types";
|
||||
import type { ToolProgress } from "../../core/plugins/plugin-types";
|
||||
import { installImglyCache, isImglyAssetCached } from "./imgly-cache";
|
||||
|
||||
export interface BackgroundRemoverOptions {
|
||||
/** Quality/size trade-off: small=quint8, medium=fp16, large=fp32. */
|
||||
quality: "small" | "medium" | "large";
|
||||
/** Use WebGPU when available (falls back to wasm automatically). */
|
||||
useGpu: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Self-hosted asset location. The @imgly model + onnxruntime-web wasm files are
|
||||
* mirrored into `public/imgly/` (see scripts/fetch-imgly-assets.mjs) so nothing
|
||||
* is fetched from a third party — the image never leaves the browser.
|
||||
*/
|
||||
function publicPath(origin: string): string {
|
||||
return new URL("/imgly/", origin).toString();
|
||||
}
|
||||
|
||||
const MODEL_BY_QUALITY: Record<
|
||||
BackgroundRemoverOptions["quality"],
|
||||
"isnet" | "isnet_fp16" | "isnet_quint8"
|
||||
> = {
|
||||
small: "isnet_quint8",
|
||||
medium: "isnet_fp16",
|
||||
large: "isnet",
|
||||
};
|
||||
|
||||
interface ResourceEntry {
|
||||
size: number;
|
||||
chunks: Array<{ name: string }>;
|
||||
}
|
||||
type ResourceMap = Record<string, ResourceEntry>;
|
||||
|
||||
function resourceKeys(options: BackgroundRemoverOptions): string[] {
|
||||
const wasmBase = options.useGpu
|
||||
? "/onnxruntime-web/ort-wasm-simd-threaded.jsep"
|
||||
: "/onnxruntime-web/ort-wasm-simd-threaded";
|
||||
|
||||
return [
|
||||
`/models/${MODEL_BY_QUALITY[options.quality]}`,
|
||||
`${wasmBase}.wasm`,
|
||||
`${wasmBase}.mjs`,
|
||||
];
|
||||
}
|
||||
|
||||
async function loadResourceMap(base: string): Promise<ResourceMap | null> {
|
||||
try {
|
||||
const res = await fetch(new URL("resources.json", base).toString());
|
||||
if (!res.ok) return null;
|
||||
return (await res.json()) as ResourceMap;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function createBackgroundRemovalProgress(
|
||||
expectedSizes: ReadonlyMap<string, number>,
|
||||
reportProgress?: (progress: ToolProgress) => void
|
||||
): (key: string, current: number, total: number) => void {
|
||||
const fetchedByKey = new Map<string, number>();
|
||||
const expectedTotal = [...expectedSizes.values()].reduce(
|
||||
(sum, size) => sum + size,
|
||||
0
|
||||
);
|
||||
let sawFetch = false;
|
||||
let lastPercentage = 0;
|
||||
|
||||
return (key, current, total) => {
|
||||
let percentage: number;
|
||||
let message: string;
|
||||
|
||||
if (key.startsWith("fetch:")) {
|
||||
sawFetch = true;
|
||||
const resourceKey = key.slice("fetch:".length);
|
||||
fetchedByKey.set(resourceKey, Math.min(current, total));
|
||||
|
||||
const fetched = [...fetchedByKey.values()].reduce(
|
||||
(sum, size) => sum + size,
|
||||
0
|
||||
);
|
||||
const knownTotal =
|
||||
expectedTotal > 0
|
||||
? expectedTotal
|
||||
: [...fetchedByKey.keys()].reduce(
|
||||
(sum, knownKey) =>
|
||||
sum + (knownKey === resourceKey ? total : fetchedByKey.get(knownKey) ?? 0),
|
||||
0
|
||||
);
|
||||
|
||||
percentage = knownTotal > 0 ? (fetched / knownTotal) * 65 : lastPercentage;
|
||||
message = "Loading AI model...";
|
||||
} else if (key.startsWith("compute:")) {
|
||||
const computePercentage = total > 0 ? current / total : 0;
|
||||
percentage = sawFetch
|
||||
? 65 + computePercentage * 35
|
||||
: computePercentage * 100;
|
||||
message =
|
||||
current === 0 ? "Preparing image..." : "Removing background...";
|
||||
} else {
|
||||
percentage = lastPercentage;
|
||||
message = "Processing...";
|
||||
}
|
||||
|
||||
lastPercentage = Math.max(lastPercentage, Math.min(100, percentage));
|
||||
reportProgress?.({ percentage: lastPercentage, message });
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Estimate how many bytes still need downloading before the tool can run with
|
||||
* the given options. Returns 0 when everything is already cached locally.
|
||||
*/
|
||||
export async function estimateUncachedDownloadBytes(
|
||||
options: BackgroundRemoverOptions,
|
||||
origin: string
|
||||
): Promise<number> {
|
||||
const base = publicPath(origin);
|
||||
const resources = await loadResourceMap(base);
|
||||
if (!resources) return 0;
|
||||
|
||||
let bytes = 0;
|
||||
for (const key of resourceKeys(options)) {
|
||||
const entry = resources[key];
|
||||
if (!entry?.chunks?.length) continue;
|
||||
// Chunks of a resource are fetched together, so the first chunk is a
|
||||
// reliable proxy for whether the whole resource is cached.
|
||||
const firstChunkUrl = new URL(entry.chunks[0].name, base).toString();
|
||||
if (!(await isImglyAssetCached(firstChunkUrl))) {
|
||||
bytes += entry.size;
|
||||
}
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
export async function removeImageBackground(
|
||||
fileName: string,
|
||||
imageData: Blob | ArrayBuffer | Uint8Array,
|
||||
options: BackgroundRemoverOptions,
|
||||
origin: string,
|
||||
reportProgress?: (progress: ToolProgress) => void
|
||||
): Promise<ToolResult> {
|
||||
// Back /imgly/ fetches with Cache Storage so the model persists between runs
|
||||
// regardless of server cache headers.
|
||||
installImglyCache();
|
||||
|
||||
const base = publicPath(origin);
|
||||
const resources = await loadResourceMap(base);
|
||||
const expectedSizes = new Map(
|
||||
resourceKeys(options).flatMap((key) => {
|
||||
const size = resources?.[key]?.size;
|
||||
return size === undefined ? [] : [[key, size] as const];
|
||||
})
|
||||
);
|
||||
const progress = createBackgroundRemovalProgress(
|
||||
expectedSizes,
|
||||
reportProgress
|
||||
);
|
||||
|
||||
const source =
|
||||
imageData instanceof Blob
|
||||
? imageData
|
||||
: new Blob([
|
||||
(imageData instanceof Uint8Array
|
||||
? imageData
|
||||
: new Uint8Array(imageData)) as unknown as BlobPart,
|
||||
]);
|
||||
|
||||
const resultBlob = await removeBackground(source, {
|
||||
publicPath: base,
|
||||
model: MODEL_BY_QUALITY[options.quality],
|
||||
device: options.useGpu ? "gpu" : "cpu",
|
||||
// We already run inside a Web Worker; avoid nested worker proxying.
|
||||
proxyToWorker: false,
|
||||
output: { format: "image/png" },
|
||||
progress,
|
||||
});
|
||||
|
||||
const outName = fileName.replace(/\.[^./\\]+$/, "") + "_no-bg.png";
|
||||
|
||||
return {
|
||||
type: "files",
|
||||
files: [
|
||||
{
|
||||
name: outName,
|
||||
mimeType: "image/png",
|
||||
blob: resultBlob,
|
||||
sizeAfter: resultBlob.size,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
37
src/tools/background-remover/run.test.ts
Normal file
37
src/tools/background-remover/run.test.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { backgroundRemoverPlugin } from "./index";
|
||||
import { runBackgroundRemover } from "./run";
|
||||
import type { BackgroundRemoverOptions } from "./remove";
|
||||
import type { ToolContext } from "../../core/plugins/plugin-types";
|
||||
|
||||
const mockContext: ToolContext = {
|
||||
signal: new AbortController().signal,
|
||||
reportProgress: vi.fn(),
|
||||
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },
|
||||
};
|
||||
|
||||
const options: BackgroundRemoverOptions = { quality: "small", useGpu: true };
|
||||
|
||||
describe("Background Remover Plugin", () => {
|
||||
it("should have correct manifest", () => {
|
||||
expect(backgroundRemoverPlugin.manifest.id).toBe("background-remover");
|
||||
expect(backgroundRemoverPlugin.manifest.category).toBe("image");
|
||||
expect(backgroundRemoverPlugin.manifest.input.type).toBe("files");
|
||||
expect(backgroundRemoverPlugin.manifest.output.type).toBe("files");
|
||||
});
|
||||
|
||||
it("should run in a worker with wasm capability", () => {
|
||||
expect(backgroundRemoverPlugin.capabilities?.worker).toBe(true);
|
||||
expect(backgroundRemoverPlugin.capabilities?.persistentWorker).toBe(true);
|
||||
expect(backgroundRemoverPlugin.capabilities?.wasm).toBe(true);
|
||||
expect(backgroundRemoverPlugin.capabilities?.customUi).toBe(true);
|
||||
expect(backgroundRemoverPlugin.worker).toBeDefined();
|
||||
expect(backgroundRemoverPlugin.customUi).toBeDefined();
|
||||
});
|
||||
|
||||
it("should throw if no image is provided", async () => {
|
||||
await expect(
|
||||
runBackgroundRemover({ files: [] }, options, mockContext)
|
||||
).rejects.toThrow("No image provided.");
|
||||
});
|
||||
});
|
||||
26
src/tools/background-remover/run.ts
Normal file
26
src/tools/background-remover/run.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import type { ToolInput } from "../../core/io/input-types";
|
||||
import type { ToolResult } from "../../core/io/output-types";
|
||||
import type { ToolContext } from "../../core/plugins/plugin-types";
|
||||
import { removeImageBackground, type BackgroundRemoverOptions } from "./remove";
|
||||
|
||||
export async function runBackgroundRemover(
|
||||
input: ToolInput,
|
||||
options: BackgroundRemoverOptions,
|
||||
context: ToolContext
|
||||
): Promise<ToolResult> {
|
||||
const files = input.files;
|
||||
|
||||
if (!files || !Array.isArray(files) || files.length === 0) {
|
||||
throw new Error("No image provided.");
|
||||
}
|
||||
|
||||
const file = files[0];
|
||||
|
||||
return removeImageBackground(
|
||||
file.name,
|
||||
file,
|
||||
options,
|
||||
self.location.origin,
|
||||
context.reportProgress
|
||||
);
|
||||
}
|
||||
44
src/tools/background-remover/worker.ts
Normal file
44
src/tools/background-remover/worker.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type {
|
||||
ToolWorkerRequest,
|
||||
ToolWorkerResponse,
|
||||
} from "../../core/plugins/worker-protocol";
|
||||
import { removeImageBackground, type BackgroundRemoverOptions } from "./remove";
|
||||
|
||||
function post(response: ToolWorkerResponse) {
|
||||
self.postMessage(response);
|
||||
}
|
||||
|
||||
self.addEventListener(
|
||||
"message",
|
||||
async (e: MessageEvent<ToolWorkerRequest<BackgroundRemoverOptions>>) => {
|
||||
const { id, input, options } = e.data;
|
||||
const files = input.files;
|
||||
|
||||
if (!files || !Array.isArray(files) || files.length === 0) {
|
||||
post({ type: "error", id, error: "No image provided." });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const file = files[0];
|
||||
const result = await removeImageBackground(
|
||||
file.name,
|
||||
file,
|
||||
options,
|
||||
self.location.origin,
|
||||
(progress) => post({ type: "progress", id, progress })
|
||||
);
|
||||
|
||||
post({ type: "success", id, result });
|
||||
} catch (error) {
|
||||
post({
|
||||
type: "error",
|
||||
id,
|
||||
error:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "Unknown error occurred while removing the background.",
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -5,4 +5,16 @@ import tailwindcss from "@tailwindcss/vite";
|
||||
// https://vite.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [react(), tailwindcss()],
|
||||
server: {
|
||||
headers: {
|
||||
"Cross-Origin-Opener-Policy": "same-origin",
|
||||
"Cross-Origin-Embedder-Policy": "require-corp",
|
||||
},
|
||||
},
|
||||
preview: {
|
||||
headers: {
|
||||
"Cross-Origin-Opener-Policy": "same-origin",
|
||||
"Cross-Origin-Embedder-Policy": "require-corp",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user