cosmicman commited on
Commit
ebb7ad4
β€’
1 Parent(s): a64c8be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +203 -12
app.py CHANGED
@@ -1,18 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
- from transformers import pipeline
 
 
 
3
 
4
- pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
 
5
 
6
- def predict(input_img):
7
- predictions = pipeline(input_img)
8
- return input_img, {p["label"]: p["score"] for p in predictions}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- gradio_app = gr.Interface(
11
- predict,
12
- inputs=gr.Image(label="Select hot dog candidate", sources=['upload', 'webcam'], type="pil"),
13
- outputs=[gr.Image(label="Processed Image"), gr.Label(label="Result", num_top_classes=2)],
14
- title="Hot Dog? Or Not?",
15
- )
16
 
17
  if __name__ == "__main__":
18
- gradio_app.launch(share=True)
 
 
1
+ from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline, UNet2DConditionModel
2
+ from diffusers.utils import load_image
3
+ from diffusers import (
4
+ DDIMScheduler,
5
+ PNDMScheduler,
6
+ LMSDiscreteScheduler,
7
+ EulerDiscreteScheduler,
8
+ EulerAncestralDiscreteScheduler,
9
+ DPMSolverMultistepScheduler,
10
+ )
11
+ import torch
12
+ import os
13
+ import random
14
+ import numpy as np
15
+ from PIL import Image
16
+ from typing import Tuple
17
  import gradio as gr
18
+ DESCRIPTION = """
19
+ # CosmicMan
20
+ - CosmicMan: A Text-to-Image Foundation Model for Humans (CVPR 2024 (Highlight))
21
+ """
22
 
23
+ if not torch.cuda.is_available():
24
+ DESCRIPTION += "\n<p>Running on CPU πŸ₯Ά This demo does not work on CPU.</p>"
25
 
26
+ schedule_map = {
27
+ "ddim" : DDIMScheduler,
28
+ "pndm" : PNDMScheduler,
29
+ "lms" : LMSDiscreteScheduler,
30
+ "euler" : EulerDiscreteScheduler,
31
+ "euler_a": EulerAncestralDiscreteScheduler,
32
+ "dpm" : DPMSolverMultistepScheduler,
33
+ }
34
+
35
+ examples = [
36
+ "A fit Caucasian elderly woman, her wavy white hair above shoulders, wears a pink floral cotton long-sleeve shirt and a cotton hat against a natural landscape in an upper body shot",
37
+ "A closeup of a doll with a purple ribbon around her neck, best quality, extremely detailed",
38
+ "A closeup of a girl with a butterfly painted on her face",
39
+ "A headshot, an asian elderly male, a blue wall, bald above eyes gray hair",
40
+ "A closeup portrait shot against a white wall, a fit Caucasian adult female with wavy blonde hair falling above her chest wears a short sleeve silk floral dress and a floral silk normal short sleeve white blouse",
41
+ "A headshot, an adult caucasian male, fit, a white wall, red crew cut curly hair, short sleeve normal blue t-shirt, best quality, extremely detailed",
42
+ "A closeup of a man wearing a red shirt with a flower design on it",
43
+ "There is a man wearing a mask and holding a cell phone",
44
+ "Two boys playing in the yard",
45
+ ]
46
+
47
+ style_list = [
48
+ {
49
+ "name": "(No style)",
50
+ "prompt": "{prompt}",
51
+ "negative_prompt": "",
52
+ },
53
+ {
54
+ "name": "Cinematic",
55
+ "prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
56
+ "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
57
+ },
58
+ {
59
+ "name": "Photographic",
60
+ "prompt": "cinematic photo {prompt} . 35mm photograph, film, bokeh, professional, 4k, highly detailed",
61
+ "negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
62
+ },
63
+ {
64
+ "name": "Anime",
65
+ "prompt": "anime artwork {prompt} . anime style, key visual, vibrant, studio anime, highly detailed",
66
+ "negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast",
67
+ },
68
+ {
69
+ "name": "Fantasy art",
70
+ "prompt": "ethereal fantasy concept art of {prompt} . magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy",
71
+ "negative_prompt": "photographic, realistic, realism, 35mm film, dslr, cropped, frame, text, deformed, glitch, noise, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, sloppy, duplicate, mutated, black and white",
72
+ },
73
+ {
74
+ "name": "Neonpunk",
75
+ "prompt": "neonpunk style {prompt} . cyberpunk, vaporwave, neon, vibes, vibrant, stunningly beautiful, crisp, detailed, sleek, ultramodern, magenta highlights, dark purple shadows, high contrast, cinematic, ultra detailed, intricate, professional",
76
+ "negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured",
77
+ }
78
+ ]
79
+
80
+ styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
81
+ STYLE_NAMES = list(styles.keys())
82
+ DEFAULT_STYLE_NAME = "(No style)"
83
+ CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "1") == "1"
84
+ MAX_SEED = np.iinfo(np.int32).max
85
+ NUM_IMAGES_PER_PROMPT = 1
86
+
87
+ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
88
+ p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
89
+ if not negative:
90
+ negative = ""
91
+ return p.replace("{prompt}", positive), n + negative
92
+
93
+ class NoWatermark:
94
+ def apply_watermark(self, img):
95
+ return img
96
+
97
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
98
+ if randomize_seed:
99
+ seed = random.randint(0, MAX_SEED)
100
+ return seed
101
+
102
+ print("Loading Model!")
103
+ schedule: str = "euler_a"
104
+ base_model_path: str = "stabilityai/stable-diffusion-xl-base-1.0"
105
+ refiner_model_path: str = "stabilityai/stable-diffusion-xl-refiner-1.0"
106
+ unet_path: str = "cosmicman/CosmicMan-SDXL"
107
+ SCHEDULER = schedule_map[schedule]
108
+ scheduler = SCHEDULER.from_pretrained(base_model_path, subfolder="scheduler")
109
+ # unet = UNet2DConditionModel.from_pretrained(unet_path)
110
+
111
+ pipe = StableDiffusionXLPipeline.from_pretrained(
112
+ base_model_path,
113
+ # unet=unet,
114
+ scheduler=scheduler,
115
+ use_safetensors=True
116
+ ).to("cuda")
117
+ pipe.watermark = NoWatermark()
118
+ refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained(
119
+ base_model_path, # we found use base_model_path instead of refiner_model_path may get a better performance
120
+ scheduler=scheduler,
121
+ use_safetensors=True
122
+ ).to("cuda")
123
+ refiner.watermark = NoWatermark()
124
+ print("Finish Loading Model!")
125
+
126
+ def generate_image(prompt,
127
+ n_prompt="",
128
+ style: str = DEFAULT_STYLE_NAME,
129
+ steps: int = 50,
130
+ height: int = 1024,
131
+ width: int = 1024,
132
+ scale: float = 7.5,
133
+ img_num: int = 4,
134
+ seeds: int = 42,
135
+ random_seed: bool = False,
136
+ ):
137
+ print("Beign to generate")
138
+ image_list = []
139
+ for i in range(img_num):
140
+ seed = int(randomize_seed_fn(seeds, random_seed))
141
+ generator = torch.Generator().manual_seed(seed)
142
+ positive_prompt, negative_prompt = apply_style(style, prompt, n_prompt)
143
+ image = pipe(positive_prompt, num_inference_steps=steps,
144
+ guidance_scale=scale, height=height,
145
+ width=width, negative_prompt=negative_prompt,
146
+ generator=generator, output_type="latent").images[0]
147
+ image = refiner(positive_prompt, negative_prompt=negative_prompt, image=image[None, :]).images[0]
148
+ image_list.append((image,f"Seed {seed}"))
149
+ return image_list
150
+
151
+ with gr.Blocks(theme=gr.themes.Soft(),css="style.css") as demo:
152
+ gr.Markdown(DESCRIPTION)
153
+ with gr.Group():
154
+ with gr.Row():
155
+ with gr.Column():
156
+ input_prompt = gr.Textbox(label="Input prompt", lines=3, max_lines=5)
157
+ negative_prompt = gr.Textbox(label="Negative prompt",value="")
158
+ run_button = gr.Button("Run", scale=0)
159
+ result = gr.Gallery(label="Result", show_label=False, elem_id="gallery", columns=[2], rows=[2], object_fit="contain", height="auto")
160
+ with gr.Accordion("Advanced options", open=False):
161
+ with gr.Row():
162
+ style_selection = gr.Radio(
163
+ show_label=True,
164
+ container=True,
165
+ interactive=True,
166
+ choices=STYLE_NAMES,
167
+ value=DEFAULT_STYLE_NAME,
168
+ label="Image Style",
169
+ )
170
+ with gr.Row():
171
+ height = gr.Slider(minimum=512, maximum=1536, value=1024, label="Height", step=64)
172
+ width = gr.Slider(minimum=512, maximum=1536, value=1024, label="Witdh", step=64)
173
+ with gr.Row():
174
+ steps = gr.Slider(minimum=1, maximum=50, value=30, label="Number of diffusion steps", step=1)
175
+ scale = gr.Number(minimum=1, maximum=12, value=7.5, label="Number of scale")
176
+ with gr.Row():
177
+ seed = gr.Slider(
178
+ label="Seed",
179
+ minimum=0,
180
+ maximum=MAX_SEED,
181
+ step=1,
182
+ value=0,
183
+ )
184
+ random_seed = gr.Checkbox(label="Randomize seed", value=True)
185
+ img_num = gr.Slider(minimum=1, maximum=4, value=4, label="Number of images", step=1)
186
+
187
+ gr.Examples(
188
+ examples=examples,
189
+ inputs=input_prompt,
190
+ outputs=result,
191
+ fn=generate_image,
192
+ cache_examples=CACHE_EXAMPLES,
193
+ )
194
+
195
+ gr.on(
196
+ triggers=[
197
+ input_prompt.submit,
198
+ negative_prompt.submit,
199
+ run_button.click,
200
+ ],
201
+ fn=generate_image,
202
+ inputs = [input_prompt, negative_prompt, style_selection, steps, height, width, scale, img_num, seed, random_seed],
203
+ outputs= result,
204
+ api_name="run")
205
 
 
 
 
 
 
 
206
 
207
  if __name__ == "__main__":
208
+ demo.queue(max_size=20).launch(show_api=False, debug=False)
209
+