akaUNik commited on
Commit
e9f2b45
·
1 Parent(s): 53c5eae
Files changed (1) hide show
  1. app.py +31 -3
app.py CHANGED
@@ -7,15 +7,30 @@ 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
@@ -23,6 +38,7 @@ MAX_IMAGE_SIZE = 1024
23
 
24
  # @spaces.GPU #[uncomment to use ZeroGPU]
25
  def infer(
 
26
  prompt,
27
  negative_prompt,
28
  seed,
@@ -33,6 +49,9 @@ 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
 
@@ -68,6 +87,14 @@ 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(
73
  label="Prompt",
@@ -138,6 +165,7 @@ with gr.Blocks(css=css) as demo:
138
  triggers=[run_button.click, prompt.submit],
139
  fn=infer,
140
  inputs=[
 
141
  prompt,
142
  negative_prompt,
143
  seed,
 
7
  import torch
8
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ MODEL_LIST = [
11
+ "stabilityai/sdxl-turbo",
12
+ "CompVis/stable-diffusion-v1-4",
13
+ "runwayml/stable-diffusion-v1-5",
14
+ "stabilityai/stable-diffusion-2-1",
15
+ ]
16
 
17
  if torch.cuda.is_available():
18
  torch_dtype = torch.float16
19
  else:
20
  torch_dtype = torch.float32
21
 
22
+ # To avoid re-initializing pipelines repeatedly, we can cache them:
23
+ model_cache = {}
24
+
25
+ def load_pipeline(model_id: str):
26
+ """Loads or retrieves a cached DiffusionPipeline."""
27
+ if model_id in model_cache:
28
+ return model_cache[model_id]
29
+ else:
30
+ pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch_dtype)
31
+ pipe.to(device)
32
+ model_cache[model_id] = pipe
33
+ return pipe
34
 
35
  MAX_SEED = np.iinfo(np.int32).max
36
  MAX_IMAGE_SIZE = 1024
 
38
 
39
  # @spaces.GPU #[uncomment to use ZeroGPU]
40
  def infer(
41
+ model_id,
42
  prompt,
43
  negative_prompt,
44
  seed,
 
49
  num_inference_steps,
50
  progress=gr.Progress(track_tqdm=True),
51
  ):
52
+ # Load the pipeline for the chosen model
53
+ pipe = load_pipeline(model_id)
54
+
55
  if randomize_seed:
56
  seed = random.randint(0, MAX_SEED)
57
 
 
87
  with gr.Column(elem_id="col-container"):
88
  gr.Markdown(" # Text-to-Image Gradio Template")
89
 
90
+ with gr.Row():
91
+ # Dropdown to select the model from Hugging Face
92
+ model_id = gr.Dropdown(
93
+ label="Model",
94
+ choices=MODEL_LIST,
95
+ value=MODEL_LIST[0], # Default model
96
+ )
97
+
98
  with gr.Row():
99
  prompt = gr.Text(
100
  label="Prompt",
 
165
  triggers=[run_button.click, prompt.submit],
166
  fn=infer,
167
  inputs=[
168
+ model_id,
169
  prompt,
170
  negative_prompt,
171
  seed,