Pclanglais commited on
Commit
7bacf76
1 Parent(s): 7c785ed

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +137 -0
  2. requirements.txt +11 -0
app.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import torch
4
+ from diffusers import LCMScheduler, AutoPipelineForText2Image
5
+ from diffusers import AutoPipelineForInpainting, LCMScheduler
6
+ from diffusers import DiffusionPipeline, LCMScheduler
7
+ from PIL import Image, ImageEnhance
8
+ import io
9
+
10
+ @spaces.GPU
11
+ def generate_image(prompt, num_inference_steps, guidance_scale):
12
+ model_id = "stabilityai/stable-diffusion-xl-base-1.0"
13
+ adapter_id = "latent-consistency/lcm-lora-sdxl"
14
+
15
+ pipe = AutoPipelineForText2Image.from_pretrained(model_id, torch_dtype=torch.float32, variant="fp16")
16
+ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
17
+ pipe.to("cuda")
18
+
19
+ # Load and fuse lcm lora
20
+ pipe.load_lora_weights(adapter_id)
21
+ pipe.fuse_lora()
22
+
23
+ # Generate the image
24
+ image = pipe(prompt=prompt, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale).images[0]
25
+
26
+ return image
27
+
28
+ def inpaint_image(prompt, init_image, mask_image, num_inference_steps, guidance_scale):
29
+ pipe = AutoPipelineForInpainting.from_pretrained(
30
+ "diffusers/stable-diffusion-xl-1.0-inpainting-0.1",
31
+ torch_dtype=torch.float32,
32
+ variant="fp16",
33
+ ).to("cuda")
34
+ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
35
+ pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl")
36
+ pipe.fuse_lora()
37
+
38
+ if init_image is not None:
39
+ init_image_path = init_image.name # Get the file path
40
+ init_image = Image.open(init_image_path).resize((1024, 1024))
41
+ else:
42
+ raise ValueError("Initial image not provided or invalid")
43
+
44
+ if mask_image is not None:
45
+ mask_image_path = mask_image.name # Get the file path
46
+ mask_image = Image.open(mask_image_path).resize((1024, 1024))
47
+ else:
48
+ raise ValueError("Mask image not provided or invalid")
49
+
50
+ # Generate the inpainted image
51
+ generator = torch.manual_seed(42)
52
+ image = pipe(
53
+ prompt=prompt,
54
+ image=init_image,
55
+ mask_image=mask_image,
56
+ generator=generator,
57
+ num_inference_steps=num_inference_steps,
58
+ guidance_scale=guidance_scale,
59
+ ).images[0]
60
+
61
+ return image
62
+
63
+ def generate_image_with_adapter(prompt, num_inference_steps, guidance_scale):
64
+ pipe = DiffusionPipeline.from_pretrained(
65
+ "stabilityai/stable-diffusion-xl-base-1.0",
66
+ variant="fp16",
67
+ torch_dtype=torch.float32
68
+ ).to("cuda")
69
+
70
+ # set scheduler
71
+ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
72
+
73
+ # Load and fuse lcm lora
74
+ pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl", adapter_name="lcm")
75
+ pipe.load_lora_weights("Pclanglais/Mickey-1928", weight_name="pytorch_lora_weights.safetensors", adapter_name="mickey")
76
+
77
+ # Combine LoRAs
78
+ pipe.set_adapters(["lcm", "mickey"], adapter_weights=[1.0, 0.8])
79
+ pipe.fuse_lora()
80
+ generator = torch.manual_seed(0)
81
+ # Generate the image
82
+ image = pipe(prompt=prompt, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale, generator=generator).images[0]
83
+ pipe.unfuse_lora()
84
+ return image
85
+
86
+
87
+ def modify_image(image, brightness, contrast):
88
+ # Function to modify brightness and contrast
89
+ image = Image.open(io.BytesIO(image))
90
+ enhancer = ImageEnhance.Brightness(image)
91
+ image = enhancer.enhance(brightness)
92
+ enhancer = ImageEnhance.Contrast(image)
93
+ image = enhancer.enhance(contrast)
94
+ return image
95
+
96
+ with gr.Blocks(gr.themes.Soft()) as demo:
97
+ with gr.Row():
98
+ image_output = gr.Image(label="Generated Image")
99
+
100
+ with gr.Row():
101
+ with gr.Accordion(label="Configuration Options"):
102
+ prompt_input = gr.Textbox(label="Prompt", placeholder="Self-portrait oil painting, a beautiful cyborg with golden hair, 8k")
103
+ steps_input = gr.Slider(minimum=1, maximum=10, label="Inference Steps", value=4)
104
+ guidance_input = gr.Slider(minimum=0, maximum=2, label="Guidance Scale", value=1)
105
+ generate_button = gr.Button("Generate Image")
106
+ with gr.Row():
107
+ with gr.Accordion(label="Wiki-Mouse Image Generation"):
108
+ adapter_prompt_input = gr.Textbox(label="Prompt", placeholder="papercut, a cute fox")
109
+ adapter_steps_input = gr.Slider(minimum=1, maximum=10, label="Inference Steps", value=4)
110
+ adapter_guidance_input = gr.Slider(minimum=0, maximum=2, label="Guidance Scale", value=1)
111
+ adapter_generate_button = gr.Button("Generate Image with Adapter")
112
+
113
+
114
+
115
+ generate_button.click(
116
+ generate_image,
117
+ inputs=[prompt_input, steps_input, guidance_input],
118
+ outputs=image_output
119
+ )
120
+
121
+ modify_button.click(
122
+ modify_image,
123
+ inputs=[image_output, brightness_slider, contrast_slider],
124
+ outputs=image_output
125
+ )
126
+ inpaint_button.click(
127
+ inpaint_image,
128
+ inputs=[inpaint_prompt_input, init_image_input, mask_image_input, inpaint_steps_input, inpaint_guidance_input],
129
+ outputs=image_output
130
+ )
131
+ adapter_generate_button.click(
132
+ generate_image_with_adapter,
133
+ inputs=[adapter_prompt_input, adapter_steps_input, adapter_guidance_input],
134
+ outputs=image_output
135
+ )
136
+
137
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ git+https://github.com/huggingface/diffusers.git
2
+ git+https://github.com/huggingface/transformers.git
3
+ git+https://github.com/huggingface/peft.git
4
+ --extra-index-url https://download.pytorch.org/whl/cu113
5
+ torch
6
+ pydantic
7
+ Pillow
8
+ accelerate
9
+ spaces
10
+ invisible_watermark
11
+