X commited on
Commit
2d746e8
·
verified ·
1 Parent(s): 75dd650

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -20
app.py CHANGED
@@ -1,4 +1,5 @@
1
-
 
2
 
3
  # Шаг 2. Импорт
4
  import torch
@@ -7,29 +8,48 @@ from PIL import Image, ImageFilter, ImageEnhance
7
  import numpy as np
8
  import gradio as gr
9
 
10
- # Шаг 3. Загрузка модели
 
 
 
 
11
  print("Загрузка модели...")
12
  model_id = "runwayml/stable-diffusion-v1-5"
13
- pipe = StableDiffusionPipeline.from_pretrained(
14
- model_id,
15
- torch_dtype=torch.float16,
16
- safety_checker=None,
17
- requires_safety_checker=False
18
- )
19
- pipe = pipe.to("cpu")
20
- pipe.enable_attention_slicing()
21
- pipe.enable_vae_slicing()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  print("Модель загружена!")
23
 
24
- # Шаг 4. Функция генерации
25
  def generate_image(prompt, negative_prompt):
26
- # УСИЛЕНИЕ ПРОМПТА (если он короткий)
27
- if len(prompt.split()) < 10:
28
- prompt = prompt + ", detailed, sharp focus, masterpiece"
29
-
30
- generator = torch.Generator(device="cuda").manual_seed(2021)
31
 
32
- with torch.autocast("cuda"):
33
  image = pipe(
34
  prompt=prompt,
35
  negative_prompt=negative_prompt if negative_prompt else "",
@@ -56,7 +76,7 @@ def generate_image(prompt, negative_prompt):
56
 
57
  return img_final
58
 
59
- # Шаг 5. Интерфейс
60
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
61
  gr.Markdown("#")
62
 
@@ -83,5 +103,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
83
  outputs=output
84
  )
85
 
86
- # Шаг 6. Запуск
87
  demo.launch(share=True, debug=False)
 
1
+ # Шаг 1. Установка
2
+ !pip install diffusers transformers accelerate safetensors gradio --quiet
3
 
4
  # Шаг 2. Импорт
5
  import torch
 
8
  import numpy as np
9
  import gradio as gr
10
 
11
+ # Шаг 3. Проверка GPU
12
+ device = "cuda" if torch.cuda.is_available() else "cpu"
13
+ print(f"Используется: {device}")
14
+
15
+ # Шаг 4. Загрузка модели (без float16 для CPU)
16
  print("Загрузка модели...")
17
  model_id = "runwayml/stable-diffusion-v1-5"
18
+
19
+ if device == "cuda":
20
+ pipe = StableDiffusionPipeline.from_pretrained(
21
+ model_id,
22
+ torch_dtype=torch.float16,
23
+ safety_checker=None,
24
+ requires_safety_checker=False
25
+ )
26
+ else:
27
+ pipe = StableDiffusionPipeline.from_pretrained(
28
+ model_id,
29
+ safety_checker=None,
30
+ requires_safety_checker=False
31
+ )
32
+
33
+ pipe = pipe.to(device)
34
+
35
+ # Экономия памяти
36
+ try:
37
+ pipe.enable_attention_slicing()
38
+ except:
39
+ pass
40
+
41
+ try:
42
+ pipe.vae.enable_slicing()
43
+ except:
44
+ pass
45
+
46
  print("Модель загружена!")
47
 
48
+ # Шаг 5. Функция генерации
49
  def generate_image(prompt, negative_prompt):
50
+ generator = torch.Generator(device=device).manual_seed(2021)
 
 
 
 
51
 
52
+ with torch.no_grad():
53
  image = pipe(
54
  prompt=prompt,
55
  negative_prompt=negative_prompt if negative_prompt else "",
 
76
 
77
  return img_final
78
 
79
+ # Шаг 6. Интерфейс
80
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
81
  gr.Markdown("#")
82
 
 
103
  outputs=output
104
  )
105
 
106
+ # Шаг 7. Запуск
107
  demo.launch(share=True, debug=False)