Nymbo commited on
Commit
13de8f8
1 Parent(s): ce8f7c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -95
app.py CHANGED
@@ -6,32 +6,28 @@ import gradio as gr
6
  import numpy as np
7
  import torch
8
  from PIL import Image
9
- from diffusers import StableDiffusionXLPipeline, EDMEulerScheduler, StableDiffusionXLInstructPix2PixPipeline, AutoencoderKL, EulerAncestralDiscreteScheduler
10
- from custom_pipeline import CosStableDiffusionXLInstructPix2PixPipeline
11
- from huggingface_hub import hf_hub_download
12
- from huggingface_hub import InferenceClient
13
 
14
- device = "cuda" if torch.cuda.is_available() else "cpu"
15
- dtype = torch.float16
16
  vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
 
 
 
17
 
18
- repo = "fluently/Fluently-XL-Final"
 
19
 
20
- pipe_best = StableDiffusionXLPipeline.from_pretrained(repo, torch_dtype=torch.float16, vae=vae)
21
- pipe_best.load_lora_weights("ehristoforu/dalle-3-xl-v2", weight_name="dalle-3-xl-lora-v2.safetensors", adapter_name="dalle")
22
- pipe_best.load_lora_weights("KingNish/Better-Image-XL-Lora", weight_name="example-03.safetensors", adapter_name="lora")
23
- pipe_best.set_adapters(["lora","dalle"], adapter_weights=[1.5, 0.5])
24
- pipe_best.to("cuda")
25
 
26
- pipe_3D = StableDiffusionXLPipeline.from_pretrained(repo, torch_dtype=torch.float16, vae=vae)
27
- pipe_3D.load_lora_weights("artificialguybr/3DRedmond-V1", weight_name="3DRedmond-3DRenderStyle-3DRenderAF.safetensors", adapter_name="3D")
28
- pipe_3D.set_adapters(["3D"])
29
- pipe_3D.to("cuda")
30
-
31
- pipe_logo = StableDiffusionXLPipeline.from_pretrained(repo, torch_dtype=torch.float16, vae=vae)
32
- pipe_logo.load_lora_weights("artificialguybr/LogoRedmond-LogoLoraForSDXL", weight_name="LogoRedmond_LogoRedAF.safetensors", adapter_name="logo")
33
- pipe_logo.set_adapters(["logo"])
34
- pipe_logo.to("cuda")
35
 
36
  def set_timesteps_patched(self, num_inference_steps: int, device = None):
37
  self.num_inference_steps = num_inference_steps
@@ -50,55 +46,86 @@ def set_timesteps_patched(self, num_inference_steps: int, device = None):
50
  # Image Editor
51
  edit_file = hf_hub_download(repo_id="stabilityai/cosxl", filename="cosxl_edit.safetensors")
52
  EDMEulerScheduler.set_timesteps = set_timesteps_patched
53
- pipe_edit = StableDiffusionXLInstructPix2PixPipeline.from_single_file(
54
- edit_file, num_in_channels=8, is_cosxl_edit=True, vae=vae, torch_dtype=torch.float16,
55
- )
56
  pipe_edit.scheduler = EDMEulerScheduler(sigma_min=0.002, sigma_max=120.0, sigma_data=1.0, prediction_type="v_prediction")
57
  pipe_edit.to("cuda")
58
 
 
 
 
 
 
 
 
 
59
  # Generator
60
- @spaces.GPU(duration=45, queue=False)
61
  def king(type ,
62
  input_image ,
63
  instruction: str ,
64
- steps: int = 8,
65
- randomize_seed: bool = False,
66
- seed: int = 25,
67
- text_cfg_scale: float = 7.3,
68
- image_cfg_scale: float = 1.7,
69
  width: int = 1024,
70
  height: int = 1024,
71
- style="BEST",
72
- use_resolution_binning: bool = True,
73
- progress=gr.Progress(track_tqdm=True),
74
  ):
75
  if type=="Image Editing" :
 
76
  if randomize_seed:
77
- seed = random.randint(0, 99999)
78
- text_cfg_scale = text_cfg_scale
79
- image_cfg_scale = image_cfg_scale
80
- input_image = input_image
81
-
82
- steps=steps
83
  generator = torch.manual_seed(seed)
84
  output_image = pipe_edit(
85
- instruction, image=input_image,
86
- guidance_scale=text_cfg_scale, image_guidance_scale=image_cfg_scale,
87
- num_inference_steps=steps, generator=generator).images[0]
88
- return seed, output_image
 
 
 
 
 
 
 
 
 
 
89
  else :
90
  if randomize_seed:
91
- seed = random.randint(0, 99999)
92
  generator = torch.Generator().manual_seed(seed)
93
- if style=="3D":
94
- instruction = f"3DRenderAF, 3D Render, {instruction}"
95
- image = pipe_3D( prompt = instruction, guidance_scale = 5, num_inference_steps = steps, width = width, height = height, generator = generator).images[0]
96
- elif style=="Logo":
97
- instruction = f"LogoRedAF, {instruction}"
98
- image = pipe_logo( prompt = instruction, guidance_scale = 5, num_inference_steps = steps, width = width, height = height, generator = generator).images[0]
99
- else:
100
- image = pipe_best( prompt = instruction, guidance_scale = 5, num_inference_steps = steps, width = width, height = height, generator = generator).images[0]
101
- return seed, image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  client = InferenceClient()
104
  # Prompt classifier
@@ -106,19 +133,25 @@ def response(instruction, input_image=None ):
106
  if input_image is None:
107
  output="Image Generation"
108
  else:
109
- text = instruction
110
- labels = ["Image Editing", "Image Generation"]
111
- classification = client.zero_shot_classification(text, labels, multi_label=True)
112
- output = classification[0]
113
- output = str(output)
114
- if "Editing" in output:
115
- output = "Image Editing"
116
- else:
117
- output = "Image Generation"
 
 
 
 
 
 
118
  return output
119
 
120
  css = '''
121
- .gradio-container{max-width: 600px !important}
122
  h1{text-align:center}
123
  footer {
124
  visibility: hidden
@@ -147,47 +180,50 @@ examples=[
147
  [
148
  "Image Generation",
149
  None,
150
- "Ironman fighting with hulk, wall painting",
151
-
 
 
 
 
152
  ],
153
  [
154
  "Image Generation",
155
  None,
156
  "Beautiful Eiffel Tower at Night",
157
-
158
  ],
159
  ]
160
 
161
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
162
- gr.HTML("<center><h1>Image Gen Pro</h1></center>")
163
  with gr.Row():
164
- with gr.Column(scale=4):
165
- instruction = gr.Textbox(lines=1, label="Instruction", interactive=True)
166
- with gr.Column(scale=1):
167
- type = gr.Dropdown(["Image Generation","Image Editing"], label="Task", value="Image Generation",interactive=True, info="AI will select option based on your query, but if it selects wrong, please choose correct one.")
168
- with gr.Column(scale=1):
169
- generate_button = gr.Button("Generate")
170
  with gr.Row():
171
- style = gr.Radio(choices=["BEST","3D","Logo"],label="Style", value="BEST", interactive=True)
 
 
 
172
  with gr.Row():
173
- input_image = gr.Image(label="Image", type="pil", interactive=True)
174
 
175
  with gr.Row():
176
- width = gr.Number(value=1024, step=16,label="Width", interactive=True)
177
- height = gr.Number(value=1024, step=16,label="Height", interactive=True)
178
-
179
- with gr.Row():
180
- text_cfg_scale = gr.Number(value=7.3, step=0.1, label="Text CFG", interactive=True)
181
- image_cfg_scale = gr.Number(value=1.7, step=0.1,label="Image CFG", interactive=True)
182
- steps = gr.Number(value=25, precision=0, label="Steps", interactive=True)
183
- randomize_seed = gr.Radio(
184
- ["Fix Seed", "Randomize Seed"],
185
- value="Randomize Seed",
186
- type="index",
187
- show_label=False,
188
- interactive=True,
189
- )
190
- seed = gr.Number(value=1371, precision=0, label="Seed", interactive=True)
 
191
 
192
  gr.Examples(
193
  examples=examples,
@@ -196,8 +232,11 @@ with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
196
  outputs=[input_image],
197
  cache_examples=False,
198
  )
199
-
 
 
200
  instruction.change(fn=response, inputs=[instruction,input_image], outputs=type, queue=False)
 
201
  input_image.upload(fn=response, inputs=[instruction,input_image], outputs=type, queue=False)
202
 
203
  gr.on(triggers=[
@@ -208,16 +247,19 @@ with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
208
  inputs=[type,
209
  input_image,
210
  instruction,
 
 
211
  steps,
212
  randomize_seed,
213
  seed,
214
- text_cfg_scale,
215
- image_cfg_scale,
216
  width,
217
  height,
218
- style
 
219
  ],
220
  outputs=[seed, input_image],
 
 
221
  )
222
 
223
- demo.queue(max_size=99999).launch()
 
6
  import numpy as np
7
  import torch
8
  from PIL import Image
9
+ from diffusers import StableDiffusionXLImg2ImgPipeline, StableDiffusionXLPipeline, EDMEulerScheduler, StableDiffusionXLInstructPix2PixPipeline, AutoencoderKL, DPMSolverMultistepScheduler
10
+ from huggingface_hub import hf_hub_download, InferenceClient
 
 
11
 
 
 
12
  vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
13
+ pipe = StableDiffusionXLPipeline.from_pretrained("SG161222/RealVisXL_V4.0", torch_dtype=torch.float16, vae=vae)
14
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config, use_karras_sigmas=True, algorithm_type="sde-dpmsolver++")
15
+ pipe.to("cuda")
16
 
17
+ refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
18
+ refiner.to("cuda")
19
 
20
+ pipe_fast = StableDiffusionXLPipeline.from_pretrained("SG161222/RealVisXL_V4.0_Lightning", torch_dtype=torch.float16, vae=vae, use_safetensors=True)
21
+ pipe_fast.to("cuda")
 
 
 
22
 
23
+ help_text = """
24
+ To optimize image results:
25
+ - Adjust the **Image CFG weight** if the image isn't changing enough or is changing too much. Lower it to allow bigger changes, or raise it to preserve original details.
26
+ - Modify the **Text CFG weight** to influence how closely the edit follows text instructions. Increase it to adhere more to the text, or decrease it for subtler changes.
27
+ - Experiment with different **random seeds** and **CFG values** for varied outcomes.
28
+ - **Rephrase your instructions** for potentially better results.
29
+ - **Increase the number of steps** for enhanced edits.
30
+ """
 
31
 
32
  def set_timesteps_patched(self, num_inference_steps: int, device = None):
33
  self.num_inference_steps = num_inference_steps
 
46
  # Image Editor
47
  edit_file = hf_hub_download(repo_id="stabilityai/cosxl", filename="cosxl_edit.safetensors")
48
  EDMEulerScheduler.set_timesteps = set_timesteps_patched
49
+ pipe_edit = StableDiffusionXLInstructPix2PixPipeline.from_single_file( edit_file, num_in_channels=8, is_cosxl_edit=True, vae=vae, torch_dtype=torch.float16 )
 
 
50
  pipe_edit.scheduler = EDMEulerScheduler(sigma_min=0.002, sigma_max=120.0, sigma_data=1.0, prediction_type="v_prediction")
51
  pipe_edit.to("cuda")
52
 
53
+ client1 = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
54
+ system_instructions1 = "<|system|>\nAct as Image Prompt Generation expert, Your task is to modify prompt by USER to more better prompt for Image Generation in Stable Diffusion XL. \n Modify the user's prompt to generate a high-quality image by incorporating essential keywords and styles according to prompt if none style is mentioned than assume realistic. The optimized prompt may include keywords according to prompt for resolution (4K, HD, 16:9 aspect ratio, , etc.), image quality (cute, masterpiece, high-quality, vivid colors, intricate details, etc.), and desired art styles (realistic, anime, 3D, logo, futuristic, fantasy, etc.). Ensure the prompt is concise, yet comprehensive and choose keywords wisely, to generate an exceptional image that meets the user's expectations. \n Your task is to reply with final optimized prompt only. If you get big prompt make it concise. and Apply all keyword at last of prompt. Reply with optimized prompt only.\n<|user|>\n"
55
+
56
+ def promptifier(prompt):
57
+ formatted_prompt = f"{system_instructions1}{prompt}\n<|assistant|>\n"
58
+ stream = client1.text_generation(formatted_prompt, max_new_tokens=100)
59
+ return stream
60
+
61
  # Generator
62
+ @spaces.GPU(duration=60, queue=False)
63
  def king(type ,
64
  input_image ,
65
  instruction: str ,
66
+ negative_prompt: str ="",
67
+ enhance_prompt: bool = True,
68
+ steps: int = 25,
69
+ randomize_seed: bool = True,
70
+ seed: int = 2404,
71
  width: int = 1024,
72
  height: int = 1024,
73
+ guidance_scale: float = 6,
74
+ fast=True,
75
+ progress=gr.Progress(track_tqdm=True)
76
  ):
77
  if type=="Image Editing" :
78
+ input_image = Image.open(input_image).convert('RGB')
79
  if randomize_seed:
80
+ seed = random.randint(0, 999999)
 
 
 
 
 
81
  generator = torch.manual_seed(seed)
82
  output_image = pipe_edit(
83
+ instruction, negative_prompt=negative_prompt, image=input_image,
84
+ guidance_scale=guidance_scale, image_guidance_scale=1.5,
85
+ width = input_image.width, height = input_image.height,
86
+ num_inference_steps=steps, generator=generator, output_type="latent",
87
+ ).images
88
+ refine = refiner(
89
+ prompt=f"{instruction}, 4k, hd, high quality, masterpiece",
90
+ negative_prompt = negative_prompt,
91
+ guidance_scale=7.5,
92
+ num_inference_steps=steps,
93
+ image=output_image,
94
+ generator=generator,
95
+ ).images[0]
96
+ return seed, refine
97
  else :
98
  if randomize_seed:
99
+ seed = random.randint(0, 999999)
100
  generator = torch.Generator().manual_seed(seed)
101
+ if enhance_prompt:
102
+ print(f"BEFORE: {instruction} ")
103
+ instruction = promptifier(instruction)
104
+ print(f"AFTER: {instruction} ")
105
+ guidance_scale2=(guidance_scale/2)
106
+ if fast:
107
+ refine = pipe_fast(prompt = instruction,
108
+ guidance_scale = guidance_scale2,
109
+ num_inference_steps = int(steps/2.5),
110
+ width = width, height = height,
111
+ generator = generator,
112
+ ).images[0]
113
+ else:
114
+ image = pipe_fast( prompt = instruction,
115
+ negative_prompt=negative_prompt,
116
+ guidance_scale = guidance_scale,
117
+ num_inference_steps = steps,
118
+ width = width, height = height,
119
+ generator = generator, output_type="latent",
120
+ ).images
121
+
122
+ refine = refiner( prompt=instruction,
123
+ negative_prompt = negative_prompt,
124
+ guidance_scale = 7.5,
125
+ num_inference_steps= steps,
126
+ image=image, generator=generator,
127
+ ).images[0]
128
+ return seed, refine
129
 
130
  client = InferenceClient()
131
  # Prompt classifier
 
133
  if input_image is None:
134
  output="Image Generation"
135
  else:
136
+ try:
137
+ text = instruction
138
+ labels = ["Image Editing", "Image Generation"]
139
+ classification = client.zero_shot_classification(text, labels, multi_label=True)
140
+ output = classification[0]
141
+ output = str(output)
142
+ if "Editing" in output:
143
+ output = "Image Editing"
144
+ else:
145
+ output = "Image Generation"
146
+ except:
147
+ if input_image is None:
148
+ output="Image Generation"
149
+ else:
150
+ output="Image Editing"
151
  return output
152
 
153
  css = '''
154
+ .gradio-container{max-width: 700px !important}
155
  h1{text-align:center}
156
  footer {
157
  visibility: hidden
 
180
  [
181
  "Image Generation",
182
  None,
183
+ "An alien grasping a sign board contain word 'ALIEN' with Neon Glow, neon, futuristic, neonpunk, neon lights",
184
+ ],
185
+ [
186
+ "Image Generation",
187
+ None,
188
+ "Beautiful Eiffel Tower at Night",
189
  ],
190
  [
191
  "Image Generation",
192
  None,
193
  "Beautiful Eiffel Tower at Night",
 
194
  ],
195
  ]
196
 
197
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
198
+ gr.HTML("<center><h1>Image Gen & Auto Edit</h1></center>")
199
  with gr.Row():
200
+ instruction = gr.Textbox(lines=1, label="Instruction", interactive=True)
201
+ generate_button = gr.Button("Run", scale=0)
 
 
 
 
202
  with gr.Row():
203
+ type = gr.Dropdown(["Image Generation","Image Editing"], label="Task", value="Image Generation",interactive=True)
204
+ enhance_prompt = gr.Checkbox(label="Enhance prompt", value=False, scale=0)
205
+ fast = gr.Checkbox(label="FAST Generation", value=True, scale=0)
206
+
207
  with gr.Row():
208
+ input_image = gr.Image(label="Image", type='filepath', interactive=True)
209
 
210
  with gr.Row():
211
+ guidance_scale = gr.Number(value=6.0, step=0.1, label="Guidance Scale", interactive=True)
212
+ steps = gr.Number(value=25, step=1, label="Steps", interactive=True)
213
+
214
+ with gr.Accordion("Advanced options", open=False):
215
+ with gr.Row():
216
+ negative_prompt = gr.Text(
217
+ label="Negative prompt",
218
+ max_lines=1,
219
+ value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, ugly, disgusting, blurry, amputation,(face asymmetry, eyes asymmetry, deformed eyes, open mouth)",
220
+ visible=True)
221
+ with gr.Row():
222
+ width = gr.Slider( label="Width", minimum=256, maximum=2048, step=64, value=1024)
223
+ height = gr.Slider( label="Height", minimum=256, maximum=2048, step=64, value=1024)
224
+ with gr.Row():
225
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value = True, interactive=True )
226
+ seed = gr.Number(value=2404, step=1, label="Seed", interactive=True)
227
 
228
  gr.Examples(
229
  examples=examples,
 
232
  outputs=[input_image],
233
  cache_examples=False,
234
  )
235
+
236
+ # gr.Markdown(help_text)
237
+
238
  instruction.change(fn=response, inputs=[instruction,input_image], outputs=type, queue=False)
239
+
240
  input_image.upload(fn=response, inputs=[instruction,input_image], outputs=type, queue=False)
241
 
242
  gr.on(triggers=[
 
247
  inputs=[type,
248
  input_image,
249
  instruction,
250
+ negative_prompt,
251
+ enhance_prompt,
252
  steps,
253
  randomize_seed,
254
  seed,
 
 
255
  width,
256
  height,
257
+ guidance_scale,
258
+ fast,
259
  ],
260
  outputs=[seed, input_image],
261
+ api_name = "image_gen_pro",
262
+ queue=False
263
  )
264
 
265
+ demo.queue(max_size=500).launch()