Yaron Koresh commited on
Commit
263a4e8
·
verified ·
1 Parent(s): 30c0eee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -41
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import urllib
2
  import requests
3
  from bs4 import BeautifulSoup
4
  from abc import ABC, abstractmethod
@@ -17,10 +16,8 @@ import requests
17
  import sys
18
  import warnings
19
  import time
20
- import asyncio
21
  import math
22
  from pathlib import Path
23
- from functools import partial
24
  from dataclasses import dataclass
25
  from typing import Any
26
  import pillow_heif
@@ -44,11 +41,6 @@ from refiners.foundationals.latent_diffusion.stable_diffusion_1.multi_upscaler i
44
  )
45
  from datetime import datetime
46
 
47
- _HEIGHT_ = None
48
- _WIDTH_ = None
49
-
50
- working = False
51
-
52
  model = T5ForConditionalGeneration.from_pretrained("t5-large")
53
  tokenizer = T5Tokenizer.from_pretrained("t5-large")
54
 
@@ -445,7 +437,7 @@ MAX_SEED = np.iinfo(np.int32).max
445
  # precision data
446
 
447
  seq=512
448
- image_steps=50
449
  img_accu=6.5
450
 
451
  # ui data
@@ -481,29 +473,6 @@ footer {
481
  }
482
  """])
483
 
484
- js="""
485
- function custom(){
486
- document.querySelector("div#prompt input").addEventListener("keydown",function(e){
487
- e.target.setAttribute("last_value",e.target.value);
488
- });
489
- document.querySelector("div#prompt input").addEventListener("input",function(e){
490
- if( e.target.value.toString().match(/[^ a-zA-Z,]|( |,){2,}/gsm) ){
491
- e.target.value = e.target.getAttribute("last_value");
492
- e.target.removeAttribute("last_value");
493
- }
494
- });
495
-
496
- document.querySelector("div#prompt2 input").addEventListener("keydown",function(e){
497
- e.target.setAttribute("last_value",e.target.value);
498
- });
499
- document.querySelector("div#prompt2 input").addEventListener("input",function(e){
500
- if( e.target.value.toString().match(/[^ a-zA-Z,]|( |,){2,}/gsm) ){
501
- e.target.value = e.target.getAttribute("last_value");
502
- e.target.removeAttribute("last_value");
503
- }
504
- });
505
- }
506
- """
507
 
508
  # torch pipes
509
 
@@ -514,7 +483,22 @@ torch.cuda.empty_cache()
514
 
515
  # functionality
516
 
517
- @spaces.GPU(duration=240)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
  def upscaler(
519
  input_image: Image.Image,
520
  prompt: str = "Accurate, Highly Detailed, Realistic, Best Quality, Hyper-Realistic, Super-Realistic, Natural, Reasonable, Logical.",
@@ -527,7 +511,7 @@ def upscaler(
527
  tile_width: int = 112,
528
  tile_height: int = 144,
529
  denoise_strength: float = 0.35,
530
- num_inference_steps: int = 30,
531
  solver: str = "DDIM",
532
  ) -> Image.Image:
533
 
@@ -572,7 +556,7 @@ def _summarize(text):
572
  gen = model.generate(
573
  toks,
574
  length_penalty=0.1,
575
- num_beams=32,
576
  early_stopping=True,
577
  max_length=512
578
  )
@@ -1262,11 +1246,47 @@ def translate(txt,to_lang="en",from_lang="auto"):
1262
  log(f'RET translate with translation as {translation}')
1263
  return translation.lower()
1264
 
1265
- @spaces.GPU(duration=240)
1266
  def handle_generation(h,w,d):
1267
 
1268
  log(f'CALL handle_generate')
1269
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1270
  if len(d) > 0:
1271
  d = re.sub(r",( ){1,}",". ",d)
1272
  d_lines = re.split(r"([\n]){1,}", d)
@@ -1282,7 +1302,7 @@ def handle_generation(h,w,d):
1282
  d = re.sub(r"([ \t]){1,}", " ", d)
1283
  d = re.sub(r"(\. \.)", ".", d)
1284
 
1285
- neg = f"Textual, Text, Sign, Label, Title, Unreal, Exceptional, Irregular, Unusual, Blurry, Smoothed, Polished, Worst Quality, Worse Quality, Painted, Movies Quality."
1286
  q = "\""
1287
  pos = f'Accurate, Detailed, Realistic.{ "" if d == "" else " " + d }'
1288
 
@@ -1324,17 +1344,17 @@ if __name__ == "__main__":
1324
  height = gr.Slider(
1325
  label="Height (px)",
1326
  minimum=512,
1327
- maximum=1024,
1328
  step=16,
1329
- value=768,
1330
  )
1331
 
1332
  width = gr.Slider(
1333
  label="Width (px)",
1334
  minimum=512,
1335
- maximum=1024,
1336
  step=16,
1337
- value=768,
1338
  )
1339
 
1340
  run = gr.Button("Generate",elem_classes="btn")
 
 
1
  import requests
2
  from bs4 import BeautifulSoup
3
  from abc import ABC, abstractmethod
 
16
  import sys
17
  import warnings
18
  import time
 
19
  import math
20
  from pathlib import Path
 
21
  from dataclasses import dataclass
22
  from typing import Any
23
  import pillow_heif
 
41
  )
42
  from datetime import datetime
43
 
 
 
 
 
 
44
  model = T5ForConditionalGeneration.from_pretrained("t5-large")
45
  tokenizer = T5Tokenizer.from_pretrained("t5-large")
46
 
 
437
  # precision data
438
 
439
  seq=512
440
+ image_steps=40
441
  img_accu=6.5
442
 
443
  # ui data
 
473
  }
474
  """])
475
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
476
 
477
  # torch pipes
478
 
 
483
 
484
  # functionality
485
 
486
+ @spaces.GPU(duration=180)
487
+ def hard_scaler(img):
488
+ return upscaler(img)
489
+
490
+ @spaces.GPU(duration=90)
491
+ def easy_scaler(img):
492
+ return upscaler(img)
493
+
494
+ def handle_upscaler(img):
495
+
496
+ w, h = img.size
497
+ if w*h > 2 * (10 ** 6):
498
+ return hard_scaler(img)
499
+ return easy_scaler(img)
500
+
501
+ @spaces.GPU(duration=180)
502
  def upscaler(
503
  input_image: Image.Image,
504
  prompt: str = "Accurate, Highly Detailed, Realistic, Best Quality, Hyper-Realistic, Super-Realistic, Natural, Reasonable, Logical.",
 
511
  tile_width: int = 112,
512
  tile_height: int = 144,
513
  denoise_strength: float = 0.35,
514
+ num_inference_steps: int = 20,
515
  solver: str = "DDIM",
516
  ) -> Image.Image:
517
 
 
556
  gen = model.generate(
557
  toks,
558
  length_penalty=0.1,
559
+ num_beams=6,
560
  early_stopping=True,
561
  max_length=512
562
  )
 
1246
  log(f'RET translate with translation as {translation}')
1247
  return translation.lower()
1248
 
 
1249
  def handle_generation(h,w,d):
1250
 
1251
  log(f'CALL handle_generate')
1252
 
1253
+ difficulty_points = 0
1254
+
1255
+ toks_len = get_tensor_length(tokenizer.encode( d, return_tensors="pt", truncation=False))
1256
+ if toks_len > 500:
1257
+ difficulty_points + 2
1258
+ else if toks_len > 50:
1259
+ difficulty_points + 1
1260
+
1261
+ pxs = h*w
1262
+ if pxs > 4 * (10 ** 6):
1263
+ difficulty_points + 3
1264
+ else if pxs > 3 * (10 ** 6):
1265
+ difficulty_points + 2
1266
+ else if pxs > 2 * (10 ** 6):
1267
+ difficulty_points + 1
1268
+
1269
+ if difficulty_points < 2:
1270
+ return easy_generation(h,w,d)
1271
+ else if difficulty_points < 4:
1272
+ return balanced_generation(h,w,d)
1273
+ else:
1274
+ return hard_generation(h,w,d)
1275
+
1276
+ @spaces.GPU(duration=90)
1277
+ def easy_generation(h,w,d):
1278
+ return generation(h,w,d)
1279
+
1280
+ @spaces.GPU(duration=150)
1281
+ def balanced_generation(h,w,d):
1282
+ return generation(h,w,d)
1283
+
1284
+ @spaces.GPU(duration=210)
1285
+ def hard_generation(h,w,d):
1286
+ return generation(h,w,d)
1287
+
1288
+ def generation(h,w,d):
1289
+
1290
  if len(d) > 0:
1291
  d = re.sub(r",( ){1,}",". ",d)
1292
  d_lines = re.split(r"([\n]){1,}", d)
 
1302
  d = re.sub(r"([ \t]){1,}", " ", d)
1303
  d = re.sub(r"(\. \.)", ".", d)
1304
 
1305
+ neg = f"Textual, Text, Signs, Labels, Titles, Unreal, Exceptional, Irregular, Unusual, Blurry, Smoothed, Polished, Worst Quality, Worse Quality, Painted, Movies Quality."
1306
  q = "\""
1307
  pos = f'Accurate, Detailed, Realistic.{ "" if d == "" else " " + d }'
1308
 
 
1344
  height = gr.Slider(
1345
  label="Height (px)",
1346
  minimum=512,
1347
+ maximum=2048,
1348
  step=16,
1349
+ value=1024,
1350
  )
1351
 
1352
  width = gr.Slider(
1353
  label="Width (px)",
1354
  minimum=512,
1355
+ maximum=2048,
1356
  step=16,
1357
+ value=1024,
1358
  )
1359
 
1360
  run = gr.Button("Generate",elem_classes="btn")