Aklavya commited on
Commit
b7faaa2
·
verified ·
1 Parent(s): add48e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -31
app.py CHANGED
@@ -6,9 +6,35 @@ import numpy as np
6
  from PIL import Image
7
  import spaces
8
  import torch
9
- from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
10
  from typing import Tuple
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
13
  styles = {
14
  "3840 x 2160": (
@@ -22,24 +48,6 @@ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str
22
  p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
23
  return p.replace("{prompt}", positive), n + negative
24
 
25
- def load_and_prepare_model(model_choice: str):
26
- if model_choice == "RealVisXL_V5.0_Lightning":
27
- model_id = "SG161222/RealVisXL_V5.0_Lightning"
28
- elif model_choice == "Stable Diffusion 2":
29
- model_id = "stabilityai/stable-diffusion-2"
30
- else:
31
- raise ValueError("Invalid model choice")
32
-
33
- pipe = StableDiffusionXLPipeline.from_pretrained(
34
- model_id,
35
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
36
- use_safetensors=True,
37
- add_watermarker=False,
38
- ).to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))
39
-
40
- pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
41
- return pipe
42
-
43
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
44
  if randomize_seed:
45
  seed = random.randint(0, np.iinfo(np.int32).max)
@@ -61,9 +69,11 @@ def generate(
61
  num_inference_steps: int = 25,
62
  randomize_seed: bool = False,
63
  ):
64
- # Load the chosen model
65
- model = load_and_prepare_model(model_choice)
66
 
 
 
 
67
  seed = int(randomize_seed_fn(seed, randomize_seed))
68
  generator = torch.Generator(device=model.device).manual_seed(seed)
69
 
@@ -90,28 +100,53 @@ with gr.Blocks(theme="soft") as demo:
90
 
91
  with gr.Row():
92
  with gr.Column(scale=3):
93
- # Dropdown to select model
94
- model_choice = gr.Dropdown(
95
- choices=["RealVisXL_V5.0_Lightning", "Stable Diffusion 2"],
96
- label="Select Model",
97
- value="RealVisXL_V5.0_Lightning", # Default value
98
  )
99
-
 
100
  prompt = gr.Textbox(
101
  label="Input Prompt",
102
  placeholder="Describe the image you want to create",
103
  lines=2,
104
  )
105
- run_button = gr.Button("Generate Image")
106
 
107
- gr.Markdown("Developed using the RealVisXL_V5.0_Lightning model.", elem_id="model_info")
 
 
 
 
 
108
  with gr.Column(scale=7):
109
- result = gr.Image(label="Generated Image", type="filepath") # Corrected type to 'filepath'
 
110
 
 
111
  run_button.click(
112
  fn=generate,
113
- inputs=[model_choice, prompt],
114
  outputs=[result],
115
  )
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  demo.launch()
 
6
  from PIL import Image
7
  import spaces
8
  import torch
9
+ from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
10
  from typing import Tuple
11
 
12
+ # Updated function to handle different models
13
+ def load_and_prepare_model(model_choice: str):
14
+ if model_choice == "RealVisXL_V5.0_Lightning":
15
+ model_id = "SG161222/RealVisXL_V5.0_Lightning"
16
+ pipe = StableDiffusionPipeline.from_pretrained(
17
+ model_id,
18
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
19
+ use_safetensors=True,
20
+ add_watermarker=False,
21
+ ).to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))
22
+ else: # Default is CompVis/stable-diffusion-v-1-4-original
23
+ model_id = "CompVis/stable-diffusion-v-1-4-original"
24
+ pipe = StableDiffusionPipeline.from_pretrained(
25
+ model_id,
26
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
27
+ use_safetensors=True,
28
+ add_watermarker=False,
29
+ ).to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))
30
+
31
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
32
+ return pipe
33
+
34
+ # Initial model setup
35
+ model_choice = "CompVis/stable-diffusion-v-1-4-original"
36
+ model = load_and_prepare_model(model_choice)
37
+
38
  def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
39
  styles = {
40
  "3840 x 2160": (
 
48
  p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
49
  return p.replace("{prompt}", positive), n + negative
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
52
  if randomize_seed:
53
  seed = random.randint(0, np.iinfo(np.int32).max)
 
69
  num_inference_steps: int = 25,
70
  randomize_seed: bool = False,
71
  ):
72
+ global model
 
73
 
74
+ # Load the selected model dynamically
75
+ model = load_and_prepare_model(model_choice)
76
+
77
  seed = int(randomize_seed_fn(seed, randomize_seed))
78
  generator = torch.Generator(device=model.device).manual_seed(seed)
79
 
 
100
 
101
  with gr.Row():
102
  with gr.Column(scale=3):
103
+ # Dropdown for selecting model
104
+ model_dropdown = gr.Dropdown(
105
+ label="Choose Model",
106
+ choices=["RealVisXL_V5.0_Lightning", "CompVis/stable-diffusion-v-1-4-original"],
107
+ value="CompVis/stable-diffusion-v-1-4-original", # Default model
108
  )
109
+
110
+ # Input Prompt Textbox
111
  prompt = gr.Textbox(
112
  label="Input Prompt",
113
  placeholder="Describe the image you want to create",
114
  lines=2,
115
  )
 
116
 
117
+ # Generate Image Button
118
+ run_button = gr.Button("Generate Image")
119
+
120
+ # Information about model
121
+ gr.Markdown("Developed using the RealVisXL_V5.0_Lightning or CompVis/stable-diffusion-v-1-4-original model.", elem_id="model_info")
122
+
123
  with gr.Column(scale=7):
124
+ # Display Generated Images (a single image in this case)
125
+ result = gr.Image(label="Generated Image", type="file")
126
 
127
+ # Set the callback for the button
128
  run_button.click(
129
  fn=generate,
130
+ inputs=[model_dropdown, prompt],
131
  outputs=[result],
132
  )
133
 
134
+ # Footer with custom style and text
135
+ gr.Markdown("""
136
+ <style>
137
+ .footer {
138
+ position: relative;
139
+ left: 0;
140
+ bottom: 0;
141
+ width: 100%;
142
+ background-color: white;
143
+ color: black;
144
+ text-align: center;
145
+ }
146
+ </style>
147
+ <div class="footer">
148
+ <p>Developed with ❤ by Aklavya(Bucky)</p>
149
+ </div>
150
+ """)
151
+
152
  demo.launch()