Spaces:
Running
on
Zero
Running
on
Zero
adamelliotfields
commited on
Commit
•
dffd0bb
1
Parent(s):
5c4e8c1
Exception handling
Browse files- app.py +9 -5
- generate.py +24 -15
app.py
CHANGED
@@ -34,15 +34,18 @@ def read_file(path: str) -> str:
|
|
34 |
return file.read()
|
35 |
|
36 |
|
37 |
-
|
38 |
-
def generate_btn_click(*args):
|
39 |
if len(args) > 0:
|
40 |
prompt = args[0]
|
41 |
else:
|
42 |
prompt = None
|
43 |
if prompt is None or prompt.strip() == "":
|
44 |
raise gr.Error("You must enter a prompt")
|
45 |
-
|
|
|
|
|
|
|
|
|
46 |
|
47 |
|
48 |
with gr.Blocks(
|
@@ -287,8 +290,9 @@ with gr.Blocks(
|
|
287 |
outputs=[tgate_step],
|
288 |
)
|
289 |
|
290 |
-
|
291 |
-
|
|
|
292 |
api_name="api",
|
293 |
concurrency_limit=5,
|
294 |
outputs=[output_images],
|
|
|
34 |
return file.read()
|
35 |
|
36 |
|
37 |
+
def handle_generate(*args):
|
|
|
38 |
if len(args) > 0:
|
39 |
prompt = args[0]
|
40 |
else:
|
41 |
prompt = None
|
42 |
if prompt is None or prompt.strip() == "":
|
43 |
raise gr.Error("You must enter a prompt")
|
44 |
+
try:
|
45 |
+
images = generate(*args, log=gr.Info, Error=gr.Error)
|
46 |
+
except RuntimeError:
|
47 |
+
raise gr.Error("RuntimeError: Please try again")
|
48 |
+
return images
|
49 |
|
50 |
|
51 |
with gr.Blocks(
|
|
|
290 |
outputs=[tgate_step],
|
291 |
)
|
292 |
|
293 |
+
gr.on(
|
294 |
+
triggers=[generate_btn.click, prompt.submit],
|
295 |
+
fn=handle_generate,
|
296 |
api_name="api",
|
297 |
concurrency_limit=5,
|
298 |
outputs=[output_images],
|
generate.py
CHANGED
@@ -6,12 +6,12 @@ from itertools import product
|
|
6 |
from os import environ
|
7 |
from types import MethodType
|
8 |
from typing import Callable
|
9 |
-
from warnings import filterwarnings
|
10 |
|
11 |
import spaces
|
12 |
import tomesd
|
13 |
import torch
|
14 |
from compel import Compel, DiffusersTextualInversionManager, ReturnedEmbeddingsType
|
|
|
15 |
from DeepCache import DeepCacheSDHelper
|
16 |
from diffusers import (
|
17 |
DEISMultistepScheduler,
|
@@ -28,6 +28,10 @@ from tgate.SD import tgate as tgate_sd
|
|
28 |
from tgate.SD_DeepCache import tgate as tgate_sd_deepcache
|
29 |
from torch._dynamo import OptimizedModule
|
30 |
|
|
|
|
|
|
|
|
|
31 |
ZERO_GPU = (
|
32 |
environ.get("SPACES_ZERO_GPU", "").lower() == "true"
|
33 |
or environ.get("SPACES_ZERO_GPU", "") == "1"
|
@@ -41,11 +45,9 @@ EMBEDDINGS = {
|
|
41 |
"./embeddings/UnrealisticDream.pt": "<unrealistic_dream>",
|
42 |
}
|
43 |
|
44 |
-
# some models use the deprecated CLIPFeatureExtractor class
|
45 |
-
# should use CLIPImageProcessor instead
|
46 |
-
filterwarnings("ignore", category=FutureWarning, module="transformers")
|
47 |
-
|
48 |
|
|
|
|
|
49 |
class Loader:
|
50 |
_instance = None
|
51 |
|
@@ -223,7 +225,8 @@ def parse_prompt(prompt: str) -> list[str]:
|
|
223 |
return prompts
|
224 |
|
225 |
|
226 |
-
|
|
|
227 |
def generate(
|
228 |
positive_prompt,
|
229 |
negative_prompt="",
|
@@ -282,20 +285,26 @@ def generate(
|
|
282 |
|
283 |
images = []
|
284 |
current_seed = seed
|
285 |
-
|
|
|
|
|
|
|
|
|
286 |
|
287 |
for i in range(num_images):
|
288 |
# seeded generator for each iteration
|
289 |
generator = torch.Generator(device=pipe.device).manual_seed(current_seed)
|
290 |
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
|
|
|
|
299 |
|
300 |
with token_merging(pipe, tome_ratio=tome_ratio):
|
301 |
# cap the tgate step
|
|
|
6 |
from os import environ
|
7 |
from types import MethodType
|
8 |
from typing import Callable
|
|
|
9 |
|
10 |
import spaces
|
11 |
import tomesd
|
12 |
import torch
|
13 |
from compel import Compel, DiffusersTextualInversionManager, ReturnedEmbeddingsType
|
14 |
+
from compel.prompt_parser import PromptParser
|
15 |
from DeepCache import DeepCacheSDHelper
|
16 |
from diffusers import (
|
17 |
DEISMultistepScheduler,
|
|
|
28 |
from tgate.SD_DeepCache import tgate as tgate_sd_deepcache
|
29 |
from torch._dynamo import OptimizedModule
|
30 |
|
31 |
+
# some models use the deprecated CLIPFeatureExtractor class (should use CLIPImageProcessor)
|
32 |
+
__import__("warnings").filterwarnings("ignore", category=FutureWarning, module="transformers")
|
33 |
+
__import__("transformers").logging.set_verbosity_error()
|
34 |
+
|
35 |
ZERO_GPU = (
|
36 |
environ.get("SPACES_ZERO_GPU", "").lower() == "true"
|
37 |
or environ.get("SPACES_ZERO_GPU", "") == "1"
|
|
|
45 |
"./embeddings/UnrealisticDream.pt": "<unrealistic_dream>",
|
46 |
}
|
47 |
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
# inspired by ComfyUI
|
50 |
+
# https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/model_management.py
|
51 |
class Loader:
|
52 |
_instance = None
|
53 |
|
|
|
225 |
return prompts
|
226 |
|
227 |
|
228 |
+
# 1024x1024 for 50 steps can take ~10s each
|
229 |
+
@spaces.GPU(duration=44)
|
230 |
def generate(
|
231 |
positive_prompt,
|
232 |
negative_prompt="",
|
|
|
285 |
|
286 |
images = []
|
287 |
current_seed = seed
|
288 |
+
|
289 |
+
try:
|
290 |
+
neg_embeds = compel(negative_prompt)
|
291 |
+
except PromptParser.ParsingException:
|
292 |
+
raise Error("ParsingException: Invalid negative prompt")
|
293 |
|
294 |
for i in range(num_images):
|
295 |
# seeded generator for each iteration
|
296 |
generator = torch.Generator(device=pipe.device).manual_seed(current_seed)
|
297 |
|
298 |
+
try:
|
299 |
+
all_positive_prompts = parse_prompt(positive_prompt)
|
300 |
+
prompt_index = i % len(all_positive_prompts)
|
301 |
+
pos_prompt = all_positive_prompts[prompt_index]
|
302 |
+
pos_embeds = compel(pos_prompt)
|
303 |
+
pos_embeds, neg_embeds = compel.pad_conditioning_tensors_to_same_length(
|
304 |
+
[pos_embeds, neg_embeds]
|
305 |
+
)
|
306 |
+
except PromptParser.ParsingException:
|
307 |
+
raise Error("ParsingException: Invalid prompt")
|
308 |
|
309 |
with token_merging(pipe, tome_ratio=tome_ratio):
|
310 |
# cap the tgate step
|