Thafx commited on
Commit
26e865d
·
1 Parent(s): 960437b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -21
app.py CHANGED
@@ -5,7 +5,7 @@ from PIL import Image
5
 
6
  model_id = 'wavymulder/Analog-Diffusion'
7
  prefix = 'analog style, '
8
-
9
  scheduler = DPMSolverMultistepScheduler.from_pretrained(model_id, subfolder="scheduler")
10
 
11
  pipe = StableDiffusionPipeline.from_pretrained(
@@ -26,8 +26,32 @@ def error_str(error, title="Error"):
26
  return f"""#### {title}
27
  {error}""" if error else ""
28
 
29
- def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt="", auto_prefix=False):
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
32
  prompt = f"{prefix} {prompt}" if auto_prefix else prompt
33
 
@@ -38,6 +62,8 @@ def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None,
38
  return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator), None
39
  except Exception as e:
40
  return None, error_str(e)
 
 
41
 
42
  def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator):
43
 
@@ -69,6 +95,11 @@ def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height
69
 
70
  return result.images[0]
71
 
 
 
 
 
 
72
  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}
73
  """
74
  with gr.Blocks(css=css) as demo:
@@ -76,14 +107,32 @@ with gr.Blocks(css=css) as demo:
76
  f"""
77
  <div class="main-div">
78
  <div>
79
- <h1>Analog Diffusion</h1>
80
  </div>
81
  <p>
82
- Demo for <a href="https://huggingface.co/wavymulder/Analog-Diffusion">Analog Diffusion</a> Stable Diffusion model.<br>
83
- {"Add the following tokens to your prompts for the model to work properly: <b>prefix</b>" if prefix else ""}
84
- </p>
85
- Running on {"<b>GPU 🔥</b>" if torch.cuda.is_available() else f"<b>CPU 🥶</b>. For faster inference it is recommended to <b>upgrade to GPU in <a href='https://huggingface.co/spaces/akhaliq/Analog-Diffusion/settings'>Settings</a></b>"} after duplicating the space<br><br>
86
- <a style="display:inline-block" href="https://thafx.com"><img src="https://bit.ly/3gLdBN6" alt="ThaFX"></a>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  </div>
88
  """
89
  )
@@ -92,7 +141,7 @@ with gr.Blocks(css=css) as demo:
92
  with gr.Column(scale=55):
93
  with gr.Group():
94
  with gr.Row():
95
- prompt = gr.Textbox(label="Prompt", show_label=False, max_lines=2,placeholder=f"{prefix} [your prompt]").style(container=False)
96
  generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))
97
 
98
  image_out = gr.Image(height=512)
@@ -102,15 +151,15 @@ with gr.Blocks(css=css) as demo:
102
  with gr.Tab("Options"):
103
  with gr.Group():
104
  neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
105
- auto_prefix = gr.Checkbox(label="Prefix styling tokens automatically ()", value=prefix, visible=prefix)
106
 
107
  with gr.Row():
108
- guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
109
- steps = gr.Slider(label="Steps", value=25, minimum=2, maximum=75, step=1)
110
 
111
  with gr.Row():
112
- width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
113
- height = gr.Slider(label="Height", value=512, minimum=64, maximum=1024, step=8)
114
 
115
  seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)
116
 
@@ -126,12 +175,7 @@ with gr.Blocks(css=css) as demo:
126
  prompt.submit(inference, inputs=inputs, outputs=outputs)
127
  generate.click(inference, inputs=inputs, outputs=outputs)
128
 
129
- gr.HTML("""
130
- <div style="border-top: 1px solid #303030;">
131
- <br>
132
- <p>This space was created using <a href="https://huggingface.co/spaces/anzorq/sd-space-creator">SD Space Creator</a>.</p>
133
- </div>
134
- """)
135
 
136
  demo.queue(concurrency_count=1)
137
- demo.launch()
 
5
 
6
  model_id = 'wavymulder/Analog-Diffusion'
7
  prefix = 'analog style, '
8
+
9
  scheduler = DPMSolverMultistepScheduler.from_pretrained(model_id, subfolder="scheduler")
10
 
11
  pipe = StableDiffusionPipeline.from_pretrained(
 
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
 
 
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
 
 
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:
 
107
  f"""
108
  <div class="main-div">
109
  <div>
110
+ <h1 style="color:yellow;">📸 Analog Diffusion 📸</h1>
111
  </div>
112
  <p>
113
+ Demo for <a href="https://huggingface.com/wavymulder/Analog-Diffusion">Analog Diffusion</a>
114
+ Stable Diffusion model by <a href="https://huggingface.co/wavymulder/"><abbr title="Wavymulder">Wavymulder</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
+ analog style photograph of <b>* 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: analog style photograph of Heath Ledger as Batman</i></q>
126
+ </code></details>
127
+ <q><em>Important note: Analog Diffusion works best at a 1:1 aspect ratio, it is also successful using tall aspect ratios as well.</em></q>
128
+ <br>
129
+ <b>Negative Prompt</b>:
130
+ <details><code>
131
+ blender illustration hdr, 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
132
+ </code></details>
133
+ <br>
134
+ Have Fun & Enjoy ⚡ <a href="https://www.thafx.com"><abbr title="Website">//THAFX</abbr></a>
135
+ <br>
136
  </div>
137
  """
138
  )
 
141
  with gr.Column(scale=55):
142
  with gr.Group():
143
  with gr.Row():
144
+ prompt = gr.Textbox(label="Prompt", show_label=False,max_lines=2,placeholder=f"{prefix} [your prompt]").style(container=False)
145
  generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))
146
 
147
  image_out = gr.Image(height=512)
 
151
  with gr.Tab("Options"):
152
  with gr.Group():
153
  neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
154
+ auto_prefix = gr.Checkbox(label="Prefix styling tokens automatically (analog style,)", value=prefix, visible=prefix)
155
 
156
  with gr.Row():
157
+ guidance = gr.Slider(label="Guidance scale", value=7, maximum=15)
158
+ steps = gr.Slider(label="Steps", value=20, minimum=2, maximum=75, step=1)
159
 
160
  with gr.Row():
161
+ width = gr.Slider(label="Width", value=768, minimum=64, maximum=1024, step=8)
162
+ height = gr.Slider(label="Height", value=768, minimum=64, maximum=1024, step=8)
163
 
164
  seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)
165
 
 
175
  prompt.submit(inference, inputs=inputs, outputs=outputs)
176
  generate.click(inference, inputs=inputs, outputs=outputs)
177
 
178
+
 
 
 
 
 
179
 
180
  demo.queue(concurrency_count=1)
181
+ demo.launch()