tomkart commited on
Commit
a57a3e4
·
verified ·
1 Parent(s): 91f99ae

Update app.py

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