How I deploy serverless containers for free

Beyond Fireship
15 Mar 202406:32

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

00:00

🖼️ 透明な背景を持つ画像を簡単にダウンロードする方法

この段落では、インターネットから画像をダウンロードする際の面倒な問題と、その解決策について説明されています。特に、画像の背景を透明にすることが望ましい場合、ダウンロードした画像が期待外れの表示になることがあると指摘されています。解決策として、remove BGやPhotoshopの新しいAIツールなど、背景を除去するツールが提案されています。また、作者が自分でアプリを開発し、背景を除去するプロセスを効率化した経験も共有されています。Dockerの使用理由やデプロイ方法についても触れられています。

05:00

🔧 PythonとDockerを使った背景除去アプリの構築とデプロイ

この段落では、背景除去のためにPythonを使用し、remove BGというPythonパッケージを活用したアプリの開発と、Dockerを使用してのアプリのコンテナ化とクラウドへのデプロイの詳細について説明されています。特に、AIモデルの重みをDockerイメージに組み込むことで、起動遅延を防いだことが強調されています。また、Google Cloud Runを使用して無料でウェブにデプロイする方法も紹介されています。最後に、Docker化されたコードの移植性についても触れられています。

Mindmap

Keywords

💡transparent background

「transparent background」とは、画像の背景が透明で、他の要素や背景と重なり合わせることができる状態を指します。このビデオでは、インターネットからダウンロードした画像が期待したように透明な背景を持っていない問題が述べられており、これを解決するために開発されたツールやアプリが紹介されています。

💡remove BG

「remove BG」は、画像の背景を除去するツールのことで、このビデオの主題の一つです。このツールを使用することで、画像の背景を効率的に削除し、プロジェクトに必要な形式で使用することができます。

💡AI Tools in Photoshop

「AI Tools in Photoshop」は、Adobe Photoshopという人気の画像編集ソフトウェアに搭載されている、人工知能を活用したツールのことで、自動で画像の背景除去やその他の画像処理タスクを簡素化する機能を提供します。

💡Docker

「Docker」は、ソフトウェアをコンテナ化するオープンソースのプラットフォームです。コンテナ化することで、ソフトウェアが実行される環境を標準化し、異なるコンピューターやクラウドサービスで同じように動作するようになります。このビデオでは、Dockerを使用して開発された背景除去アプリを簡単に配布する方法が説明されています。

💡containerization

「containerization」は、ソフトウェアを実行するための環境を一つの「コンテナ」としてパッケージ化するプロセスです。これにより、ソフトウェアは同じ状態で異なる場所やデバイスで実行でき、依存関係や設定の違いによる問題を回避できます。ビデオでは、背景除去アプリをコンテナ化することで、簡単に複数のコンピューターで使用できるようにしたことが説明されています。

💡cloud deployment

「cloud deployment」は、インターネットを通じてアクセスできるクラウドコンピューティング環境にソフトウェアを展開することを指します。これにより、ユーザーは自分のコンピューターでソフトウェアをインストールすることなく、必要な時にアクセスすることができます。ビデオでは、Dockerを使用して開発したアプリケーションをクラウドに展開する方法が紹介されています。

💡Google Cloud Run

「Google Cloud Run」は、Google Cloud Platformのサービスで、コンテナ化されたアプリケーションを簡単にデプロイし、管理できるフルマネージドのプラットフォームです。このサービスを使うことで、開発者はアプリケーションをクラウド上で実行し、スケーリングやメンテナンスを心配する必要がなくなります。ビデオでは、Google Cloud Runを使用して、背景除去アプリをインターネット上に公開する方法が説明されています。

💡Python

「Python」は、Pythonプログラミング言語で書かれたソフトウェアアプリケーションを指します。このビデオでは、背景除去アプリがPythonで開発されており、その理由として、Pythonには背景除去に便利なライブラリが存在する点が説明されています。

💡Flask

「Flask」は、Pythonで作られた軽量のウェブフレームワークで、Webアプリケーションの開発に利用されます。ビデオでは、Flaskを使用して、画像をドラッグ&ドロップして背景除去を実行するシンプルなWebアプリケーションを構築する方法が説明されています。

💡Pillow

「Pillow」は、Pythonの画像処理ライブラリで、图像の読み込み、表示、加工、保存などを行うことができます。ビデオ中では、Pillowを使用して画像を開き、背景除去ライブラリの「remove」関数を呼び出すことで、背景を除去した新しい画像を得ることができます。

💡digital ocean

「DigitalOcean」は、クラウドインフラストラクチャサービスのプロバイダーで、アプリケーションのホスティングやデVELOPMENT、デプロイメントなどに利用されるプラットフォームです。ビデオでは、DigitalOceanの「App Platform」サービスが、無料で利用できるクラウドサービスの例として言及されています。

💡artifact registry

「artifact registry」は、Google Cloud Platformのコンテナレジストリーサービスで、コンテナイメージやその他のアーティファクトを保存し、管理することができます。ビデオでは、artifact registryを使用して、Google Cloudにアプリケーションをデプロイする方法が説明されています。

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

play00:00

one of the most annoying things in the

play00:01

world is when you're trying to steal an

play00:03

image from the internet and it looks

play00:04

like it's going to have a transparent

play00:06

background but when you download it it

play00:08

looks like this luckily nowadays there

play00:10

are all kinds of good tools for removing

play00:11

backgrounds from images like remove BG

play00:14

or the new AI Tools in Photoshop now if

play00:16

you watch my videos you'll notice I use

play00:18

a lot of images with the background

play00:19

removed and it's extremely inefficient

play00:21

to have to go into Photoshop upload an

play00:23

image remove its background and then

play00:25

reexport it and bring it back into Adobe

play00:27

Premiere as a developer this

play00:28

inefficiency is to unacceptable and my

play00:31

only option was to build my own app from

play00:32

scratch in today's video I want to show

play00:34

you how I built this background remover

play00:36

from scratch but more importantly I want

play00:38

to talk about why I dockerized it and

play00:40

explain how I deployed it to the cloud

play00:41

for free earlier this week on the main

play00:43

Channel I made a Docker video which you

play00:45

should definitely check out first if you

play00:46

have no idea what Docker is but that

play00:48

video was sponsored by Docker this video

play00:50

is not sponsored I don't work with

play00:52

sponsors very often but when I do it's

play00:54

usually with tools that I have firsthand

play00:56

experience with and Docker is something

play00:57

I use all the time by containerizing

play00:59

this background remover which is just a

play01:01

python web app I'm able to run it

play01:03

locally with the click of a button and

play01:04

also deploy it to the cloud with a

play01:06

single command without Docker I would

play01:07

have to go into my terminal make sure I

play01:09

have the right python dependencies

play01:10

installed and then run the app in the

play01:12

background every time I want to use it

play01:13

and on top of that deployment to the

play01:15

cloud would be a lot more complex and

play01:17

also more expensive first I want to talk

play01:19

about the app itself the reason I built

play01:20

this app in Python and not my typical

play01:22

choice of JavaScript is that there's a

play01:24

python package called remove BG which is

play01:26

based on the uet model to magically

play01:28

remove the background with AI I oh man I

play01:30

just realized I said the a word again I

play01:32

guess we have to reset the counter but

play01:33

really this video has nothing to do with

play01:35

AI the model itself is heavily

play01:37

abstracted to the point where all we do

play01:39

is open an image with pillow call the

play01:40

remove function from this Library which

play01:42

returns a new image with the background

play01:44

removed it's an extremely practical use

play01:46

case for image models but I don't want

play01:47

to use it from the terminal I want to be

play01:49

able to drag and drop images directly

play01:50

from my browser into it so I can then

play01:52

drag the result directly back into my

play01:54

Adobe Premiere timeline to do that I

play01:56

built a little app with flask which

play01:57

creates a single HTTP route that handles

play02:00

both git and post methods git displays

play02:02

the initial web page and then when we

play02:03

drag an image into that web page it

play02:05

makes a post request which calls that

play02:07

remove background function now the

play02:08

website itself is rendered in this

play02:10

index.html file which uses nothing but

play02:13

plain JavaScript and CSS there's an HTML

play02:15

form with a file input then when that

play02:17

form is submitted it makes a post

play02:19

request to the root URL I also wrote a

play02:21

little bit of JavaScript here that will

play02:23

automatically submit the form when a

play02:24

file is dropped onto it just to make the

play02:26

process even more efficient that's the

play02:28

entire app and now I can run it from the

play02:29

terminal with the python command but now

play02:31

here is where Docker comes in I want to

play02:33

be able to use this code on multiple

play02:34

computers and I also want to deploy it

play02:36

to the web so I could even use it from

play02:38

my phone or some other device and I want

play02:40

everyone in the world to have the

play02:41

opportunity to use my awesome invention

play02:43

at first you'll need to have Docker

play02:44

installed I'm doing that through Docker

play02:46

desktop but in the past I've used tools

play02:48

like podman which is developed by red

play02:50

hat and is also a good option but now we

play02:52

need to go into our code and create a

play02:53

Docker file the docker file itself is

play02:56

very simple it starts with the official

play02:58

python base image creates a working

play02:59

directory for the app installs the

play03:01

dependencies copies the code exposes a

play03:03

port and then runs the app the only

play03:06

unusual thing I'm doing here is taking

play03:08

the actual AI model weights and copying

play03:10

them into the docker image the weights

play03:12

are about 175 megab and doing this

play03:14

prevents the actual python package from

play03:16

downloading asynchronously which will

play03:18

slow things down in general now let's

play03:20

build the image and make sure to give it

play03:21

a tag that'll take a minute then if we

play03:23

go into Docker desktop we should see it

play03:25

here in the images panel to actually run

play03:27

the image as a container we simply hit

play03:29

the play button button and make sure to

play03:30

map the port to something we can use on

play03:32

Local Host and now the app is always

play03:34

ready to go in the background with

play03:35

Docker pretty cool and that's how I use

play03:37

this tool 90% of the time but I also

play03:39

want to show you how to deploy it to the

play03:41

web there's a bunch of different options

play03:42

for deploying containers to the cloud

play03:44

and there's also some free options if

play03:46

you're cheap like me the most well-known

play03:47

option is elastic container engine on

play03:49

AWS with a related service called

play03:52

fargate that can deploy your container

play03:53

in a serverless way which means it will

play03:55

scale down to zero when it's not in use

play03:57

and then scale back up once the requests

play03:59

are coming in you've also got services

play04:01

like the app platform on digital ocean

play04:03

which starts at $0 a month but my go-to

play04:05

for deploying random utilities like this

play04:07

is Google Cloud run to deploy something

play04:09

that's dockerized though you first need

play04:10

to get your image on a registry every

play04:12

cloud has one built in and on Google

play04:14

it's called artifact registry what you

play04:16

do is create a repository for your

play04:18

images that'll store them in a specific

play04:20

region and then you can copy this link

play04:22

up here which can be used as a tag on

play04:24

your images so it knows where to upload

play04:26

them let's go into the terminal and use

play04:28

the docker tag command to tag our

play04:30

existing image with this namespace once

play04:32

that's done we can use the docker push

play04:34

command to upload it to Google Cloud now

play04:36

one caveat is that of course you need a

play04:37

Google Cloud account and you'll also

play04:39

need the g-cloud CLI tool installed on

play04:41

your system but once that's done you

play04:43

should then be able to see the image in

play04:44

Google Cloud one nice thing about this

play04:46

is that if you want to use this image on

play04:48

a different machine you can simply pull

play04:50

it from this repo but now let's head

play04:51

over to Cloud run and deploy it to the

play04:53

internet create a new service then the

play04:55

first thing you'll do is Select that

play04:56

container image now from here we have a

play04:58

bunch of configuration options but if

play05:00

you want to make this a public web

play05:01

service the most important one is to

play05:03

allow unauthenticated invocations that

play05:06

means anybody can access it from a

play05:07

public API or URL the next option is CPU

play05:10

allocation one problem with serverless

play05:12

deployments is that when the app is not

play05:14

being used it scales down to zero which

play05:16

is great because it means you're not

play05:17

paying for anything but the trade-off is

play05:19

a cold start which means it takes like

play05:21

four or 5 seconds for the thing to boot

play05:22

up when the next request comes in in my

play05:24

case that's not a problem but if you

play05:26

want to eliminate cold starts you can

play05:27

make sure that the CPU is always

play05:29

allocated it's just going to cost more

play05:31

because you'll always be burning through

play05:32

these free CPU seconds every month now

play05:34

from there let's go down to the

play05:35

container options and one thing we'll

play05:37

also want to change here is the

play05:38

allocated memory for the container it

play05:40

takes a lot of memory to run the AI

play05:42

model so let's bump this up to 2 gb one

play05:45

other thing I want to do is also

play05:46

decrease the amount of Auto scaling this

play05:48

thing can do instead of 100 maximum

play05:50

instances I'm only going to allow three

play05:52

we just don't need to be prepared to

play05:53

scale for this type of app but it's nice

play05:55

to have that option if you're building

play05:56

something viral let's go ahead and

play05:58

deploy it and a few minutes later we

play05:59

should have a URL where we can actually

play06:02

access our python app on the web pretty

play06:04

awesome and one huge benefit of having

play06:06

this all dockerized is that our code is

play06:08

portable so if we want to get off Cloud

play06:09

run we could take that to any other

play06:11

cloud service and deploy it there just

play06:12

as easily and that's basically all there

play06:14

is to it there's a ton of other stuff we

play06:15

could talk about when it comes to Docker

play06:17

and Cloud run but let me know what you

play06:19

want to see next in the comments I do

play06:20

have one update for fireship pro members

play06:22

I'm currently finishing up a new stripe

play06:24

course that's designed specifically for

play06:26

people building software as a service

play06:27

products more details to come on that

play06:29

soon thanks for watching and I will see

play06:31

you in the next one

Rate This

5.0 / 5 (0 votes)

Related Tags
背景除去PythonDockerクラウド無料でホスティングAI自動化WebアプリGoogle Cloud Run開発
Do you need a summary in English?