Prgckwb commited on
Commit
207d269
1 Parent(s): e11bea1

:tada: add external model

Browse files
Files changed (3) hide show
  1. app.py +58 -29
  2. example.csv +4 -0
  3. requirements.txt +2 -0
app.py CHANGED
@@ -3,6 +3,7 @@ import spaces
3
  import torch
4
  from PIL import Image
5
  from diffusers import DiffusionPipeline
 
6
 
7
  DIFFUSERS_MODEL_IDS = [
8
  # SD Models
@@ -32,17 +33,24 @@ if device == 'cuda':
32
  ).to(device)
33
 
34
 
35
-
36
  @spaces.GPU()
37
  @torch.inference_mode()
38
  def inference(
39
  model_id: str,
40
  prompt: str,
41
  negative_prompt: str = "",
 
 
 
 
 
42
  progress=gr.Progress(track_tqdm=True),
43
  ) -> Image.Image:
44
  progress(0, "Starting inference...")
45
 
 
 
 
46
  global current_model_id, pipe
47
 
48
  if model_id != current_model_id:
@@ -58,13 +66,21 @@ def inference(
58
  except Exception as e:
59
  raise gr.Error(str(e))
60
 
61
- if device != 'cuda':
62
- raise gr.Error("This model requires a GPU to run. Please switch to a GPU runtime.")
63
-
64
- image = pipe(
65
  prompt,
66
  negative_prompt=negative_prompt,
67
- ).images[0]
 
 
 
 
 
 
 
 
 
 
68
 
69
  return image
70
 
@@ -75,41 +91,54 @@ if __name__ == "__main__":
75
 
76
  with gr.Row():
77
  with gr.Column():
78
- inputs = [
79
- gr.Dropdown(
80
- label="Model ID",
81
- choices=MODEL_CHOICES,
82
- value="stabilityai/stable-diffusion-3-medium-diffusers",
83
- ),
84
- gr.Text(label="Prompt", value=""),
85
- gr.Text(label="Negative Prompt", value=""),
86
- ]
87
 
88
  with gr.Accordion("Additional Settings (W.I.P)", open=False):
 
 
89
  with gr.Row():
90
- width_component = gr.Number(label="Width", value=512, step=64, minimum=64, maximum=1024)
91
- height_component = gr.Number(label="Height", value=512, step=64, minimum=64, maximum=1024)
 
92
 
93
- additional_inputs = [
94
- width_component,
95
- height_component,
96
- gr.Number(label="Guidance Scale", value=7.5, step=0.5, minimum=0, maximum=10),
97
- gr.Slider(label="Num Inference Steps", value=None, minimum=1, maximum=1000, step=1)
98
- ]
99
 
100
  with gr.Column():
101
- outputs = [
102
- gr.Image(label="Image", type="pil"),
103
- ]
 
 
 
 
 
 
 
 
 
104
 
105
  btn = gr.Button("Generate")
106
- btn.click(fn=inference, inputs=inputs, outputs=outputs)
 
 
 
 
107
 
108
  gr.Examples(
109
  examples=[
110
  ['stabilityai/stable-diffusion-3-medium-diffusers', 'A cat holding a sign that says Hello world', ""],
111
- ['stabilityai/stable-diffusion-3-medium-diffusers', 'Beautiful pixel art of a Wizard with hovering text "Achivement unlocked: Diffusion models can spell now"', ''],
112
- ['stabilityai/stable-diffusion-3-medium-diffusers', 'A corgi wearing sunglasses says "U-Net is OVER!!"', ''],
 
 
 
113
  ],
114
  inputs=inputs,
115
  )
 
3
  import torch
4
  from PIL import Image
5
  from diffusers import DiffusionPipeline
6
+ from diffusers.utils import make_image_grid
7
 
8
  DIFFUSERS_MODEL_IDS = [
9
  # SD Models
 
33
  ).to(device)
34
 
35
 
 
36
  @spaces.GPU()
37
  @torch.inference_mode()
38
  def inference(
39
  model_id: str,
40
  prompt: str,
41
  negative_prompt: str = "",
42
+ width: int = 512,
43
+ height: int = 512,
44
+ guidance_scale: float = 7.5,
45
+ num_inference_steps: int = 50,
46
+ num_images: int = 4,
47
  progress=gr.Progress(track_tqdm=True),
48
  ) -> Image.Image:
49
  progress(0, "Starting inference...")
50
 
51
+ if device != 'cuda':
52
+ raise gr.Error("This model requires a GPU to run. Please switch to a GPU runtime.")
53
+
54
  global current_model_id, pipe
55
 
56
  if model_id != current_model_id:
 
66
  except Exception as e:
67
  raise gr.Error(str(e))
68
 
69
+ # Generation
70
+ images = pipe(
 
 
71
  prompt,
72
  negative_prompt=negative_prompt,
73
+ width=width,
74
+ height=height,
75
+ guidance_scale=guidance_scale,
76
+ num_inference_steps=num_inference_steps,
77
+ num_images_per_prompt=num_images,
78
+ ).images
79
+
80
+ if num_images % 2 == 1:
81
+ image = make_image_grid(images, rows=num_images, cols=1)
82
+ else:
83
+ image = make_image_grid(images, rows=2, cols=num_images // 2)
84
 
85
  return image
86
 
 
91
 
92
  with gr.Row():
93
  with gr.Column():
94
+ model_id = gr.Dropdown(
95
+ label="Model ID",
96
+ choices=MODEL_CHOICES,
97
+ value="stabilityai/stable-diffusion-3-medium-diffusers",
98
+ )
99
+ prompt = gr.Text(label="Prompt", value="")
 
 
 
100
 
101
  with gr.Accordion("Additional Settings (W.I.P)", open=False):
102
+ negative_prompt = gr.Text(label="Negative Prompt", value="")
103
+
104
  with gr.Row():
105
+ width = gr.Number(label="Width", value=512, step=64, minimum=64, maximum=2048)
106
+ height = gr.Number(label="Height", value=512, step=64, minimum=64, maximum=2048)
107
+ num_images = gr.Number(label="Num Images", value=4, minimum=1, maximum=10, step=1)
108
 
109
+ guidance_scale = gr.Slider(label="Guidance Scale", value=7.5, step=0.5, minimum=0, maximum=10)
110
+ num_inference_step = gr.Slider(label="Num Inference Steps", value=50, minimum=1, maximum=100,
111
+ step=1)
 
 
 
112
 
113
  with gr.Column():
114
+ output_image = gr.Image(label="Image", type="pil")
115
+
116
+ inputs = [
117
+ model_id,
118
+ prompt,
119
+ negative_prompt,
120
+ width,
121
+ height,
122
+ guidance_scale,
123
+ num_inference_step,
124
+ num_images,
125
+ ]
126
 
127
  btn = gr.Button("Generate")
128
+ btn.click(
129
+ fn=inference,
130
+ inputs=inputs,
131
+ outputs=output_image
132
+ )
133
 
134
  gr.Examples(
135
  examples=[
136
  ['stabilityai/stable-diffusion-3-medium-diffusers', 'A cat holding a sign that says Hello world', ""],
137
+ ['stabilityai/stable-diffusion-3-medium-diffusers',
138
+ 'Beautiful pixel art of a Wizard with hovering text "Achivement unlocked: Diffusion models can spell now"',
139
+ ''],
140
+ ['stabilityai/stable-diffusion-3-medium-diffusers', 'A corgi wearing sunglasses says "U-Net is OVER!!"',
141
+ ''],
142
  ],
143
  inputs=inputs,
144
  )
example.csv ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ model_uri,description,additional_info
2
+ stabilityai/stable-diffusion-3-medium-diffusers,"A cat holding a sign that says Hello world",
3
+ stabilityai/stable-diffusion-3-medium-diffusers,"Beautiful pixel art of a Wizard with hovering text "Achivement unlocked: Diffusion models can spell now"",
4
+ stabilityai/stable-diffusion-3-medium-diffusers,"A corgi wearing sunglasses says ""U-Net is OVER!!"",
requirements.txt CHANGED
@@ -7,3 +7,5 @@ transformers
7
  ftfy
8
  accelerate
9
  sentencepiece
 
 
 
7
  ftfy
8
  accelerate
9
  sentencepiece
10
+ spaces
11
+ Pillow