hysts HF staff commited on
Commit
d818369
1 Parent(s): 2423f10
Files changed (4) hide show
  1. .pre-commit-config.yaml +60 -35
  2. .style.yapf +0 -5
  3. app.py +27 -40
  4. 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
- rev: v4.2.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: double-quote-string-fixer
13
- - id: end-of-file-fixer
14
- - id: mixed-line-ending
15
- args: ['--fix=lf']
16
- - id: requirements-txt-fixer
17
- - id: trailing-whitespace
18
- - repo: https://github.com/myint/docformatter
19
- rev: v1.4
20
- hooks:
21
- - id: docformatter
22
- args: ['--in-place']
23
- - repo: https://github.com/pycqa/isort
24
- rev: 5.12.0
25
- hooks:
26
- - id: isort
27
- - repo: https://github.com/pre-commit/mirrors-mypy
28
- rev: v0.991
29
- hooks:
30
- - id: mypy
31
- args: ['--ignore-missing-imports']
32
- - repo: https://github.com/google/yapf
33
- rev: v0.32.0
34
- hooks:
35
- - id: yapf
36
- args: ['--parallel', '--in-place']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = '# [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(
24
- 'CACHE_EXAMPLES') == '1'
25
 
26
  model = Model()
27
 
28
- with gr.Blocks(css='style.css') as demo:
29
  gr.Markdown(DESCRIPTION)
30
 
31
  with gr.Row():
32
  with gr.Group():
33
  with gr.Row():
34
- prompt = gr.Textbox(label='Prompt')
35
  with gr.Row():
36
  num_images = gr.Slider(
37
- label='Number of images',
38
  minimum=1,
39
  maximum=9,
40
  step=1,
41
  value=1,
42
  )
43
  with gr.Row():
44
- num_steps = gr.Slider(label='Number of inference steps',
45
- minimum=1,
46
- maximum=50,
47
- step=1,
48
- value=10)
49
  with gr.Row():
50
- seed = gr.Slider(label='Seed',
51
- minimum=0,
52
- maximum=100000,
53
- step=1,
54
- value=100)
55
  with gr.Row():
56
- run_button = gr.Button('Run')
57
 
58
  with gr.Column():
59
- result = gr.Gallery(label='Result', object_fit='scale-down')
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='Examples 1',
67
  examples=[
68
- ['an image of <gta5-artwork(0)>'],
69
- ['an image of <jane(0)>'],
70
- ['an image of <jane(3)>'],
71
- ['an image of <cat-toy(0)>'],
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='Examples 2',
81
  examples=[
82
- [
83
- 'an image of a cat in the style of <gta5-artwork(0)>'
84
- ],
85
- ['a painting of a dog in the style of <jane(0)>'],
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='Examples 3',
99
  examples=[
100
- ['an image of <jane[0]>'],
101
- ['an image of <jane|0|>'],
102
- ['an image of <jane|3|>'],
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='run',
127
  )
128
 
129
- with gr.Accordion('About available prompts', open=False):
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('SYSTEM') == 'spaces':
13
- with open('patch') as f:
14
- subprocess.run(shlex.split('patch -p1'),
15
- cwd='multires_textual_inversion',
16
- stdin=f)
17
 
18
- sys.path.insert(0, 'multires_textual_inversion')
19
 
20
  from pipeline import MultiResPipeline, load_learned_concepts
21
 
22
 
23
  class Model:
24
  def __init__(self):
25
- self.device = torch.device(
26
- 'cuda:0' if torch.cuda.is_available() else 'cpu')
27
- model_id = 'runwayml/stable-diffusion-v1-5'
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='scaled_linear',
39
  num_train_timesteps=1000,
40
  trained_betas=None,
41
  predict_epsilon=True,
42
  thresholding=False,
43
- algorithm_type='dpmsolver++',
44
- solver_type='midpoint',
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([prompt] * n_images,
54
- self.string_to_param_dict,
55
- num_inference_steps=n_steps,
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
+ )