trashchenkov commited on
Commit
5408021
·
verified ·
1 Parent(s): 84eb9ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -22
app.py CHANGED
@@ -2,27 +2,28 @@ 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
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
11
-
12
- if torch.cuda.is_available():
13
- torch_dtype = torch.float16
14
- else:
15
- torch_dtype = torch.float32
16
-
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
- pipe = pipe.to(device)
19
 
20
  MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 1024
22
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- # @spaces.GPU #[uncomment to use ZeroGPU]
25
  def infer(
 
26
  prompt,
27
  negative_prompt,
28
  seed,
@@ -33,6 +34,11 @@ def infer(
33
  num_inference_steps,
34
  progress=gr.Progress(track_tqdm=True),
35
  ):
 
 
 
 
 
36
  if randomize_seed:
37
  seed = random.randint(0, MAX_SEED)
38
 
@@ -50,7 +56,6 @@ def infer(
50
 
51
  return image, seed
52
 
53
-
54
  examples = [
55
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
56
  "An astronaut riding a green horse",
@@ -66,7 +71,14 @@ css = """
66
 
67
  with gr.Blocks(css=css) as demo:
68
  with gr.Column(elem_id="col-container"):
69
- gr.Markdown(" # Text-to-Image Gradio Template")
 
 
 
 
 
 
 
70
 
71
  with gr.Row():
72
  prompt = gr.Text(
@@ -86,7 +98,6 @@ with gr.Blocks(css=css) as demo:
86
  label="Negative prompt",
87
  max_lines=1,
88
  placeholder="Enter a negative prompt",
89
- visible=False,
90
  )
91
 
92
  seed = gr.Slider(
@@ -94,10 +105,10 @@ with gr.Blocks(css=css) as demo:
94
  minimum=0,
95
  maximum=MAX_SEED,
96
  step=1,
97
- value=0,
98
  )
99
 
100
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
101
 
102
  with gr.Row():
103
  width = gr.Slider(
@@ -105,7 +116,7 @@ with gr.Blocks(css=css) as demo:
105
  minimum=256,
106
  maximum=MAX_IMAGE_SIZE,
107
  step=32,
108
- value=1024, # Replace with defaults that work for your model
109
  )
110
 
111
  height = gr.Slider(
@@ -113,31 +124,33 @@ with gr.Blocks(css=css) as demo:
113
  minimum=256,
114
  maximum=MAX_IMAGE_SIZE,
115
  step=32,
116
- value=1024, # Replace with defaults that work for your model
117
  )
118
 
119
  with gr.Row():
120
  guidance_scale = gr.Slider(
121
  label="Guidance scale",
122
  minimum=0.0,
123
- maximum=10.0,
124
  step=0.1,
125
- value=0.0, # Replace with defaults that work for your model
126
  )
127
 
128
  num_inference_steps = gr.Slider(
129
  label="Number of inference steps",
130
  minimum=1,
131
- maximum=50,
132
  step=1,
133
- value=2, # Replace with defaults that work for your model
134
  )
135
 
136
  gr.Examples(examples=examples, inputs=[prompt])
 
137
  gr.on(
138
  triggers=[run_button.click, prompt.submit],
139
  fn=infer,
140
  inputs=[
 
141
  prompt,
142
  negative_prompt,
143
  seed,
 
2
  import numpy as np
3
  import random
4
 
 
5
  from diffusers import DiffusionPipeline
6
  import torch
7
 
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
 
 
 
9
 
10
  MAX_SEED = np.iinfo(np.int32).max
11
  MAX_IMAGE_SIZE = 1024
12
 
13
+ def load_pipeline(model_id):
14
+ if torch.cuda.is_available():
15
+ torch_dtype = torch.float16
16
+ else:
17
+ torch_dtype = torch.float32
18
+
19
+ pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch_dtype)
20
+ return pipe.to(device)
21
+
22
+ # Initialize with default model
23
+ pipe = load_pipeline("CompVis/stable-diffusion-v1-4")
24
 
 
25
  def infer(
26
+ model_id,
27
  prompt,
28
  negative_prompt,
29
  seed,
 
34
  num_inference_steps,
35
  progress=gr.Progress(track_tqdm=True),
36
  ):
37
+ global pipe
38
+
39
+ if model_id:
40
+ pipe = load_pipeline(model_id)
41
+
42
  if randomize_seed:
43
  seed = random.randint(0, MAX_SEED)
44
 
 
56
 
57
  return image, seed
58
 
 
59
  examples = [
60
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
61
  "An astronaut riding a green horse",
 
71
 
72
  with gr.Blocks(css=css) as demo:
73
  with gr.Column(elem_id="col-container"):
74
+ gr.Markdown(" # Text-to-Image Gradio Template with Model Selection")
75
+
76
+ model_id = gr.Text(
77
+ label="Model ID",
78
+ show_label=True,
79
+ placeholder="Enter HuggingFace model ID (e.g., CompVis/stable-diffusion-v1-4)",
80
+ value="CompVis/stable-diffusion-v1-4",
81
+ )
82
 
83
  with gr.Row():
84
  prompt = gr.Text(
 
98
  label="Negative prompt",
99
  max_lines=1,
100
  placeholder="Enter a negative prompt",
 
101
  )
102
 
103
  seed = gr.Slider(
 
105
  minimum=0,
106
  maximum=MAX_SEED,
107
  step=1,
108
+ value=42,
109
  )
110
 
111
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
112
 
113
  with gr.Row():
114
  width = gr.Slider(
 
116
  minimum=256,
117
  maximum=MAX_IMAGE_SIZE,
118
  step=32,
119
+ value=512,
120
  )
121
 
122
  height = gr.Slider(
 
124
  minimum=256,
125
  maximum=MAX_IMAGE_SIZE,
126
  step=32,
127
+ value=512,
128
  )
129
 
130
  with gr.Row():
131
  guidance_scale = gr.Slider(
132
  label="Guidance scale",
133
  minimum=0.0,
134
+ maximum=20.0,
135
  step=0.1,
136
+ value=7.0,
137
  )
138
 
139
  num_inference_steps = gr.Slider(
140
  label="Number of inference steps",
141
  minimum=1,
142
+ maximum=100,
143
  step=1,
144
+ value=20,
145
  )
146
 
147
  gr.Examples(examples=examples, inputs=[prompt])
148
+
149
  gr.on(
150
  triggers=[run_button.click, prompt.submit],
151
  fn=infer,
152
  inputs=[
153
+ model_id,
154
  prompt,
155
  negative_prompt,
156
  seed,