pva22 commited on
Commit
439649f
·
1 Parent(s): 66f569d
Files changed (1) hide show
  1. app.py +31 -6
app.py CHANGED
@@ -1,11 +1,18 @@
1
  import gradio as gr
2
- from diffusers import StableDiffusionPipeline
3
  import torch
4
 
5
  # Загрузка модели
6
- def load_model(model_id):
7
  pipe = StableDiffusionPipeline.from_pretrained(model_id) #torch_dtype=torch.float16
8
  pipe.to("cpu") #gpu
 
 
 
 
 
 
 
9
  return pipe
10
 
11
  # Генерация изображения
@@ -16,12 +23,15 @@ def generate_image(
16
  seed: int,
17
  guidance_scale: float,
18
  num_inference_steps: int,
 
 
 
19
  ):
20
  # Установка начального состояния (seed)
21
  generator = torch.manual_seed(seed)
22
 
23
  # Загрузка модели
24
- pipe = load_model(model_id)
25
 
26
  # Генерация
27
  image = pipe(
@@ -30,13 +40,15 @@ def generate_image(
30
  guidance_scale=guidance_scale,
31
  num_inference_steps=num_inference_steps,
32
  generator=generator,
 
 
33
  ).images[0]
34
 
35
  return image
36
 
37
  # Интерфейс Gradio
38
  with gr.Blocks() as demo:
39
- gr.Markdown("## Stable Diffusion Generator")
40
 
41
  model_id = gr.Textbox(label="Model ID", value="CompVis/stable-diffusion-v1-4")
42
  prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here")
@@ -44,16 +56,29 @@ with gr.Blocks() as demo:
44
  seed = gr.Number(label="Seed", value=42, precision=0)
45
  guidance_scale = gr.Slider(label="Guidance Scale", minimum=1, maximum=20, value=7)
46
  num_inference_steps = gr.Slider(label="Number of Inference Steps", minimum=1, maximum=50, value=20)
 
 
 
47
 
48
  output = gr.Image(label="Generated Image")
49
  submit = gr.Button("Generate")
50
-
 
 
 
51
  submit.click(
52
  fn=generate_image,
53
- inputs=[model_id, prompt, negative_prompt, seed, guidance_scale, num_inference_steps],
54
  outputs=output,
55
  )
56
 
 
 
 
 
 
 
 
57
  # Запуск
58
  if __name__ == "__main__":
59
  demo.launch()
 
1
  import gradio as gr
2
+ from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler, EulerDiscreteScheduler
3
  import torch
4
 
5
  # Загрузка модели
6
+ def load_model(model_id, scheduler_name):
7
  pipe = StableDiffusionPipeline.from_pretrained(model_id) #torch_dtype=torch.float16
8
  pipe.to("cpu") #gpu
9
+
10
+ # Установка шедулера
11
+ if scheduler_name == "DPM":
12
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
13
+ elif scheduler_name == "Euler":
14
+ pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
15
+
16
  return pipe
17
 
18
  # Генерация изображения
 
23
  seed: int,
24
  guidance_scale: float,
25
  num_inference_steps: int,
26
+ scheduler_name: str,
27
+ height: int,
28
+ width: int
29
  ):
30
  # Установка начального состояния (seed)
31
  generator = torch.manual_seed(seed)
32
 
33
  # Загрузка модели
34
+ pipe = load_model(model_id, scheduler_name)
35
 
36
  # Генерация
37
  image = pipe(
 
40
  guidance_scale=guidance_scale,
41
  num_inference_steps=num_inference_steps,
42
  generator=generator,
43
+ height=height,
44
+ width=width
45
  ).images[0]
46
 
47
  return image
48
 
49
  # Интерфейс Gradio
50
  with gr.Blocks() as demo:
51
+ gr.Markdown("## Домашнее задание 3. Часть 1. Знакомство с Gradio и HuggingFace.")
52
 
53
  model_id = gr.Textbox(label="Model ID", value="CompVis/stable-diffusion-v1-4")
54
  prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here")
 
56
  seed = gr.Number(label="Seed", value=42, precision=0)
57
  guidance_scale = gr.Slider(label="Guidance Scale", minimum=1, maximum=20, value=7)
58
  num_inference_steps = gr.Slider(label="Number of Inference Steps", minimum=1, maximum=50, value=20)
59
+ scheduler_name = gr.Dropdown(label='Sheduler', choices=["DPM", "Euler"], value="DPM")
60
+ heigth = gr.Slider(label="Heigth", minimum=256, maximum=1024, step=64, value=512)
61
+ width = gr.Slider(label="Width", minimum=256, maximum=1024, step=64, value=512)
62
 
63
  output = gr.Image(label="Generated Image")
64
  submit = gr.Button("Generate")
65
+
66
+ def reset_inputs():
67
+ return "", "", 42, 7, 20, "DPM", 512, 512, None
68
+
69
  submit.click(
70
  fn=generate_image,
71
+ inputs=[model_id, prompt, negative_prompt, seed, guidance_scale, num_inference_steps, scheduler_name, heigth, width],
72
  outputs=output,
73
  )
74
 
75
+ next_generation = gr.Button("Next generation")
76
+ next_generation.click(
77
+ fn=reset_inputs,
78
+ input=[],
79
+ output=[prompt, negative_prompt, seed, guidance_scale, num_inference_steps, scheduler_name, heigth, width],
80
+ )
81
+
82
  # Запуск
83
  if __name__ == "__main__":
84
  demo.launch()