Thafx commited on
Commit
2f939ec
0 Parent(s):

Duplicate from Thafx/sdoj

Browse files
Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. app.py +183 -0
  4. requirements.txt +11 -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,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: SD:Open Journey v4
3
+ emoji: 👁
4
+ colorFrom: green
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 3.18.0
8
+ app_file: app.py
9
+ pinned: true
10
+ duplicated_from: Thafx/sdoj
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, DPMSolverMultistepScheduler
2
+ import gradio as gr
3
+ import torch
4
+ from PIL import Image
5
+
6
+ model_id = 'prompthero/openjourney-v4'
7
+ prefix = 'mdjrny-v4 style,'
8
+
9
+ scheduler = DPMSolverMultistepScheduler.from_pretrained(model_id, subfolder="scheduler")
10
+
11
+ pipe = StableDiffusionPipeline.from_pretrained(
12
+ model_id,
13
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
14
+ scheduler=scheduler)
15
+
16
+ pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(
17
+ model_id,
18
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
19
+ scheduler=scheduler)
20
+
21
+ if torch.cuda.is_available():
22
+ pipe = pipe.to("cuda")
23
+ pipe_i2i = pipe_i2i.to("cuda")
24
+
25
+ def error_str(error, title="Error"):
26
+ return f"""#### {title}
27
+ {error}""" if error else ""
28
+
29
+
30
+ def _parse_args(prompt, generator):
31
+ parser = argparse.ArgumentParser(
32
+ description="making it work."
33
+ )
34
+ parser.add_argument(
35
+ "--no-half-vae", help="no half vae"
36
+ )
37
+
38
+ cmdline_args = parser.parse_args()
39
+ command = cmdline_args.command
40
+ conf_file = cmdline_args.conf_file
41
+ conf_args = Arguments(conf_file)
42
+ opt = conf_args.readArguments()
43
+
44
+ if cmdline_args.config_overrides:
45
+ for config_override in cmdline_args.config_overrides.split(";"):
46
+ config_override = config_override.strip()
47
+ if config_override:
48
+ var_val = config_override.split("=")
49
+ assert (
50
+ len(var_val) == 2
51
+ ), f"Config override '{var_val}' does not have the form 'VAR=val'"
52
+ conf_args.add_opt(opt, var_val[0], var_val[1], force_override=True)
53
+
54
+ def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt="", auto_prefix=False):
55
+ generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
56
+ prompt = f"{prefix} {prompt}" if auto_prefix else prompt
57
+
58
+ try:
59
+ if img is not None:
60
+ return img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator), None
61
+ else:
62
+ return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator), None
63
+ except Exception as e:
64
+ return None, error_str(e)
65
+
66
+
67
+
68
+ def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator):
69
+
70
+ result = pipe(
71
+ prompt,
72
+ negative_prompt = neg_prompt,
73
+ num_inference_steps = int(steps),
74
+ guidance_scale = guidance,
75
+ width = width,
76
+ height = height,
77
+ generator = generator)
78
+
79
+ return result.images[0]
80
+
81
+ def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator):
82
+
83
+ ratio = min(height / img.height, width / img.width)
84
+ img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
85
+ result = pipe_i2i(
86
+ prompt,
87
+ negative_prompt = neg_prompt,
88
+ init_image = img,
89
+ num_inference_steps = int(steps),
90
+ strength = strength,
91
+ guidance_scale = guidance,
92
+ width = width,
93
+ height = height,
94
+ generator = generator)
95
+
96
+ return result.images[0]
97
+
98
+ def fake_safety_checker(images, **kwargs):
99
+ return result.images[0], [False] * len(images)
100
+
101
+ pipe.safety_checker = fake_safety_checker
102
+
103
+ css = """.main-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.main-div div h1{font-weight:900;margin-bottom:7px}.main-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem}
104
+ """
105
+ with gr.Blocks(css=css) as demo:
106
+ gr.HTML(
107
+ f"""
108
+ <div class="main-div">
109
+ <div>
110
+ <h1 style="color:green;">Open Journey V4 🦾</h1>
111
+ </div>
112
+ <p>
113
+ Demo for <a href="https://huggingface.com/prompthero/openjourney-v4">Open Journey V4</a>
114
+ Stable Diffusion model by <a href="https://huggingface.co/Prompthero/"><abbr title="Prompthero">Prompthero</abbr></a>. {"" if prefix else ""}
115
+ Running on {"<b>GPU 🔥</b>" if torch.cuda.is_available() else f"<b>CPU ⚡</b>"}.
116
+ </p>
117
+ <p>Please use the prompt template below to achieve the desired result:
118
+ </p>
119
+
120
+ <b>Prompt</b>:
121
+ <details><code>
122
+ mdjrny-v4 style, <b>* the subject * </b>, (high detailed skin:1.2), 8k uhd, dslr, soft lighting, high quality, film grain, realistic, photo-realistic, full length frame, High detail RAW color art, piercing, diffused soft lighting, shallow depth of field, sharp focus, hyperrealism, cinematic lighting
123
+ <br>
124
+ <br>
125
+ <q><i>Example: full body cyborg| full-length portrait| detailed face| symmetric| steampunk| cyberpunk| cyborg| intricate detailed| to scale| hyperrealistic| cinematic lighting| digital art| concept art| mdjrny-v4 style, (high detailed skin:1.2), 8k uhd, dslr, soft lighting, high quality, film grain, Fujifilm XT3
126
+ </i></q>
127
+ </code></details>
128
+ <q><em>Important note: The "mdjrny-v4 style" prefix is not necessary anymore (yay!)</em></q>
129
+ <br>
130
+ <b>Negative Prompt</b>:
131
+ <details><code>
132
+ lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature
133
+ </code></details>
134
+
135
+ <br>
136
+ Have Fun & Enjoy ⚡ <a href="https://www.thafx.com"><abbr title="Website">//THAFX</abbr></a>
137
+ <br>
138
+ </div>
139
+ """
140
+ )
141
+ with gr.Row():
142
+
143
+ with gr.Column(scale=55):
144
+ with gr.Group():
145
+ with gr.Row():
146
+ prompt = gr.Textbox(label="Prompt", show_label=False,max_lines=2,placeholder=f"{prefix} [your prompt]").style(container=False)
147
+ generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))
148
+
149
+ image_out = gr.Image(height=512)
150
+ error_output = gr.Markdown()
151
+
152
+ with gr.Column(scale=45):
153
+ with gr.Tab("Options"):
154
+ with gr.Group():
155
+ neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
156
+ auto_prefix = gr.Checkbox(label="Prefix styling tokens automatically (mdjrny-v4 style,)", value=prefix, visible=prefix)
157
+
158
+ with gr.Row():
159
+ guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
160
+ steps = gr.Slider(label="Steps", value=25, minimum=2, maximum=75, step=1)
161
+
162
+ with gr.Row():
163
+ width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
164
+ height = gr.Slider(label="Height", value=512, minimum=64, maximum=1024, step=8)
165
+
166
+ seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)
167
+
168
+ with gr.Tab("Image to image"):
169
+ with gr.Group():
170
+ image = gr.Image(label="Image", height=256, tool="editor", type="pil")
171
+ strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
172
+
173
+ auto_prefix.change(lambda x: gr.update(placeholder=f"{prefix} [your prompt]" if x else "[Your prompt]"), inputs=auto_prefix, outputs=prompt, queue=False)
174
+
175
+ inputs = [prompt, guidance, steps, width, height, seed, image, strength, neg_prompt, auto_prefix]
176
+ outputs = [image_out, error_output]
177
+ prompt.submit(inference, inputs=inputs, outputs=outputs)
178
+ generate.click(inference, inputs=inputs, outputs=outputs)
179
+
180
+
181
+
182
+ demo.queue(concurrency_count=1)
183
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu113
2
+ invisible-watermark
3
+ fonts
4
+ font-roboto
5
+ numpy
6
+ torch
7
+ diffusers
8
+ #transformers
9
+ git+https://github.com/huggingface/transformers
10
+ accelerate
11
+ ftfy