This demo repo illustrates how to run GitHub Codespaces to fine-tune Stable Diffusion with your own concept using textual inversion, and deploy it to Replicate as a cloud API.
🐲 For a live running example, check out @chenxwh's Spyro the Dragon model.
- Set up your environment
- Fine-tune your concept
- Test your trained concept locally
- Deploy your trained concept on Replicate
-
Clone this repo and install Cog if you haven't already. If you're using GitHub Codespaces, you can install Cog and all the required dependencies by running this command:
curl https://replicate.github.io/codespaces/scripts/install-cog.sh | bash
-
Run
cog run bash
to install the dependencies and set up the environment for running
textual_inversion
-
Run
huggingface-cli login
and paste your HuggingFace token. You will also need to agree to the terms for accessing CompVis/stable-diffusion-v1-4 on the HuggingFace website.
- Prepare your 3-5 images for your concept (e.g. images in
./dragon
), and modify training parameters intrain
accordingly. You probably need to modify the following:and then run--train_data_dir="dragon" --learnable_property="style" --placeholder_token="<spyro-dragon>" --initializer_token="game" --output_dir="textual_inversion_spyro-dragon"
The fine-tuning of your concept should be started now, it may take more than one hour to finish.bash train
- Once the concept is trained, you can test it (still in the
cog run bash
env) using this command:The output should be saved atpython inference.py --model_dir <path_to_model_with_your_trained_concept> --prompt <prompt_with_your_trained_concept>
<your_output_path>
-
If all works fine, it's time to push your model to Replicate so other people can try your cool concept!
First, change the
model_id
inpredict.py
with your trained concept (same asoutput_dir
fromtrain
).Create a new model at replicate.com/create. Follow the guide and then you will see the command to log in and push your model to your created page. For instance, in a new terminal:
cog login cog push r8.im/cjwbw/sd-textual-inversion-spyro-dragon
You will be prompted to provide your Replicate token after
cog login
to get permission for pushing to the page you created.Once your model is pushed, you can run predictions on the website like this here or use the API:
import replicate model = replicate.models.get("cjwbw/sd-textual-inversion-spyro-dragon") output = model.predict(prompt="Golden Gate Bridge in style of <spyro-dragon>")