Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,28 +3,46 @@ 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 =
|
| 30 |
generate_images(prompt)
|
|
|
|
|
|
| 3 |
import imageio
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
def get_prompt():
|
| 7 |
+
"""Ensures user provides a valid prompt, avoiding EOFError."""
|
| 8 |
+
while True:
|
| 9 |
+
try:
|
| 10 |
+
prompt = input("Enter your prompt: ").strip()
|
| 11 |
+
if prompt:
|
| 12 |
+
return prompt
|
| 13 |
+
else:
|
| 14 |
+
print("Prompt cannot be empty. Please enter a valid prompt.")
|
| 15 |
+
except EOFError:
|
| 16 |
+
print("\nError: Input required. Restarting prompt entry...")
|
| 17 |
+
continue
|
| 18 |
+
|
| 19 |
def generate_images(prompt, num_images=5, output_folder="generated_frames"):
|
| 20 |
+
"""Generates images using Stable Diffusion and creates an animated GIF."""
|
| 21 |
+
|
| 22 |
# Ensure output folder exists
|
| 23 |
os.makedirs(output_folder, exist_ok=True)
|
| 24 |
|
| 25 |
# Load model (CPU only)
|
| 26 |
+
print("Loading Stable Diffusion model...")
|
| 27 |
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
|
| 28 |
pipe.to("cpu") # Force to run on CPU
|
| 29 |
+
|
| 30 |
images = []
|
| 31 |
for i in range(num_images):
|
| 32 |
+
print(f"Generating image {i+1}/{num_images}...")
|
| 33 |
image = pipe(prompt).images[0] # Generate image
|
| 34 |
image_path = os.path.join(output_folder, f"frame_{i}.png")
|
| 35 |
image.save(image_path)
|
| 36 |
+
images.append(imageio.v3.imread(image_path)) # Use v3 API to avoid deprecation warnings
|
| 37 |
+
|
| 38 |
# Create GIF animation
|
| 39 |
gif_path = os.path.join(output_folder, "animation.gif")
|
| 40 |
imageio.mimsave(gif_path, images, duration=0.5)
|
| 41 |
|
| 42 |
+
print(f"\n✅ Animation saved at: {gif_path}")
|
| 43 |
return gif_path
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
+
prompt = get_prompt() # Get user input safely
|
| 47 |
generate_images(prompt)
|
| 48 |
+
|