ShreeDeepthi commited on
Commit
3c485b0
·
verified ·
1 Parent(s): 2ca17e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -30
app.py CHANGED
@@ -2,47 +2,46 @@ import torch
2
  from diffusers import StableDiffusionPipeline
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
 
 
 
 
 
 
 
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()