mochifz commited on
Commit
9196578
·
verified ·
1 Parent(s): 01e3f2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -142
app.py CHANGED
@@ -1,146 +1,107 @@
1
  import gradio as gr
2
- import numpy as np
3
- import random
4
- from diffusers import DiffusionPipeline
5
- import torch
6
-
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
-
9
- if torch.cuda.is_available():
10
- torch.cuda.max_memory_allocated(device=device)
11
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
12
- pipe.enable_xformers_memory_efficient_attention()
13
- pipe = pipe.to(device)
14
- else:
15
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
16
- pipe = pipe.to(device)
17
-
18
- MAX_SEED = np.iinfo(np.int32).max
19
- MAX_IMAGE_SIZE = 1024
20
-
21
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
22
-
23
- if randomize_seed:
24
- seed = random.randint(0, MAX_SEED)
25
-
26
- generator = torch.Generator().manual_seed(seed)
27
-
28
- image = pipe(
29
- prompt = prompt,
30
- negative_prompt = negative_prompt,
31
- guidance_scale = guidance_scale,
32
- num_inference_steps = num_inference_steps,
33
- width = width,
34
- height = height,
35
- generator = generator
36
- ).images[0]
37
-
38
- return image
39
-
40
- examples = [
41
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
42
- "An astronaut riding a green horse",
43
- "A delicious ceviche cheesecake slice",
44
- ]
45
-
46
- css="""
47
- #col-container {
48
- margin: 0 auto;
49
- max-width: 520px;
50
  }
51
- """
52
-
53
- if torch.cuda.is_available():
54
- power_device = "GPU"
55
- else:
56
- power_device = "CPU"
57
-
58
- with gr.Blocks(css=css) as demo:
59
-
60
- with gr.Column(elem_id="col-container"):
61
- gr.Markdown(f"""
62
- # Text-to-Image Gradio Template
63
- Currently running on {power_device}.
64
- """)
65
-
66
- with gr.Row():
67
-
68
- prompt = gr.Text(
69
- label="Prompt",
70
- show_label=False,
71
- max_lines=1,
72
- placeholder="Enter your prompt",
73
- container=False,
74
- )
75
-
76
- run_button = gr.Button("Run", scale=0)
77
-
78
- result = gr.Image(label="Result", show_label=False)
79
-
80
- with gr.Accordion("Advanced Settings", open=False):
81
-
82
- negative_prompt = gr.Text(
83
- label="Negative prompt",
84
- max_lines=1,
85
- placeholder="Enter a negative prompt",
86
- visible=False,
87
- )
88
-
89
- seed = gr.Slider(
90
- label="Seed",
91
- minimum=0,
92
- maximum=MAX_SEED,
93
- step=1,
94
- value=0,
95
- )
96
-
97
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
98
-
99
- with gr.Row():
100
-
101
- width = gr.Slider(
102
- label="Width",
103
- minimum=256,
104
- maximum=MAX_IMAGE_SIZE,
105
- step=32,
106
- value=512,
107
- )
108
-
109
- height = gr.Slider(
110
- label="Height",
111
- minimum=256,
112
- maximum=MAX_IMAGE_SIZE,
113
- step=32,
114
- value=512,
115
- )
116
-
117
- with gr.Row():
118
-
119
- guidance_scale = gr.Slider(
120
- label="Guidance scale",
121
- minimum=0.0,
122
- maximum=10.0,
123
- step=0.1,
124
- value=0.0,
125
- )
126
-
127
- num_inference_steps = gr.Slider(
128
- label="Number of inference steps",
129
- minimum=1,
130
- maximum=12,
131
- step=1,
132
- value=2,
133
- )
134
-
135
- gr.Examples(
136
- examples = examples,
137
- inputs = [prompt]
138
- )
139
-
140
- run_button.click(
141
- fn = infer,
142
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
143
- outputs = [result]
144
- )
145
 
146
- demo.queue().launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch, random, time
3
+ from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, EulerDiscreteScheduler
 
 
 
4
  device = "cuda" if torch.cuda.is_available() else "cpu"
5
+ translations = {
6
+ 'en': {
7
+ 'model': 'Model Path',
8
+ 'loading': 'Loading',
9
+ 'input': 'Input Image',
10
+ 'prompt': 'Prompt',
11
+ 'negative_prompt': 'Negative Prompt',
12
+ 'generate': 'Generate',
13
+ 'strength': 'Strength',
14
+ 'guidance_scale': 'Guidance Scale',
15
+ 'num_inference_steps': 'Number of Inference Steps',
16
+ 'width': 'Width',
17
+ 'height': 'Height',
18
+ 'seed': 'Seed',
19
+ },
20
+ 'zh': {
21
+ 'model': '模型路径',
22
+ 'loading': '载入',
23
+ 'input': '输入图像',
24
+ 'prompt': '提示',
25
+ 'negative_prompt': '负面提示',
26
+ 'generate': '生成',
27
+ 'strength': '强度',
28
+ 'guidance_scale': '指导尺度',
29
+ 'num_inference_steps': '推理步数',
30
+ 'width': '宽度',
31
+ 'height': '高度',
32
+ 'seed': '种子',
33
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  }
35
+ language='zh'
36
+ def generate_new_seed():
37
+ return random.randint(1, 2147483647)
38
+ def update_language(new_language):
39
+ return [
40
+ gr.Textbox.update(placeholder=translations[new_language]['model']),
41
+ gr.Button.update(value=translations[new_language]['loading']),
42
+ gr.Image.update(label=translations[new_language]['input']),
43
+ gr.Textbox.update(placeholder=translations[new_language]['prompt']),
44
+ gr.Textbox.update(placeholder=translations[new_language]['negative_prompt']),
45
+ gr.Button.update(value=translations[new_language]['generate']),
46
+ gr.Slider.update(label=translations[new_language]['strength']),
47
+ gr.Slider.update(label=translations[new_language]['guidance_scale']),
48
+ gr.Slider.update(label=translations[new_language]['num_inference_steps']),
49
+ gr.Slider.update(label=translations[new_language]['width']),
50
+ gr.Slider.update(label=translations[new_language]['height']),
51
+ gr.Number.update(label=translations[new_language]['seed'])
52
+ ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
+ text2img = None
55
+ img2img = None
56
+ def Generate(image_input, prompt, negative_prompt, strength, guidance_scale, num_inference_steps, width, height, seed):
57
+ if seed == -1:
58
+ seed = generate_new_seed()
59
+ generator = torch.Generator(device).manual_seed(int(seed))
60
+ global text2img, img2img
61
+ start_time = time.time()
62
+ if image_input is None:
63
+ image = text2img(prompt=prompt, negative_prompt=negative_prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, width=width, height=height, num_images_per_prompt=1, generator=generator).images[0]
64
+ else:
65
+ image = img2img(image=image_input, strength=0.75, prompt=prompt, negative_prompt=negative_prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, width=width, height=height, num_images_per_prompt=1, generator=generator).images[0]
66
+ minutes, seconds = divmod(round(time.time() - start_time), 60)
67
+ return image, f"{minutes:02d}:{seconds:02d}"
68
+ def Loading(model):
69
+ global text2img, img2img
70
+ text2img = StableDiffusionPipeline.from_pretrained(model, torch_dtype=torch.float16).to(device)
71
+ text2img.safety_checker = None
72
+ text2img.scheduler = EulerDiscreteScheduler.from_config(text2img.scheduler.config)
73
+ text2img.enable_xformers_memory_efficient_attention()
74
+ text2img.vae.enable_xformers_memory_efficient_attention()
75
+ img2img = StableDiffusionImg2ImgPipeline(**text2img.components)
76
+ return model
77
+ with gr.Blocks() as demo:
78
+ with gr.Row():
79
+ model = gr.Textbox(value="John6666/pony-realism-v21main-sdxl", label=translations[language]['model'])
80
+ loading = gr.Button(translations[language]['loading'])
81
+ set_language = gr.Dropdown(list(translations.keys()), label="Language", value=language)
82
+ with gr.Row():
83
+ with gr.Column():
84
+ with gr.Row():
85
+ image_input = gr.Image(label=translations[language]['input'])
86
+ with gr.Column():
87
+ prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder=translations[language]['prompt'], show_label=False, lines=3)
88
+ negative_prompt = gr.Textbox("deformed, distorted, disfigured, poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers, disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation,lowres,jpeg artifacts,username,logo,signature,watermark,monochrome,greyscale", placeholder=translations[language]['negative_prompt'], show_label=False, lines=3)
89
+ generate = gr.Button(translations[language]['generate'])
90
+ with gr.Row():
91
+ with gr.Column():
92
+ strength = gr.Slider(minimum=0, maximum=1, value=0.8, step=0.01, label=translations[language]['strength'])
93
+ guidance_scale = gr.Slider(minimum=1, maximum=15, value=7.5, step=0.5, label=translations[language]['guidance_scale'])
94
+ num_inference_steps = gr.Slider(minimum=1, maximum=100, value=50, step=1, label=translations[language]['num_inference_steps'])
95
+ width = gr.Slider(minimum=512, maximum=2048, value=512, step=8, label=translations[language]['width'])
96
+ height = gr.Slider(minimum=512, maximum=2048, value=512, step=8, label=translations[language]['height'])
97
+ with gr.Row():
98
+ seed = gr.Number(value=-1, label=translations[language]['seed'])
99
+ set_seed = gr.Button("🎲")
100
+ with gr.Column():
101
+ image_output = gr.Image()
102
+ text_output = gr.Textbox(label="time")
103
+ set_seed.click(generate_new_seed, None, seed)
104
+ generate.click(Generate, [image_input, prompt, negative_prompt, strength, guidance_scale, num_inference_steps, width, height, seed], [image_output, text_output])
105
+ loading.click(Loading, model, model)
106
+ set_language.change(update_language, set_language, [model, loading, image_input, prompt, negative_prompt, generate, strength, guidance_scale, num_inference_steps, width, height, seed])
107
+ demo.queue(concurrency_count=24, max_size=32).launch(max_threads=128)