artificialguybr commited on
Commit
f8cfb21
1 Parent(s): ee87c51

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ import torch
4
+ from diffusers import Transformer2DModel
5
+ from scripts.diffusers_patches import pixart_sigma_init_patched_inputs, PixArtSigmaPipeline
6
+
7
+ assert getattr(Transformer2DModel, '_init_patched_inputs', False), "Need to Upgrade diffusers: pip install git+https://github.com/huggingface/diffusers"
8
+ setattr(Transformer2DModel, '_init_patched_inputs', pixart_sigma_init_patched_inputs)
9
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
10
+ weight_dtype = torch.float16
11
+
12
+ transformer = Transformer2DModel.from_pretrained(
13
+ "PixArt-alpha/PixArt-Sigma-XL-2-1024-MS",
14
+ subfolder='transformer',
15
+ torch_dtype=weight_dtype,
16
+ use_safetensors=True,
17
+ )
18
+ pipe = PixArtSigmaPipeline.from_pretrained(
19
+ "PixArt-alpha/pixart_sigma_sdxlvae_T5_diffusers",
20
+ transformer=transformer,
21
+ torch_dtype=weight_dtype,
22
+ use_safetensors=True,
23
+ )
24
+ pipe.to(device)
25
+
26
+ @spaces.GPU(duration=120)
27
+ def generate(prompt, negative_prompt, num_inference_steps, guidance_scale, height, width):
28
+ image = pipe(
29
+ prompt,
30
+ negative_prompt=negative_prompt,
31
+ num_inference_steps=num_inference_steps,
32
+ guidance_scale=guidance_scale,
33
+ height=height,
34
+ width=width
35
+ ).images[0]
36
+ return image
37
+
38
+ interface = gr.Interface(
39
+ fn=generate,
40
+ inputs=[
41
+ gr.Text(label="Prompt"),
42
+ gr.Text(label="Negative Prompt"),
43
+ gr.Slider(minimum=1, maximum=500, value=100, step=1, label="Number of Inference Steps"),
44
+ gr.Slider(minimum=1, maximum=20, value=4.5, step=0.1, label="Guidance Scale"),
45
+ gr.Slider(minimum=64, maximum=1024, value=512, step=64, label="Height"),
46
+ gr.Slider(minimum=64, maximum=1024, value=512, step=64, label="Width"),
47
+ ],
48
+ outputs=gr.Image(label="Generated Image"),
49
+ title="PixArt Sigma Image Generation",
50
+ description="Generate images using the PixArt Sigma model.",
51
+ )
52
+
53
+ interface.launch()