Spaces:
Sleeping
Sleeping
Yaron Koresh
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -24,31 +24,28 @@ 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
|
28 |
|
29 |
# logging
|
30 |
|
31 |
warnings.filterwarnings("ignore")
|
32 |
root = logging.getLogger()
|
33 |
root.setLevel(logging.DEBUG)
|
34 |
-
handler = logging.StreamHandler(sys.
|
35 |
handler.setLevel(logging.DEBUG)
|
36 |
formatter = logging.Formatter('\n >>> [%(levelname)s] %(asctime)s %(name)s: %(message)s\n')
|
37 |
handler.setFormatter(formatter)
|
38 |
root.addHandler(handler)
|
39 |
-
handler2 = logging.StreamHandler(sys.stderr)
|
40 |
-
handler2.setLevel(logging.DEBUG)
|
41 |
-
formatter = logging.Formatter('\n >>> [%(levelname)s] %(asctime)s %(name)s: %(message)s\n')
|
42 |
-
handler2.setFormatter(formatter)
|
43 |
-
root.addHandler(handler2)
|
44 |
|
45 |
# constant data
|
46 |
|
47 |
-
dtype = torch.float16
|
48 |
if torch.cuda.is_available():
|
49 |
device = "cuda"
|
|
|
50 |
else:
|
51 |
device = "cpu"
|
|
|
|
|
52 |
base = "SG161222/Realistic_Vision_V6.0_B1_noVAE"
|
53 |
adapter = MotionAdapter.from_pretrained("guoyww/animatediff-motion-adapter-v1-5-3", torch_dtype=dtype, device=device)
|
54 |
|
@@ -61,11 +58,10 @@ result = []
|
|
61 |
|
62 |
seq=512
|
63 |
fast=True
|
64 |
-
fps=
|
65 |
-
time=3
|
66 |
width=896
|
67 |
height=896
|
68 |
-
step=
|
69 |
accu=8.5
|
70 |
|
71 |
# ui data
|
@@ -112,7 +108,8 @@ function custom(){
|
|
112 |
|
113 |
# torch pipes
|
114 |
|
115 |
-
|
|
|
116 |
pipe.scheduler = DDIMScheduler(
|
117 |
clip_sample=False,
|
118 |
beta_start=0.00085,
|
@@ -138,8 +135,8 @@ def xpath_finder(str,pattern):
|
|
138 |
def translate(text,lang):
|
139 |
if text == None or lang == None:
|
140 |
return ""
|
141 |
-
text = re.sub(f'[{punctuation}]', '', re.sub('[
|
142 |
-
lang = re.sub(f'[{punctuation}]', '', re.sub('[
|
143 |
if text == "" or lang == "":
|
144 |
return ""
|
145 |
if len(text) > 38:
|
@@ -151,7 +148,7 @@ def translate(text,lang):
|
|
151 |
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15',
|
152 |
'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15'
|
153 |
]
|
154 |
-
padded_chars = re.sub("
|
155 |
query_text = f'Please translate {padded_chars}, into {lang}'
|
156 |
url = f'https://www.google.com/search?q={query_text}'
|
157 |
content = str(requests.get(
|
@@ -167,40 +164,43 @@ def translate(text,lang):
|
|
167 |
trgt_text = xpath_finder(content,'//*[@id="tw-target-text"]/*')
|
168 |
if trgt_lang == lang:
|
169 |
translated = trgt_text
|
170 |
-
ret = re.sub(f'[{punctuation}]', '', re.sub('[
|
171 |
-
print(ret)
|
172 |
return ret
|
173 |
|
174 |
def generate_random_string(length):
|
175 |
characters = str(ascii_letters + digits)
|
176 |
return ''.join(random.choice(characters) for _ in range(length))
|
177 |
|
178 |
-
def pipe_generate(img,p1,p2,motion):
|
179 |
global last_motion
|
180 |
global pipe
|
181 |
|
182 |
-
if
|
183 |
-
|
184 |
-
pipe.unload_lora_weights()
|
185 |
-
if motion != "":
|
186 |
-
pipe.load_lora_weights(motion, adapter_name="motion")
|
187 |
-
pipe.fuse_lora()
|
188 |
-
pipe.set_adapters("motion", [0.7])
|
189 |
-
last_motion = motion
|
190 |
-
|
191 |
-
pipe.to(device,dtype=dtype)
|
192 |
-
|
193 |
-
if img == None:
|
194 |
-
img = pipe(
|
195 |
prompt=p1,
|
|
|
196 |
height=height,
|
197 |
width=width,
|
198 |
guidance_scale=accu,
|
|
|
199 |
num_inference_steps=step,
|
200 |
max_sequence_length=seq,
|
|
|
201 |
generator=torch.Generator(device).manual_seed(0)
|
202 |
).images[0]
|
203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
return pipe(
|
205 |
prompt=p1,
|
206 |
negative_prompt=p2,
|
@@ -210,32 +210,54 @@ def pipe_generate(img,p1,p2,motion):
|
|
210 |
num_inference_steps=step,
|
211 |
guidance_scale=accu,
|
212 |
num_frames=(fps*time)
|
213 |
-
)
|
214 |
|
215 |
-
def handle_generate(*
|
216 |
|
217 |
-
inp = list(
|
218 |
|
219 |
inp[1] = translate(inp[1],"english")
|
220 |
inp[2] = translate(inp[2],"english")
|
221 |
|
222 |
if inp[2] != "":
|
223 |
-
|
|
|
|
|
|
|
224 |
|
225 |
-
inp[2] = f"
|
226 |
|
227 |
-
_do = ['photographed', 'realistic', 'dynamic poze', 'deep field', 'reasonable', "natural", 'rough', 'best quality', 'focused', "highly detailed"]
|
|
|
228 |
if inp[1] != "":
|
229 |
-
|
|
|
230 |
inp[1] = ", ".join(_do)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
|
232 |
ln = len(result)
|
233 |
pipe_out = [pipe_generate(*inp) for i in range(ln)]
|
234 |
|
235 |
names = []
|
236 |
for i in pipe_out:
|
237 |
-
name = generate_random_string(12)+".png"
|
238 |
-
|
|
|
|
|
|
|
239 |
names.append( name )
|
240 |
return names
|
241 |
|
@@ -243,31 +265,45 @@ def ui():
|
|
243 |
global result
|
244 |
with gr.Blocks(theme=gr.themes.Soft(),css=css,js=js) as demo:
|
245 |
gr.Markdown(f"""
|
246 |
-
# MULTI-LANGUAGE GIF CREATOR
|
247 |
""")
|
248 |
with gr.Row(elem_id="col-container"):
|
249 |
with gr.Column():
|
250 |
with gr.Row():
|
251 |
-
img = gr.Image(label="
|
|
|
252 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
253 |
prompt = gr.Textbox(
|
254 |
elem_id="prompt",
|
255 |
-
placeholder="
|
256 |
container=False,
|
257 |
max_lines=1
|
258 |
)
|
259 |
with gr.Row():
|
260 |
prompt2 = gr.Textbox(
|
261 |
elem_id="prompt2",
|
262 |
-
placeholder="
|
263 |
container=False,
|
264 |
max_lines=1
|
265 |
)
|
266 |
-
with gr.Row(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
motion = gr.Dropdown(
|
268 |
-
label='
|
269 |
show_label=True,
|
270 |
-
container=
|
271 |
choices=[
|
272 |
("(No Effect)", ""),
|
273 |
("Zoom in", "guoyww/#animatediff-motion-lora-zoom-in"),
|
@@ -282,11 +318,10 @@ def ui():
|
|
282 |
value="",
|
283 |
interactive=True
|
284 |
)
|
285 |
-
with gr.Column():
|
286 |
-
with gr.Row():
|
287 |
-
run_button = gr.Button("START",elem_classes="btn",scale=0)
|
288 |
with gr.Row():
|
289 |
-
result.append(gr.Image(interactive=False,elem_classes="image-container", label="Result", show_label=
|
|
|
|
|
290 |
|
291 |
gr.on(
|
292 |
triggers=[
|
@@ -295,7 +330,7 @@ def ui():
|
|
295 |
prompt2.submit
|
296 |
],
|
297 |
fn=handle_generate,
|
298 |
-
inputs=[img,prompt,prompt2,motion],
|
299 |
outputs=result
|
300 |
)
|
301 |
demo.queue().launch()
|
@@ -306,4 +341,4 @@ if __name__ == "__main__":
|
|
306 |
os.chdir(os.path.abspath(os.path.dirname(__file__)))
|
307 |
ui()
|
308 |
|
309 |
-
# end
|
|
|
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 AnimateDiffPipeline, MotionAdapter, DDIMScheduler, StableDiffusionPipeline
|
28 |
|
29 |
# logging
|
30 |
|
31 |
warnings.filterwarnings("ignore")
|
32 |
root = logging.getLogger()
|
33 |
root.setLevel(logging.DEBUG)
|
34 |
+
handler = logging.StreamHandler(sys.stderr)
|
35 |
handler.setLevel(logging.DEBUG)
|
36 |
formatter = logging.Formatter('\n >>> [%(levelname)s] %(asctime)s %(name)s: %(message)s\n')
|
37 |
handler.setFormatter(formatter)
|
38 |
root.addHandler(handler)
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
# constant data
|
41 |
|
|
|
42 |
if torch.cuda.is_available():
|
43 |
device = "cuda"
|
44 |
+
dtype = torch.float16
|
45 |
else:
|
46 |
device = "cpu"
|
47 |
+
dtype = torch.float16
|
48 |
+
|
49 |
base = "SG161222/Realistic_Vision_V6.0_B1_noVAE"
|
50 |
adapter = MotionAdapter.from_pretrained("guoyww/animatediff-motion-adapter-v1-5-3", torch_dtype=dtype, device=device)
|
51 |
|
|
|
58 |
|
59 |
seq=512
|
60 |
fast=True
|
61 |
+
fps=18
|
|
|
62 |
width=896
|
63 |
height=896
|
64 |
+
step=30
|
65 |
accu=8.5
|
66 |
|
67 |
# ui data
|
|
|
108 |
|
109 |
# torch pipes
|
110 |
|
111 |
+
image_pipe = StableDiffusionPipeline.from_pretrained(base, torch_dtype=dtype, safety_checker=None).to(device)
|
112 |
+
pipe = AnimateDiffPipeline.from_pretrained(base, torch_dtype=dtype, motion_adapter=adapter).to(device)
|
113 |
pipe.scheduler = DDIMScheduler(
|
114 |
clip_sample=False,
|
115 |
beta_start=0.00085,
|
|
|
135 |
def translate(text,lang):
|
136 |
if text == None or lang == None:
|
137 |
return ""
|
138 |
+
text = re.sub(f'[{punctuation}]', '', re.sub('[ ]+', ' ', text)).lower().strip()
|
139 |
+
lang = re.sub(f'[{punctuation}]', '', re.sub('[ ]+', ' ', lang)).lower().strip()
|
140 |
if text == "" or lang == "":
|
141 |
return ""
|
142 |
if len(text) > 38:
|
|
|
148 |
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15',
|
149 |
'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15'
|
150 |
]
|
151 |
+
padded_chars = re.sub("(^-)|(-$)","",text.replace("","-").replace("- -"," ")).strip()
|
152 |
query_text = f'Please translate {padded_chars}, into {lang}'
|
153 |
url = f'https://www.google.com/search?q={query_text}'
|
154 |
content = str(requests.get(
|
|
|
164 |
trgt_text = xpath_finder(content,'//*[@id="tw-target-text"]/*')
|
165 |
if trgt_lang == lang:
|
166 |
translated = trgt_text
|
167 |
+
ret = re.sub(f'[{punctuation}]', '', re.sub('[ ]+', ' ', translated)).lower().strip()
|
|
|
168 |
return ret
|
169 |
|
170 |
def generate_random_string(length):
|
171 |
characters = str(ascii_letters + digits)
|
172 |
return ''.join(random.choice(characters) for _ in range(length))
|
173 |
|
174 |
+
def pipe_generate(img,p1,p2,motion,time,title):
|
175 |
global last_motion
|
176 |
global pipe
|
177 |
|
178 |
+
if img is None:
|
179 |
+
img = image_pipe(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
prompt=p1,
|
181 |
+
negative_prompt=p2,
|
182 |
height=height,
|
183 |
width=width,
|
184 |
guidance_scale=accu,
|
185 |
+
num_images_per_prompt=1,
|
186 |
num_inference_steps=step,
|
187 |
max_sequence_length=seq,
|
188 |
+
need_safetycheck=False,
|
189 |
generator=torch.Generator(device).manual_seed(0)
|
190 |
).images[0]
|
191 |
|
192 |
+
if time == 0.0:
|
193 |
+
return img
|
194 |
+
|
195 |
+
if last_motion != motion:
|
196 |
+
if last_motion != "":
|
197 |
+
pipe.unload_lora_weights()
|
198 |
+
if motion != "":
|
199 |
+
pipe.load_lora_weights(motion, adapter_name="motion")
|
200 |
+
pipe.fuse_lora()
|
201 |
+
pipe.set_adapters("motion", [0.7])
|
202 |
+
last_motion = motion
|
203 |
+
|
204 |
return pipe(
|
205 |
prompt=p1,
|
206 |
negative_prompt=p2,
|
|
|
210 |
num_inference_steps=step,
|
211 |
guidance_scale=accu,
|
212 |
num_frames=(fps*time)
|
213 |
+
).frames[0]
|
214 |
|
215 |
+
def handle_generate(*_inp):
|
216 |
|
217 |
+
inp = list(_inp)
|
218 |
|
219 |
inp[1] = translate(inp[1],"english")
|
220 |
inp[2] = translate(inp[2],"english")
|
221 |
|
222 |
if inp[2] != "":
|
223 |
+
arr = []
|
224 |
+
for wrd in inp[2].split():
|
225 |
+
arr.append(wrd)
|
226 |
+
inp[2] = ", " + ", ".join(arr)
|
227 |
|
228 |
+
inp[2] = f"creative, fake, error, dreamy, unreal, pixelated, bright, deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, weird, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, unspecified, general, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck{inp[2]}"
|
229 |
|
230 |
+
_do = ['photographed', "environment", 'realistic', 'true', 'genuine', 'dynamic poze', 'authentic', 'deep field', 'reasonable', "natural", 'rough', "real world", 'best quality', 'focused', "highly detailed"]
|
231 |
+
|
232 |
if inp[1] != "":
|
233 |
+
for wrd in inp[1].split():
|
234 |
+
_do.append(wrd)
|
235 |
inp[1] = ", ".join(_do)
|
236 |
+
|
237 |
+
if inp[5] != "":
|
238 |
+
_do.append(f'centered readable large bold written text: {inp[5]}')
|
239 |
+
|
240 |
+
inp[1] = ", ".join(_do)
|
241 |
+
|
242 |
+
print(f"""
|
243 |
+
|
244 |
+
- - - -
|
245 |
+
{inp[1]}
|
246 |
+
{inp[2]}
|
247 |
+
- - - -
|
248 |
+
|
249 |
+
""")
|
250 |
|
251 |
ln = len(result)
|
252 |
pipe_out = [pipe_generate(*inp) for i in range(ln)]
|
253 |
|
254 |
names = []
|
255 |
for i in pipe_out:
|
256 |
+
name = generate_random_string(12) + ( ".png" if time == 0 else ".gif" )
|
257 |
+
if inp[4] == 0.0:
|
258 |
+
i.save(name)
|
259 |
+
else:
|
260 |
+
export_to_gif(i,name,fps=fps)
|
261 |
names.append( name )
|
262 |
return names
|
263 |
|
|
|
265 |
global result
|
266 |
with gr.Blocks(theme=gr.themes.Soft(),css=css,js=js) as demo:
|
267 |
gr.Markdown(f"""
|
268 |
+
# MULTI-LANGUAGE GIF/PNG CREATOR
|
269 |
""")
|
270 |
with gr.Row(elem_id="col-container"):
|
271 |
with gr.Column():
|
272 |
with gr.Row():
|
273 |
+
img = gr.Image(label="Upload photo",show_label=True,container=False,type="pil")
|
274 |
+
with gr.Column(scale=0.66):
|
275 |
with gr.Row():
|
276 |
+
title = gr.Textbox(
|
277 |
+
placeholder="Logo title",
|
278 |
+
container=False,
|
279 |
+
max_lines=1
|
280 |
+
)
|
281 |
prompt = gr.Textbox(
|
282 |
elem_id="prompt",
|
283 |
+
placeholder="Included keywords",
|
284 |
container=False,
|
285 |
max_lines=1
|
286 |
)
|
287 |
with gr.Row():
|
288 |
prompt2 = gr.Textbox(
|
289 |
elem_id="prompt2",
|
290 |
+
placeholder="Excluded keywords",
|
291 |
container=False,
|
292 |
max_lines=1
|
293 |
)
|
294 |
+
with gr.Row():
|
295 |
+
time = gr.Slider(
|
296 |
+
minimum=0.0,
|
297 |
+
maximum=8.0,
|
298 |
+
value=0.0,
|
299 |
+
step=1.0,
|
300 |
+
label="GIF/PNG Duration (0s = PNG)"
|
301 |
+
)
|
302 |
+
with gr.Row():
|
303 |
motion = gr.Dropdown(
|
304 |
+
label='GIF camera movement',
|
305 |
show_label=True,
|
306 |
+
container=False,
|
307 |
choices=[
|
308 |
("(No Effect)", ""),
|
309 |
("Zoom in", "guoyww/#animatediff-motion-lora-zoom-in"),
|
|
|
318 |
value="",
|
319 |
interactive=True
|
320 |
)
|
|
|
|
|
|
|
321 |
with gr.Row():
|
322 |
+
result.append(gr.Image(interactive=False,elem_classes="image-container", label="Result", show_label=True, type='filepath', show_share_button=False))
|
323 |
+
with gr.Row():
|
324 |
+
run_button = gr.Button("Start!",elem_classes="btn",scale=0)
|
325 |
|
326 |
gr.on(
|
327 |
triggers=[
|
|
|
330 |
prompt2.submit
|
331 |
],
|
332 |
fn=handle_generate,
|
333 |
+
inputs=[img,prompt,prompt2,motion,time,title],
|
334 |
outputs=result
|
335 |
)
|
336 |
demo.queue().launch()
|
|
|
341 |
os.chdir(os.path.abspath(os.path.dirname(__file__)))
|
342 |
ui()
|
343 |
|
344 |
+
# end
|