JagadishR commited on
Commit
0badb6a
1 Parent(s): 55476dc

Upload 8 files

Browse files
Files changed (8) hide show
  1. LICENSE +242 -0
  2. README.md +5 -4
  3. app.py +298 -0
  4. gitattributes +35 -0
  5. model.py +113 -0
  6. pipeline.py +471 -0
  7. requirements.txt +12 -0
  8. style_template.py +59 -0
LICENSE ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Tencent is pleased to support the open source community by making PhotoMaker available.
2
+
3
+ Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
4
+
5
+ PhotoMaker is licensed under the Apache License Version 2.0 except for the third-party components listed below.
6
+
7
+
8
+ Terms of the Apache License Version 2.0:
9
+ ---------------------------------------------
10
+ Apache License
11
+
12
+ Version 2.0, January 2004
13
+
14
+ http://www.apache.org/licenses/
15
+
16
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
17
+ 1. Definitions.
18
+
19
+ “License” shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
20
+
21
+ “Licensor” shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
22
+
23
+ “Legal Entity” shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, “control” means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
24
+
25
+ “You” (or “Your”) shall mean an individual or Legal Entity exercising permissions granted by this License.
26
+
27
+ “Source” form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
28
+
29
+ “Object” form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
30
+
31
+ “Work” shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
32
+
33
+ “Derivative Works” shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
34
+
35
+ “Contribution” shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, “submitted” means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as “Not a Contribution.”
36
+
37
+ “Contributor” shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
38
+
39
+ 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
40
+
41
+ 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
42
+
43
+ 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
44
+
45
+ You must give any other recipients of the Work or Derivative Works a copy of this License; and
46
+
47
+ You must cause any modified files to carry prominent notices stating that You changed the files; and
48
+
49
+ You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
50
+
51
+ If the Work includes a “NOTICE” text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
52
+
53
+ You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
54
+
55
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
56
+
57
+ 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
58
+
59
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
60
+
61
+ 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
62
+
63
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
64
+
65
+ END OF TERMS AND CONDITIONS
66
+
67
+ SDXL unstable YamerMix checkpoints follow the LICENSE with:
68
+ Copyright (c) 2023 Stability AI CreativeML Open RAIL++-M License dated July 26, 2023
69
+
70
+ Section I: PREAMBLE Multimodal generative models are being widely adopted and used, and
71
+ have the potential to transform the way artists, among other individuals, conceive and
72
+ benefit from AI or ML technologies as a tool for content creation. Notwithstanding the
73
+ current and potential benefits that these artifacts can bring to society at large, there
74
+ are also concerns about potential misuses of them, either due to their technical
75
+ limitations or ethical considerations. In short, this license strives for both the open
76
+ and responsible downstream use of the accompanying model. When it comes to the open
77
+ character, we took inspiration from open source permissive licenses regarding the grant
78
+ of IP rights. Referring to the downstream responsible use, we added use-based
79
+ restrictions not permitting the use of the model in very specific scenarios, in order
80
+ for the licensor to be able to enforce the license in case potential misuses of the
81
+ Model may occur. At the same time, we strive to promote open and responsible research on
82
+ generative models for art and content generation. Even though downstream derivative
83
+ versions of the model could be released under different licensing terms, the latter will
84
+ always have to include - at minimum - the same use-based restrictions as the ones in the
85
+ original license (this license). We believe in the intersection between open and
86
+ responsible AI development; thus, this agreement aims to strike a balance between both
87
+ in order to enable responsible open-science in the field of AI. This CreativeML Open
88
+ RAIL++-M License governs the use of the model (and its derivatives) and is informed by
89
+ the model card associated with the model. NOW THEREFORE, You and Licensor agree as
90
+ follows: Definitions "License" means the terms and conditions for use, reproduction, and
91
+ Distribution as defined in this document. "Data" means a collection of information
92
+ and/or content extracted from the dataset used with the Model, including to train,
93
+ pretrain, or otherwise evaluate the Model. The Data is not licensed under this License.
94
+ "Output" means the results of operating a Model as embodied in informational content
95
+ resulting therefrom. "Model" means any accompanying machine-learning based assemblies
96
+ (including checkpoints), consisting of learnt weights, parameters (including optimizer
97
+ states), corresponding to the model architecture as embodied in the Complementary
98
+ Material, that have been trained or tuned, in whole or in part on the Data, using the
99
+ Complementary Material. "Derivatives of the Model" means all modifications to the Model,
100
+ works based on the Model, or any other model which is created or initialized by transfer
101
+ of patterns of the weights, parameters, activations or output of the Model, to the other
102
+ model, in order to cause the other model to perform similarly to the Model, including -
103
+ but not limited to - distillation methods entailing the use of intermediate data
104
+ representations or methods based on the generation of synthetic data by the Model for
105
+ training the other model. "Complementary Material" means the accompanying source code
106
+ and scripts used to define, run, load, benchmark or evaluate the Model, and used to
107
+ prepare data for training or evaluation, if any. This includes any accompanying
108
+ documentation, tutorials, examples, etc, if any. "Distribution" means any transmission,
109
+ reproduction, publication or other sharing of the Model or Derivatives of the Model to a
110
+ third party, including providing the Model as a hosted service made available by
111
+ electronic or other remote means - e.g. API-based or web access. "Licensor" means the
112
+ copyright owner or entity authorized by the copyright owner that is granting the
113
+ License, including the persons or entities that may have rights in the Model and/or
114
+ distributing the Model. "You" (or "Your") means an individual or Legal Entity exercising
115
+ permissions granted by this License and/or making use of the Model for whichever purpose
116
+ and in any field of use, including usage of the Model in an end-use application - e.g.
117
+ chatbot, translator, image generator. "Third Parties" means individuals or legal
118
+ entities that are not under common control with Licensor or You. "Contribution" means
119
+ any work of authorship, including the original version of the Model and any
120
+ modifications or additions to that Model or Derivatives of the Model thereof, that is
121
+ intentionally submitted to Licensor for inclusion in the Model by the copyright owner or
122
+ by an individual or Legal Entity authorized to submit on behalf of the copyright owner.
123
+ For the purposes of this definition, "submitted" means any form of electronic, verbal,
124
+ or written communication sent to the Licensor or its representatives, including but not
125
+ limited to communication on electronic mailing lists, source code control systems, and
126
+ issue tracking systems that are managed by, or on behalf of, the Licensor for the
127
+ purpose of discussing and improving the Model, but excluding communication that is
128
+ conspicuously marked or otherwise designated in writing by the copyright owner as "Not a
129
+ Contribution." "Contributor" means Licensor and any individual or Legal Entity on behalf
130
+ of whom a Contribution has been received by Licensor and subsequently incorporated
131
+ within the Model.
132
+
133
+ Section II: INTELLECTUAL PROPERTY RIGHTS Both copyright and patent grants apply to the
134
+ Model, Derivatives of the Model and Complementary Material. The Model and Derivatives of
135
+ the Model are subject to additional terms as described in
136
+
137
+ Section III. Grant of Copyright License. Subject to the terms and conditions of this
138
+ License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
139
+ no-charge, royalty-free, irrevocable copyright license to reproduce, prepare, publicly
140
+ display, publicly perform, sublicense, and distribute the Complementary Material, the
141
+ Model, and Derivatives of the Model. Grant of Patent License. Subject to the terms and
142
+ conditions of this License and where and as applicable, each Contributor hereby grants
143
+ to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
144
+ (except as stated in this paragraph) patent license to make, have made, use, offer to
145
+ sell, sell, import, and otherwise transfer the Model and the Complementary Material,
146
+ where such license applies only to those patent claims licensable by such Contributor
147
+ that are necessarily infringed by their Contribution(s) alone or by combination of their
148
+ Contribution(s) with the Model to which such Contribution(s) was submitted. If You
149
+ institute patent litigation against any entity (including a cross-claim or counterclaim
150
+ in a lawsuit) alleging that the Model and/or Complementary Material or a Contribution
151
+ incorporated within the Model and/or Complementary Material constitutes direct or
152
+ contributory patent infringement, then any patent licenses granted to You under this
153
+ License for the Model and/or Work shall terminate as of the date such litigation is
154
+ asserted or filed. Section III: CONDITIONS OF USAGE, DISTRIBUTION AND REDISTRIBUTION
155
+ Distribution and Redistribution. You may host for Third Party remote access purposes
156
+ (e.g. software-as-a-service), reproduce and distribute copies of the Model or
157
+ Derivatives of the Model thereof in any medium, with or without modifications, provided
158
+ that You meet the following conditions: Use-based restrictions as referenced in
159
+ paragraph 5 MUST be included as an enforceable provision by You in any type of legal
160
+ agreement (e.g. a license) governing the use and/or distribution of the Model or
161
+ Derivatives of the Model, and You shall give notice to subsequent users You Distribute
162
+ to, that the Model or Derivatives of the Model are subject to paragraph 5. This
163
+ provision does not apply to the use of Complementary Material. You must give any Third
164
+ Party recipients of the Model or Derivatives of the Model a copy of this License; You
165
+ must cause any modified files to carry prominent notices stating that You changed the
166
+ files; You must retain all copyright, patent, trademark, and attribution notices
167
+ excluding those notices that do not pertain to any part of the Model, Derivatives of the
168
+ Model. You may add Your own copyright statement to Your modifications and may provide
169
+ additional or different license terms and conditions - respecting paragraph 4.a. - for
170
+ use, reproduction, or Distribution of Your modifications, or for any such Derivatives of
171
+ the Model as a whole, provided Your use, reproduction, and Distribution of the Model
172
+ otherwise complies with the conditions stated in this License. Use-based restrictions.
173
+ The restrictions set forth in Attachment A are considered Use-based restrictions.
174
+ Therefore You cannot use the Model and the Derivatives of the Model for the specified
175
+ restricted uses. You may use the Model subject to this License, including only for
176
+ lawful purposes and in accordance with the License. Use may include creating any content
177
+ with, finetuning, updating, running, training, evaluating and/or reparametrizing the
178
+ Model. You shall require all of Your users who use the Model or a Derivative of the
179
+ Model to comply with the terms of this paragraph (paragraph 5). The Output You Generate.
180
+ Except as set forth herein, Licensor claims no rights in the Output You generate using
181
+ the Model. You are accountable for the Output you generate and its subsequent uses. No
182
+ use of the output can contravene any provision as stated in the License.
183
+
184
+ Section IV: OTHER PROVISIONS Updates and Runtime Restrictions. To the maximum extent
185
+ permitted by law, Licensor reserves the right to restrict (remotely or otherwise) usage
186
+ of the Model in violation of this License. Trademarks and related. Nothing in this
187
+ License permits You to make use of Licensors’ trademarks, trade names, logos or to
188
+ otherwise suggest endorsement or misrepresent the relationship between the parties; and
189
+ any rights not expressly granted herein are reserved by the Licensors. Disclaimer of
190
+ Warranty. Unless required by applicable law or agreed to in writing, Licensor provides
191
+ the Model and the Complementary Material (and each Contributor provides its
192
+ Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
193
+ express or implied, including, without limitation, any warranties or conditions of
194
+ TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
195
+ solely responsible for determining the appropriateness of using or redistributing the
196
+ Model, Derivatives of the Model, and the Complementary Material and assume any risks
197
+ associated with Your exercise of permissions under this License. Limitation of
198
+ Liability. In no event and under no legal theory, whether in tort (including
199
+ negligence), contract, or otherwise, unless required by applicable law (such as
200
+ deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be
201
+ liable to You for damages, including any direct, indirect, special, incidental, or
202
+ consequential damages of any character arising as a result of this License or out of the
203
+ use or inability to use the Model and the Complementary Material (including but not
204
+ limited to damages for loss of goodwill, work stoppage, computer failure or malfunction,
205
+ or any and all other commercial damages or losses), even if such Contributor has been
206
+ advised of the possibility of such damages. Accepting Warranty or Additional Liability.
207
+ While redistributing the Model, Derivatives of the Model and the Complementary Material
208
+ thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty,
209
+ indemnity, or other liability obligations and/or rights consistent with this License.
210
+ However, in accepting such obligations, You may act only on Your own behalf and on Your
211
+ sole responsibility, not on behalf of any other Contributor, and only if You agree to
212
+ indemnify, defend, and hold each Contributor harmless for any liability incurred by, or
213
+ claims asserted against, such Contributor by reason of your accepting any such warranty
214
+ or additional liability. If any provision of this License is held to be invalid, illegal
215
+ or unenforceable, the remaining provisions shall be unaffected thereby and remain valid
216
+ as if such provision had not been set forth herein.
217
+
218
+ END OF TERMS AND CONDITIONS
219
+
220
+ Attachment A Use Restrictions
221
+ You agree not to use the Model or Derivatives of the Model:
222
+ In any way that violates any applicable national, federal, state, local or
223
+ international law or regulation; For the purpose of exploiting, harming or attempting to
224
+ exploit or harm minors in any way; To generate or disseminate verifiably false
225
+ information and/or content with the purpose of harming others; To generate or
226
+ disseminate personal identifiable information that can be used to harm an individual; To
227
+ defame, disparage or otherwise harass others; For fully automated decision making that
228
+ adversely impacts an individual’s legal rights or otherwise creates or modifies a
229
+ binding, enforceable obligation; For any use intended to or which has the effect of
230
+ discriminating against or harming individuals or groups based on online or offline
231
+ social behavior or known or predicted personal or personality characteristics; To
232
+ exploit any of the vulnerabilities of a specific group of persons based on their age,
233
+ social, physical or mental characteristics, in order to materially distort the behavior
234
+ of a person pertaining to that group in a manner that causes or is likely to cause that
235
+ person or another person physical or psychological harm; For any use intended to or
236
+ which has the effect of discriminating against individuals or groups based on legally
237
+ protected characteristics or categories; To provide medical advice and medical results
238
+ interpretation; To generate or disseminate information for the purpose to be used for
239
+ administration of justice, law enforcement, immigration or asylum processes, such as
240
+ predicting an individual will commit fraud/crime commitment (e.g. by text profiling,
241
+ drawing causal relationships between assertions made in documents, indiscriminate and
242
+ arbitrarily-targeted use).
README.md CHANGED
@@ -1,12 +1,13 @@
1
  ---
2
- title: FaceAI
3
- emoji: 💻
4
- colorFrom: yellow
5
  colorTo: red
6
  sdk: gradio
7
- sdk_version: 4.26.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: PhotoMaker Style
3
+ emoji: 📷 🎨
4
+ colorFrom: indigo
5
  colorTo: red
6
  sdk: gradio
7
+ sdk_version: 4.14.0
8
  app_file: app.py
9
  pinned: false
10
+ license: apache-2.0
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,298 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ import random
4
+ import os
5
+
6
+ from diffusers.utils import load_image
7
+ from diffusers import EulerAncestralDiscreteScheduler
8
+
9
+ from huggingface_hub import hf_hub_download
10
+ import spaces
11
+ import gradio as gr
12
+
13
+ from pipeline import PhotoMakerStableDiffusionXLPipeline
14
+ from style_template import styles
15
+
16
+ # Download civitai models
17
+ civitai_model_path = "./civitai_models"
18
+ os.makedirs(civitai_model_path, exist_ok=True)
19
+ base_model_name = "sdxlUnstableDiffusers_v11.safetensors"
20
+ base_model_path = os.path.join(civitai_model_path, base_model_name)
21
+ if not os.path.exists(base_model_path):
22
+ base_model_path = hf_hub_download(repo_id="Paper99/sdxlUnstableDiffusers_v11", filename=base_model_name, repo_type="model")
23
+
24
+ lora_model_name = "xl_more_art-full.safetensors"
25
+ lora_path = os.path.join(civitai_model_path, lora_model_name)
26
+ if not os.path.exists(lora_path):
27
+ lora_path = hf_hub_download(repo_id="Paper99/sdxlUnstableDiffusers_v11", filename=lora_model_name, repo_type="model")
28
+
29
+ # global variable
30
+ device = "cuda" if torch.cuda.is_available() else "cpu"
31
+ MAX_SEED = np.iinfo(np.int32).max
32
+ STYLE_NAMES = list(styles.keys())
33
+ DEFAULT_STYLE_NAME = "(No style)"
34
+
35
+ # download PhotoMaker checkpoint to cache
36
+ photomaker_ckpt = hf_hub_download(repo_id="TencentARC/PhotoMaker", filename="photomaker-v1.bin", repo_type="model")
37
+
38
+ pipe = PhotoMakerStableDiffusionXLPipeline.from_single_file(
39
+ base_model_path,
40
+ torch_dtype=torch.bfloat16,
41
+ original_config_file=None,
42
+ ).to(device)
43
+
44
+ pipe.load_photomaker_adapter(
45
+ os.path.dirname(photomaker_ckpt),
46
+ subfolder="",
47
+ weight_name=os.path.basename(photomaker_ckpt),
48
+ trigger_word="img"
49
+ )
50
+ pipe.id_encoder.to(device)
51
+
52
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
53
+ pipe.load_lora_weights(os.path.dirname(lora_path), weight_name=lora_model_name, adapter_name="xl_more_art-full")
54
+ pipe.set_adapters(["photomaker", "xl_more_art-full"], adapter_weights=[1.0, 0.5])
55
+ pipe.fuse_lora()
56
+
57
+ @spaces.GPU
58
+ def generate_image(upload_images, prompt, negative_prompt, style_name, num_steps, style_strength_ratio, num_outputs, guidance_scale, seed, progress=gr.Progress(track_tqdm=True)):
59
+ # check the trigger word
60
+ image_token_id = pipe.tokenizer.convert_tokens_to_ids(pipe.trigger_word)
61
+ input_ids = pipe.tokenizer.encode(prompt)
62
+ if image_token_id not in input_ids:
63
+ raise gr.Error(f"Cannot find the trigger word '{pipe.trigger_word}' in text prompt! Please refer to step 2️⃣")
64
+
65
+ if input_ids.count(image_token_id) > 1:
66
+ raise gr.Error(f"Cannot use multiple trigger words '{pipe.trigger_word}' in text prompt!")
67
+
68
+ # apply the style template
69
+ prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
70
+
71
+ if upload_images is None:
72
+ raise gr.Error(f"Cannot find any input face image! Please refer to step 1️⃣")
73
+
74
+ input_id_images = []
75
+ for img in upload_images:
76
+ input_id_images.append(load_image(img))
77
+
78
+ generator = torch.Generator(device=device).manual_seed(seed)
79
+
80
+ print("Start inference...")
81
+ print(f"[Debug] Prompt: {prompt}, \n[Debug] Neg Prompt: {negative_prompt}")
82
+ start_merge_step = int(float(style_strength_ratio) / 100 * num_steps)
83
+ if start_merge_step > 30:
84
+ start_merge_step = 30
85
+ print(start_merge_step)
86
+ images = pipe(
87
+ prompt=prompt,
88
+ input_id_images=input_id_images,
89
+ negative_prompt=negative_prompt,
90
+ num_images_per_prompt=num_outputs,
91
+ num_inference_steps=num_steps,
92
+ start_merge_step=start_merge_step,
93
+ generator=generator,
94
+ guidance_scale=guidance_scale,
95
+ ).images
96
+ return images, gr.update(visible=True)
97
+
98
+ def swap_to_gallery(images):
99
+ return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
100
+
101
+ def upload_example_to_gallery(images, prompt, style, negative_prompt):
102
+ return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
103
+
104
+ def remove_back_to_files():
105
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
106
+
107
+ def remove_tips():
108
+ return gr.update(visible=False)
109
+
110
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
111
+ if randomize_seed:
112
+ seed = random.randint(0, MAX_SEED)
113
+ return seed
114
+
115
+ def apply_style(style_name: str, positive: str, negative: str = "") -> tuple[str, str]:
116
+ p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
117
+ return p.replace("{prompt}", positive), n + ' ' + negative
118
+
119
+ def get_image_path_list(folder_name):
120
+ image_basename_list = os.listdir(folder_name)
121
+ image_path_list = sorted([os.path.join(folder_name, basename) for basename in image_basename_list])
122
+ return image_path_list
123
+
124
+ def get_example():
125
+ case = [
126
+ [
127
+ get_image_path_list('./examples/yangmi_woman'),
128
+ "a woman img, retro futurism, retro game art style but extremely beautiful, intricate details, masterpiece, best quality, space-themed, cosmic, celestial, stars, galaxies, nebulas, planets, science fiction, highly detailed",
129
+ 35,
130
+ "realistic, photo-realistic, worst quality, greyscale, bad anatomy, bad hands, error, text",
131
+ ],
132
+ [
133
+ get_image_path_list('./examples/lenna_woman'),
134
+ "A girl img riding dragon over a whimsical castle, 3d CGI, art by Pixar, half-body, screenshot from animation",
135
+ 20,
136
+ "realistic, photo-realistic, bad quality, bad anatomy, worst quality, low quality, lowres, extra fingers, blur, blurry, ugly, wrong proportions, watermark, image artifacts, bad eyes",
137
+ ],
138
+ ]
139
+ return case
140
+
141
+ ### Description and style
142
+ logo = r"""
143
+ <center><img src='https://photo-maker.github.io/assets/logo.png' alt='PhotoMaker logo' style="width:80px; margin-bottom:10px"></center>
144
+ """
145
+ title = r"""
146
+ <h1 align="center">PhotoMaker: Customizing Realistic Human Photos via Stacked ID Embedding</h1>
147
+ <h3 align="center">-- Stylization version --</h3>
148
+ """
149
+
150
+ description = r"""
151
+ <b>Official 🤗 Gradio demo</b> for <a href='https://github.com/TencentARC/PhotoMaker' target='_blank'><b>PhotoMaker: Customizing Realistic Human Photos via Stacked ID Embedding</b></a>.<br>
152
+ <br>
153
+ For photo-realistic generation, you could use our other gradio demo [PhotoMaker](https://huggingface.co/spaces/TencentARC/PhotoMaker).
154
+ <br>
155
+ ❗️❗️❗️[<b>Important</b>] Personalization steps:<br>
156
+ 1️⃣ Upload images of someone you want to customize. One image is ok, but more is better. Although we do not perform face detection, the face in the uploaded image should <b>occupy the majority of the image</b>.<br>
157
+ 2️⃣ Enter a text prompt, making sure to <b>follow the class word</b> you want to customize with the <b>trigger word</b>: `img`, such as: `man img` or `woman img` or `girl img`.<br>
158
+ 3️⃣ Choose your preferred style template.<br>
159
+ 4️⃣ Click the <b>Submit</b> button to start customizing.
160
+ """
161
+
162
+ article = r"""
163
+
164
+ If PhotoMaker is helpful, please help to ⭐ the <a href='https://github.com/TencentARC/PhotoMaker' target='_blank'>Github Repo</a>. Thanks!
165
+ [![GitHub Stars](https://img.shields.io/github/stars/TencentARC/PhotoMaker?style=social)](https://github.com/TencentARC/PhotoMaker)
166
+ ---
167
+ 📝 **Citation**
168
+ <br>
169
+ If our work is useful for your research, please consider citing:
170
+
171
+ ```bibtex
172
+ @inproceedings{li2023photomaker,
173
+ title={PhotoMaker: Customizing Realistic Human Photos via Stacked ID Embedding},
174
+ author={Li, Zhen and Cao, Mingdeng and Wang, Xintao and Qi, Zhongang and Cheng, Ming-Ming and Shan, Ying},
175
+ booktitle={IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
176
+ year={2024}
177
+ }
178
+ ```
179
+ 📋 **License**
180
+ <br>
181
+ Apache-2.0 LICENSE. Please refer to the [LICENSE file](https://huggingface.co/TencentARC/PhotoMaker/blob/main/LICENSE) for details.
182
+
183
+ 📧 **Contact**
184
+ <br>
185
+ If you have any questions, please feel free to reach me out at <b>zhenli1031@gmail.com</b>.
186
+ """
187
+
188
+ tips = r"""
189
+ ### Usage tips of PhotoMaker
190
+ 1. Upload more photos of the person to be customized to **improve ID fidelty**. If the input is Asian face(s), maybe consider adding 'asian' before the class word, e.g., `asian woman img`
191
+ 2. When stylizing, does the generated face look too realistic? Adjust the **Style strength** to 30-50, the larger the number, the less ID fidelty, but the stylization ability will be better.
192
+ 3. If you want to generate realistic photos, you could try switching to our other gradio application [PhotoMaker](https://huggingface.co/spaces/TencentARC/PhotoMaker).
193
+ 4. For **faster** speed, reduce the number of generated images and sampling steps. However, please note that reducing the sampling steps may compromise the ID fidelity.
194
+ """
195
+ # 3. Don't make the prompt too long, as we will trim it if it exceeds 77 tokens. But we will fix it in the future.
196
+
197
+ css = '''
198
+ .gradio-container {width: 85% !important}
199
+ '''
200
+ with gr.Blocks(css=css) as demo:
201
+ gr.Markdown(logo)
202
+ gr.Markdown(title)
203
+ gr.Markdown(description)
204
+ # gr.DuplicateButton(
205
+ # value="Duplicate Space for private use ",
206
+ # elem_id="duplicate-button",
207
+ # visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
208
+ # )
209
+ with gr.Row():
210
+ with gr.Column():
211
+ files = gr.Files(
212
+ label="Drag (Select) 1 or more photos of your face",
213
+ file_types=["image"]
214
+ )
215
+ uploaded_files = gr.Gallery(label="Your images", visible=False, columns=5, rows=1, height=200)
216
+ with gr.Column(visible=False) as clear_button:
217
+ remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
218
+ prompt = gr.Textbox(label="Prompt",
219
+ info="Try something like 'a photo of a man/woman img', 'img' is the trigger word.",
220
+ placeholder="A photo of a [man/woman img]...")
221
+ style = gr.Dropdown(label="Style template", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME)
222
+ submit = gr.Button("Submit")
223
+
224
+ with gr.Accordion(open=False, label="Advanced Options"):
225
+ negative_prompt = gr.Textbox(
226
+ label="Negative Prompt",
227
+ placeholder="low quality",
228
+ value="nsfw, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry",
229
+ )
230
+ num_steps = gr.Slider(
231
+ label="Number of sample steps",
232
+ minimum=20,
233
+ maximum=100,
234
+ step=1,
235
+ value=50,
236
+ )
237
+ style_strength_ratio = gr.Slider(
238
+ label="Style strength (%)",
239
+ minimum=15,
240
+ maximum=50,
241
+ step=1,
242
+ value=20,
243
+ )
244
+ num_outputs = gr.Slider(
245
+ label="Number of output images",
246
+ minimum=1,
247
+ maximum=4,
248
+ step=1,
249
+ value=2,
250
+ )
251
+ guidance_scale = gr.Slider(
252
+ label="Guidance scale",
253
+ minimum=0.1,
254
+ maximum=10.0,
255
+ step=0.1,
256
+ value=5,
257
+ )
258
+ seed = gr.Slider(
259
+ label="Seed",
260
+ minimum=0,
261
+ maximum=MAX_SEED,
262
+ step=1,
263
+ value=0,
264
+ )
265
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
266
+ with gr.Column():
267
+ gallery = gr.Gallery(label="Generated Images")
268
+ usage_tips = gr.Markdown(label="Usage tips of PhotoMaker", value=tips ,visible=False)
269
+
270
+ files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
271
+ remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])
272
+
273
+ submit.click(
274
+ fn=remove_tips,
275
+ outputs=usage_tips,
276
+ ).then(
277
+ fn=randomize_seed_fn,
278
+ inputs=[seed, randomize_seed],
279
+ outputs=seed,
280
+ queue=False,
281
+ api_name=False,
282
+ ).then(
283
+ fn=generate_image,
284
+ inputs=[files, prompt, negative_prompt, style, num_steps, style_strength_ratio, num_outputs, guidance_scale, seed],
285
+ outputs=[gallery, usage_tips]
286
+ )
287
+
288
+ gr.Examples(
289
+ examples=get_example(),
290
+ inputs=[files, prompt, style_strength_ratio, negative_prompt],
291
+ run_on_click=True,
292
+ fn=upload_example_to_gallery,
293
+ outputs=[uploaded_files, clear_button, files],
294
+ )
295
+
296
+ gr.Markdown(article)
297
+
298
+ demo.launch()
gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
model.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Merge image encoder and fuse module to create a ID Encoder
2
+ # send multiple ID images, we can directly obtain the updated text encoder containing a stacked ID embedding
3
+
4
+ import torch
5
+ import torch.nn as nn
6
+ from transformers.models.clip.modeling_clip import CLIPVisionModelWithProjection
7
+ from transformers.models.clip.configuration_clip import CLIPVisionConfig
8
+ from transformers import PretrainedConfig
9
+
10
+ VISION_CONFIG_DICT = {
11
+ "hidden_size": 1024,
12
+ "intermediate_size": 4096,
13
+ "num_attention_heads": 16,
14
+ "num_hidden_layers": 24,
15
+ "patch_size": 14,
16
+ "projection_dim": 768
17
+ }
18
+
19
+ class MLP(nn.Module):
20
+ def __init__(self, in_dim, out_dim, hidden_dim, use_residual=True):
21
+ super().__init__()
22
+ if use_residual:
23
+ assert in_dim == out_dim
24
+ self.layernorm = nn.LayerNorm(in_dim)
25
+ self.fc1 = nn.Linear(in_dim, hidden_dim)
26
+ self.fc2 = nn.Linear(hidden_dim, out_dim)
27
+ self.use_residual = use_residual
28
+ self.act_fn = nn.GELU()
29
+
30
+ def forward(self, x):
31
+ residual = x
32
+ x = self.layernorm(x)
33
+ x = self.fc1(x)
34
+ x = self.act_fn(x)
35
+ x = self.fc2(x)
36
+ if self.use_residual:
37
+ x = x + residual
38
+ return x
39
+
40
+
41
+ class FuseModule(nn.Module):
42
+ def __init__(self, embed_dim):
43
+ super().__init__()
44
+ self.mlp1 = MLP(embed_dim * 2, embed_dim, embed_dim, use_residual=False)
45
+ self.mlp2 = MLP(embed_dim, embed_dim, embed_dim, use_residual=True)
46
+ self.layer_norm = nn.LayerNorm(embed_dim)
47
+
48
+ def fuse_fn(self, prompt_embeds, id_embeds):
49
+ stacked_id_embeds = torch.cat([prompt_embeds, id_embeds], dim=-1)
50
+ stacked_id_embeds = self.mlp1(stacked_id_embeds) + prompt_embeds
51
+ stacked_id_embeds = self.mlp2(stacked_id_embeds)
52
+ stacked_id_embeds = self.layer_norm(stacked_id_embeds)
53
+ return stacked_id_embeds
54
+
55
+ def forward(
56
+ self,
57
+ prompt_embeds,
58
+ id_embeds,
59
+ class_tokens_mask,
60
+ ) -> torch.Tensor:
61
+ # id_embeds shape: [b, max_num_inputs, 1, 2048]
62
+ id_embeds = id_embeds.to(prompt_embeds.dtype)
63
+ num_inputs = class_tokens_mask.sum().unsqueeze(0) # TODO: check for training case
64
+ batch_size, max_num_inputs = id_embeds.shape[:2]
65
+ # seq_length: 77
66
+ seq_length = prompt_embeds.shape[1]
67
+ # flat_id_embeds shape: [b*max_num_inputs, 1, 2048]
68
+ flat_id_embeds = id_embeds.view(
69
+ -1, id_embeds.shape[-2], id_embeds.shape[-1]
70
+ )
71
+ # valid_id_mask [b*max_num_inputs]
72
+ valid_id_mask = (
73
+ torch.arange(max_num_inputs, device=flat_id_embeds.device)[None, :]
74
+ < num_inputs[:, None]
75
+ )
76
+ valid_id_embeds = flat_id_embeds[valid_id_mask.flatten()]
77
+
78
+ prompt_embeds = prompt_embeds.view(-1, prompt_embeds.shape[-1])
79
+ class_tokens_mask = class_tokens_mask.view(-1)
80
+ valid_id_embeds = valid_id_embeds.view(-1, valid_id_embeds.shape[-1])
81
+ # slice out the image token embeddings
82
+ image_token_embeds = prompt_embeds[class_tokens_mask]
83
+ stacked_id_embeds = self.fuse_fn(image_token_embeds, valid_id_embeds)
84
+ assert class_tokens_mask.sum() == stacked_id_embeds.shape[0], f"{class_tokens_mask.sum()} != {stacked_id_embeds.shape[0]}"
85
+ prompt_embeds.masked_scatter_(class_tokens_mask[:, None], stacked_id_embeds.to(prompt_embeds.dtype))
86
+ updated_prompt_embeds = prompt_embeds.view(batch_size, seq_length, -1)
87
+ return updated_prompt_embeds
88
+
89
+ class PhotoMakerIDEncoder(CLIPVisionModelWithProjection):
90
+ def __init__(self):
91
+ super().__init__(CLIPVisionConfig(**VISION_CONFIG_DICT))
92
+ self.visual_projection_2 = nn.Linear(1024, 1280, bias=False)
93
+ self.fuse_module = FuseModule(2048)
94
+
95
+ def forward(self, id_pixel_values, prompt_embeds, class_tokens_mask):
96
+ b, num_inputs, c, h, w = id_pixel_values.shape
97
+ id_pixel_values = id_pixel_values.view(b * num_inputs, c, h, w)
98
+
99
+ shared_id_embeds = self.vision_model(id_pixel_values)[1]
100
+ id_embeds = self.visual_projection(shared_id_embeds)
101
+ id_embeds_2 = self.visual_projection_2(shared_id_embeds)
102
+
103
+ id_embeds = id_embeds.view(b, num_inputs, 1, -1)
104
+ id_embeds_2 = id_embeds_2.view(b, num_inputs, 1, -1)
105
+
106
+ id_embeds = torch.cat((id_embeds, id_embeds_2), dim=-1)
107
+ updated_prompt_embeds = self.fuse_module(prompt_embeds, id_embeds, class_tokens_mask)
108
+
109
+ return updated_prompt_embeds
110
+
111
+
112
+ if __name__ == "__main__":
113
+ PhotoMakerIDEncoder()
pipeline.py ADDED
@@ -0,0 +1,471 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Callable, Dict, List, Optional, Union, Tuple
2
+ from collections import OrderedDict
3
+ import os
4
+ import PIL
5
+ import numpy as np
6
+
7
+ import torch
8
+ from torchvision import transforms as T
9
+
10
+ from safetensors import safe_open
11
+ from huggingface_hub.utils import validate_hf_hub_args
12
+ from transformers import CLIPImageProcessor, CLIPTokenizer
13
+ from diffusers import StableDiffusionXLPipeline
14
+ from diffusers.pipelines.stable_diffusion_xl import StableDiffusionXLPipelineOutput
15
+ from diffusers.utils import (
16
+ _get_model_file,
17
+ is_transformers_available,
18
+ logging,
19
+ )
20
+
21
+ from model import PhotoMakerIDEncoder
22
+
23
+ PipelineImageInput = Union[
24
+ PIL.Image.Image,
25
+ torch.FloatTensor,
26
+ List[PIL.Image.Image],
27
+ List[torch.FloatTensor],
28
+ ]
29
+
30
+
31
+ class PhotoMakerStableDiffusionXLPipeline(StableDiffusionXLPipeline):
32
+ @validate_hf_hub_args
33
+ def load_photomaker_adapter(
34
+ self,
35
+ pretrained_model_name_or_path_or_dict: Union[str, Dict[str, torch.Tensor]],
36
+ weight_name: str,
37
+ subfolder: str = '',
38
+ trigger_word: str = 'img',
39
+ **kwargs,
40
+ ):
41
+ """
42
+ #TODO
43
+ Parameters:
44
+ pretrained_model_name_or_path_or_dict (`str` or `os.PathLike` or `dict`):
45
+ Can be either:
46
+
47
+ - A string, the *model id* (for example `google/ddpm-celebahq-256`) of a pretrained model hosted on
48
+ the Hub.
49
+ - A path to a *directory* (for example `./my_model_directory`) containing the model weights saved
50
+ with [`ModelMixin.save_pretrained`].
51
+ - A [torch state
52
+ dict](https://pytorch.org/tutorials/beginner/saving_loading_models.html#what-is-a-state-dict).
53
+
54
+ weight_name (`str`):
55
+ The subfolder location of a model file within a larger model repository on the Hub or locally.
56
+
57
+ subfolder (`str`, defaults to `""`):
58
+ The subfolder location of a model file within a larger model repository on the Hub or locally.
59
+
60
+ trigger_word (`str`, *optional*, defaults to `"img"`):
61
+ The subfolder location of a model file within a larger model repository on the Hub or locally.
62
+ """
63
+
64
+ # Load the main state dict first.
65
+ cache_dir = kwargs.pop("cache_dir", None)
66
+ force_download = kwargs.pop("force_download", False)
67
+ resume_download = kwargs.pop("resume_download", False)
68
+ proxies = kwargs.pop("proxies", None)
69
+ local_files_only = kwargs.pop("local_files_only", None)
70
+ token = kwargs.pop("token", None)
71
+ revision = kwargs.pop("revision", None)
72
+
73
+ user_agent = {
74
+ "file_type": "attn_procs_weights",
75
+ "framework": "pytorch",
76
+ }
77
+
78
+ if not isinstance(pretrained_model_name_or_path_or_dict, dict):
79
+ model_file = _get_model_file(
80
+ pretrained_model_name_or_path_or_dict,
81
+ weights_name=weight_name,
82
+ cache_dir=cache_dir,
83
+ force_download=force_download,
84
+ resume_download=resume_download,
85
+ proxies=proxies,
86
+ local_files_only=local_files_only,
87
+ token=token,
88
+ revision=revision,
89
+ subfolder=subfolder,
90
+ user_agent=user_agent,
91
+ )
92
+ if weight_name.endswith(".safetensors"):
93
+ state_dict = {"id_encoder": {}, "lora_weights": {}}
94
+ with safe_open(model_file, framework="pt", device="cpu") as f:
95
+ for key in f.keys():
96
+ if key.startswith("id_encoder."):
97
+ state_dict["id_encoder"][key.replace("id_encoder.", "")] = f.get_tensor(key)
98
+ elif key.startswith("lora_weights."):
99
+ state_dict["lora_weights"][key.replace("lora_weights.", "")] = f.get_tensor(key)
100
+ else:
101
+ state_dict = torch.load(model_file, map_location="cpu")
102
+ else:
103
+ state_dict = pretrained_model_name_or_path_or_dict
104
+
105
+ keys = list(state_dict.keys())
106
+ if keys != ["id_encoder", "lora_weights"]:
107
+ raise ValueError("Required keys are (`id_encoder` and `lora_weights`) missing from the state dict.")
108
+
109
+ self.trigger_word = trigger_word
110
+ # load finetuned CLIP image encoder and fuse module here if it has not been registered to the pipeline yet
111
+ print(f"Loading PhotoMaker components [1] id_encoder from [{pretrained_model_name_or_path_or_dict}]...")
112
+ id_encoder = PhotoMakerIDEncoder()
113
+ id_encoder.load_state_dict(state_dict["id_encoder"], strict=True)
114
+ id_encoder = id_encoder.to(self.device, dtype=self.unet.dtype)
115
+ self.id_encoder = id_encoder
116
+ self.id_image_processor = CLIPImageProcessor()
117
+
118
+ # load lora into models
119
+ print(f"Loading PhotoMaker components [2] lora_weights from [{pretrained_model_name_or_path_or_dict}]")
120
+ self.load_lora_weights(state_dict["lora_weights"], adapter_name="photomaker")
121
+
122
+ # Add trigger word token
123
+ if self.tokenizer is not None:
124
+ self.tokenizer.add_tokens([self.trigger_word], special_tokens=True)
125
+
126
+ self.tokenizer_2.add_tokens([self.trigger_word], special_tokens=True)
127
+
128
+
129
+ def encode_prompt_with_trigger_word(
130
+ self,
131
+ prompt: str,
132
+ prompt_2: Optional[str] = None,
133
+ num_id_images: int = 1,
134
+ device: Optional[torch.device] = None,
135
+ prompt_embeds: Optional[torch.FloatTensor] = None,
136
+ pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
137
+ class_tokens_mask: Optional[torch.LongTensor] = None,
138
+ ):
139
+ device = device or self._execution_device
140
+
141
+ if prompt is not None and isinstance(prompt, str):
142
+ batch_size = 1
143
+ elif prompt is not None and isinstance(prompt, list):
144
+ batch_size = len(prompt)
145
+ else:
146
+ batch_size = prompt_embeds.shape[0]
147
+
148
+ # Find the token id of the trigger word
149
+ image_token_id = self.tokenizer_2.convert_tokens_to_ids(self.trigger_word)
150
+
151
+ # Define tokenizers and text encoders
152
+ tokenizers = [self.tokenizer, self.tokenizer_2] if self.tokenizer is not None else [self.tokenizer_2]
153
+ text_encoders = (
154
+ [self.text_encoder, self.text_encoder_2] if self.text_encoder is not None else [self.text_encoder_2]
155
+ )
156
+
157
+ if prompt_embeds is None:
158
+ prompt_2 = prompt_2 or prompt
159
+ prompt_embeds_list = []
160
+ prompts = [prompt, prompt_2]
161
+ for prompt, tokenizer, text_encoder in zip(prompts, tokenizers, text_encoders):
162
+ input_ids = tokenizer.encode(prompt) # TODO: batch encode
163
+ clean_index = 0
164
+ clean_input_ids = []
165
+ class_token_index = []
166
+ # Find out the corrresponding class word token based on the newly added trigger word token
167
+ for i, token_id in enumerate(input_ids):
168
+ if token_id == image_token_id:
169
+ class_token_index.append(clean_index - 1)
170
+ else:
171
+ clean_input_ids.append(token_id)
172
+ clean_index += 1
173
+
174
+ if len(class_token_index) != 1:
175
+ raise ValueError(
176
+ f"PhotoMaker currently does not support multiple trigger words in a single prompt.\
177
+ Trigger word: {self.trigger_word}, Prompt: {prompt}."
178
+ )
179
+ class_token_index = class_token_index[0]
180
+
181
+ # Expand the class word token and corresponding mask
182
+ class_token = clean_input_ids[class_token_index]
183
+ clean_input_ids = clean_input_ids[:class_token_index] + [class_token] * num_id_images + \
184
+ clean_input_ids[class_token_index+1:]
185
+
186
+ # Truncation or padding
187
+ max_len = tokenizer.model_max_length
188
+ if len(clean_input_ids) > max_len:
189
+ clean_input_ids = clean_input_ids[:max_len]
190
+ else:
191
+ clean_input_ids = clean_input_ids + [tokenizer.pad_token_id] * (
192
+ max_len - len(clean_input_ids)
193
+ )
194
+
195
+ class_tokens_mask = [True if class_token_index <= i < class_token_index+num_id_images else False \
196
+ for i in range(len(clean_input_ids))]
197
+
198
+ clean_input_ids = torch.tensor(clean_input_ids, dtype=torch.long).unsqueeze(0)
199
+ class_tokens_mask = torch.tensor(class_tokens_mask, dtype=torch.bool).unsqueeze(0)
200
+
201
+ prompt_embeds = text_encoder(
202
+ clean_input_ids.to(device),
203
+ output_hidden_states=True,
204
+ )
205
+
206
+ # We are only ALWAYS interested in the pooled output of the final text encoder
207
+ pooled_prompt_embeds = prompt_embeds[0]
208
+ prompt_embeds = prompt_embeds.hidden_states[-2]
209
+ prompt_embeds_list.append(prompt_embeds)
210
+
211
+ prompt_embeds = torch.concat(prompt_embeds_list, dim=-1)
212
+
213
+ prompt_embeds = prompt_embeds.to(dtype=self.text_encoder_2.dtype, device=device)
214
+ class_tokens_mask = class_tokens_mask.to(device=device) # TODO: ignoring two-prompt case
215
+
216
+ return prompt_embeds, pooled_prompt_embeds, class_tokens_mask
217
+
218
+
219
+ @torch.no_grad()
220
+ def __call__(
221
+ self,
222
+ prompt: Union[str, List[str]] = None,
223
+ prompt_2: Optional[Union[str, List[str]]] = None,
224
+ height: Optional[int] = None,
225
+ width: Optional[int] = None,
226
+ num_inference_steps: int = 50,
227
+ denoising_end: Optional[float] = None,
228
+ guidance_scale: float = 5.0,
229
+ negative_prompt: Optional[Union[str, List[str]]] = None,
230
+ negative_prompt_2: Optional[Union[str, List[str]]] = None,
231
+ num_images_per_prompt: Optional[int] = 1,
232
+ eta: float = 0.0,
233
+ generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
234
+ latents: Optional[torch.FloatTensor] = None,
235
+ prompt_embeds: Optional[torch.FloatTensor] = None,
236
+ negative_prompt_embeds: Optional[torch.FloatTensor] = None,
237
+ pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
238
+ negative_pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
239
+ output_type: Optional[str] = "pil",
240
+ return_dict: bool = True,
241
+ cross_attention_kwargs: Optional[Dict[str, Any]] = None,
242
+ guidance_rescale: float = 0.0,
243
+ original_size: Optional[Tuple[int, int]] = None,
244
+ crops_coords_top_left: Tuple[int, int] = (0, 0),
245
+ target_size: Optional[Tuple[int, int]] = None,
246
+ callback: Optional[Callable[[int, int, torch.FloatTensor], None]] = None,
247
+ callback_steps: int = 1,
248
+ # Added parameters (for PhotoMaker)
249
+ input_id_images: PipelineImageInput = None,
250
+ class_tokens_mask: Optional[torch.LongTensor] = None,
251
+ prompt_embeds_text_only: Optional[torch.FloatTensor] = None,
252
+ pooled_prompt_embeds_text_only: Optional[torch.FloatTensor] = None,
253
+ start_merge_step: int = 0,
254
+ ):
255
+ # TODO: doc
256
+ # 0. Default height and width to unet
257
+ height = height or self.unet.config.sample_size * self.vae_scale_factor
258
+ width = width or self.unet.config.sample_size * self.vae_scale_factor
259
+
260
+ original_size = original_size or (height, width)
261
+ target_size = target_size or (height, width)
262
+
263
+ # 1. Check inputs. Raise error if not correct
264
+ self.check_inputs(
265
+ prompt,
266
+ prompt_2,
267
+ height,
268
+ width,
269
+ callback_steps,
270
+ negative_prompt,
271
+ negative_prompt_2,
272
+ prompt_embeds,
273
+ negative_prompt_embeds,
274
+ pooled_prompt_embeds,
275
+ negative_pooled_prompt_embeds,
276
+ )
277
+ #
278
+ if prompt_embeds is not None and class_tokens_mask is None:
279
+ raise ValueError(
280
+ "If `prompt_embeds` are provided, `class_tokens_mask` also have to be passed. Make sure to generate `class_tokens_mask` from the same tokenizer that was used to generate `prompt_embeds`."
281
+ )
282
+ # check the input id images
283
+ if input_id_images is None:
284
+ raise ValueError(
285
+ "Provide `input_id_images`. Cannot leave `input_id_images` undefined for PhotoMaker pipeline."
286
+ )
287
+ if not isinstance(input_id_images, list):
288
+ input_id_images = [input_id_images]
289
+
290
+ # 2. Define call parameters
291
+ if prompt is not None and isinstance(prompt, str):
292
+ batch_size = 1
293
+ elif prompt is not None and isinstance(prompt, list):
294
+ batch_size = len(prompt)
295
+ else:
296
+ batch_size = prompt_embeds.shape[0]
297
+
298
+ device = self._execution_device
299
+
300
+ # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
301
+ # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
302
+ # corresponds to doing no classifier free guidance.
303
+ do_classifier_free_guidance = guidance_scale > 1.0
304
+
305
+ assert do_classifier_free_guidance
306
+
307
+ # 3. Encode input prompt
308
+ num_id_images = len(input_id_images)
309
+
310
+ (
311
+ prompt_embeds,
312
+ pooled_prompt_embeds,
313
+ class_tokens_mask,
314
+ ) = self.encode_prompt_with_trigger_word(
315
+ prompt=prompt,
316
+ prompt_2=prompt_2,
317
+ device=device,
318
+ num_id_images=num_id_images,
319
+ prompt_embeds=prompt_embeds,
320
+ pooled_prompt_embeds=pooled_prompt_embeds,
321
+ class_tokens_mask=class_tokens_mask,
322
+ )
323
+
324
+ # 4. Encode input prompt without the trigger word for delayed conditioning
325
+ prompt_text_only = prompt.replace(" "+self.trigger_word, "") # sensitive to white space
326
+ (
327
+ prompt_embeds_text_only,
328
+ negative_prompt_embeds,
329
+ pooled_prompt_embeds_text_only, # TODO: replace the pooled_prompt_embeds with text only prompt
330
+ negative_pooled_prompt_embeds,
331
+ ) = self.encode_prompt(
332
+ prompt=prompt_text_only,
333
+ prompt_2=prompt_2,
334
+ device=device,
335
+ num_images_per_prompt=num_images_per_prompt,
336
+ do_classifier_free_guidance=do_classifier_free_guidance,
337
+ negative_prompt=negative_prompt,
338
+ negative_prompt_2=negative_prompt_2,
339
+ prompt_embeds=prompt_embeds_text_only,
340
+ negative_prompt_embeds=negative_prompt_embeds,
341
+ pooled_prompt_embeds=pooled_prompt_embeds_text_only,
342
+ negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
343
+ )
344
+
345
+ # 5. Prepare the input ID images
346
+ dtype = next(self.id_encoder.parameters()).dtype
347
+ if not isinstance(input_id_images[0], torch.Tensor):
348
+ id_pixel_values = self.id_image_processor(input_id_images, return_tensors="pt").pixel_values
349
+
350
+ id_pixel_values = id_pixel_values.unsqueeze(0).to(device=device, dtype=dtype) # TODO: multiple prompts
351
+
352
+ # 6. Get the update text embedding with the stacked ID embedding
353
+ prompt_embeds = self.id_encoder(id_pixel_values, prompt_embeds, class_tokens_mask)
354
+
355
+ bs_embed, seq_len, _ = prompt_embeds.shape
356
+ # duplicate text embeddings for each generation per prompt, using mps friendly method
357
+ prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1)
358
+ prompt_embeds = prompt_embeds.view(bs_embed * num_images_per_prompt, seq_len, -1)
359
+ pooled_prompt_embeds = pooled_prompt_embeds.repeat(1, num_images_per_prompt).view(
360
+ bs_embed * num_images_per_prompt, -1
361
+ )
362
+
363
+ # 7. Prepare timesteps
364
+ self.scheduler.set_timesteps(num_inference_steps, device=device)
365
+ timesteps = self.scheduler.timesteps
366
+
367
+ # 8. Prepare latent variables
368
+ num_channels_latents = self.unet.config.in_channels
369
+ latents = self.prepare_latents(
370
+ batch_size * num_images_per_prompt,
371
+ num_channels_latents,
372
+ height,
373
+ width,
374
+ prompt_embeds.dtype,
375
+ device,
376
+ generator,
377
+ latents,
378
+ )
379
+
380
+ # 9. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
381
+ extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta)
382
+
383
+ # 10. Prepare added time ids & embeddings
384
+ if self.text_encoder_2 is None:
385
+ text_encoder_projection_dim = int(pooled_prompt_embeds.shape[-1])
386
+ else:
387
+ text_encoder_projection_dim = self.text_encoder_2.config.projection_dim
388
+
389
+ add_time_ids = self._get_add_time_ids(
390
+ original_size,
391
+ crops_coords_top_left,
392
+ target_size,
393
+ dtype=prompt_embeds.dtype,
394
+ text_encoder_projection_dim=text_encoder_projection_dim,
395
+ )
396
+ add_time_ids = torch.cat([add_time_ids, add_time_ids], dim=0)
397
+ add_time_ids = add_time_ids.to(device).repeat(batch_size * num_images_per_prompt, 1)
398
+
399
+ # 11. Denoising loop
400
+ num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order
401
+ with self.progress_bar(total=num_inference_steps) as progress_bar:
402
+ for i, t in enumerate(timesteps):
403
+ latent_model_input = (
404
+ torch.cat([latents] * 2) if do_classifier_free_guidance else latents
405
+ )
406
+ latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
407
+
408
+ if i <= start_merge_step:
409
+ current_prompt_embeds = torch.cat(
410
+ [negative_prompt_embeds, prompt_embeds_text_only], dim=0
411
+ )
412
+ add_text_embeds = torch.cat([negative_pooled_prompt_embeds, pooled_prompt_embeds_text_only], dim=0)
413
+ else:
414
+ current_prompt_embeds = torch.cat(
415
+ [negative_prompt_embeds, prompt_embeds], dim=0
416
+ )
417
+ add_text_embeds = torch.cat([negative_pooled_prompt_embeds, pooled_prompt_embeds], dim=0)
418
+ # predict the noise residual
419
+ added_cond_kwargs = {"text_embeds": add_text_embeds, "time_ids": add_time_ids}
420
+ noise_pred = self.unet(
421
+ latent_model_input,
422
+ t,
423
+ encoder_hidden_states=current_prompt_embeds,
424
+ cross_attention_kwargs=cross_attention_kwargs,
425
+ added_cond_kwargs=added_cond_kwargs,
426
+ return_dict=False,
427
+ )[0]
428
+
429
+ # perform guidance
430
+ if do_classifier_free_guidance:
431
+ noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
432
+ noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
433
+
434
+ if do_classifier_free_guidance and guidance_rescale > 0.0:
435
+ # Based on 3.4. in https://arxiv.org/pdf/2305.08891.pdf
436
+ noise_pred = rescale_noise_cfg(noise_pred, noise_pred_text, guidance_rescale=guidance_rescale)
437
+
438
+ # compute the previous noisy sample x_t -> x_t-1
439
+ latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
440
+
441
+ # call the callback, if provided
442
+ if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
443
+ progress_bar.update()
444
+ if callback is not None and i % callback_steps == 0:
445
+ callback(i, t, latents)
446
+
447
+ # make sure the VAE is in float32 mode, as it overflows in float16
448
+ if self.vae.dtype == torch.float16 and self.vae.config.force_upcast:
449
+ self.upcast_vae()
450
+ latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)
451
+
452
+ if not output_type == "latent":
453
+ image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]
454
+ else:
455
+ image = latents
456
+ return StableDiffusionXLPipelineOutput(images=image)
457
+
458
+ # apply watermark if available
459
+ # if self.watermark is not None:
460
+ # image = self.watermark.apply_watermark(image)
461
+
462
+ image = self.image_processor.postprocess(image, output_type=output_type)
463
+
464
+ # Offload last model to CPU
465
+ if hasattr(self, "final_offload_hook") and self.final_offload_hook is not None:
466
+ self.final_offload_hook.offload()
467
+
468
+ if not return_dict:
469
+ return (image,)
470
+
471
+ return StableDiffusionXLPipelineOutput(images=image)
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ diffusers==0.25.0
2
+ torch==2.0.0
3
+ torchvision==0.15.1
4
+ transformers==4.36.2
5
+ accelerate
6
+ safetensors
7
+ einops
8
+ onnxruntime-gpu
9
+ spaces==0.19.4
10
+ omegaconf
11
+ peft
12
+ huggingface-hub==0.20.2
style_template.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ style_list = [
2
+ {
3
+ "name": "(No style)",
4
+ "prompt": "{prompt}",
5
+ "negative_prompt": "",
6
+ },
7
+ {
8
+ "name": "Cinematic",
9
+ "prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
10
+ "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
11
+ },
12
+ {
13
+ "name": "Disney Charactor",
14
+ "prompt": "A Pixar animation character of {prompt} . pixar-style, studio anime, Disney, high-quality",
15
+ "negative_prompt": "lowres, bad anatomy, bad hands, text, bad eyes, bad arms, bad legs, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, blurry, grayscale, noisy, sloppy, messy, grainy, highly detailed, ultra textured, photo",
16
+ },
17
+ {
18
+ "name": "Digital Art",
19
+ "prompt": "concept art {prompt} . digital artwork, illustrative, painterly, matte painting, highly detailed",
20
+ "negative_prompt": "photo, photorealistic, realism, ugly",
21
+ },
22
+ {
23
+ "name": "Photographic (Default)",
24
+ "prompt": "cinematic photo {prompt} . 35mm photograph, film, bokeh, professional, 4k, highly detailed",
25
+ "negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
26
+ },
27
+ {
28
+ "name": "Fantasy art",
29
+ "prompt": "ethereal fantasy concept art of {prompt} . magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy",
30
+ "negative_prompt": "photographic, realistic, realism, 35mm film, dslr, cropped, frame, text, deformed, glitch, noise, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, sloppy, duplicate, mutated, black and white",
31
+ },
32
+ {
33
+ "name": "Neonpunk",
34
+ "prompt": "neonpunk style {prompt} . cyberpunk, vaporwave, neon, vibes, vibrant, stunningly beautiful, crisp, detailed, sleek, ultramodern, magenta highlights, dark purple shadows, high contrast, cinematic, ultra detailed, intricate, professional",
35
+ "negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured",
36
+ },
37
+ {
38
+ "name": "Enhance",
39
+ "prompt": "breathtaking {prompt} . award-winning, professional, highly detailed",
40
+ "negative_prompt": "ugly, deformed, noisy, blurry, distorted, grainy",
41
+ },
42
+ {
43
+ "name": "Comic book",
44
+ "prompt": "comic {prompt} . graphic illustration, comic art, graphic novel art, vibrant, highly detailed",
45
+ "negative_prompt": "photograph, deformed, glitch, noisy, realistic, stock photo",
46
+ },
47
+ {
48
+ "name": "Lowpoly",
49
+ "prompt": "low-poly style {prompt} . low-poly game art, polygon mesh, jagged, blocky, wireframe edges, centered composition",
50
+ "negative_prompt": "noisy, sloppy, messy, grainy, highly detailed, ultra textured, photo",
51
+ },
52
+ {
53
+ "name": "Line art",
54
+ "prompt": "line art drawing {prompt} . professional, sleek, modern, minimalist, graphic, line art, vector graphics",
55
+ "negative_prompt": "anime, photorealistic, 35mm film, deformed, glitch, blurry, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, mutated, realism, realistic, impressionism, expressionism, oil, acrylic",
56
+ }
57
+ ]
58
+
59
+ styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}