nielsgl commited on
Commit
8a8373d
β€’
1 Parent(s): 0e1f3d9

update app

Browse files
Files changed (3) hide show
  1. README.md +6 -1
  2. app.py +119 -0
  3. requirements.txt +9 -0
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: Dreambooth Bored Ape
3
- emoji: 🏒
4
  colorFrom: red
5
  colorTo: pink
6
  sdk: gradio
@@ -8,6 +8,11 @@ sdk_version: 3.23.0
8
  app_file: app.py
9
  pinned: false
10
  license: creativeml-openrail-m
 
 
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
1
  ---
2
  title: Dreambooth Bored Ape
3
+ emoji: 🐡
4
  colorFrom: red
5
  colorTo: pink
6
  sdk: gradio
 
8
  app_file: app.py
9
  pinned: false
10
  license: creativeml-openrail-m
11
+ tags:
12
+ - keras-dreambooth
13
+ - scifi
14
  ---
15
 
16
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
17
+
18
+ ---
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import from_pretrained_keras
2
+ from keras_cv import models
3
+ import gradio as gr
4
+
5
+ from tensorflow import keras
6
+
7
+ from diffusers import StableDiffusionPipeline
8
+
9
+ keras.mixed_precision.set_global_policy("mixed_float16")
10
+
11
+ # prepare model
12
+ resolution = 512
13
+
14
+ # checkpoint of the converted Stable Diffusion from KerasCV
15
+ model_ckpt = "nielsgl/dreambooth-bored-ape"
16
+ pipeline = StableDiffusionPipeline.from_pretrained(model_ckpt)
17
+ pipeline.to("cuda")
18
+
19
+ unique_id = "drawbayc"
20
+ class_label = "monkey"
21
+ prompt = f"A drawing of {unique_id} {class_label} as a cowboy"
22
+ image = pipeline(prompt, num_inference_steps=50).images[0]
23
+
24
+ # generate images
25
+ def infer(prompt, negative_prompt, guidance_scale=10, num_inference_steps=50):
26
+ neg = negative_prompt if negative_prompt else None
27
+ imgs = []
28
+ while len(imgs) != 2:
29
+ next_prompt = pipeline(prompt, negative_prompt=neg, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, num_images_per_prompt=5)
30
+ for img, is_neg in zip(next_prompt.images, next_prompt.nsfw_content_detected):
31
+ if not is_neg:
32
+ imgs.append(img)
33
+ if len(imgs) == 2:
34
+ break
35
+
36
+ return imgs
37
+
38
+ output = gr.Gallery(label="Outputs").style(grid=(1,2))
39
+
40
+ # customize interface
41
+ title = "KerasCV Stable Diffusion Demo on images of Bored Apes."
42
+ description = "This is a dreambooth model fine-tuned on images the NFT collection of the Bored Ape Yacht Club. To try it, input the concept with `drawbayc ape`."
43
+ examples=[
44
+ ["A drawing of a drawbayc ape as a cowboy", "", 12, 50],
45
+ ["A drawing of a drawbayc ape as a clown", "", 12, 50],
46
+ ["A drawing of a drawbayc ape as a turtle", "", 12, 50],
47
+
48
+ ]
49
+
50
+ base_14 = "https://huggingface.co/nielsgl/dreambooth-bored-ape/resolve/main/"
51
+
52
+ model_card_1 = f"""
53
+ # KerasCV Stable Diffusion in Diffusers πŸ§¨πŸ€—
54
+
55
+ # KerasCV Stable Diffusion in Diffusers πŸ§¨πŸ€—
56
+
57
+ DreamBooth model for the `drawbayc ape` concept trained by nielsgl on the `nielsgl/bayc-tiny` dataset, images from this [Kaggle dataset](https://www.kaggle.com/datasets/stanleyjzheng/bored-apes-yacht-club).
58
+ It can be used by modifying the `instance_prompt`: **a drawing of drawbayc ape**
59
+
60
+ ## Description
61
+
62
+ The Stable Diffusion V2 pipeline contained in the corresponding repository (`nielsgl/dreambooth-bored-ape`) was created using a modified version of [this Space](https://huggingface.co/spaces/sayakpaul/convert-kerascv-sd-diffusers) for StableDiffusionV2 from KerasCV. The purpose is to convert the KerasCV Stable Diffusion weights in a way that is compatible with [Diffusers](https://github.com/huggingface/diffusers). This allows users to fine-tune using KerasCV and use the fine-tuned weights in Diffusers taking advantage of its nifty features (like [schedulers](https://huggingface.co/docs/diffusers/main/en/using-diffusers/schedulers), [fast attention](https://huggingface.co/docs/diffusers/optimization/fp16), etc.).
63
+ This model was created as part of the Keras DreamBooth Sprint πŸ”₯. Visit the [organisation page](https://huggingface.co/keras-dreambooth) for instructions on how to take part!
64
+
65
+ ## Demo
66
+
67
+ """
68
+
69
+ model_card_2 = f"""
70
+ ## Examples
71
+
72
+ ### Stable Diffusion V2.1
73
+
74
+ ## Examples
75
+
76
+ > A drawing of drawbayc monkey dressed as an astronaut
77
+
78
+ ![a drawing of drawbayc monkey dressed as an astronaut]({base_14}examples/astronaut.jpg)
79
+
80
+ > A drawing of drawbayc monkey dressed as the pope
81
+
82
+ ![> A drawing of drawbayc monkey dressed as an astronaut]({base_14}examples/pope.jpg)
83
+
84
+
85
+ ## Usage with Stable Diffusion V2.1
86
+
87
+ ```python
88
+ from diffusers import StableDiffusionPipeline
89
+
90
+ pipeline = StableDiffusionPipeline.from_pretrained('nielsgl/dreambooth-bored-ape')
91
+ image = pipeline().images[0]
92
+ image
93
+ ```
94
+
95
+ """
96
+
97
+ with gr.Blocks() as demo:
98
+ with gr.Row():
99
+ gr.Markdown(model_card_1)
100
+ with gr.Row():
101
+ with gr.Column():
102
+ prompt_pos = gr.Textbox(label="Positive Prompt", value="a drawing of drawbayc ape as an astronaut")
103
+ prompt_neg = gr.Textbox(label="Negative Prompt", value="bad anatomy, blurry")
104
+
105
+ prompt_gs = gr.Number(label='Guidance scale', value=12)
106
+ prompt_steps = gr.Slider(label="Inference Steps",value=50)
107
+ prompt_btn = gr.Button("Generate")
108
+ with gr.Column():
109
+ output = gr.Gallery(label="Outputs").style(grid=(1,2))
110
+ prompt_btn.click(infer, inputs=[prompt_pos, prompt_neg, prompt_gs, prompt_steps], outputs=[output])
111
+ with gr.Row():
112
+ gr.Examples(examples, inputs=[prompt_pos, prompt_neg, prompt_gs, prompt_steps], outputs=output, fn=infer, cache_examples=True)
113
+ with gr.Row():
114
+ with gr.Column():
115
+ gr.Markdown(model_card_2)
116
+ with gr.Column():
117
+ gr.Markdown(" ")
118
+
119
+ demo.queue().launch()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu113
2
+ torch
3
+ keras-cv
4
+ tensorflow
5
+ huggingface-hub
6
+ diffusers
7
+ transformers
8
+ pycocotools
9
+ accelerate