Bordoglor commited on
Commit
6812ded
·
verified ·
1 Parent(s): e9d41a2

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +160 -0
app.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+
5
+ # import spaces #[uncomment to use ZeroGPU]
6
+ from diffusers import DiffusionPipeline
7
+ import torch
8
+
9
+ model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
10
+
11
+ MAX_SEED = np.iinfo(np.int32).max
12
+ MAX_IMAGE_SIZE = 1024
13
+
14
+
15
+ # @spaces.GPU #[uncomment to use ZeroGPU]
16
+ def infer(
17
+ model,
18
+ prompt,
19
+ negative_prompt,
20
+ seed,
21
+ randomize_seed,
22
+ width,
23
+ height,
24
+ guidance_scale,
25
+ num_inference_steps,
26
+ progress=gr.Progress(track_tqdm=True),
27
+ ):
28
+ if randomize_seed:
29
+ seed = random.randint(0, MAX_SEED)
30
+
31
+ generator = torch.Generator().manual_seed(seed)
32
+
33
+ device = "cuda" if torch.cuda.is_available() else "cpu"
34
+
35
+ if torch.cuda.is_available():
36
+ torch_dtype = torch.float16
37
+ else:
38
+ torch_dtype = torch.float32
39
+
40
+ pipe = DiffusionPipeline.from_pretrained(model, torch_dtype=torch_dtype)
41
+ pipe = pipe.to(device)
42
+
43
+ image = pipe(
44
+ prompt=prompt,
45
+ negative_prompt=negative_prompt,
46
+ guidance_scale=guidance_scale,
47
+ num_inference_steps=num_inference_steps,
48
+ width=width,
49
+ height=height,
50
+ generator=generator,
51
+ ).images[0]
52
+
53
+ return image, seed
54
+
55
+ css = """
56
+ #col-container {
57
+ margin: 0 auto;
58
+ max-width: 640px;
59
+ }
60
+ """
61
+
62
+ with gr.Blocks(css=css) as demo:
63
+ with gr.Column(elem_id="col-container"):
64
+ gr.Markdown(" # Text-to-Image Gradio Template")
65
+
66
+
67
+ with gr.Row():
68
+ model = gr.Dropdown(
69
+ choices=["stabilityai/sdxl-turbo", "CompVis/stable-diffusion-v1-4"],
70
+ value=model_repo_id,
71
+ label="Model",
72
+ info="Choose which diffusion model to use"
73
+ )
74
+
75
+
76
+ with gr.Row():
77
+ prompt = gr.Text(
78
+ label="Prompt",
79
+ show_label=False,
80
+ max_lines=1,
81
+ placeholder="Enter your prompt",
82
+ container=False,
83
+ )
84
+
85
+ run_button = gr.Button("Run", scale=0, variant="primary")
86
+
87
+ result = gr.Image(label="Result", show_label=False)
88
+
89
+ with gr.Accordion("Advanced Settings", open=True):
90
+ negative_prompt = gr.Text(
91
+ label="Negative prompt",
92
+ value="dog, cat",
93
+ max_lines=1,
94
+ placeholder="Enter a negative prompt",
95
+ visible=True,
96
+ )
97
+
98
+ seed = gr.Slider(
99
+ label="Seed",
100
+ minimum=0,
101
+ maximum=MAX_SEED,
102
+ step=1,
103
+ value=42,
104
+ )
105
+
106
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
107
+
108
+ with gr.Row():
109
+ width = gr.Slider(
110
+ label="Width",
111
+ minimum=256,
112
+ maximum=MAX_IMAGE_SIZE,
113
+ step=32,
114
+ value=1024, # Replace with defaults that work for your model
115
+ )
116
+
117
+ height = gr.Slider(
118
+ label="Height",
119
+ minimum=256,
120
+ maximum=MAX_IMAGE_SIZE,
121
+ step=32,
122
+ value=1024, # Replace with defaults that work for your model
123
+ )
124
+
125
+ with gr.Row():
126
+ guidance_scale = gr.Slider(
127
+ label="Guidance scale",
128
+ minimum=0.0,
129
+ maximum=10.0,
130
+ step=0.1,
131
+ value=7.0, # Replace with defaults that work for your model
132
+ )
133
+
134
+ num_inference_steps = gr.Slider(
135
+ label="Number of inference steps",
136
+ minimum=1,
137
+ maximum=50,
138
+ step=1,
139
+ value=20, # Replace with defaults that work for your model
140
+ )
141
+
142
+ gr.on(
143
+ triggers=[run_button.click, prompt.submit],
144
+ fn=infer,
145
+ inputs=[
146
+ model,
147
+ prompt,
148
+ negative_prompt,
149
+ seed,
150
+ randomize_seed,
151
+ width,
152
+ height,
153
+ guidance_scale,
154
+ num_inference_steps,
155
+ ],
156
+ outputs=[result, seed],
157
+ )
158
+
159
+ if __name__ == "__main__":
160
+ demo.launch()