Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,47 +2,46 @@ import torch
|
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
import imageio
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 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 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 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(
|
| 35 |
image.save(image_path)
|
| 36 |
-
images.append(imageio.v3.imread(image_path)) # Use v3 API
|
| 37 |
|
| 38 |
# Create GIF animation
|
| 39 |
-
gif_path = os.path.join(
|
| 40 |
imageio.mimsave(gif_path, images, duration=0.5)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
return gif_path
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
import imageio
|
| 4 |
import os
|
| 5 |
+
import gradio as gr
|
| 6 |
|
| 7 |
+
# Ensure the output directory exists
|
| 8 |
+
OUTPUT_FOLDER = "generated_frames"
|
| 9 |
+
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Load Stable Diffusion Model
|
| 12 |
+
print("Loading Stable Diffusion model...")
|
| 13 |
+
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
|
| 14 |
+
pipe.to("cpu") # Force to run on CPU
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
def generate_images(prompt, num_images=5):
|
| 17 |
+
"""Generates images from a prompt and creates an animated GIF."""
|
| 18 |
+
|
| 19 |
images = []
|
| 20 |
for i in range(num_images):
|
| 21 |
print(f"Generating image {i+1}/{num_images}...")
|
| 22 |
image = pipe(prompt).images[0] # Generate image
|
| 23 |
+
image_path = os.path.join(OUTPUT_FOLDER, f"frame_{i}.png")
|
| 24 |
image.save(image_path)
|
| 25 |
+
images.append(imageio.v3.imread(image_path)) # Use v3 API
|
| 26 |
|
| 27 |
# Create GIF animation
|
| 28 |
+
gif_path = os.path.join(OUTPUT_FOLDER, "animation.gif")
|
| 29 |
imageio.mimsave(gif_path, images, duration=0.5)
|
| 30 |
|
| 31 |
+
return gif_path # Return GIF path for download
|
|
|
|
| 32 |
|
| 33 |
+
# Gradio UI
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("# Stable Diffusion Animation Generator 🎨✨")
|
| 36 |
+
|
| 37 |
+
with gr.Row():
|
| 38 |
+
prompt_input = gr.Textbox(label="Enter your prompt")
|
| 39 |
+
generate_button = gr.Button("Generate Animation 🎥")
|
| 40 |
+
|
| 41 |
+
gif_output = gr.File(label="Download your GIF")
|
| 42 |
|
| 43 |
+
generate_button.click(fn=generate_images, inputs=prompt_input, outputs=gif_output)
|
| 44 |
+
|
| 45 |
+
# Launch Gradio App
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|