Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers import StableDiffusionPipeline
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import imageio
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Load Stable Diffusion model
|
| 7 |
+
model_id = "runwayml/stable-diffusion-v1-5" # Or your custom fine-tuned model
|
| 8 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype="float16", safety_checker=None)
|
| 9 |
+
pipe.to("cuda") # Use GPU if available
|
| 10 |
+
|
| 11 |
+
def generate_frames(prompt, num_frames=5, style="realistic"):
|
| 12 |
+
"""
|
| 13 |
+
Generate coherent frames for animation.
|
| 14 |
+
"""
|
| 15 |
+
frames = []
|
| 16 |
+
for i in range(num_frames):
|
| 17 |
+
frame_prompt = f"{prompt}, frame {i+1}, {style}"
|
| 18 |
+
image = pipe(frame_prompt).images[0]
|
| 19 |
+
frame_path = f"frame_{i+1}.png"
|
| 20 |
+
image.save(frame_path)
|
| 21 |
+
frames.append(frame_path)
|
| 22 |
+
return frames
|
| 23 |
+
|
| 24 |
+
def create_animation(frames, output_path="output.gif", duration=500):
|
| 25 |
+
"""
|
| 26 |
+
Combine frames into an animated GIF.
|
| 27 |
+
"""
|
| 28 |
+
images = [Image.open(frame) for frame in frames]
|
| 29 |
+
images[0].save(output_path, save_all=True, append_images=images[1:], duration=duration, loop=0)
|
| 30 |
+
return output_path
|
| 31 |
+
|
| 32 |
+
def generate_gif(prompt, num_frames, style):
|
| 33 |
+
"""
|
| 34 |
+
Generate an animation GIF from user input.
|
| 35 |
+
"""
|
| 36 |
+
frames = generate_frames(prompt, num_frames, style)
|
| 37 |
+
gif_path = create_animation(frames)
|
| 38 |
+
return gif_path
|
| 39 |
+
|
| 40 |
+
# Gradio Interface
|
| 41 |
+
interface = gr.Interface(
|
| 42 |
+
fn=generate_gif,
|
| 43 |
+
inputs=[
|
| 44 |
+
gr.Textbox(label="Story or Prompt", placeholder="Enter your short story or prompt here..."),
|
| 45 |
+
gr.Slider(2, 10, value=5, label="Number of Frames"),
|
| 46 |
+
gr.Textbox(label="Style", placeholder="e.g., realistic, cartoon, surreal")
|
| 47 |
+
],
|
| 48 |
+
outputs=gr.File(label="Download Your Animation"),
|
| 49 |
+
title="Text-to-Animation Generator",
|
| 50 |
+
description="Generate short animations (GIFs) from a story or prompt using Stable Diffusion."
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Run the app
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
interface.launch()
|