KingNish commited on
Commit
fb3409b
1 Parent(s): ce7a615

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -31
app.py CHANGED
@@ -6,19 +6,14 @@ import gradio as gr
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:
@@ -51,11 +46,11 @@ pipe_edit.scheduler = EDMEulerScheduler(sigma_min=0.002, sigma_max=120.0, sigma_
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
@@ -71,7 +66,6 @@ def king(type ,
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" :
@@ -102,22 +96,17 @@ def king(type ,
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,
@@ -202,7 +191,6 @@ with gr.Blocks(css=css) as demo:
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)
@@ -255,7 +243,6 @@ with gr.Blocks(css=css) as demo:
255
  width,
256
  height,
257
  guidance_scale,
258
- fast,
259
  ],
260
  outputs=[seed, input_image],
261
  api_name = "image_gen_pro",
 
6
  import numpy as np
7
  import torch
8
  from PIL import Image
9
+ from diffusers import EDMEulerScheduler, StableDiffusionXLInstructPix2PixPipeline
10
  from huggingface_hub import hf_hub_download, InferenceClient
11
+ from diffusers import DiffusionPipeline
12
 
13
+ dtype = torch.bfloat16
14
+ device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
15
 
16
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16, revision="refs/pr/1").to(device)
 
 
 
 
17
 
18
  help_text = """
19
  To optimize image results:
 
46
  pipe_edit.to("cuda")
47
 
48
  client1 = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
49
+ system_instructions1 = "<|system|>\nAct as Image Prompt Generation expert, Your task is to modify prompt by USER to more better and detailed prompt for Image Generation. \n Ensure the prompt is deatiled, yet descriptive to generate an exceptional image that meets the user's expectations. \n Your task is to reply with final optimized prompt only. Reply with optimized prompt only.\n<|user|>\n"
50
 
51
  def promptifier(prompt):
52
  formatted_prompt = f"{system_instructions1}{prompt}\n<|assistant|>\n"
53
+ stream = client1.text_generation(formatted_prompt, max_new_tokens=300)
54
  return stream
55
 
56
  # Generator
 
66
  width: int = 1024,
67
  height: int = 1024,
68
  guidance_scale: float = 6,
 
69
  progress=gr.Progress(track_tqdm=True)
70
  ):
71
  if type=="Image Editing" :
 
96
  print(f"BEFORE: {instruction} ")
97
  instruction = promptifier(instruction)
98
  print(f"AFTER: {instruction} ")
99
+
100
+ image = pipe(
101
+ prompt = instruction,
102
+ negative_prompt = negative_prompt,
103
+ width = width,
104
+ height = height,
105
+ num_inference_steps = (steps/5),
106
+ generator = generator,
107
+ guidance_scale=0.0,
108
+ output_type="latent"
109
+ ).images
 
 
 
 
 
110
 
111
  refine = refiner( prompt=instruction,
112
  negative_prompt = negative_prompt,
 
191
  with gr.Row():
192
  type = gr.Dropdown(["Image Generation","Image Editing"], label="Task", value="Image Generation",interactive=True)
193
  enhance_prompt = gr.Checkbox(label="Enhance prompt", value=False, scale=0)
 
194
 
195
  with gr.Row():
196
  input_image = gr.Image(label="Image", type='filepath', interactive=True)
 
243
  width,
244
  height,
245
  guidance_scale,
 
246
  ],
247
  outputs=[seed, input_image],
248
  api_name = "image_gen_pro",