How I deploy serverless containers for free
Summary
TLDRこの動画は、インターネットからダウンロードした画像の背景を簡単に削除する方法について説明しています。作成者は、Pythonで作ったWebアプリを使って背景を除去し、Dockerを使って複数のコンピュータで使用し、クラウドに無料で展開する方法についても紹介しています。特に、AIモデルを用いた背景除去のPythonパッケージ「remove BG」を使用し、Flaskで簡単なHTTPルートを作成。Dockerを通じてコードを複数のマシンで実行し、Google Cloud Runで公開したプロセスも解説されています。
Takeaways
- 🌐 インターネットから画像をダウンロードするとき、背景が透明に見えるものが実際には白い背景になっていることがイライラする。
- 🛠️ 現在、remove BGやPhotoshopの新しいAIツールなど、背景を削除するツールが豊富に存在している。
- 🎥 動画クリエイターは、背景を除去した画像を多く使用し、効率的な方法で作業を進める必要がある。
- 💻 開発者にとって、Photoshopで画像をアップロード、背景を削除、再エクスポート、Premiereに戻すプロセスは極めて非効率的である。
- 🚀 自分自身のアプリを構築し、効率を向上させる唯一の選択肢は、アプリをゼロから開発することだった。
- 🗂️ Dockerを使用して、PythonのWebアプリである背景除去ツールをコンテナ化し、ローカルでもクラウドにでも簡単に実行できる。
- 🐍 Pythonを選択した理由は、remove BGというPythonパッケージがあるからで、これはUEトモデルを基に背景を自動除去する。
- 🔗 Flaskを使用して、ドラッグ&ドロップで画像をアップロードし、背景を除去するシンプルなHTTPルートを作成。
- 📄 Dockerファイルを作成し、Pythonのベースイメージを使用して、依存関係をインストールし、コードをコピーして、ポートを公開し、アプリを実行する。
- 🏢 Google Cloud Runを使用して無料でクラウドにデプロイし、世界中の誰でもこのアプリを使用できるようにする。
- 🔄 Docker化することで、コードが移植可能で、異なるマシンで使用したり、他のクラウドサービスに簡単にデプロイしたりできる。
Q & A
どのような問題を解決するために動画の作成者が背景除去アプリを開発しましたか?
-動画の作成者は、インターネットから画像をダウンロードする際に背景が透明に見えるものが実際にはそうではないことが非常に面倒であるため、背景を自動的に除去するアプリを開発しました。
remove BGやPhotoshopのAIツールなどの既存のツールと比べて、作成者のアプリの利点は何ですか?
-既存のツールは効率が悪く、画像をアップロード、背景を除去し、再エクスポートし、Adobe Premiereに戻す必要があります。作成者のアプリは、Pythonのremove BGパッケージを使用し、AIを利用して背景を除去することで、これらの手間を省くことができます。
Dockerを使用してアプリをコンテナ化することの利点は何ですか?
-Dockerを使用することで、アプリを複数のコンピューターで使用し、クラウドにデプロイすることができます。これにより、アプリをローカルで実行する場合や、携帯電話などからアクセスする場合も簡単に使用できます。また、Dockerを使用することで、Pythonの依存関係やAIモデルのウェイトをダウンロードするために必要な時間を節約できます。
動画の作成者はなぜPythonを選んでアプリを開発しましたか?
-Pythonを使用することで、remove BGというPythonパッケージを使用して背景を除去することができます。また、Flaskを使用してシンプルなHTTPルートを作成し、画像をドラッグ&ドロップして処理できるようにしました。
アプリのWebサイトはどのように動作しますか?
-アプリのWebサイトは、index.htmlファイルでレンダリングされ、JavaScriptとCSSを使用してシンプルなHTMLフォームを作成しています。フォームにはファイルの入力があり、フォームが送信されるとPOSTリクエストがルートURLに送信され、背景除去関数が呼び出されます。
Dockerイメージを作成するために必要な手順は何ですか?
-Dockerイメージを作成するためには、Dockerファイルを作成し、Pythonのベースイメージを使用して作業ディレクトリを設定し、依存関係をインストールし、コードをコピー、ポートを公開し、アプリを実行する必要があります。また、AIモデルのウェイトをDockerイメージにコピーして、非同期ダウンロードによる遅延を避けることも重要です。
Google Cloud Runを使用してアプリをデプロイするにはどうすればいいですか?
-Google Cloud Runでアプリをデプロイするには、まずイメージをGoogleのアーティファクトレジストリにアップロードし、新しいサービスを作成し、コンテナイメージを選択します。次に、パブリックウェブサービスとして設定し、CPU割り当てやコンテナのメモリを調整します。最後に、Auto scalingの設定を調整してデプロイします。
Dockerを使用してアプリをクラウドにデプロイする際のメリットは何ですか?
-Dockerを使用することで、アプリのコードがポータブルになり、異なるクラウドサービスに簡単にデプロイできます。また、サーバーレスデプロイを使用することで、使用していないときはコストをかけらずに、リクエストが来た際にスケールすることができます。
動画の作成者がアプリをローカルで使用する際にDockerの利点をどのように説明しましたか?
-Dockerを使用することで、アプリをローカルで実行する際にターミナルからPythonコマンドを実行するだけで済みます。これにより、依存関係のインストールやアプリのバックグラウンド実行などの手間が省け、効率的になります。
動画の作成者がGoogle Cloud Runでアプリをデプロイする際に設定したCPU割り当てとメモリの量はどれくらいですか?
-動画の作成者は、Google Cloud Runでアプリをデプロイする際に、CPU割り当てを常に確保してcold startを避けることが望ましいと述べています。また、AIモデルがメモリを多く使用するため、コンテナのメモリを2GBに設定しました。
動画の作成者がアプリを公開するためにGoogle Cloud Runで設定した最大インスタンス数は何ですか?
-動画の作成者は、Google Cloud Runでアプリをデプロイする際に、最大インスタンス数を3に設定しました。これは、アプリがウイルス的に拡散する必要がないと判断したためです。
Outlines
🖼️ 透明な背景を持つ画像を簡単にダウンロードする方法
この段落では、インターネットから画像をダウンロードする際の面倒な問題と、その解決策について説明されています。特に、画像の背景を透明にすることが望ましい場合、ダウンロードした画像が期待外れの表示になることがあると指摘されています。解決策として、remove BGやPhotoshopの新しいAIツールなど、背景を除去するツールが提案されています。また、作者が自分でアプリを開発し、背景を除去するプロセスを効率化した経験も共有されています。Dockerの使用理由やデプロイ方法についても触れられています。
🔧 PythonとDockerを使った背景除去アプリの構築とデプロイ
この段落では、背景除去のためにPythonを使用し、remove BGというPythonパッケージを活用したアプリの開発と、Dockerを使用してのアプリのコンテナ化とクラウドへのデプロイの詳細について説明されています。特に、AIモデルの重みをDockerイメージに組み込むことで、起動遅延を防いだことが強調されています。また、Google Cloud Runを使用して無料でウェブにデプロイする方法も紹介されています。最後に、Docker化されたコードの移植性についても触れられています。
Mindmap
Keywords
💡transparent background
💡remove BG
💡AI Tools in Photoshop
💡Docker
💡containerization
💡cloud deployment
💡Google Cloud Run
💡Python
💡Flask
💡Pillow
💡digital ocean
💡artifact registry
Highlights
The annoyance of downloading images with non-transparent backgrounds despite appearing so online.
The existence of tools like remove.bg and AI tools in Photoshop for background removal.
The inefficiency of using Photoshop for background removal in a video editing workflow.
The creation of a custom app from scratch for efficient background removal.
The use of Docker for containerizing the app for ease of use and deployment.
The app's ability to run locally with a single command and deploy to the cloud.
The use of the Python programming language for the app development.
Leverage of the remove.bg Python package based on the uet model for AI background removal.
The app's capability to handle both GET and POST methods for image display and processing.
The simplicity of the app's website, using plain JavaScript and CSS.
The convenience of drag-and-drop functionality for image processing.
The Docker file creation for the app, including the AI model weights for efficient operation.
Building the Docker image and tagging it for easy identification and use.
Running the app as a container with Docker Desktop.
The deployment of the Dockerized app to the web using Google Cloud Run.
The option to allow unauthenticated invocations for public access.
Adjusting CPU allocation and container memory for optimal performance.
The portability of the Dockerized code for deployment on any cloud service.
Transcripts
one of the most annoying things in the
world is when you're trying to steal an
image from the internet and it looks
like it's going to have a transparent
background but when you download it it
looks like this luckily nowadays there
are all kinds of good tools for removing
backgrounds from images like remove BG
or the new AI Tools in Photoshop now if
you watch my videos you'll notice I use
a lot of images with the background
removed and it's extremely inefficient
to have to go into Photoshop upload an
image remove its background and then
reexport it and bring it back into Adobe
Premiere as a developer this
inefficiency is to unacceptable and my
only option was to build my own app from
scratch in today's video I want to show
you how I built this background remover
from scratch but more importantly I want
to talk about why I dockerized it and
explain how I deployed it to the cloud
for free earlier this week on the main
Channel I made a Docker video which you
should definitely check out first if you
have no idea what Docker is but that
video was sponsored by Docker this video
is not sponsored I don't work with
sponsors very often but when I do it's
usually with tools that I have firsthand
experience with and Docker is something
I use all the time by containerizing
this background remover which is just a
python web app I'm able to run it
locally with the click of a button and
also deploy it to the cloud with a
single command without Docker I would
have to go into my terminal make sure I
have the right python dependencies
installed and then run the app in the
background every time I want to use it
and on top of that deployment to the
cloud would be a lot more complex and
also more expensive first I want to talk
about the app itself the reason I built
this app in Python and not my typical
choice of JavaScript is that there's a
python package called remove BG which is
based on the uet model to magically
remove the background with AI I oh man I
just realized I said the a word again I
guess we have to reset the counter but
really this video has nothing to do with
AI the model itself is heavily
abstracted to the point where all we do
is open an image with pillow call the
remove function from this Library which
returns a new image with the background
removed it's an extremely practical use
case for image models but I don't want
to use it from the terminal I want to be
able to drag and drop images directly
from my browser into it so I can then
drag the result directly back into my
Adobe Premiere timeline to do that I
built a little app with flask which
creates a single HTTP route that handles
both git and post methods git displays
the initial web page and then when we
drag an image into that web page it
makes a post request which calls that
remove background function now the
website itself is rendered in this
index.html file which uses nothing but
plain JavaScript and CSS there's an HTML
form with a file input then when that
form is submitted it makes a post
request to the root URL I also wrote a
little bit of JavaScript here that will
automatically submit the form when a
file is dropped onto it just to make the
process even more efficient that's the
entire app and now I can run it from the
terminal with the python command but now
here is where Docker comes in I want to
be able to use this code on multiple
computers and I also want to deploy it
to the web so I could even use it from
my phone or some other device and I want
everyone in the world to have the
opportunity to use my awesome invention
at first you'll need to have Docker
installed I'm doing that through Docker
desktop but in the past I've used tools
like podman which is developed by red
hat and is also a good option but now we
need to go into our code and create a
Docker file the docker file itself is
very simple it starts with the official
python base image creates a working
directory for the app installs the
dependencies copies the code exposes a
port and then runs the app the only
unusual thing I'm doing here is taking
the actual AI model weights and copying
them into the docker image the weights
are about 175 megab and doing this
prevents the actual python package from
downloading asynchronously which will
slow things down in general now let's
build the image and make sure to give it
a tag that'll take a minute then if we
go into Docker desktop we should see it
here in the images panel to actually run
the image as a container we simply hit
the play button button and make sure to
map the port to something we can use on
Local Host and now the app is always
ready to go in the background with
Docker pretty cool and that's how I use
this tool 90% of the time but I also
want to show you how to deploy it to the
web there's a bunch of different options
for deploying containers to the cloud
and there's also some free options if
you're cheap like me the most well-known
option is elastic container engine on
AWS with a related service called
fargate that can deploy your container
in a serverless way which means it will
scale down to zero when it's not in use
and then scale back up once the requests
are coming in you've also got services
like the app platform on digital ocean
which starts at $0 a month but my go-to
for deploying random utilities like this
is Google Cloud run to deploy something
that's dockerized though you first need
to get your image on a registry every
cloud has one built in and on Google
it's called artifact registry what you
do is create a repository for your
images that'll store them in a specific
region and then you can copy this link
up here which can be used as a tag on
your images so it knows where to upload
them let's go into the terminal and use
the docker tag command to tag our
existing image with this namespace once
that's done we can use the docker push
command to upload it to Google Cloud now
one caveat is that of course you need a
Google Cloud account and you'll also
need the g-cloud CLI tool installed on
your system but once that's done you
should then be able to see the image in
Google Cloud one nice thing about this
is that if you want to use this image on
a different machine you can simply pull
it from this repo but now let's head
over to Cloud run and deploy it to the
internet create a new service then the
first thing you'll do is Select that
container image now from here we have a
bunch of configuration options but if
you want to make this a public web
service the most important one is to
allow unauthenticated invocations that
means anybody can access it from a
public API or URL the next option is CPU
allocation one problem with serverless
deployments is that when the app is not
being used it scales down to zero which
is great because it means you're not
paying for anything but the trade-off is
a cold start which means it takes like
four or 5 seconds for the thing to boot
up when the next request comes in in my
case that's not a problem but if you
want to eliminate cold starts you can
make sure that the CPU is always
allocated it's just going to cost more
because you'll always be burning through
these free CPU seconds every month now
from there let's go down to the
container options and one thing we'll
also want to change here is the
allocated memory for the container it
takes a lot of memory to run the AI
model so let's bump this up to 2 gb one
other thing I want to do is also
decrease the amount of Auto scaling this
thing can do instead of 100 maximum
instances I'm only going to allow three
we just don't need to be prepared to
scale for this type of app but it's nice
to have that option if you're building
something viral let's go ahead and
deploy it and a few minutes later we
should have a URL where we can actually
access our python app on the web pretty
awesome and one huge benefit of having
this all dockerized is that our code is
portable so if we want to get off Cloud
run we could take that to any other
cloud service and deploy it there just
as easily and that's basically all there
is to it there's a ton of other stuff we
could talk about when it comes to Docker
and Cloud run but let me know what you
want to see next in the comments I do
have one update for fireship pro members
I'm currently finishing up a new stripe
course that's designed specifically for
people building software as a service
products more details to come on that
soon thanks for watching and I will see
you in the next one
Voir Plus de Vidéos Connexes
5.0 / 5 (0 votes)