vilarin commited on
Commit
ce19625
1 Parent(s): 4efab5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -36
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  import torch
3
- from diffusers import DiffusionPipeline, UNet2DConditionModel, LCMScheduler
4
  from huggingface_hub import hf_hub_download
5
  import spaces
6
  from PIL import Image
@@ -10,13 +10,8 @@ from translatepy import Translator
10
  translator = Translator()
11
 
12
  # Constants
13
- base = "stabilityai/stable-diffusion-xl-base-1.0"
14
- repo = "tianweiy/DMD2"
15
- checkpoints = {
16
- "1-Step" : ["dmd2_sdxl_1step_unet_fp16.bin", 1],
17
- "4-Step" : ["dmd2_sdxl_4step_unet_fp16.bin", 4],
18
- }
19
- loaded = None
20
 
21
  CSS = """
22
  .gradio-container {
@@ -34,38 +29,42 @@ JS = """function () {
34
  }
35
  }"""
36
 
37
-
 
 
 
 
38
 
39
  # Ensure model and scheduler are initialized in GPU-enabled function
40
  if torch.cuda.is_available():
41
- unet = UNet2DConditionModel.from_config(base, subfolder="unet").to("cuda", torch.float16)
42
- pipe = DiffusionPipeline.from_pretrained(base, torch_dtype=torch.float16, variant="fp16").to("cuda")
43
 
44
 
45
  # Function
46
  @spaces.GPU()
47
- def generate_image(prompt, ckpt="4-Step"):
48
- global loaded
 
 
 
 
 
49
 
50
  prompt = str(translator.translate(prompt, 'English'))
51
 
52
- print(prompt)
53
 
54
- checkpoint = checkpoints[ckpt][0]
55
- num_inference_steps = checkpoints[ckpt][1]
56
-
57
- if loaded != num_inference_steps:
58
- pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
59
- pipe.unet.load_state_dict(torch.load(hf_hub_download(repo, checkpoint), map_location="cuda"))
60
- loaded = num_inference_steps
61
-
62
- if loaded == 1:
63
- timesteps=[399]
64
- else:
65
- timesteps=[999, 749, 499, 249]
66
-
67
- results = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=0, timesteps=timesteps)
68
- return results.images[0]
69
 
70
 
71
  examples = [
@@ -82,14 +81,46 @@ examples = [
82
  # Gradio Interface
83
 
84
  with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
85
- gr.HTML("<h1><center>Adobe DMD2🦖</center></h1>")
86
- gr.HTML("<p><center><a href='https://huggingface.co/tianweiy/DMD2'>DMD2</a> text-to-image generation</center><br><center>Multi-Languages, 4-step is higher quality & 2X slower</center></p>")
87
  with gr.Group():
88
  with gr.Row():
89
- prompt = gr.Textbox(label='Enter Your Prompt', scale=8)
90
- ckpt = gr.Dropdown(label='Steps',choices=['1-Step', '4-Step'], value='4-Step', interactive=True)
91
  submit = gr.Button(scale=1, variant='primary')
92
- img = gr.Image(label='DMD2 Generated Image')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  gr.Examples(
94
  examples=examples,
95
  inputs=prompt,
@@ -99,11 +130,11 @@ with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
99
  )
100
 
101
  prompt.submit(fn=generate_image,
102
- inputs=[prompt, ckpt],
103
  outputs=img,
104
  )
105
  submit.click(fn=generate_image,
106
- inputs=[prompt, ckpt],
107
  outputs=img,
108
  )
109
 
 
1
  import gradio as gr
2
  import torch
3
+ from diffusers import StableDiffusionXLPipeline, AutoencoderKL
4
  from huggingface_hub import hf_hub_download
5
  import spaces
6
  from PIL import Image
 
10
  translator = Translator()
11
 
12
  # Constants
13
+ base = "Corcelio/openvision"
14
+ vae = "madebyollin/sdxl-vae-fp16-fix"
 
 
 
 
 
15
 
16
  CSS = """
17
  .gradio-container {
 
29
  }
30
  }"""
31
 
32
+ # Load VAE component
33
+ vae = AutoencoderKL.from_pretrained(
34
+ "madebyollin/sdxl-vae-fp16-fix",
35
+ torch_dtype=torch.float16
36
+ )
37
 
38
  # Ensure model and scheduler are initialized in GPU-enabled function
39
  if torch.cuda.is_available():
40
+ pipe = StableDiffusionXLPipeline.from_pretrained(base, vae=vae, torch_dtype=torch.float16).to("cuda")
41
+
42
 
43
 
44
  # Function
45
  @spaces.GPU()
46
+ def generate_image(
47
+ prompt,
48
+ negative,
49
+ width=1024,
50
+ height=1024,
51
+ scale=1.5,
52
+ steps=30):
53
 
54
  prompt = str(translator.translate(prompt, 'English'))
55
 
56
+ print(f'prompt:{prompt}')
57
 
58
+ image = pipe(
59
+ prompt,
60
+ negative_prompt=negative,
61
+ width=width,
62
+ height=height,
63
+ guidance_scale=scale,
64
+ num_inference_steps=steps,
65
+ )
66
+ print(image.images[0])
67
+ return image.images[0]
 
 
 
 
 
68
 
69
 
70
  examples = [
 
81
  # Gradio Interface
82
 
83
  with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
84
+ gr.HTML("<h1><center>OpenVision</center></h1>")
85
+ gr.HTML("<p><center><a href='https://huggingface.co/Corcelio/openvision'>OpenVision</a> text-to-image generation</center><br><center>Multi-Languages. Midjourney Aesthetic for All Your Images</center></p>")
86
  with gr.Group():
87
  with gr.Row():
88
+ prompt = gr.Textbox(label='Enter Your Prompt', scale=6)
 
89
  submit = gr.Button(scale=1, variant='primary')
90
+ img = gr.Image(label='OpenVision Generated Image')
91
+ with gr.Accordion("Advanced Options", open=False):
92
+ with gr.Row():
93
+ negative = gr.Textbox(label="Negative prompt", value="low quality")
94
+ with gr.Row():
95
+ width = gr.Slider(
96
+ label="Width",
97
+ minimum=512,
98
+ maximum=1280,
99
+ step=8,
100
+ value=1024,
101
+ )
102
+ height = gr.Slider(
103
+ label="Height",
104
+ minimum=512,
105
+ maximum=1280,
106
+ step=8,
107
+ value=1024,
108
+ )
109
+ with gr.Row():
110
+ scale = gr.Slider(
111
+ label="Guidance Scale",
112
+ minimum=0,
113
+ maximum=50,
114
+ step=0.1,
115
+ value=1.5,
116
+ )
117
+ steps = gr.Slider(
118
+ label="Steps",
119
+ minimum=1,
120
+ maximum=50,
121
+ step=1,
122
+ value=30,
123
+ )
124
  gr.Examples(
125
  examples=examples,
126
  inputs=prompt,
 
130
  )
131
 
132
  prompt.submit(fn=generate_image,
133
+ inputs=[prompt, negative, width, height, scale, steps],
134
  outputs=img,
135
  )
136
  submit.click(fn=generate_image,
137
+ inputs=[prompt, negative, width, height, scale, steps],
138
  outputs=img,
139
  )
140