Spaces:
Runtime error
Runtime error
Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455from diffusers import StableDiffusionPipelinefrom PIL import Imageimport imageioimport gradio as gr
# Load Stable Diffusion modelmodel_id = "runwayml/stable-diffusion-v1-5" # Or your custom fine-tuned modelpipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype="float16", safety_checker=None)pipe.to("cuda") # Use GPU if available
def generate_frames(prompt, num_frames=5, style="realistic"): """ Generate coherent frames for animation. """ frames = [] for i in range(num_frames): frame_prompt = f"{prompt}, frame {i+1}, {style}" image = pipe(frame_prompt).images[0] frame_path = f"frame_{i+1}.png" image.save(frame_path) frames.append(frame_path) return frames
def create_animation(frames, output_path="output.gif", duration=500): """ Combine frames into an animated GIF. """ images = [Image.open(frame) for frame in frames] images[0].save(output_path, save_all=True, append_images=images[1:], duration=duration, loop=0) return output_path
def generate_gif(prompt, num_frames, style): """ Generate an animation GIF from user input. """ frames = generate_frames(prompt, num_frames, style) gif_path = create_animation(frames) return gif_path
# Gradio Interfaceinterface = gr.Interface( fn=generate_gif, inputs=[ gr.Textbox(label="Story or Prompt", placeholder="Enter your short story or prompt here..."), gr.Slider(2, 10, value=5, label="Number of Frames"), gr.Textbox(label="Style", placeholder="e.g., realistic, cartoon, surreal") ], outputs=gr.File(label="Download Your Animation"), title="Text-to-Animation Generator", description="Generate short animations (GIFs) from a story or prompt using Stable Diffusion.")
# Run the appif __name__ == "__main__": interface.launch()app.pyAdded+55