Welcome to one of the most used modern development workflows - feature branch development!
Massive thanks to DeepLearning.ai for coordinating this event!
Just a reminder that this repository is going to build off of the previous tutorial which you can find here:
Video Tutorials
Repositories Used
Also, make sure you have an activate Hugging Face account!
Once you've caught up using those you'll have the base knowledge you'll need to get started on this repository!
-
First steps, let's create a repository on GitHub! Make sure to include a README.md, a License and a
.gitignore
-
Now, we're going to clone our repository from GitHub using the command:
git clone <YOUR REPO SSH LINK HERE>
-
Now we can add this repository as an upstream remote to our local repo with the following command:
git remote add upstream [email protected]:FourthBrain/Branching-out-of-the-Notebook.git
-
Next, we're going to pull this repo into our local using the following command:
git pull upstream main -Xtheirs --allow-unrelated-histories
Command Explanation
This command uses two flags:
-Xtheirs
this flag tells git to keep their files, should their be any merge conflicts.--allow-unrelated-histories
this flag tells git to not worry about the fact that these are two separate repositories!
-
Now that we have the app in our local branch, we'll need to do some set-up before we continue! The first thing we'll want to do is head to this link and read/accept the ToS!
-
Next, we'll want to create a Read key on HuggingFace by following this process!
-
Now that you have your Hugging Face Read token, you'll want to make a file in the
v1
directory called:hf_token.py
(this is already in.gitignore
), which will be a simple.py
file that only contains one row of code:hf_token = "<YOUR TOKEN HERE>"
-
At this point, your repository should look something like this:
-
Now we'll want to set-up a local
conda
env using the following command:conda create --name stable_diffusion_demo python=3.10 pip diffusers
-
Once you've create
hf_token.py
and added your read key (again making sure to accept the ToS here, and set-up yourconda
env, you can runretrieve_model.py
. This will take a while, and consume ~5GB of space - as it's pulling the v1-4 Stable Diffusion weights from Hugging Face!conda activate stable_diffusion_demo cd v1 python3 retrieve_model.py
-
Once that is done, you will be able to build the Docker image! This process will take some time (~5min.), and is dependent on your hardware!
docker build -t stable_diff_demo .
-
Now that you've built the container - it's time to run it! We'll use the following command to ensure our container removes itself when we stop it!
docker run --rm --name stable_diff_demo_container -p 5000:5000 stable_diff_demo
-
Head on over to localhost:5000/docs to play around with the new model! (CPU inference can take a long time, depending on your hardware. ~3-5min.)
Now that you've tested your app locally, it's time to add a feature branch do some work, and get ready to merge!
-
First things first we'll want to make a new branch called
feature_branch_img2img
, you'll want to use the command:git checkout -b feature_branch_img2img
-
Once you have that done, you can check to make sure you're on the correct branch using the command:
git branch
-
After confirming you're on the correct branch - you can finally add your feature! We're going to add the
img2img
endpoint to our FastAPI application (located inv1/app/app.py
, using the following code:# create a img2img route @app.post("/img2img") def img2img(prompt: str, img: UploadFile = File(...)): device = get_device() img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained("../model") img2img_pipe.to(device) img = Image.open(img.file).convert("RGB") init_img = img.resize((768, 512)) # generate an image img = img2img_pipe(prompt, init_img).images[0] img = np.array(img) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) res, img = cv2.imencode(".png", img) del img2img_pipe if torch.cuda.is_available(): torch.cuda.empty_cache() return StreamingResponse(io.BytesIO(img.tobytes()), media_type="image/png")
-
Once you have updated your
app.py
, you can once again test it using the processed outlined above! -
Now that we've made the changes - we're ready to stage, and then commit them! First up, let's stage our changes using:
git add .
-
Next up, we'll commit the changes with a useful/helpful message using:
git commit -m "<YOUR HELPFUL MESSAGE HERE>"
-
Now that we've commited those changes - we need to push them to our remote repository. You'll notice that we're setting a new upstream in the upcoming command, that's because while the branch we created on our local exists; the remote is not aware of it! So we need to create the branch on the remote, as well! We can do this in one step with the following command:
git push --set-upstream origin feature_branch_img2img
-
With that, you're all done on the local side! The only thing left to do is navigate to your remote repo on GitHub.com!
-
When you arrive at your repository on GitHub.com, you should notice a brand new banner is present:
-
Once you click on the green "Compare & pull request" button - you'll be brought to the pull request screen where you'll want to leave an insightful comment, and add an appropriate title (it will use your commit message as a default title!)
-
Once you've done that, you can click the green "Create pull request" button!
-
After creating the pull request, you'll see there's an option to assign reviewers - which you can do by clicking the cog wheel icon, and either selecting (or typing) the name of your reviewer! (Remember: they have to be a collaborator on the repository!)
-
After review (usually the reviewer will do this step), you can finally merge your changes into the trunk by clicking the green "Merge pull request" button! It will ask you to confirm, and leave additional comments - which should absolutely do!
-
Afterwards, it will prompt you if you want to delete the branch. It is dependent on your organization if you will or not - but it is usually considered good practice to delete "hanging" branches!
With that, you're all done!
To recap, we've: Built a web-app based on the Diffusers library, "Dockerized" it, created a feature branch, added a feature, pushed our branch to GitHub, and created a PR!