yuvanelson commited on
Commit
0947398
·
verified ·
1 Parent(s): 237c39c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import random
3
+ import gradio as gr
4
+ import numpy as np
5
+ import torch
6
+ from PIL import Image
7
+ from diffusers import (
8
+ StableDiffusionXLImg2ImgPipeline,
9
+ StableDiffusionXLPipeline,
10
+ EDMEulerScheduler,
11
+ AutoencoderKL,
12
+ DPMSolverMultistepScheduler
13
+ )
14
+ from huggingface_hub import hf_hub_download, InferenceClient
15
+
16
+ # Load models and pipelines
17
+ vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
18
+ pipe = StableDiffusionXLPipeline.from_pretrained("SG161222/RealVisXL_V4.0", torch_dtype=torch.float16, vae=vae)
19
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config, use_karras_sigmas=True)
20
+ pipe.to("cuda")
21
+
22
+ refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", vae=vae, torch_dtype=torch.float16, use_safetensors=True)
23
+ refiner.to("cuda")
24
+
25
+ pipe_fast = StableDiffusionXLPipeline.from_pretrained("SG161222/RealVisXL_V4.0_Lightning", torch_dtype=torch.float16, vae=vae, use_safetensors=True)
26
+ pipe_fast.to("cuda")
27
+
28
+ # Inference Client for prompt optimization
29
+ client1 = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
30
+ system_instructions = "<|system|>\nOptimize the following prompt for better image generation. Make it concise, high-quality, and descriptive.\n<|user|>\n"
31
+
32
+ # Function to optimize prompts
33
+ def promptifier(prompt):
34
+ return client1.text_generation(f"{system_instructions}{prompt}\n<|assistant|>\n", max_new_tokens=100)
35
+
36
+ # Image generation/editing function
37
+ def king(task, input_image, instruction, negative_prompt="", enhance_prompt=True, steps=25, randomize_seed=True, seed=2404, width=1024, height=1024, guidance_scale=6, fast=True):
38
+ if randomize_seed:
39
+ seed = random.randint(0, 999999)
40
+ generator = torch.manual_seed(seed)
41
+
42
+ if enhance_prompt:
43
+ instruction = promptifier(instruction)
44
+
45
+ if task == "Image Editing" and input_image:
46
+ input_image = Image.open(input_image).convert('RGB')
47
+ output_image = pipe_edit(instruction, negative_prompt=negative_prompt, image=input_image, guidance_scale=guidance_scale, width=width, height=height, num_inference_steps=steps, generator=generator).images
48
+ refined_image = refiner(prompt=f"{instruction}, 4k, hd, high quality", negative_prompt=negative_prompt, guidance_scale=7.5, num_inference_steps=steps, image=output_image, generator=generator).images[0]
49
+ return seed, refined_image
50
+ else:
51
+ if fast:
52
+ output_image = pipe_fast(prompt=instruction, guidance_scale=guidance_scale/2, num_inference_steps=int(steps/2.5), width=width, height=height, generator=generator).images[0]
53
+ else:
54
+ latent_image = pipe_fast(prompt=instruction, negative_prompt=negative_prompt, guidance_scale=guidance_scale, num_inference_steps=steps, width=width, height=height, generator=generator, output_type="latent").images
55
+ refined_image = refiner(prompt=instruction, negative_prompt=negative_prompt, guidance_scale=7.5, num_inference_steps=steps, image=latent_image, generator=generator).images[0]
56
+ return seed, refined_image
57
+
58
+ # Define the interface layout
59
+ css = '''
60
+ .gradio-container{max-width: 700px !important}
61
+ h1{text-align:center}
62
+ footer { visibility: hidden }
63
+ '''
64
+
65
+ examples = [
66
+ ["Image Generation", None, "A luxurious supercar with a unique design. The car should have a pearl white finish, and gold accents. 4k, realistic."],
67
+ ["Image Editing", "./supercar.png", "make it red"],
68
+ ["Image Editing", "./red_car.png", "add some snow"],
69
+ ["Image Generation", None, "An alien grasping a sign board containing the word 'ALIEN' with Neon Glow, neon, futuristic, neonpunk."],
70
+ ]
71
+
72
+ # Define Gradio interface
73
+ with gr.Blocks(css=css) as demo:
74
+ gr.Markdown("# Image Generation & Editing")
75
+
76
+ with gr.Row():
77
+ instruction = gr.Textbox(lines=1, label="Instruction")
78
+ generate_button = gr.Button("Run")
79
+
80
+ with gr.Row():
81
+ task = gr.Dropdown(["Image Generation", "Image Editing"], label="Task", value="Image Generation")
82
+ enhance_prompt = gr.Checkbox(label="Enhance prompt", value=False)
83
+ fast = gr.Checkbox(label="Fast Generation", value=True)
84
+
85
+ with gr.Row():
86
+ input_image = gr.Image(label="Image", type='filepath')
87
+
88
+ with gr.Row():
89
+ guidance_scale = gr.Slider(6.0, 1.0, 15.0, label="Guidance Scale")
90
+ steps = gr.Slider(25, 1, 100, label="Steps")
91
+
92
+ with gr.Accordion("Advanced options", open=False):
93
+ negative_prompt = gr.Text(value="deformed, distorted, blurry", label="Negative prompt")
94
+ width = gr.Slider(1024, 256, 2048, step=64, label="Width")
95
+ height = gr.Slider(1024, 256, 2048, step=64, label="Height")
96
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
97
+ seed = gr.Number(value=2404, label="Seed")
98
+
99
+ gr.Examples(examples=examples, inputs=[task, input_image, instruction], fn=king, outputs=[seed, input_image])
100
+
101
+ generate_button.click(king, [task, input_image, instruction, negative_prompt, enhance_prompt, steps, randomize_seed, seed, width, height, guidance_scale, fast], [seed, input_image])
102
+
103
+ demo.launch()