Jim Eric Skogman commited on
Commit
06e80a6
1 Parent(s): ca9b741

Initial commit

Browse files
Files changed (3) hide show
  1. app.py +55 -0
  2. inference.py +103 -0
  3. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import gradio as gr
4
+ from inference import load_models, cache_path
5
+ from PIL import Image
6
+ from os import path
7
+
8
+ canvas_size = 512
9
+
10
+ if not path.exists(cache_path):
11
+ os.makedirs(cache_path, exist_ok=True)
12
+
13
+ with gr.Blocks() as demo:
14
+ infer = load_models()
15
+
16
+ with gr.Column():
17
+ with gr.Row():
18
+ with gr.Column():
19
+ s = gr.Slider(label="steps", minimum=4, maximum=8, step=1, value=4, interactive=True)
20
+ c = gr.Slider(label="cfg", minimum=0.1, maximum=3, step=0.1, value=1, interactive=True)
21
+ i_s = gr.Slider(label="sketch strength", minimum=0.1, maximum=0.9, step=0.1, value=0.9, interactive=True)
22
+ with gr.Column():
23
+ mod = gr.Text(label="Model HuggingFace id (after changing this wait until the model downloads in the console)", value="Lykon/dreamshaper-7", interactive=True)
24
+ t = gr.Text(label="Prompt", value="Scary warewolf, 8K, realistic, colorful, long sharp teeth, splash art", interactive=True)
25
+ se = gr.Number(label="seed", value=1337, interactive=True)
26
+ with gr.Row(equal_height=True):
27
+ i = gr.Image(source="canvas", tool="color-sketch", shape=(canvas_size, canvas_size), width=canvas_size, height=canvas_size, type="pil")
28
+ o = gr.Image(width=canvas_size, height=canvas_size)
29
+
30
+ def process_image(p, im, steps, cfg, image_strength, seed):
31
+ if not im:
32
+ return Image.new("RGB", (canvas_size, canvas_size))
33
+ return infer(
34
+ prompt=p,
35
+ image=im,
36
+ num_inference_steps=steps,
37
+ guidance_scale=cfg,
38
+ strength=image_strength,
39
+ seed=int(seed)
40
+ )
41
+
42
+ reactive_controls = [t, i, s, c, i_s, se]
43
+
44
+ for control in reactive_controls:
45
+ control.change(fn=process_image, inputs=reactive_controls, outputs=o)
46
+
47
+ def update_model(model_name):
48
+ global infer
49
+ infer = load_models(model_name)
50
+
51
+ mod.change(fn=update_model, inputs=mod)
52
+
53
+
54
+ if __name__ == "__main__":
55
+ demo.launch()
inference.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ from os import path
4
+ from contextlib import nullcontext
5
+ import time
6
+ from sys import platform
7
+ import torch
8
+
9
+ cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
10
+
11
+ os.environ["TRANSFORMERS_CACHE"] = cache_path
12
+ os.environ["HF_HUB_CACHE"] = cache_path
13
+ os.environ["HF_HOME"] = cache_path
14
+ is_mac = platform == "darwin"
15
+
16
+ def should_use_fp16():
17
+ if is_mac:
18
+ return True
19
+
20
+ gpu_props = torch.cuda.get_device_properties("cuda")
21
+
22
+ if gpu_props.major < 6:
23
+ return False
24
+
25
+ nvidia_16_series = ["1660", "1650", "1630"]
26
+
27
+ for x in nvidia_16_series:
28
+ if x in gpu_props.name:
29
+ return False
30
+
31
+ return True
32
+
33
+ class timer:
34
+ def __init__(self, method_name="timed process"):
35
+ self.method = method_name
36
+
37
+ def __enter__(self):
38
+ self.start = time.time()
39
+ print(f"{self.method} starts")
40
+
41
+ def __exit__(self, exc_type, exc_val, exc_tb):
42
+ end = time.time()
43
+ print(f"{self.method} took {str(round(end - self.start, 2))}s")
44
+
45
+
46
+ def load_models(model_id="Lykon/dreamshaper-7"):
47
+ from diffusers import AutoPipelineForImage2Image, LCMScheduler
48
+ from diffusers.utils import load_image
49
+
50
+ if not is_mac:
51
+ torch.backends.cuda.matmul.allow_tf32 = True
52
+
53
+ use_fp16 = should_use_fp16()
54
+
55
+ lcm_lora_id = "latent-consistency/lcm-lora-sdv1-5"
56
+
57
+ if use_fp16:
58
+ pipe = AutoPipelineForImage2Image.from_pretrained(
59
+ model_id,
60
+ cache_dir=cache_path,
61
+ torch_dtype=torch.float16,
62
+ variant="fp16",
63
+ safety_checker=None
64
+ )
65
+ else:
66
+ pipe = AutoPipelineForImage2Image.from_pretrained(
67
+ model_id,
68
+ cache_dir=cache_path,
69
+ safety_checker=None
70
+ )
71
+
72
+ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
73
+
74
+ pipe.load_lora_weights(lcm_lora_id)
75
+ pipe.fuse_lora()
76
+
77
+ device = "mps" if is_mac else "cuda"
78
+
79
+ pipe.to(device=device)
80
+
81
+ generator = torch.Generator()
82
+
83
+ def infer(
84
+ prompt,
85
+ image,
86
+ num_inference_steps=4,
87
+ guidance_scale=1,
88
+ strength=0.9,
89
+ seed=random.randrange(0, 2**63)
90
+ ):
91
+ with torch.inference_mode():
92
+ with torch.autocast("cuda") if device == "cuda" else nullcontext():
93
+ with timer("inference"):
94
+ return pipe(
95
+ prompt=prompt,
96
+ image=load_image(image),
97
+ generator=generator.manual_seed(seed),
98
+ num_inference_steps=num_inference_steps,
99
+ guidance_scale=guidance_scale,
100
+ strength=strength
101
+ ).images[0]
102
+
103
+ return infer
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ diffusers
2
+ flask
3
+ torch
4
+ transformers
5
+ accelerate
6
+ pillow
7
+ gradio==3.41.2