Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import imageio
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def generate_images(prompt, num_images=5, output_folder="generated_frames"):
|
| 7 |
+
# Ensure output folder exists
|
| 8 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 9 |
+
|
| 10 |
+
# Load model (CPU only)
|
| 11 |
+
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
|
| 12 |
+
pipe.to("cpu") # Force to run on CPU
|
| 13 |
+
|
| 14 |
+
images = []
|
| 15 |
+
for i in range(num_images):
|
| 16 |
+
image = pipe(prompt).images[0] # Generate image
|
| 17 |
+
image_path = os.path.join(output_folder, f"frame_{i}.png")
|
| 18 |
+
image.save(image_path)
|
| 19 |
+
images.append(imageio.imread(image_path))
|
| 20 |
+
|
| 21 |
+
# Create GIF animation
|
| 22 |
+
gif_path = os.path.join(output_folder, "animation.gif")
|
| 23 |
+
imageio.mimsave(gif_path, images, duration=0.5)
|
| 24 |
+
|
| 25 |
+
print(f"Animation saved at {gif_path}")
|
| 26 |
+
return gif_path
|
| 27 |
+
|
| 28 |
+
if __name__ == "__main__":
|
| 29 |
+
prompt = input("Enter your prompt: ")
|
| 30 |
+
generate_images(prompt)
|