amos1088 commited on
Commit
683afc3
·
1 Parent(s): bf98d5f

test gradio

Browse files
Files changed (2) hide show
  1. app.py +54 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import AnimateDiffPipeline, DDIMScheduler, MotionAdapter
4
+ from diffusers.utils import export_to_gif
5
+ from PIL import Image
6
+ import io
7
+
8
+ # Load the motion adapter
9
+ adapter = MotionAdapter.from_pretrained("guoyww/animatediff-motion-adapter-v1-5-2", torch_dtype=torch.float16)
10
+ model_id = "SG161222/Realistic_Vision_V5.1_noVAE"
11
+ pipe = AnimateDiffPipeline.from_pretrained(model_id, motion_adapter=adapter, torch_dtype=torch.float16)
12
+ scheduler = DDIMScheduler.from_pretrained(
13
+ model_id,
14
+ subfolder="scheduler",
15
+ clip_sample=False,
16
+ timestep_spacing="linspace",
17
+ beta_schedule="linear",
18
+ steps_offset=1,
19
+ )
20
+ pipe.scheduler = scheduler
21
+ pipe.enable_vae_slicing()
22
+ pipe.enable_model_cpu_offload()
23
+
24
+ # Define the animation function
25
+ def generate_animation(prompt, negative_prompt, num_frames, guidance_scale, num_inference_steps):
26
+ output = pipe(
27
+ prompt=prompt,
28
+ negative_prompt=negative_prompt,
29
+ num_frames=num_frames,
30
+ guidance_scale=guidance_scale,
31
+ num_inference_steps=num_inference_steps,
32
+ generator=torch.Generator("cpu").manual_seed(42),
33
+ )
34
+ frames = output.frames[0]
35
+ gif_path = "animation.gif"
36
+ export_to_gif(frames, gif_path)
37
+ return gif_path
38
+
39
+ # Gradio Interface
40
+ iface = gr.Interface(
41
+ fn=generate_animation,
42
+ inputs=[
43
+ gr.Textbox(value="masterpiece, best quality, highly detailed...", label="Prompt"),
44
+ gr.Textbox(value="bad quality, worse quality", label="Negative Prompt"),
45
+ gr.Slider(1, 24, value=16, label="Number of Frames"),
46
+ gr.Slider(1.0, 10.0, value=7.5, step=0.1, label="Guidance Scale"),
47
+ gr.Slider(1, 50, value=25, label="Inference Steps"),
48
+ ],
49
+ outputs=gr.Image(label="Generated Animation"),
50
+ title="Animated Stable Diffusion",
51
+ description="Generate animations based on your prompt using Stable Diffusion.",
52
+ )
53
+
54
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch
2
+ diffusers
3
+ transformers
4
+ accelerate
5
+ gradio