amos1088 commited on
Commit
58c4ba9
·
1 Parent(s): 3dede3a

test gradio

Browse files
Files changed (1) hide show
  1. app.py +33 -44
app.py CHANGED
@@ -1,70 +1,59 @@
1
- import torch
2
- from diffusers.models import MotionAdapter
3
- from diffusers import AnimateDiffSDXLPipeline, DDIMScheduler
4
- from diffusers.utils import export_to_gif
5
-
6
  import gradio as gr
7
  from huggingface_hub import login
8
  import os
9
  import spaces,tempfile
10
  import torch
11
- from diffusers import StableDiffusionXLPipeline
12
- from PIL import Image
13
- import torch
14
- from diffusers import AutoPipelineForText2Image, DDIMScheduler
15
- from diffusers import AutoPipelineForText2Image
16
- from diffusers.utils import load_image
17
- import torch
18
- from diffusers.models import MotionAdapter
19
- from diffusers import AnimateDiffSDXLPipeline, DDIMScheduler
20
- from diffusers.utils import export_to_gif
21
-
22
  token = os.getenv("HF_TOKEN")
23
  login(token=token)
24
 
25
 
26
- adapter = MotionAdapter.from_pretrained("guoyww/animatediff-motion-adapter-sdxl-beta", torch_dtype=torch.float16)
 
 
 
 
 
27
 
28
- model_id = "stabilityai/sdxl-turbo"
29
- scheduler = DDIMScheduler.from_pretrained(
 
 
30
  model_id,
31
  subfolder="scheduler",
32
- clip_sample=False,
33
- timestep_spacing="linspace",
34
  beta_schedule="linear",
35
- steps_offset=1,
 
36
  )
37
- pipe = AnimateDiffSDXLPipeline.from_pretrained(
38
  model_id,
39
- motion_adapter=adapter,
 
 
40
  scheduler=scheduler,
41
  torch_dtype=torch.float16,
42
- variant="fp16",
43
- ).to("cuda")
44
- pipe.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
45
-
46
- # enable memory savings
47
- pipe.enable_vae_slicing()
48
- pipe.enable_vae_tiling()
49
- pipeline = pipe
50
 
51
 
52
  @spaces.GPU
53
  def generate_image(prompt, reference_image, controlnet_conditioning_scale,num_frames):
54
- style_images = [load_image(f) for f in reference_image]
55
-
56
- pipeline.set_ip_adapter_scale(controlnet_conditioning_scale)
57
 
58
- output = pipeline(
59
  prompt=prompt,
60
- ip_adapter_image=[style_images],
61
- negative_prompt="",
62
- guidance_scale=5,
63
- num_inference_steps=30,
64
  num_frames=num_frames,
65
- )
66
- frames = output.frames[0]
67
- export_to_gif(frames, "animation.gif")
 
 
 
68
 
69
  return "animation.gif"
70
 
@@ -74,7 +63,7 @@ interface = gr.Interface(
74
  inputs=[
75
  gr.Textbox(label="Prompt"),
76
  # gr.Image( type= "filepath",label="Reference Image (Style)"),
77
- gr.File(type="filepath",file_count="multiple",label="Reference Image (Style)"),
78
  gr.Slider(label="Control Net Conditioning Scale", minimum=0, maximum=1.0, step=0.1, value=1.0),
79
  gr.Slider(label="Number of frames", minimum=0, maximum=1.0, step=0.1, value=1.0),
80
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import login
3
  import os
4
  import spaces,tempfile
5
  import torch
6
+ from diffusers import AnimateDiffSparseControlNetPipeline
7
+ from diffusers.models import AutoencoderKL, MotionAdapter, SparseControlNetModel
8
+ from diffusers.schedulers import DPMSolverMultistepScheduler
9
+ from diffusers.utils import export_to_gif, load_image
 
 
 
 
 
 
 
10
  token = os.getenv("HF_TOKEN")
11
  login(token=token)
12
 
13
 
14
+ model_id = "SG161222/Realistic_Vision_V5.1_noVAE"
15
+ motion_adapter_id = "guoyww/animatediff-motion-adapter-v1-5-3"
16
+ controlnet_id = "guoyww/animatediff-sparsectrl-rgb"
17
+ lora_adapter_id = "guoyww/animatediff-motion-lora-v1-5-3"
18
+ vae_id = "stabilityai/sd-vae-ft-mse"
19
+ device = "cuda"
20
 
21
+ motion_adapter = MotionAdapter.from_pretrained(motion_adapter_id, torch_dtype=torch.float16).to(device)
22
+ controlnet = SparseControlNetModel.from_pretrained(controlnet_id, torch_dtype=torch.float16).to(device)
23
+ vae = AutoencoderKL.from_pretrained(vae_id, torch_dtype=torch.float16).to(device)
24
+ scheduler = DPMSolverMultistepScheduler.from_pretrained(
25
  model_id,
26
  subfolder="scheduler",
 
 
27
  beta_schedule="linear",
28
+ algorithm_type="dpmsolver++",
29
+ use_karras_sigmas=True,
30
  )
31
+ pipe = AnimateDiffSparseControlNetPipeline.from_pretrained(
32
  model_id,
33
+ motion_adapter=motion_adapter,
34
+ controlnet=controlnet,
35
+ vae=vae,
36
  scheduler=scheduler,
37
  torch_dtype=torch.float16,
38
+ ).to(device)
39
+ pipe.load_lora_weights(lora_adapter_id, adapter_name="motion_lora")
 
 
 
 
 
 
40
 
41
 
42
  @spaces.GPU
43
  def generate_image(prompt, reference_image, controlnet_conditioning_scale,num_frames):
44
+ style_images = [load_image(f.name) for f in reference_image]
 
 
45
 
46
+ video = pipe(
47
  prompt=prompt,
48
+ negative_prompt="low quality, worst quality",
49
+ num_inference_steps=25,
 
 
50
  num_frames=num_frames,
51
+ conditioning_frames=style_images,
52
+ controlnet_frame_indices=[0],
53
+ controlnet_conditioning_scale=controlnet_conditioning_scale,
54
+ generator=torch.Generator().manual_seed(42),
55
+ ).frames[0]
56
+ export_to_gif(video, "output.gif")
57
 
58
  return "animation.gif"
59
 
 
63
  inputs=[
64
  gr.Textbox(label="Prompt"),
65
  # gr.Image( type= "filepath",label="Reference Image (Style)"),
66
+ gr.File(type="file",file_count="multiple",label="Reference Image (Style)"),
67
  gr.Slider(label="Control Net Conditioning Scale", minimum=0, maximum=1.0, step=0.1, value=1.0),
68
  gr.Slider(label="Number of frames", minimum=0, maximum=1.0, step=0.1, value=1.0),
69