alfredplpl commited on
Commit
bfda42f
1 Parent(s): a6c0409

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -33,10 +33,13 @@ def error_str(error, title="Error"):
33
  {error}""" if error else ""
34
 
35
 
36
- def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt="", disable_auto_prompt_correction=False):
37
 
38
  generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
39
-
 
 
 
40
  try:
41
  if img is not None:
42
  return img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator, disable_auto_prompt_correction), None
@@ -44,14 +47,15 @@ def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None,
44
  return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator, disable_auto_prompt_correction), None
45
  except Exception as e:
46
  return None, error_str(e)
47
- def auto_prompt_correction(prompt_ui,neg_prompt_ui):
48
  # auto prompt correction
 
49
  prompt=str(prompt_ui)
50
  neg_prompt=str(neg_prompt_ui)
51
  prompt=prompt.lower()
52
  neg_prompt=neg_prompt.lower()
53
  if(prompt=="" and neg_prompt==""):
54
- prompt="anime, a portrait of a girl, 4k, detailed"
55
  neg_prompt=f"(((deformed))), blurry, ((((bad anatomy)))), {neg_prompt}, bad pupil, disfigured, poorly drawn face, mutation, mutated, (extra limb), (ugly), (poorly drawn hands), bad hands, fused fingers, messy drawing, broken legs censor, low quality, ((mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), ((bad eyes)), ui, error, missing fingers, fused fingers, one hand with more than 5 fingers, one hand with less than 5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, long body, uncoordinated body, unnatural body, lowres, jpeg artifacts, 2d, 3d, cg, text"
56
 
57
  splited_prompt=prompt.replace(","," ").replace("_"," ").split(" ")
@@ -61,27 +65,24 @@ def auto_prompt_correction(prompt_ui,neg_prompt_ui):
61
  human_words=["girl","maid","female","woman","boy","male","man","guy"]
62
  for word in human_words:
63
  if( word in splited_prompt):
64
- prompt=f"anime, {prompt}, 4k, detailed"
65
  neg_prompt=f"(((deformed))), blurry, ((((bad anatomy)))), {neg_prompt}, bad pupil, disfigured, poorly drawn face, mutation, mutated, (extra limb), (ugly), (poorly drawn hands), bad hands, fused fingers, messy drawing, broken legs censor, low quality, ((mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), ((bad eyes)), ui, error, missing fingers, fused fingers, one hand with more than 5 fingers, one hand with less than 5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, long body, uncoordinated body, unnatural body, lowres, jpeg artifacts, 2d, 3d, cg, text"
66
 
67
  animal_words=["cat","dog","bird"]
68
  for word in animal_words:
69
  if( word in splited_prompt):
70
- prompt=f"anime, a {word}, 4k, detailed"
71
  neg_prompt=f"(((deformed))), blurry, ((((bad anatomy)))), {neg_prompt}, bad pupil, disfigured, poorly drawn face, mutation, mutated, (extra limb), (ugly), (poorly drawn hands), bad hands, fused fingers, messy drawing, broken legs censor, low quality, ((mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), ((bad eyes)), ui, error, missing fingers, fused fingers, one hand with more than 5 fingers, one hand with less than 5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, long body, uncoordinated body, unnatural body, lowres, jpeg artifacts, 2d, 3d, cg, text"
72
 
73
  background_words=["mount fuji","mt. fuji","building", "buildings", "tokyo", "kyoto", "shibuya", "shinjuku"]
74
  for word in background_words:
75
  if( word in splited_prompt):
76
- prompt=f"anime, shinkai makoto, {word}, 4k, 8k, highly detailed"
77
  neg_prompt=f"(((deformed))), {neg_prompt}, photo, people, low quality, ui, error, lowres, jpeg artifacts, 2d, 3d, cg, text"
78
 
79
  return prompt,neg_prompt
80
 
81
- def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator,disable_auto_prompt_correction):
82
- if(not disable_auto_prompt_correction):
83
- prompt,neg_prompt=auto_prompt_correction(prompt,neg_prompt)
84
-
85
  result = pipe(
86
  prompt,
87
  negative_prompt = neg_prompt,
@@ -93,10 +94,7 @@ def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator,dis
93
 
94
  return result.images[0]
95
 
96
- def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator,disable_auto_prompt_correction):
97
- if(not disable_auto_prompt_correction):
98
- prompt,neg_prompt=auto_prompt_correction(prompt,neg_prompt)
99
-
100
  ratio = min(height / img.height, width / img.width)
101
  img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
102
  result = pipe_i2i(
@@ -142,6 +140,7 @@ with gr.Blocks(css=css) as demo:
142
  with gr.Column(scale=55):
143
  with gr.Group():
144
  with gr.Row():
 
145
  prompt = gr.Textbox(label="Prompt", show_label=False, max_lines=2,placeholder="[your prompt]").style(container=False)
146
  generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))
147
 
@@ -169,7 +168,7 @@ with gr.Blocks(css=css) as demo:
169
  image = gr.Image(label="Image", height=256, tool="editor", type="pil")
170
  strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
171
 
172
- inputs = [prompt, guidance, steps, width, height, seed, image, strength, neg_prompt, disable_auto_prompt_correction]
173
 
174
  outputs = [image_out, error_output]
175
  prompt.submit(inference, inputs=inputs, outputs=outputs)
 
33
  {error}""" if error else ""
34
 
35
 
36
+ def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt="", cool_japan_type="Anime", disable_auto_prompt_correction=False):
37
 
38
  generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
39
+
40
+ if(not disable_auto_prompt_correction):
41
+ prompt,neg_prompt=auto_prompt_correction(prompt,neg_prompt,cool_japan_type)
42
+
43
  try:
44
  if img is not None:
45
  return img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator, disable_auto_prompt_correction), None
 
47
  return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator, disable_auto_prompt_correction), None
48
  except Exception as e:
49
  return None, error_str(e)
50
+ def auto_prompt_correction(prompt_ui,neg_prompt_ui,cool_japan_type_ui):
51
  # auto prompt correction
52
+ cool_japan_type=str(cool_japan_type_ui)
53
  prompt=str(prompt_ui)
54
  neg_prompt=str(neg_prompt_ui)
55
  prompt=prompt.lower()
56
  neg_prompt=neg_prompt.lower()
57
  if(prompt=="" and neg_prompt==""):
58
+ prompt=f"{cool_japan_type}, a portrait of a girl, 4k, detailed"
59
  neg_prompt=f"(((deformed))), blurry, ((((bad anatomy)))), {neg_prompt}, bad pupil, disfigured, poorly drawn face, mutation, mutated, (extra limb), (ugly), (poorly drawn hands), bad hands, fused fingers, messy drawing, broken legs censor, low quality, ((mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), ((bad eyes)), ui, error, missing fingers, fused fingers, one hand with more than 5 fingers, one hand with less than 5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, long body, uncoordinated body, unnatural body, lowres, jpeg artifacts, 2d, 3d, cg, text"
60
 
61
  splited_prompt=prompt.replace(","," ").replace("_"," ").split(" ")
 
65
  human_words=["girl","maid","female","woman","boy","male","man","guy"]
66
  for word in human_words:
67
  if( word in splited_prompt):
68
+ prompt=f"{cool_japan_type}, {prompt}, 4k, detailed"
69
  neg_prompt=f"(((deformed))), blurry, ((((bad anatomy)))), {neg_prompt}, bad pupil, disfigured, poorly drawn face, mutation, mutated, (extra limb), (ugly), (poorly drawn hands), bad hands, fused fingers, messy drawing, broken legs censor, low quality, ((mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), ((bad eyes)), ui, error, missing fingers, fused fingers, one hand with more than 5 fingers, one hand with less than 5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, long body, uncoordinated body, unnatural body, lowres, jpeg artifacts, 2d, 3d, cg, text"
70
 
71
  animal_words=["cat","dog","bird"]
72
  for word in animal_words:
73
  if( word in splited_prompt):
74
+ prompt=f"{cool_japan_type}, a {word}, 4k, detailed"
75
  neg_prompt=f"(((deformed))), blurry, ((((bad anatomy)))), {neg_prompt}, bad pupil, disfigured, poorly drawn face, mutation, mutated, (extra limb), (ugly), (poorly drawn hands), bad hands, fused fingers, messy drawing, broken legs censor, low quality, ((mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), ((bad eyes)), ui, error, missing fingers, fused fingers, one hand with more than 5 fingers, one hand with less than 5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, long body, uncoordinated body, unnatural body, lowres, jpeg artifacts, 2d, 3d, cg, text"
76
 
77
  background_words=["mount fuji","mt. fuji","building", "buildings", "tokyo", "kyoto", "shibuya", "shinjuku"]
78
  for word in background_words:
79
  if( word in splited_prompt):
80
+ prompt=f"{cool_japan_type}, shinkai makoto, {word}, 4k, 8k, highly detailed"
81
  neg_prompt=f"(((deformed))), {neg_prompt}, photo, people, low quality, ui, error, lowres, jpeg artifacts, 2d, 3d, cg, text"
82
 
83
  return prompt,neg_prompt
84
 
85
+ def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator):
 
 
 
86
  result = pipe(
87
  prompt,
88
  negative_prompt = neg_prompt,
 
94
 
95
  return result.images[0]
96
 
97
+ def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator):
 
 
 
98
  ratio = min(height / img.height, width / img.width)
99
  img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
100
  result = pipe_i2i(
 
140
  with gr.Column(scale=55):
141
  with gr.Group():
142
  with gr.Row():
143
+ cool_japan_type=gr.Radio(["Anime", "Manga", "Game"])
144
  prompt = gr.Textbox(label="Prompt", show_label=False, max_lines=2,placeholder="[your prompt]").style(container=False)
145
  generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))
146
 
 
168
  image = gr.Image(label="Image", height=256, tool="editor", type="pil")
169
  strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
170
 
171
+ inputs = [prompt, guidance, steps, width, height, seed, image, strength, neg_prompt, cool_japan_type, disable_auto_prompt_correction]
172
 
173
  outputs = [image_out, error_output]
174
  prompt.submit(inference, inputs=inputs, outputs=outputs)