Yaron Koresh commited on
Commit
6db6a8d
·
verified ·
1 Parent(s): f651e3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -33
app.py CHANGED
@@ -16,7 +16,7 @@ import warnings
16
  #import spaces
17
  import torch
18
  import gradio as gr
19
- from numpy import array
20
  from lxml.html import fromstring
21
  #from transformers import pipeline
22
  #from diffusers.pipelines.flux import FluxPipeline
@@ -30,6 +30,9 @@ from diffusers import DiffusionPipeline, AnimateDiffPipeline, MotionAdapter, Eul
30
  from numba import cuda, njit as cpu, void, int64 as int, float64 as float, boolean as bool
31
  from numba.cuda import jit as gpu, grid
32
  from numba.types import unicode_type as string
 
 
 
33
  # logging
34
 
35
  warnings.filterwarnings("ignore")
@@ -46,11 +49,13 @@ formatter = logging.Formatter('\n >>> [%(levelname)s] %(asctime)s %(name)s: %(me
46
  handler2.setFormatter(formatter)
47
  root.addHandler(handler2)
48
 
49
- # data
50
 
51
- out=array([""])
52
- inp=[]
53
  last_motion=array([""])
 
 
 
54
  dtype = torch.float16
55
  device = "cuda"
56
  #repo = "ByteDance/AnimateDiff-Lightning"
@@ -61,6 +66,8 @@ vae = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse").to(device, dtyp
61
  #unet = UNet2DConditionModel.from_config("emilianJR/epiCRealism",subfolder="unet").to(device, dtype).load_state_dict(load_file(hf_hub_download("emilianJR/epiCRealism", "unet/diffusion_pytorch_model.safetensors"), device=device), strict=False)
62
  adapter = MotionAdapter.from_pretrained("guoyww/animatediff-motion-adapter-v1-5-3", torch_dtype=dtype, device=device)
63
 
 
 
64
  fast=True
65
  fps=10
66
  time=1
@@ -69,6 +76,8 @@ height=768
69
  step=40
70
  accu=10
71
 
 
 
72
  css="""
73
  input, input::placeholder {
74
  text-align: center !important;
@@ -162,53 +171,46 @@ def generate_random_string(length):
162
  return ''.join(random.choice(characters) for _ in range(length))
163
 
164
  @gpu(void())
165
- def calc():
166
- global inp
167
- global out
168
  global last_motion
169
 
170
  x = grid(1)
171
 
172
- if last_motion[0] != inp[3]:
173
  pipe.unload_lora_weights()
174
  if inp[3] != "":
175
- pipe.load_lora_weights(inp[3], adapter_name="motion")
176
  pipe.fuse_lora()
177
- pipe.set_adapters([3], [0.7])
178
- last_motion[0] = inp[3]
179
 
180
  pipe.to(device,dtype)
181
 
182
- if inp[2]=="":
183
- out[x] = pipe(
184
- prompt=inp[1],
185
  height=height,
186
  width=width,
187
- ip_adapter_image=inp[0].convert("RGB").resize((width,height)),
188
  num_inference_steps=step,
189
  guidance_scale=accu,
190
  num_frames=(fps*time)
191
  )
192
 
193
- out[x] = pipe(
194
- prompt=inp[1],
195
- negative_prompt=inp[2],
196
  height=height,
197
  width=width,
198
- ip_adapter_image=inp[0].convert("RGB").resize((width,height)),
199
  num_inference_steps=step,
200
  guidance_scale=accu,
201
  num_frames=(fps*time)
202
  )
203
 
204
- def handle(*args):
205
- global inp
206
- global out
207
-
208
- inp = args
209
-
210
- out = array([],dtype=string)
211
-
212
  inp[1] = translate(inp[1],"english")
213
  inp[2] = translate(inp[2],"english")
214
 
@@ -225,20 +227,24 @@ def handle(*args):
225
 
226
  ln = len(result)
227
 
228
- calc[ln,32]()
 
 
 
 
229
 
230
  for i in range(ln):
231
- name = generate_random_string[1,32](12)+".png"
232
- export_to_gif(out[i].frames[0],name,fps=fps)
233
- out[i] = name
234
 
235
- return out
236
 
237
  def ui():
238
  with gr.Blocks(theme=gr.themes.Soft(),css=css,js=js) as demo:
239
  with gr.Column(elem_id="col-container"):
240
  gr.Markdown(f"""
241
- # MULTI-LANGUAGE IMAGE GENERATOR
242
  """)
243
  with gr.Row():
244
  global img
 
16
  #import spaces
17
  import torch
18
  import gradio as gr
19
+ from numpy import asarray as array
20
  from lxml.html import fromstring
21
  #from transformers import pipeline
22
  #from diffusers.pipelines.flux import FluxPipeline
 
30
  from numba import cuda, njit as cpu, void, int64 as int, float64 as float, boolean as bool
31
  from numba.cuda import jit as gpu, grid
32
  from numba.types import unicode_type as string
33
+ from PIL.Image import fromarray as array2image
34
+ import numpy as np
35
+
36
  # logging
37
 
38
  warnings.filterwarnings("ignore")
 
49
  handler2.setFormatter(formatter)
50
  root.addHandler(handler2)
51
 
52
+ # output data
53
 
54
+ out_pipe=array([""])
 
55
  last_motion=array([""])
56
+
57
+ # constant data
58
+
59
  dtype = torch.float16
60
  device = "cuda"
61
  #repo = "ByteDance/AnimateDiff-Lightning"
 
66
  #unet = UNet2DConditionModel.from_config("emilianJR/epiCRealism",subfolder="unet").to(device, dtype).load_state_dict(load_file(hf_hub_download("emilianJR/epiCRealism", "unet/diffusion_pytorch_model.safetensors"), device=device), strict=False)
67
  adapter = MotionAdapter.from_pretrained("guoyww/animatediff-motion-adapter-v1-5-3", torch_dtype=dtype, device=device)
68
 
69
+ # precision data
70
+
71
  fast=True
72
  fps=10
73
  time=1
 
76
  step=40
77
  accu=10
78
 
79
+ # ui data
80
+
81
  css="""
82
  input, input::placeholder {
83
  text-align: center !important;
 
171
  return ''.join(random.choice(characters) for _ in range(length))
172
 
173
  @gpu(void())
174
+ def calc(img,p1,p2,motion):
175
+ global out_pipe
 
176
  global last_motion
177
 
178
  x = grid(1)
179
 
180
+ if last_motion[0] != motion:
181
  pipe.unload_lora_weights()
182
  if inp[3] != "":
183
+ pipe.load_lora_weights(motion, adapter_name="motion")
184
  pipe.fuse_lora()
185
+ pipe.set_adapters("motion", [0.7])
186
+ last_motion[0] = motion
187
 
188
  pipe.to(device,dtype)
189
 
190
+ if p2=="":
191
+ out_pipe[x] = pipe(
192
+ prompt=p1,
193
  height=height,
194
  width=width,
195
+ ip_adapter_image=array2image(img).convert("RGB").resize((width,height)),
196
  num_inference_steps=step,
197
  guidance_scale=accu,
198
  num_frames=(fps*time)
199
  )
200
 
201
+ out_pipe[x] = pipe(
202
+ prompt=p1,
203
+ negative_prompt=p2,
204
  height=height,
205
  width=width,
206
+ ip_adapter_image=array2image(img).convert("RGB").resize((width,height)),
207
  num_inference_steps=step,
208
  guidance_scale=accu,
209
  num_frames=(fps*time)
210
  )
211
 
212
+ def handle(*inp):
213
+
 
 
 
 
 
 
214
  inp[1] = translate(inp[1],"english")
215
  inp[2] = translate(inp[2],"english")
216
 
 
227
 
228
  ln = len(result)
229
 
230
+ inp[0] = array(inp[0])
231
+ inp[1] = array(inp[1])
232
+ inp[2] = array(inp[2])
233
+ inp[3] = array(inp[3])
234
+ calc[ln,32](*inp)
235
 
236
  for i in range(ln):
237
+ name = generate_random_string(12)+".png"
238
+ export_to_gif(out_pipe[i].frames[0],name,fps=fps)
239
+ out_pipe[i] = name
240
 
241
+ return out_pipe
242
 
243
  def ui():
244
  with gr.Blocks(theme=gr.themes.Soft(),css=css,js=js) as demo:
245
  with gr.Column(elem_id="col-container"):
246
  gr.Markdown(f"""
247
+ # MULTI-LANGUAGE GIF CREATOR
248
  """)
249
  with gr.Row():
250
  global img