Spaces:
Sleeping
Sleeping
Yaron Koresh
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -538,43 +538,30 @@ def upscaler(
|
|
538 |
|
539 |
log(f'CALL upscaler')
|
540 |
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
controlnet_scale_decay=controlnet_decay,
|
562 |
-
condition_scale=condition_scale,
|
563 |
-
tile_size=(tile_height, tile_width),
|
564 |
-
denoise_strength=denoise_strength,
|
565 |
-
num_inference_steps=num_inference_steps,
|
566 |
-
loras_scale={"more_details": 0.5, "sdxl_render": 1.0},
|
567 |
-
solver_type=solver_type,
|
568 |
-
)
|
569 |
|
570 |
-
|
571 |
-
_WIDTH_ = _WIDTH_ * upscale_factor
|
572 |
-
|
573 |
-
log(f'RET upscaler')
|
574 |
|
575 |
-
|
576 |
-
|
577 |
-
return enhanced_image
|
578 |
|
579 |
def get_tensor_length(tensor):
|
580 |
nums = list(tensor.size())
|
@@ -631,41 +618,33 @@ def generate_random_string(length):
|
|
631 |
|
632 |
def add_song_cover_text(img,top_title=None,bottom_title=None):
|
633 |
|
634 |
-
|
635 |
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
|
641 |
-
|
642 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
643 |
|
644 |
-
|
645 |
-
|
646 |
-
if top_title:
|
647 |
-
rows = len(top_title.split("\n"))
|
648 |
-
textheight=min(math.ceil( w / 10 ), math.ceil( h / 5 ))
|
649 |
-
font = ImageFont.truetype(r"Alef-Bold.ttf", textheight)
|
650 |
-
textwidth = draw.textlength(top_title,font)
|
651 |
-
x = math.ceil((w - textwidth) / 2)
|
652 |
-
y = h - (textheight * rows / 2) - (h / 2)
|
653 |
-
y = math.ceil(y - (h / 2 * labels_distance))
|
654 |
-
draw.text((x, y), top_title, (255,255,255), font=font, spacing=2, stroke_width=math.ceil(textheight/20), stroke_fill=(0,0,0))
|
655 |
-
|
656 |
-
if bottom_title:
|
657 |
-
rows = len(bottom_title.split("\n"))
|
658 |
-
textheight=min(math.ceil( w / 10 ), math.ceil( h / 5 ))
|
659 |
-
font = ImageFont.truetype(r"Alef-Bold.ttf", textheight)
|
660 |
-
textwidth = draw.textlength(bottom_title,font)
|
661 |
-
x = math.ceil((w - textwidth) / 2)
|
662 |
-
y = h - (textheight * rows / 2) - (h / 2)
|
663 |
-
y = math.ceil(y + (h / 2 * labels_distance))
|
664 |
-
draw.text((x, y), bottom_title, (0,0,0), font=font, spacing=2, stroke_width=math.ceil(textheight/20), stroke_fill=(255,255,255))
|
665 |
-
|
666 |
-
working = False
|
667 |
-
|
668 |
-
return img
|
669 |
|
670 |
google_translate_endpoint = "https://translate.google.com/m"
|
671 |
language_codes = {
|
@@ -1284,67 +1263,54 @@ def translate(txt,to_lang="en",from_lang="auto"):
|
|
1284 |
log(f'RET translate with translation as {translation}')
|
1285 |
return translation.lower()
|
1286 |
|
1287 |
-
@spaces.GPU(duration=
|
1288 |
def handle_generation(h,w,d):
|
1289 |
|
1290 |
log(f'CALL handle_generate')
|
1291 |
|
1292 |
-
|
1293 |
-
|
1294 |
-
|
1295 |
-
|
1296 |
-
if not working:
|
1297 |
-
|
1298 |
-
working = True
|
1299 |
-
|
1300 |
-
if len(d) > 0:
|
1301 |
-
d = re.sub(r",( ){1,}",". ",d)
|
1302 |
-
d_lines = re.split(r"([\n]){1,}", d)
|
1303 |
-
|
1304 |
-
for line_index in range(len(d_lines)):
|
1305 |
-
d_lines[line_index] = d_lines[line_index].strip()
|
1306 |
-
if d_lines[line_index] != "" and re.sub(r'[\.]$', '', d_lines[line_index]) == d_lines[line_index]:
|
1307 |
-
d_lines[line_index] = d_lines[line_index] + "."
|
1308 |
-
d = " ".join(d_lines)
|
1309 |
|
1310 |
-
|
1311 |
-
|
1312 |
-
|
1313 |
-
|
1314 |
-
|
1315 |
-
d = re.sub(r"([ \t]){1,}", " ", d)
|
1316 |
-
d = re.sub(r"(\. \.)", ".", d)
|
1317 |
-
d = re.sub(r"[,]", ".", d).lower().strip()
|
1318 |
-
|
1319 |
-
neg = f"Textual, Text, Blurry, Distorted, Exceptional, Irregular, Unusual, Shiny, Smoothed, Polished, Low Quality, Worst Quality, Normal Quality, Anime Quality, Paint Quality, Movie Quality."
|
1320 |
-
q = "\""
|
1321 |
-
pos = f'Convincing Realism{ "." if d == "" else " from " + d }'
|
1322 |
-
|
1323 |
-
print(f"""
|
1324 |
-
Positive: {pos}
|
1325 |
|
1326 |
-
|
1327 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1328 |
|
1329 |
-
|
1330 |
-
prompt=pos,
|
1331 |
-
negative_prompt=neg,
|
1332 |
-
height=h,
|
1333 |
-
width=w,
|
1334 |
-
output_type="pil",
|
1335 |
-
guidance_scale=img_accu,
|
1336 |
-
num_images_per_prompt=1,
|
1337 |
-
num_inference_steps=image_steps,
|
1338 |
-
max_sequence_length=seq,
|
1339 |
-
generator=torch.Generator(device).manual_seed(random.randint(0, MAX_SEED))
|
1340 |
-
).images[0]
|
1341 |
-
|
1342 |
-
working = False
|
1343 |
-
|
1344 |
-
_HEIGHT_ = h
|
1345 |
-
_WIDTH_ = w
|
1346 |
-
|
1347 |
-
return img
|
1348 |
|
1349 |
# entry
|
1350 |
|
|
|
538 |
|
539 |
log(f'CALL upscaler')
|
540 |
|
541 |
+
manual_seed(seed)
|
542 |
+
|
543 |
+
solver_type: type[Solver] = getattr(solvers, solver)
|
544 |
+
|
545 |
+
log(f'DBG upscaler 1')
|
546 |
+
|
547 |
+
enhanced_image = enhancer.upscale(
|
548 |
+
image=input_image,
|
549 |
+
prompt=prompt,
|
550 |
+
negative_prompt=negative_prompt,
|
551 |
+
upscale_factor=upscale_factor,
|
552 |
+
controlnet_scale=controlnet_scale,
|
553 |
+
controlnet_scale_decay=controlnet_decay,
|
554 |
+
condition_scale=condition_scale,
|
555 |
+
tile_size=(tile_height, tile_width),
|
556 |
+
denoise_strength=denoise_strength,
|
557 |
+
num_inference_steps=num_inference_steps,
|
558 |
+
loras_scale={"more_details": 0.5, "sdxl_render": 1.0},
|
559 |
+
solver_type=solver_type,
|
560 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
561 |
|
562 |
+
log(f'RET upscaler')
|
|
|
|
|
|
|
563 |
|
564 |
+
return enhanced_image
|
|
|
|
|
565 |
|
566 |
def get_tensor_length(tensor):
|
567 |
nums = list(tensor.size())
|
|
|
618 |
|
619 |
def add_song_cover_text(img,top_title=None,bottom_title=None):
|
620 |
|
621 |
+
w, h = img.size
|
622 |
|
623 |
+
draw = ImageDraw.Draw(img,mode="RGBA")
|
624 |
+
|
625 |
+
labels_distance = 1/3
|
626 |
+
|
627 |
+
if top_title:
|
628 |
+
rows = len(top_title.split("\n"))
|
629 |
+
textheight=min(math.ceil( w / 10 ), math.ceil( h / 5 ))
|
630 |
+
font = ImageFont.truetype(r"Alef-Bold.ttf", textheight)
|
631 |
+
textwidth = draw.textlength(top_title,font)
|
632 |
+
x = math.ceil((w - textwidth) / 2)
|
633 |
+
y = h - (textheight * rows / 2) - (h / 2)
|
634 |
+
y = math.ceil(y - (h / 2 * labels_distance))
|
635 |
+
draw.text((x, y), top_title, (255,255,255), font=font, spacing=2, stroke_width=math.ceil(textheight/20), stroke_fill=(0,0,0))
|
636 |
+
|
637 |
+
if bottom_title:
|
638 |
+
rows = len(bottom_title.split("\n"))
|
639 |
+
textheight=min(math.ceil( w / 10 ), math.ceil( h / 5 ))
|
640 |
+
font = ImageFont.truetype(r"Alef-Bold.ttf", textheight)
|
641 |
+
textwidth = draw.textlength(bottom_title,font)
|
642 |
+
x = math.ceil((w - textwidth) / 2)
|
643 |
+
y = h - (textheight * rows / 2) - (h / 2)
|
644 |
+
y = math.ceil(y + (h / 2 * labels_distance))
|
645 |
+
draw.text((x, y), bottom_title, (0,0,0), font=font, spacing=2, stroke_width=math.ceil(textheight/20), stroke_fill=(255,255,255))
|
646 |
|
647 |
+
return img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
648 |
|
649 |
google_translate_endpoint = "https://translate.google.com/m"
|
650 |
language_codes = {
|
|
|
1263 |
log(f'RET translate with translation as {translation}')
|
1264 |
return translation.lower()
|
1265 |
|
1266 |
+
@spaces.GPU(duration=100)
|
1267 |
def handle_generation(h,w,d):
|
1268 |
|
1269 |
log(f'CALL handle_generate')
|
1270 |
|
1271 |
+
if len(d) > 0:
|
1272 |
+
d = re.sub(r",( ){1,}",". ",d)
|
1273 |
+
d_lines = re.split(r"([\n]){1,}", d)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1274 |
|
1275 |
+
for line_index in range(len(d_lines)):
|
1276 |
+
d_lines[line_index] = d_lines[line_index].strip()
|
1277 |
+
if d_lines[line_index] != "" and re.sub(r'[\.]$', '', d_lines[line_index]) == d_lines[line_index]:
|
1278 |
+
d_lines[line_index] = d_lines[line_index] + "."
|
1279 |
+
d = " ".join(d_lines)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1280 |
|
1281 |
+
d = re.sub(r"([ \t]){1,}", " ", d).lower().strip()
|
1282 |
+
if len(d) > 400:
|
1283 |
+
d = d if d == "" else summarize(translate(d))
|
1284 |
+
else:
|
1285 |
+
d = d if d == "" else translate(d)
|
1286 |
+
d = re.sub(r"([ \t]){1,}", " ", d)
|
1287 |
+
d = re.sub(r"(\. \.)", ".", d)
|
1288 |
+
d = re.sub(r"[,]", ".", d).lower().strip()
|
1289 |
+
|
1290 |
+
neg = f"Textual, Text, Blurry, Distorted, Exceptional, Irregular, Unusual, Shiny, Smoothed, Polished, Low Quality, Worst Quality, Normal Quality, Anime Quality, Paint Quality, Movie Quality."
|
1291 |
+
q = "\""
|
1292 |
+
pos = f'Convincing Realism{ "." if d == "" else " from " + d }'
|
1293 |
+
|
1294 |
+
print(f"""
|
1295 |
+
Positive: {pos}
|
1296 |
+
|
1297 |
+
Negative: {neg}
|
1298 |
+
""")
|
1299 |
+
|
1300 |
+
img = image_pipe(
|
1301 |
+
prompt=pos,
|
1302 |
+
negative_prompt=neg,
|
1303 |
+
height=h,
|
1304 |
+
width=w,
|
1305 |
+
output_type="pil",
|
1306 |
+
guidance_scale=img_accu,
|
1307 |
+
num_images_per_prompt=1,
|
1308 |
+
num_inference_steps=image_steps,
|
1309 |
+
max_sequence_length=seq,
|
1310 |
+
generator=torch.Generator(device).manual_seed(random.randint(0, MAX_SEED))
|
1311 |
+
).images[0]
|
1312 |
|
1313 |
+
return img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1314 |
|
1315 |
# entry
|
1316 |
|