Ryukijano merve HF staff commited on
Commit
c4ba9bf
·
0 Parent(s):

Duplicate from jax-diffusers-event/canny_coyo1m

Browse files

Co-authored-by: Merve Noyan <[email protected]>

Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +14 -0
  3. app.py +59 -0
  4. requirements.txt +8 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Canny Coyo1m
3
+ emoji: 🌖
4
+ colorFrom: indigo
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 3.24.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ duplicated_from: jax-diffusers-event/canny_coyo1m
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import jax
3
+ import numpy as np
4
+ import jax.numpy as jnp
5
+ from flax.jax_utils import replicate
6
+ from flax.training.common_utils import shard
7
+ from PIL import Image
8
+ from diffusers import FlaxStableDiffusionControlNetPipeline, FlaxControlNetModel
9
+ import cv2
10
+
11
+ def create_key(seed=0):
12
+ return jax.random.PRNGKey(seed)
13
+
14
+ def canny_filter(image):
15
+ gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
16
+ blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
17
+ edges_image = cv2.Canny(blurred_image, 50, 150)
18
+ return edges_image
19
+
20
+ # load control net and stable diffusion v1-5
21
+ controlnet, controlnet_params = FlaxControlNetModel.from_pretrained(
22
+ "jax-diffusers-event/canny-coyo1m", dtype=jnp.bfloat16
23
+ )
24
+ pipe, params = FlaxStableDiffusionControlNetPipeline.from_pretrained(
25
+ "runwayml/stable-diffusion-v1-5", controlnet=controlnet, revision="flax", dtype=jnp.bfloat16
26
+ )
27
+
28
+ def infer(prompts, negative_prompts, image):
29
+ params["controlnet"] = controlnet_params
30
+
31
+ num_samples = 1 #jax.device_count()
32
+ rng = create_key(0)
33
+ rng = jax.random.split(rng, jax.device_count())
34
+ im = canny_filter(image)
35
+ canny_image = Image.fromarray(im)
36
+
37
+ prompt_ids = pipe.prepare_text_inputs([prompts] * num_samples)
38
+ negative_prompt_ids = pipe.prepare_text_inputs([negative_prompts] * num_samples)
39
+ processed_image = pipe.prepare_image_inputs([canny_image] * num_samples)
40
+
41
+ p_params = replicate(params)
42
+ prompt_ids = shard(prompt_ids)
43
+ negative_prompt_ids = shard(negative_prompt_ids)
44
+ processed_image = shard(processed_image)
45
+
46
+ output = pipe(
47
+ prompt_ids=prompt_ids,
48
+ image=processed_image,
49
+ params=p_params,
50
+ prng_seed=rng,
51
+ num_inference_steps=50,
52
+ neg_prompt_ids=negative_prompt_ids,
53
+ jit=True,
54
+ ).images
55
+
56
+ output_images = pipe.numpy_to_pil(np.asarray(output.reshape((num_samples,) + output.shape[-3:])))
57
+ return output_images
58
+
59
+ gr.Interface(infer, inputs=["text", "text", "image"], outputs="gallery").launch()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ transformers
2
+ flax
3
+ jax[cuda11_pip]
4
+ -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html
5
+ jaxlib
6
+ git+https://github.com/huggingface/diffusers@main
7
+ opencv-python
8
+ torch