Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,8 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from diffusers import
|
4 |
from huggingface_hub import hf_hub_download
|
5 |
import spaces
|
6 |
from PIL import Image
|
@@ -10,13 +12,8 @@ from translatepy import Translator
|
|
10 |
translator = Translator()
|
11 |
|
12 |
# Constants
|
13 |
-
|
14 |
-
|
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 {
|
@@ -35,37 +32,44 @@ JS = """function () {
|
|
35 |
}"""
|
36 |
|
37 |
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
# Ensure model and scheduler are initialized in GPU-enabled function
|
40 |
if torch.cuda.is_available():
|
41 |
-
|
42 |
-
|
|
|
43 |
|
44 |
|
45 |
# Function
|
46 |
@spaces.GPU()
|
47 |
-
def generate_image(
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
prompt = str(translator.translate(prompt, 'English'))
|
51 |
|
52 |
-
print(prompt)
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
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 +86,53 @@ examples = [
|
|
82 |
# Gradio Interface
|
83 |
|
84 |
with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
|
85 |
-
gr.HTML("<h1><center>
|
86 |
-
gr.HTML("<p><center><a href='https://huggingface.co/
|
87 |
with gr.Group():
|
88 |
with gr.Row():
|
89 |
-
prompt = gr.Textbox(label='Enter Your Prompt', scale=
|
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='
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
gr.Examples(
|
94 |
examples=examples,
|
95 |
inputs=prompt,
|
@@ -99,11 +142,11 @@ with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
|
|
99 |
)
|
100 |
|
101 |
prompt.submit(fn=generate_image,
|
102 |
-
inputs=[prompt,
|
103 |
outputs=img,
|
104 |
)
|
105 |
submit.click(fn=generate_image,
|
106 |
-
inputs=[prompt,
|
107 |
outputs=img,
|
108 |
)
|
109 |
|
|
|
1 |
+
|
2 |
+
|
3 |
import gradio as gr
|
4 |
import torch
|
5 |
+
from diffusers import StableDiffusionXLPipeline, AutoencoderKL, KDPM2AncestralDiscreteScheduler
|
6 |
from huggingface_hub import hf_hub_download
|
7 |
import spaces
|
8 |
from PIL import Image
|
|
|
12 |
translator = Translator()
|
13 |
|
14 |
# Constants
|
15 |
+
model = "stabilityai/stable-diffusion-3-medium"
|
16 |
+
vae_model = "madebyollin/sdxl-vae-fp16-fix"
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
CSS = """
|
19 |
.gradio-container {
|
|
|
32 |
}"""
|
33 |
|
34 |
|
35 |
+
# Load VAE component
|
36 |
+
vae = AutoencoderKL.from_pretrained(
|
37 |
+
vae_model,
|
38 |
+
torch_dtype=torch.float16
|
39 |
+
)
|
40 |
|
41 |
# Ensure model and scheduler are initialized in GPU-enabled function
|
42 |
if torch.cuda.is_available():
|
43 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(model, vae=vae, torch_dtype=torch.float16).to("cuda")
|
44 |
+
|
45 |
+
pipe.scheduler = KDPM2AncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
46 |
|
47 |
|
48 |
# Function
|
49 |
@spaces.GPU()
|
50 |
+
def generate_image(
|
51 |
+
prompt,
|
52 |
+
negative="low quality",
|
53 |
+
width=1024,
|
54 |
+
height=1024,
|
55 |
+
scale=1.5,
|
56 |
+
steps=30,
|
57 |
+
clip=3):
|
58 |
|
59 |
prompt = str(translator.translate(prompt, 'English'))
|
60 |
|
61 |
+
print(f'prompt:{prompt}')
|
62 |
|
63 |
+
image = pipe(
|
64 |
+
prompt,
|
65 |
+
negative_prompt=negative,
|
66 |
+
width=width,
|
67 |
+
height=height,
|
68 |
+
guidance_scale=scale,
|
69 |
+
num_inference_steps=steps,
|
70 |
+
clip_skip=clip,
|
71 |
+
)
|
72 |
+
return image.images[0]
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
|
75 |
examples = [
|
|
|
86 |
# Gradio Interface
|
87 |
|
88 |
with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
|
89 |
+
gr.HTML("<h1><center>Mobius💠</center></h1>")
|
90 |
+
gr.HTML("<p><center><a href='https://huggingface.co/Corcelio/mobius'>mobius</a> text-to-image generation</center><br><center>Multi-Languages. Adding default prompts to enhance.</center></p>")
|
91 |
with gr.Group():
|
92 |
with gr.Row():
|
93 |
+
prompt = gr.Textbox(label='Enter Your Prompt', value="best quality, HD, aesthetic", scale=6)
|
|
|
94 |
submit = gr.Button(scale=1, variant='primary')
|
95 |
+
img = gr.Image(label='Mobius Generated Image')
|
96 |
+
with gr.Accordion("Advanced Options", open=False):
|
97 |
+
with gr.Row():
|
98 |
+
negative = gr.Textbox(label="Negative prompt", value="low quality")
|
99 |
+
with gr.Row():
|
100 |
+
width = gr.Slider(
|
101 |
+
label="Width",
|
102 |
+
minimum=512,
|
103 |
+
maximum=1280,
|
104 |
+
step=8,
|
105 |
+
value=1024,
|
106 |
+
)
|
107 |
+
height = gr.Slider(
|
108 |
+
label="Height",
|
109 |
+
minimum=512,
|
110 |
+
maximum=1280,
|
111 |
+
step=8,
|
112 |
+
value=1024,
|
113 |
+
)
|
114 |
+
with gr.Row():
|
115 |
+
scale = gr.Slider(
|
116 |
+
label="Guidance",
|
117 |
+
minimum=3.5,
|
118 |
+
maximum=7,
|
119 |
+
step=0.1,
|
120 |
+
value=7,
|
121 |
+
)
|
122 |
+
steps = gr.Slider(
|
123 |
+
label="Steps",
|
124 |
+
minimum=1,
|
125 |
+
maximum=50,
|
126 |
+
step=1,
|
127 |
+
value=50,
|
128 |
+
)
|
129 |
+
clip = gr.Slider(
|
130 |
+
label="Clip Skip",
|
131 |
+
minimum=1,
|
132 |
+
maximum=10,
|
133 |
+
step=1,
|
134 |
+
value=3,
|
135 |
+
)
|
136 |
gr.Examples(
|
137 |
examples=examples,
|
138 |
inputs=prompt,
|
|
|
142 |
)
|
143 |
|
144 |
prompt.submit(fn=generate_image,
|
145 |
+
inputs=[prompt, negative, width, height, scale, steps, clip],
|
146 |
outputs=img,
|
147 |
)
|
148 |
submit.click(fn=generate_image,
|
149 |
+
inputs=[prompt, negative, width, height, scale, steps, clip],
|
150 |
outputs=img,
|
151 |
)
|
152 |
|