AnastasiaSh commited on
Commit
4c5e156
·
verified ·
1 Parent(s): e3e09ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -89
app.py CHANGED
@@ -1,27 +1,36 @@
1
  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
  model,
27
  prompt,
@@ -33,56 +42,77 @@ def infer(
33
  num_inference_steps,
34
  progress=gr.Progress(track_tqdm=True),
35
  ):
36
-
37
- global model_repo_id
 
38
  if model != model_repo_id:
39
- print(model, model_repo_id)
40
- pipe = DiffusionPipeline.from_pretrained(model, torch_dtype=torch_dtype)
41
- pipe = pipe.to(device)
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
- available_models = [
70
- "CompVis/stable-diffusion-v1-4",
71
- "stabilityai/sdxl-turbo",
72
- "runwayml/stable-diffusion-v1-5",
73
- "prompthero/openjourney"
74
-
75
- ]
76
 
 
77
  with gr.Blocks(css=css) as demo:
78
  with gr.Column(elem_id="col-container"):
79
- gr.Markdown(" # Text-to-Image App")
80
- model = gr.Dropdown(
81
- label="Model Selection",
82
- choices=available_models,
83
- value="CompVis/stable-diffusion-v1-4",
 
84
  interactive=True
85
  )
 
 
86
  prompt = gr.Text(
87
  label="Prompt",
88
  show_label=False,
@@ -90,85 +120,81 @@ with gr.Blocks(css=css) as demo:
90
  placeholder="Enter your prompt",
91
  container=False,
92
  )
93
-
94
  negative_prompt = gr.Text(
95
  label="Negative prompt",
96
  max_lines=1,
97
  placeholder="Enter a negative prompt",
98
  visible=True,
99
  )
 
 
100
  seed = gr.Slider(
101
- label="Seed",
102
- minimum=0,
103
- maximum=MAX_SEED,
104
- step=1,
105
- value=42,
106
  )
 
 
107
  guidance_scale = gr.Slider(
108
- label="Guidance scale",
109
- minimum=0.0,
110
- maximum=10.0,
111
- step=0.1,
112
- value=7.0, # Replace with defaults that work for your model
113
  )
114
-
115
  num_inference_steps = gr.Slider(
116
- label="Number of inference steps",
117
- minimum=1,
118
- maximum=50,
119
- step=1,
120
- value=20, # Replace with defaults that work for your model
121
  )
122
-
123
- run_button = gr.Button("Run", scale=0, variant="primary")
124
 
 
 
 
 
125
  result = gr.Image(label="Result", show_label=False)
126
-
127
 
 
128
  with gr.Accordion("Advanced Settings", open=False):
129
-
130
-
131
-
132
-
133
-
134
-
135
  with gr.Row():
136
  width = gr.Slider(
137
  label="Width",
138
  minimum=256,
139
  maximum=MAX_IMAGE_SIZE,
140
  step=32,
141
- value=1024, # Replace with defaults that work for your model
142
  )
143
-
144
  height = gr.Slider(
145
  label="Height",
146
  minimum=256,
147
  maximum=MAX_IMAGE_SIZE,
148
  step=32,
149
- value=1024, # Replace with defaults that work for your model
150
  )
151
 
152
-
153
-
154
-
155
-
156
  gr.Examples(examples=examples, inputs=[prompt])
157
- gr.on(
158
- triggers=[run_button.click, prompt.submit],
159
- fn=infer,
160
- inputs=[
161
- model,
162
- prompt,
163
- negative_prompt,
164
- seed,
165
- width,
166
- height,
167
- guidance_scale,
168
- num_inference_steps,
169
- ],
170
- outputs=[result, seed],
171
- )
172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  if __name__ == "__main__":
174
  demo.launch()
 
1
  import gradio as gr
2
  import numpy as np
 
 
 
 
3
  import torch
4
+ from diffusers import DiffusionPipeline
5
+ from peft import PeftModel
6
+ import re
7
 
8
+ # Устройство и тип данных
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
11
 
12
+ # Регулярное выражение для проверки корректности модели
13
+ VALID_REPO_ID_REGEX = re.compile(r"^[a-zA-Z0-9._\-]+/[a-zA-Z0-9._\-]+$")
 
 
14
 
15
+ def is_valid_repo_id(repo_id):
16
+ return bool(VALID_REPO_ID_REGEX.match(repo_id)) and not repo_id.endswith(('-', '.'))
17
 
18
+ # Базовые константы
19
  MAX_SEED = np.iinfo(np.int32).max
20
  MAX_IMAGE_SIZE = 1024
21
 
22
+ # Изначально загружаем модель по умолчанию
23
+ model_repo_id = "CompVis/stable-diffusion-v1-4"
24
+ pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype, safety_checker=None).to(device)
25
+
26
+ # Попробуем подгрузить LoRA-модификации (unet + text_encoder)
27
+ try:
28
+ pipe.unet = PeftModel.from_pretrained(pipe.unet, "./unet")
29
+ pipe.text_encoder = PeftModel.from_pretrained(pipe.text_encoder, "./text_encoder")
30
+ except Exception as e:
31
+ # Если не удалось, можно вывести предупреждение или поднять ошибку
32
+ print(f"Не удалось подгрузить LoRA по умолчанию: {e}")
33
 
 
34
  def infer(
35
  model,
36
  prompt,
 
42
  num_inference_steps,
43
  progress=gr.Progress(track_tqdm=True),
44
  ):
45
+ global model_repo_id, pipe
46
+
47
+ # Если пользователь ввёл другую модель, пробуем её загрузить с нуля
48
  if model != model_repo_id:
49
+ if not is_valid_repo_id(model):
50
+ raise gr.Error(f"Некорректный идентификатор модели: '{model}'. Проверьте название.")
51
+
52
+ try:
53
+ new_pipe = DiffusionPipeline.from_pretrained(model, torch_dtype=torch_dtype).to(device)
54
+
55
+ # Повторно подгружаем LoRA для нового пайплайна
56
+ try:
57
+ new_pipe.unet = PeftModel.from_pretrained(new_pipe.unet, "./unet")
58
+ new_pipe.text_encoder = PeftModel.from_pretrained(new_pipe.text_encoder, "./text_encoder")
59
+ except Exception as e:
60
+ raise gr.Error(f"Не удалось подгрузить LoRA: {e}")
61
+
62
+ # Обновляем глобальные переменные
63
+ pipe = new_pipe
64
+ model_repo_id = model
65
+
66
+ except Exception as e:
67
+ raise gr.Error(f"Не удалось загрузить модель '{model}'.\nОшибка: {e}")
68
+
69
+ # Создаём генератор случайных чисел для детерминированности
70
+ generator = torch.Generator(device=device).manual_seed(seed)
71
+
72
+ # Пытаемся сгенерировать изображение
73
+ try:
74
+ image = pipe(
75
+ prompt=prompt,
76
+ negative_prompt=negative_prompt,
77
+ guidance_scale=guidance_scale,
78
+ num_inference_steps=num_inference_steps,
79
+ width=width,
80
+ height=height,
81
+ generator=generator,
82
+ ).images[0]
83
+ except Exception as e:
84
+ raise gr.Error(f"Ошибка при генерации изображения: {e}")
85
 
86
  return image, seed
87
 
88
+ # Примеры для удобного тестирования
89
  examples = [
90
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
91
  "An astronaut riding a green horse",
92
  "A delicious ceviche cheesecake slice",
93
  ]
94
 
95
+ # Дополнительный CSS для оформления
96
  css = """
97
  #col-container {
98
  margin: 0 auto;
99
  max-width: 640px;
100
  }
101
  """
 
 
 
 
 
 
 
102
 
103
+ # Создаём Gradio-прил��жение
104
  with gr.Blocks(css=css) as demo:
105
  with gr.Column(elem_id="col-container"):
106
+ gr.Markdown("# Text-to-Image App")
107
+
108
+ # Поле для ввода/смены модели
109
+ model = gr.Textbox(
110
+ label="Model",
111
+ value="CompVis/stable-diffusion-v1-4", # Значение по умолчанию
112
  interactive=True
113
  )
114
+
115
+ # Основные поля для Prompt и Negative Prompt
116
  prompt = gr.Text(
117
  label="Prompt",
118
  show_label=False,
 
120
  placeholder="Enter your prompt",
121
  container=False,
122
  )
 
123
  negative_prompt = gr.Text(
124
  label="Negative prompt",
125
  max_lines=1,
126
  placeholder="Enter a negative prompt",
127
  visible=True,
128
  )
129
+
130
+ # Слайдер для выбора seed
131
  seed = gr.Slider(
132
+ label="Seed",
133
+ minimum=0,
134
+ maximum=MAX_SEED,
135
+ step=1,
136
+ value=42,
137
  )
138
+
139
+ # Слайдеры для guidance_scale и num_inference_steps
140
  guidance_scale = gr.Slider(
141
+ label="Guidance scale",
142
+ minimum=0.0,
143
+ maximum=10.0,
144
+ step=0.1,
145
+ value=7.0,
146
  )
 
147
  num_inference_steps = gr.Slider(
148
+ label="Number of inference steps",
149
+ minimum=1,
150
+ maximum=50,
151
+ step=1,
152
+ value=20,
153
  )
 
 
154
 
155
+ # Кнопка запуска
156
+ run_button = gr.Button("Run", variant="primary")
157
+
158
+ # Поле для отображения результата
159
  result = gr.Image(label="Result", show_label=False)
 
160
 
161
+ # Продвинутые настройки (Accordion)
162
  with gr.Accordion("Advanced Settings", open=False):
 
 
 
 
 
 
163
  with gr.Row():
164
  width = gr.Slider(
165
  label="Width",
166
  minimum=256,
167
  maximum=MAX_IMAGE_SIZE,
168
  step=32,
169
+ value=512,
170
  )
 
171
  height = gr.Slider(
172
  label="Height",
173
  minimum=256,
174
  maximum=MAX_IMAGE_SIZE,
175
  step=32,
176
+ value=512,
177
  )
178
 
179
+ # Примеры
 
 
 
180
  gr.Examples(examples=examples, inputs=[prompt])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
 
182
+ # Связка кнопки "Run" с функцией "infer"
183
+ run_button.click(
184
+ infer,
185
+ inputs=[
186
+ model,
187
+ prompt,
188
+ negative_prompt,
189
+ seed,
190
+ width,
191
+ height,
192
+ guidance_scale,
193
+ num_inference_steps,
194
+ ],
195
+ outputs=[result, seed],
196
+ )
197
+
198
+ # Запуск
199
  if __name__ == "__main__":
200
  demo.launch()