Spaces:
Running
on
Zero
Running
on
Zero
Update
Browse files- .pre-commit-config.yaml +60 -35
- .style.yapf +0 -5
- app.py +27 -40
- model.py +16 -24
.pre-commit-config.yaml
CHANGED
@@ -1,36 +1,61 @@
|
|
1 |
-
exclude: patch
|
2 |
repos:
|
3 |
-
- repo: https://github.com/pre-commit/pre-commit-hooks
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
- repo: https://github.com/pre-commit/mirrors-mypy
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
exclude: ^patch
|
2 |
repos:
|
3 |
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
4 |
+
rev: v4.6.0
|
5 |
+
hooks:
|
6 |
+
- id: check-executables-have-shebangs
|
7 |
+
- id: check-json
|
8 |
+
- id: check-merge-conflict
|
9 |
+
- id: check-shebang-scripts-are-executable
|
10 |
+
- id: check-toml
|
11 |
+
- id: check-yaml
|
12 |
+
- id: end-of-file-fixer
|
13 |
+
- id: mixed-line-ending
|
14 |
+
args: ["--fix=lf"]
|
15 |
+
- id: requirements-txt-fixer
|
16 |
+
- id: trailing-whitespace
|
17 |
+
- repo: https://github.com/myint/docformatter
|
18 |
+
rev: v1.7.5
|
19 |
+
hooks:
|
20 |
+
- id: docformatter
|
21 |
+
args: ["--in-place"]
|
22 |
+
- repo: https://github.com/pycqa/isort
|
23 |
+
rev: 5.13.2
|
24 |
+
hooks:
|
25 |
+
- id: isort
|
26 |
+
args: ["--profile", "black"]
|
27 |
+
- repo: https://github.com/pre-commit/mirrors-mypy
|
28 |
+
rev: v1.10.0
|
29 |
+
hooks:
|
30 |
+
- id: mypy
|
31 |
+
args: ["--ignore-missing-imports"]
|
32 |
+
additional_dependencies:
|
33 |
+
[
|
34 |
+
"types-python-slugify",
|
35 |
+
"types-requests",
|
36 |
+
"types-PyYAML",
|
37 |
+
"types-pytz",
|
38 |
+
]
|
39 |
+
- repo: https://github.com/psf/black
|
40 |
+
rev: 24.4.2
|
41 |
+
hooks:
|
42 |
+
- id: black
|
43 |
+
language_version: python3.10
|
44 |
+
args: ["--line-length", "119"]
|
45 |
+
- repo: https://github.com/kynan/nbstripout
|
46 |
+
rev: 0.7.1
|
47 |
+
hooks:
|
48 |
+
- id: nbstripout
|
49 |
+
args:
|
50 |
+
[
|
51 |
+
"--extra-keys",
|
52 |
+
"metadata.interpreter metadata.kernelspec cell.metadata.pycharm",
|
53 |
+
]
|
54 |
+
- repo: https://github.com/nbQA-dev/nbQA
|
55 |
+
rev: 1.8.5
|
56 |
+
hooks:
|
57 |
+
- id: nbqa-black
|
58 |
+
- id: nbqa-pyupgrade
|
59 |
+
args: ["--py37-plus"]
|
60 |
+
- id: nbqa-isort
|
61 |
+
args: ["--float-to-top"]
|
.style.yapf
DELETED
@@ -1,5 +0,0 @@
|
|
1 |
-
[style]
|
2 |
-
based_on_style = pep8
|
3 |
-
blank_line_before_nested_class_or_def = false
|
4 |
-
spaces_before_comment = 2
|
5 |
-
split_before_logical_operator = true
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
@@ -9,66 +9,57 @@ import torch
|
|
9 |
|
10 |
from model import Model
|
11 |
|
12 |
-
DESCRIPTION =
|
13 |
|
14 |
-
DETAILS =
|
15 |
- To run the Semi Resolution-Dependent sampler, use the format: `<jane(number)>`.
|
16 |
- To run the Fully Resolution-Dependent sampler, use the format: `<jane[number]>`.
|
17 |
- To run the Fixed Resolution sampler, use the format: `<jane|number|>`.
|
18 |
|
19 |
For this demo, only `<jane>`, `<gta5-artwork>` and `<cat-toy>` are available.
|
20 |
Also, `number` should be an integer in [0, 9].
|
21 |
-
|
22 |
|
23 |
-
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv(
|
24 |
-
'CACHE_EXAMPLES') == '1'
|
25 |
|
26 |
model = Model()
|
27 |
|
28 |
-
with gr.Blocks(css=
|
29 |
gr.Markdown(DESCRIPTION)
|
30 |
|
31 |
with gr.Row():
|
32 |
with gr.Group():
|
33 |
with gr.Row():
|
34 |
-
prompt = gr.Textbox(label=
|
35 |
with gr.Row():
|
36 |
num_images = gr.Slider(
|
37 |
-
label=
|
38 |
minimum=1,
|
39 |
maximum=9,
|
40 |
step=1,
|
41 |
value=1,
|
42 |
)
|
43 |
with gr.Row():
|
44 |
-
num_steps = gr.Slider(label=
|
45 |
-
minimum=1,
|
46 |
-
maximum=50,
|
47 |
-
step=1,
|
48 |
-
value=10)
|
49 |
with gr.Row():
|
50 |
-
seed = gr.Slider(label=
|
51 |
-
minimum=0,
|
52 |
-
maximum=100000,
|
53 |
-
step=1,
|
54 |
-
value=100)
|
55 |
with gr.Row():
|
56 |
-
run_button = gr.Button(
|
57 |
|
58 |
with gr.Column():
|
59 |
-
result = gr.Gallery(label=
|
60 |
|
61 |
with gr.Row():
|
62 |
with gr.Group():
|
63 |
fn = lambda x: model.run(x, 2, 10, 100)
|
64 |
with gr.Row():
|
65 |
gr.Examples(
|
66 |
-
label=
|
67 |
examples=[
|
68 |
-
[
|
69 |
-
[
|
70 |
-
[
|
71 |
-
[
|
72 |
],
|
73 |
inputs=prompt,
|
74 |
outputs=result,
|
@@ -77,16 +68,12 @@ with gr.Blocks(css='style.css') as demo:
|
|
77 |
)
|
78 |
with gr.Row():
|
79 |
gr.Examples(
|
80 |
-
label=
|
81 |
examples=[
|
82 |
-
[
|
83 |
-
|
84 |
-
],
|
85 |
-
[
|
86 |
-
['a painting of a dog in the style of <jane(5)>'],
|
87 |
-
[
|
88 |
-
'a painting of a <cat-toy(0)> in the style of <jane(3)>'
|
89 |
-
],
|
90 |
],
|
91 |
inputs=prompt,
|
92 |
outputs=result,
|
@@ -95,11 +82,11 @@ with gr.Blocks(css='style.css') as demo:
|
|
95 |
)
|
96 |
with gr.Row():
|
97 |
gr.Examples(
|
98 |
-
label=
|
99 |
examples=[
|
100 |
-
[
|
101 |
-
[
|
102 |
-
[
|
103 |
],
|
104 |
inputs=prompt,
|
105 |
outputs=result,
|
@@ -123,10 +110,10 @@ with gr.Blocks(css='style.css') as demo:
|
|
123 |
fn=model.run,
|
124 |
inputs=inputs,
|
125 |
outputs=result,
|
126 |
-
api_name=
|
127 |
)
|
128 |
|
129 |
-
with gr.Accordion(
|
130 |
gr.Markdown(DETAILS)
|
131 |
|
132 |
demo.queue(max_size=10).launch()
|
|
|
9 |
|
10 |
from model import Model
|
11 |
|
12 |
+
DESCRIPTION = "# [Multiresolution Textual Inversion](https://github.com/giannisdaras/multires_textual_inversion)"
|
13 |
|
14 |
+
DETAILS = """
|
15 |
- To run the Semi Resolution-Dependent sampler, use the format: `<jane(number)>`.
|
16 |
- To run the Fully Resolution-Dependent sampler, use the format: `<jane[number]>`.
|
17 |
- To run the Fixed Resolution sampler, use the format: `<jane|number|>`.
|
18 |
|
19 |
For this demo, only `<jane>`, `<gta5-artwork>` and `<cat-toy>` are available.
|
20 |
Also, `number` should be an integer in [0, 9].
|
21 |
+
"""
|
22 |
|
23 |
+
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES") == "1"
|
|
|
24 |
|
25 |
model = Model()
|
26 |
|
27 |
+
with gr.Blocks(css="style.css") as demo:
|
28 |
gr.Markdown(DESCRIPTION)
|
29 |
|
30 |
with gr.Row():
|
31 |
with gr.Group():
|
32 |
with gr.Row():
|
33 |
+
prompt = gr.Textbox(label="Prompt")
|
34 |
with gr.Row():
|
35 |
num_images = gr.Slider(
|
36 |
+
label="Number of images",
|
37 |
minimum=1,
|
38 |
maximum=9,
|
39 |
step=1,
|
40 |
value=1,
|
41 |
)
|
42 |
with gr.Row():
|
43 |
+
num_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=10)
|
|
|
|
|
|
|
|
|
44 |
with gr.Row():
|
45 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=100000, step=1, value=100)
|
|
|
|
|
|
|
|
|
46 |
with gr.Row():
|
47 |
+
run_button = gr.Button("Run")
|
48 |
|
49 |
with gr.Column():
|
50 |
+
result = gr.Gallery(label="Result", object_fit="scale-down")
|
51 |
|
52 |
with gr.Row():
|
53 |
with gr.Group():
|
54 |
fn = lambda x: model.run(x, 2, 10, 100)
|
55 |
with gr.Row():
|
56 |
gr.Examples(
|
57 |
+
label="Examples 1",
|
58 |
examples=[
|
59 |
+
["an image of <gta5-artwork(0)>"],
|
60 |
+
["an image of <jane(0)>"],
|
61 |
+
["an image of <jane(3)>"],
|
62 |
+
["an image of <cat-toy(0)>"],
|
63 |
],
|
64 |
inputs=prompt,
|
65 |
outputs=result,
|
|
|
68 |
)
|
69 |
with gr.Row():
|
70 |
gr.Examples(
|
71 |
+
label="Examples 2",
|
72 |
examples=[
|
73 |
+
["an image of a cat in the style of <gta5-artwork(0)>"],
|
74 |
+
["a painting of a dog in the style of <jane(0)>"],
|
75 |
+
["a painting of a dog in the style of <jane(5)>"],
|
76 |
+
["a painting of a <cat-toy(0)> in the style of <jane(3)>"],
|
|
|
|
|
|
|
|
|
77 |
],
|
78 |
inputs=prompt,
|
79 |
outputs=result,
|
|
|
82 |
)
|
83 |
with gr.Row():
|
84 |
gr.Examples(
|
85 |
+
label="Examples 3",
|
86 |
examples=[
|
87 |
+
["an image of <jane[0]>"],
|
88 |
+
["an image of <jane|0|>"],
|
89 |
+
["an image of <jane|3|>"],
|
90 |
],
|
91 |
inputs=prompt,
|
92 |
outputs=result,
|
|
|
110 |
fn=model.run,
|
111 |
inputs=inputs,
|
112 |
outputs=result,
|
113 |
+
api_name="run",
|
114 |
)
|
115 |
|
116 |
+
with gr.Accordion("About available prompts", open=False):
|
117 |
gr.Markdown(DETAILS)
|
118 |
|
119 |
demo.queue(max_size=10).launch()
|
model.py
CHANGED
@@ -9,48 +9,40 @@ import PIL.Image
|
|
9 |
import torch
|
10 |
from diffusers import DPMSolverMultistepScheduler
|
11 |
|
12 |
-
if os.getenv(
|
13 |
-
with open(
|
14 |
-
subprocess.run(shlex.split(
|
15 |
-
cwd='multires_textual_inversion',
|
16 |
-
stdin=f)
|
17 |
|
18 |
-
sys.path.insert(0,
|
19 |
|
20 |
from pipeline import MultiResPipeline, load_learned_concepts
|
21 |
|
22 |
|
23 |
class Model:
|
24 |
def __init__(self):
|
25 |
-
self.device = torch.device(
|
26 |
-
|
27 |
-
|
28 |
-
if self.device.type == 'cpu':
|
29 |
pipe = MultiResPipeline.from_pretrained(model_id)
|
30 |
else:
|
31 |
-
pipe = MultiResPipeline.from_pretrained(model_id,
|
32 |
-
torch_dtype=torch.float16,
|
33 |
-
revision='fp16')
|
34 |
self.pipe = pipe.to(self.device)
|
35 |
self.pipe.scheduler = DPMSolverMultistepScheduler(
|
36 |
beta_start=0.00085,
|
37 |
beta_end=0.012,
|
38 |
-
beta_schedule=
|
39 |
num_train_timesteps=1000,
|
40 |
trained_betas=None,
|
41 |
predict_epsilon=True,
|
42 |
thresholding=False,
|
43 |
-
algorithm_type=
|
44 |
-
solver_type=
|
45 |
lower_order_final=True,
|
46 |
)
|
47 |
-
self.string_to_param_dict = load_learned_concepts(
|
48 |
-
self.pipe, 'textual_inversion_outputs/')
|
49 |
|
50 |
-
def run(self, prompt: str, n_images: int, n_steps: int,
|
51 |
-
seed: int) -> list[PIL.Image.Image]:
|
52 |
generator = torch.Generator(device=self.device).manual_seed(seed)
|
53 |
-
return self.pipe(
|
54 |
-
|
55 |
-
|
56 |
-
generator=generator)
|
|
|
9 |
import torch
|
10 |
from diffusers import DPMSolverMultistepScheduler
|
11 |
|
12 |
+
if os.getenv("SYSTEM") == "spaces":
|
13 |
+
with open("patch") as f:
|
14 |
+
subprocess.run(shlex.split("patch -p1"), cwd="multires_textual_inversion", stdin=f)
|
|
|
|
|
15 |
|
16 |
+
sys.path.insert(0, "multires_textual_inversion")
|
17 |
|
18 |
from pipeline import MultiResPipeline, load_learned_concepts
|
19 |
|
20 |
|
21 |
class Model:
|
22 |
def __init__(self):
|
23 |
+
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
24 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
25 |
+
if self.device.type == "cpu":
|
|
|
26 |
pipe = MultiResPipeline.from_pretrained(model_id)
|
27 |
else:
|
28 |
+
pipe = MultiResPipeline.from_pretrained(model_id, torch_dtype=torch.float16, revision="fp16")
|
|
|
|
|
29 |
self.pipe = pipe.to(self.device)
|
30 |
self.pipe.scheduler = DPMSolverMultistepScheduler(
|
31 |
beta_start=0.00085,
|
32 |
beta_end=0.012,
|
33 |
+
beta_schedule="scaled_linear",
|
34 |
num_train_timesteps=1000,
|
35 |
trained_betas=None,
|
36 |
predict_epsilon=True,
|
37 |
thresholding=False,
|
38 |
+
algorithm_type="dpmsolver++",
|
39 |
+
solver_type="midpoint",
|
40 |
lower_order_final=True,
|
41 |
)
|
42 |
+
self.string_to_param_dict = load_learned_concepts(self.pipe, "textual_inversion_outputs/")
|
|
|
43 |
|
44 |
+
def run(self, prompt: str, n_images: int, n_steps: int, seed: int) -> list[PIL.Image.Image]:
|
|
|
45 |
generator = torch.Generator(device=self.device).manual_seed(seed)
|
46 |
+
return self.pipe(
|
47 |
+
[prompt] * n_images, self.string_to_param_dict, num_inference_steps=n_steps, generator=generator
|
48 |
+
)
|
|