Yaron Koresh commited on
Commit
169516c
·
verified ·
1 Parent(s): f031971

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -43
app.py CHANGED
@@ -21,13 +21,13 @@ import torch
21
  import gradio as gr
22
  from numpy import asarray as array
23
  from lxml.html import fromstring
24
- from diffusers.utils import export_to_gif, load_image
25
  from huggingface_hub import hf_hub_download
26
  from safetensors.torch import load_file, save_file
27
- from diffusers import StableDiffusionPipeline
28
- from diffusers import AnimateDiffPipeline, DDIMScheduler
29
- from diffusers.models import AutoencoderKL, MotionAdapter
30
- from diffusers.schedulers import DPMSolverMultistepScheduler
31
  from PIL import Image, ImageDraw, ImageFont
32
 
33
  # logging
@@ -53,7 +53,7 @@ else:
53
  #base = "emilianJR/epiCRealism"
54
  base = "SG161222/Realistic_Vision_V5.1_noVAE"
55
  vae_id = "stabilityai/sd-vae-ft-mse"
56
- motion_adapter = "guoyww/animatediff-motion-adapter-v1-5-3"
57
 
58
  # variable data
59
 
@@ -114,18 +114,11 @@ function custom(){
114
  # torch pipes
115
 
116
  image_pipe = StableDiffusionPipeline.from_pretrained(base, torch_dtype=dtype, safety_checker=None).to(device)
117
-
118
- adapter = MotionAdapter.from_pretrained(motion_adapter, torch_dtype=dtype, safety_checker=None).to(device)
119
  vae = AutoencoderKL.from_pretrained(vae_id, torch_dtype=torch.float16).to(device)
 
120
 
121
- scheduler = DPMSolverMultistepScheduler.from_pretrained(
122
- base,
123
- subfolder="scheduler",
124
- beta_schedule="linear",
125
- algorithm_type="dpmsolver++",
126
- use_karras_sigmas=True
127
- )
128
- scheduler2 = DDIMScheduler.from_pretrained(
129
  base,
130
  subfolder="scheduler",
131
  clip_sample=False,
@@ -134,19 +127,18 @@ scheduler2 = DDIMScheduler.from_pretrained(
134
  steps_offset=1,
135
  )
136
 
137
- pipe = AnimateDiffPipeline.from_pretrained(
138
- base,
139
- motion_adapter=adapter,
140
- torch_dtype=torch.float16
141
  ).to(device)
 
142
 
143
- pipe.scheduler = scheduler2
144
- pipe.vae = vae
145
-
146
- pipe.enable_vae_slicing()
147
- pipe.load_ip_adapter("h94/IP-Adapter", subfolder="models", weight_name="ip-adapter_sd15.bin")
148
- pipe.enable_model_cpu_offload()
149
- pipe.enable_free_init(method="butterworth", use_fast_sampling=fast)
150
 
151
  # functionality
152
 
@@ -216,6 +208,15 @@ def pipe_generate(img,p1,p2,motion,time,title):
216
  generator=torch.Generator(device).manual_seed(int(str(random.random()).split(".")[1]))
217
  ).images[0]
218
 
 
 
 
 
 
 
 
 
 
219
  if time == 0.0:
220
  return img
221
 
@@ -227,13 +228,14 @@ def pipe_generate(img,p1,p2,motion,time,title):
227
  pipe.fuse_lora()
228
  pipe.set_adapters("motion", [0.7])
229
  last_motion = motion
230
-
231
- return pipe(
232
  prompt=p1,
233
  negative_prompt=p2,
234
- ip_adapter_image=load_image(img),
235
  num_inference_steps=step,
236
  guidance_scale=accu,
 
237
  num_frames=(fps*time),
238
  need_safetycheck=False,
239
  generator=torch.Generator(device).manual_seed(int(str(random.random()).split(".")[1]))
@@ -266,27 +268,18 @@ def handle_generate(*_inp):
266
 
267
  pipe_out = pipe_generate(*inp)
268
 
269
- if inp[5] != "":
270
- draw = ImageDraw.Draw(pipe_out)
271
- textheight=84
272
- font = ImageFont.truetype(r"OpenSans-Bold.ttf", textheight)
273
- textwidth = draw.textlength(inp[5],font)
274
- x = (width - textwidth) // 2
275
- y = (height - textheight) // 2
276
- draw.text((x, y), inp[5],font=font)
277
-
278
- name = generate_random_string(12) + ( ".png" if time == 0 else ".gif" )
279
  if inp[4] == 0.0:
280
  pipe_out.save(name)
281
  else:
282
- export_to_gif(pipe_out,name,fps=fps)
283
  return name
284
 
285
  def ui():
286
  global result
287
  with gr.Blocks(theme=gr.themes.Soft(),css=css,js=js) as demo:
288
  gr.Markdown(f"""
289
- # MULTI-LANGUAGE GIF/PNG CREATOR
290
  """)
291
  with gr.Row(elem_id="col-container"):
292
  with gr.Column():
@@ -318,7 +311,7 @@ def ui():
318
  maximum=600.0,
319
  value=0.0,
320
  step=5.0,
321
- label="GIF/PNG Duration (0s = PNG)"
322
  )
323
  with gr.Row():
324
  motion = gr.Dropdown(
 
21
  import gradio as gr
22
  from numpy import asarray as array
23
  from lxml.html import fromstring
24
+ from diffusers.utils import export_to_video, load_image
25
  from huggingface_hub import hf_hub_download
26
  from safetensors.torch import load_file, save_file
27
+ from diffusers import StableDiffusionPipeline, CogVideoXImageToVideoPipeline
28
+ #from diffusers import AnimateDiffPipeline, DDIMScheduler
29
+ #from diffusers.models import AutoencoderKL, MotionAdapter
30
+ #from diffusers.schedulers import DPMSolverMultistepScheduler
31
  from PIL import Image, ImageDraw, ImageFont
32
 
33
  # logging
 
53
  #base = "emilianJR/epiCRealism"
54
  base = "SG161222/Realistic_Vision_V5.1_noVAE"
55
  vae_id = "stabilityai/sd-vae-ft-mse"
56
+ #motion_adapter = "guoyww/animatediff-motion-adapter-v1-5-3"
57
 
58
  # variable data
59
 
 
114
  # torch pipes
115
 
116
  image_pipe = StableDiffusionPipeline.from_pretrained(base, torch_dtype=dtype, safety_checker=None).to(device)
117
+ #adapter = MotionAdapter.from_pretrained(motion_adapter, torch_dtype=dtype, safety_checker=None).to(device)
 
118
  vae = AutoencoderKL.from_pretrained(vae_id, torch_dtype=torch.float16).to(device)
119
+ image_pipe.vae = vae
120
 
121
+ scheduler = DDIMScheduler.from_pretrained(
 
 
 
 
 
 
 
122
  base,
123
  subfolder="scheduler",
124
  clip_sample=False,
 
127
  steps_offset=1,
128
  )
129
 
130
+ video_pipe = CogVideoXImageToVideoPipeline.from_pretrained(
131
+ "THUDM/CogVideoX-5b-I2V",
132
+ torch_dtype=torch.bfloat16,
133
+ safety_checker=None
134
  ).to(device)
135
+ video_pipe.scheduler = scheduler2
136
 
137
+ video_pipe.vae.enable_tiling()
138
+ video_pipe.vae.enable_slicing()
139
+ #pipe.load_ip_adapter("h94/IP-Adapter", subfolder="models", weight_name="ip-adapter_sd15.bin")
140
+ video_pipe.enable_model_cpu_offload()
141
+ video_pipe.enable_free_init(method="butterworth", use_fast_sampling=fast)
 
 
142
 
143
  # functionality
144
 
 
208
  generator=torch.Generator(device).manual_seed(int(str(random.random()).split(".")[1]))
209
  ).images[0]
210
 
211
+ if title != "":
212
+ draw = ImageDraw.Draw(pipe_out)
213
+ textheight=84
214
+ font = ImageFont.truetype(r"OpenSans-Bold.ttf", textheight)
215
+ textwidth = draw.textlength(title,font)
216
+ x = (width - textwidth) // 2
217
+ y = (height - textheight) // 2
218
+ draw.text((x, y), title,font=font)
219
+
220
  if time == 0.0:
221
  return img
222
 
 
228
  pipe.fuse_lora()
229
  pipe.set_adapters("motion", [0.7])
230
  last_motion = motion
231
+
232
+ return video_pipe(
233
  prompt=p1,
234
  negative_prompt=p2,
235
+ image=img,
236
  num_inference_steps=step,
237
  guidance_scale=accu,
238
+ num_videos_per_prompt=1,
239
  num_frames=(fps*time),
240
  need_safetycheck=False,
241
  generator=torch.Generator(device).manual_seed(int(str(random.random()).split(".")[1]))
 
268
 
269
  pipe_out = pipe_generate(*inp)
270
 
271
+ name = generate_random_string(12) + ( ".png" if time == 0 else ".mp4" )
 
 
 
 
 
 
 
 
 
272
  if inp[4] == 0.0:
273
  pipe_out.save(name)
274
  else:
275
+ export_to_video(pipe_out,name,fps=fps)
276
  return name
277
 
278
  def ui():
279
  global result
280
  with gr.Blocks(theme=gr.themes.Soft(),css=css,js=js) as demo:
281
  gr.Markdown(f"""
282
+ # MULTI-LANGUAGE MP4/PNG CREATOR
283
  """)
284
  with gr.Row(elem_id="col-container"):
285
  with gr.Column():
 
311
  maximum=600.0,
312
  value=0.0,
313
  step=5.0,
314
+ label="MP4/PNG Duration (0s = PNG)"
315
  )
316
  with gr.Row():
317
  motion = gr.Dropdown(