Paulina commited on
Commit
a9c4647
·
1 Parent(s): dd0f699
Files changed (1) hide show
  1. app.py +28 -4
app.py CHANGED
@@ -4,6 +4,9 @@ import torch
4
  from diffusers import StableDiffusionPipeline
5
  from PIL import Image
6
  import numpy as np
 
 
 
7
 
8
  MODEL_ID = "runwayml/stable-diffusion-v1-5"
9
 
@@ -47,15 +50,33 @@ def generate_image(prompt, seed, num_inference_steps):
47
  # Set the random seed for reproducibility
48
  generator = torch.Generator(device=device).manual_seed(int(seed))
49
 
50
- # Generate the image
 
 
 
 
 
 
 
 
 
 
51
  with torch.no_grad():
52
  result = pipeline(
53
  prompt=prompt,
54
  num_inference_steps=int(num_inference_steps),
55
  generator=generator,
 
 
56
  )
57
 
58
- return result.images[0]
 
 
 
 
 
 
59
 
60
 
61
  def create_interface():
@@ -86,9 +107,12 @@ def create_interface():
86
  info="Number of denoising steps (more steps = higher quality but slower)",
87
  ),
88
  ],
89
- outputs=gr.Image(label="Generated Image", type="pil"),
 
 
 
90
  title="Stable Diffusion Image Generator",
91
- description="Generate images from text using Stable Diffusion. Enter a prompt, set the seed for reproducibility, and adjust the number of diffusion steps.",
92
  examples=[
93
  ["A beautiful sunset over mountains", 42, 50],
94
  ["A cat wearing a space suit, digital art", 123, 50],
 
4
  from diffusers import StableDiffusionPipeline
5
  from PIL import Image
6
  import numpy as np
7
+ import imageio
8
+ import tempfile
9
+ import os
10
 
11
  MODEL_ID = "runwayml/stable-diffusion-v1-5"
12
 
 
50
  # Set the random seed for reproducibility
51
  generator = torch.Generator(device=device).manual_seed(int(seed))
52
 
53
+ # Store intermediate images
54
+ frames = []
55
+
56
+ def callback(step: int, timestep: int, latents):
57
+ # Decode latents to image
58
+ with torch.no_grad():
59
+ image = pipeline.decode_latents(latents)
60
+ image = pipeline.numpy_to_pil(image)[0]
61
+ frames.append(image)
62
+
63
+ # Generate the image with callback for each step
64
  with torch.no_grad():
65
  result = pipeline(
66
  prompt=prompt,
67
  num_inference_steps=int(num_inference_steps),
68
  generator=generator,
69
+ callback=callback,
70
+ callback_steps=1,
71
  )
72
 
73
+ # Save frames as video
74
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
75
+ video_path = tmpfile.name
76
+ imageio.mimsave(video_path, frames, fps=5)
77
+
78
+ # Return final image and video path
79
+ return result.images[0], video_path
80
 
81
 
82
  def create_interface():
 
107
  info="Number of denoising steps (more steps = higher quality but slower)",
108
  ),
109
  ],
110
+ outputs=[
111
+ gr.Image(label="Generated Image", type="pil"),
112
+ gr.Video(label="Diffusion Steps Video"),
113
+ ],
114
  title="Stable Diffusion Image Generator",
115
+ description="Generate images from text using Stable Diffusion. Enter a prompt, set the seed for reproducibility, and adjust the number of diffusion steps. Watch the diffusion process as a video.",
116
  examples=[
117
  ["A beautiful sunset over mountains", 42, 50],
118
  ["A cat wearing a space suit, digital art", 123, 50],