Spaces:
Running
Running
Add files
Browse files- .pre-commit-config.yaml +55 -0
- .vscode/settings.json +26 -0
- LICENSE +21 -0
- README.md +1 -0
- app.py +169 -0
- requirements.txt +7 -0
- style.css +16 -0
.pre-commit-config.yaml
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
repos:
|
2 |
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
3 |
+
rev: v4.5.0
|
4 |
+
hooks:
|
5 |
+
- id: check-executables-have-shebangs
|
6 |
+
- id: check-json
|
7 |
+
- id: check-merge-conflict
|
8 |
+
- id: check-shebang-scripts-are-executable
|
9 |
+
- id: check-toml
|
10 |
+
- id: check-yaml
|
11 |
+
- id: end-of-file-fixer
|
12 |
+
- id: mixed-line-ending
|
13 |
+
args: ["--fix=lf"]
|
14 |
+
- id: requirements-txt-fixer
|
15 |
+
- id: trailing-whitespace
|
16 |
+
- repo: https://github.com/myint/docformatter
|
17 |
+
rev: v1.7.5
|
18 |
+
hooks:
|
19 |
+
- id: docformatter
|
20 |
+
args: ["--in-place"]
|
21 |
+
- repo: https://github.com/pycqa/isort
|
22 |
+
rev: 5.12.0
|
23 |
+
hooks:
|
24 |
+
- id: isort
|
25 |
+
args: ["--profile", "black"]
|
26 |
+
- repo: https://github.com/pre-commit/mirrors-mypy
|
27 |
+
rev: v1.7.1
|
28 |
+
hooks:
|
29 |
+
- id: mypy
|
30 |
+
args: ["--ignore-missing-imports"]
|
31 |
+
additional_dependencies:
|
32 |
+
["types-python-slugify", "types-requests", "types-PyYAML"]
|
33 |
+
- repo: https://github.com/psf/black
|
34 |
+
rev: 23.11.0
|
35 |
+
hooks:
|
36 |
+
- id: black
|
37 |
+
language_version: python3.10
|
38 |
+
args: ["--line-length", "119"]
|
39 |
+
- repo: https://github.com/kynan/nbstripout
|
40 |
+
rev: 0.6.1
|
41 |
+
hooks:
|
42 |
+
- id: nbstripout
|
43 |
+
args:
|
44 |
+
[
|
45 |
+
"--extra-keys",
|
46 |
+
"metadata.interpreter metadata.kernelspec cell.metadata.pycharm",
|
47 |
+
]
|
48 |
+
- repo: https://github.com/nbQA-dev/nbQA
|
49 |
+
rev: 1.7.1
|
50 |
+
hooks:
|
51 |
+
- id: nbqa-black
|
52 |
+
- id: nbqa-pyupgrade
|
53 |
+
args: ["--py37-plus"]
|
54 |
+
- id: nbqa-isort
|
55 |
+
args: ["--float-to-top"]
|
.vscode/settings.json
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"editor.formatOnSave": true,
|
3 |
+
"files.insertFinalNewline": false,
|
4 |
+
"[python]": {
|
5 |
+
"editor.defaultFormatter": "ms-python.black-formatter",
|
6 |
+
"editor.formatOnType": true,
|
7 |
+
"editor.codeActionsOnSave": {
|
8 |
+
"source.organizeImports": true
|
9 |
+
}
|
10 |
+
},
|
11 |
+
"[jupyter]": {
|
12 |
+
"files.insertFinalNewline": false
|
13 |
+
},
|
14 |
+
"black-formatter.args": [
|
15 |
+
"--line-length=119"
|
16 |
+
],
|
17 |
+
"isort.args": ["--profile", "black"],
|
18 |
+
"flake8.args": [
|
19 |
+
"--max-line-length=119"
|
20 |
+
],
|
21 |
+
"ruff.lint.args": [
|
22 |
+
"--line-length=119"
|
23 |
+
],
|
24 |
+
"notebook.output.scrolling": true,
|
25 |
+
"notebook.formatOnCellExecution": true
|
26 |
+
}
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 hysts
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -8,6 +8,7 @@ sdk_version: 4.8.0
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
|
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
11 |
+
suggested_hardware: a10g-large
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
from __future__ import annotations
|
4 |
+
|
5 |
+
import os
|
6 |
+
import random
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
import numpy as np
|
10 |
+
import PIL.Image
|
11 |
+
import spaces
|
12 |
+
import torch
|
13 |
+
from diffusers import AutoPipelineForText2Image
|
14 |
+
|
15 |
+
DESCRIPTION = "# Kandinsky 3"
|
16 |
+
if not torch.cuda.is_available():
|
17 |
+
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
|
18 |
+
|
19 |
+
MAX_SEED = np.iinfo(np.int32).max
|
20 |
+
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES") == "1"
|
21 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1024"))
|
22 |
+
MAX_NUM_INFERENCE_STEPS = int(os.getenv("MAX_NUM_INFERENCE_STEPS", "1024"))
|
23 |
+
|
24 |
+
if torch.cuda.is_available():
|
25 |
+
pipe = AutoPipelineForText2Image.from_pretrained(
|
26 |
+
"kandinsky-community/kandinsky-3", variant="fp16", torch_dtype=torch.float16
|
27 |
+
)
|
28 |
+
pipe.enable_model_cpu_offload()
|
29 |
+
|
30 |
+
|
31 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
32 |
+
if randomize_seed:
|
33 |
+
seed = random.randint(0, MAX_SEED)
|
34 |
+
return seed
|
35 |
+
|
36 |
+
|
37 |
+
@spaces.GPU
|
38 |
+
def generate(
|
39 |
+
prompt: str,
|
40 |
+
negative_prompt: str = "",
|
41 |
+
seed: int = 0,
|
42 |
+
width: int = 1024,
|
43 |
+
height: int = 1024,
|
44 |
+
guidance_scale: float = 3.0,
|
45 |
+
num_inference_steps: int = 25,
|
46 |
+
progress=gr.Progress(track_tqdm=True),
|
47 |
+
) -> PIL.Image.Image:
|
48 |
+
if num_inference_steps > MAX_NUM_INFERENCE_STEPS:
|
49 |
+
raise ValueError(f"Number of inference steps must be less than or equal to {MAX_NUM_INFERENCE_STEPS}")
|
50 |
+
if width > MAX_IMAGE_SIZE or height > MAX_IMAGE_SIZE:
|
51 |
+
raise ValueError(f"Image width and height must be less than or equal to {MAX_IMAGE_SIZE}")
|
52 |
+
|
53 |
+
generator = torch.Generator().manual_seed(seed)
|
54 |
+
return pipe(
|
55 |
+
prompt,
|
56 |
+
negative_prompt=negative_prompt,
|
57 |
+
num_inference_steps=num_inference_steps,
|
58 |
+
guidance_scale=guidance_scale,
|
59 |
+
generator=generator,
|
60 |
+
width=width,
|
61 |
+
height=height,
|
62 |
+
).images[0]
|
63 |
+
|
64 |
+
|
65 |
+
examples = [
|
66 |
+
"A photograph of the inside of a subway train. There are raccoons sitting on the seats. One of them is reading a newspaper. The window shows the city in the background.",
|
67 |
+
"A beautiful landscape outdoors scene in the crochet knitting art style, drawing in style by Alfons Mucha",
|
68 |
+
"gorgeous phoenix, cosmic, darkness, epic, cinematic, moonlight, stars, high - definition, texture,Oscar-Claude Monet",
|
69 |
+
"a yellow house at the edge of the danish fjord, in the style of eiko ojala, ingrid baars, ad posters, mountainous vistas, george ault, realistic details, dark white and dark gray, 4k",
|
70 |
+
"dragon fruit head, upper body, realistic, illustration by Joshua Hoffine Norman Rockwell, scary, creepy, biohacking, futurism, Zaha Hadid style",
|
71 |
+
"Amazing playful nice cute strawberry character, dynamic poze, surreal fantazy garden background, gorgeous masterpice, award winning photo, soft natural lighting, 3d, Blender, Octane render, tilt - shift, deep field, colorful, I can't believe how beautiful this is, colorful, cute and sweet baby - loved photo",
|
72 |
+
"beautiful fairy-tale desert, in the sky a wave of sand merges with the milky way, stars, cosmism, digital art, 8k",
|
73 |
+
"Car, mustang, movie, person, poster, car cover, person, in the style of alessandro gottardo, gold and cyan, gerald harvey jones, reflections, highly detailed illustrations, industrial urban scenes",
|
74 |
+
"cloud in blue sky, a red lip, collage art, shuji terayama, dreamy objects, surreal, criterion collection, showa era, intricate details, mirror",
|
75 |
+
]
|
76 |
+
|
77 |
+
with gr.Blocks(css="style.css") as demo:
|
78 |
+
gr.Markdown(DESCRIPTION)
|
79 |
+
gr.DuplicateButton(
|
80 |
+
value="Duplicate Space for private use",
|
81 |
+
elem_id="duplicate-button",
|
82 |
+
visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
|
83 |
+
)
|
84 |
+
with gr.Group():
|
85 |
+
with gr.Row():
|
86 |
+
prompt = gr.Text(
|
87 |
+
label="Prompt",
|
88 |
+
show_label=False,
|
89 |
+
max_lines=1,
|
90 |
+
placeholder="Enter your prompt",
|
91 |
+
container=False,
|
92 |
+
)
|
93 |
+
run_button = gr.Button("Run", scale=0)
|
94 |
+
result = gr.Image(label="Result", show_label=False)
|
95 |
+
with gr.Accordion("Advanced options", open=False):
|
96 |
+
negative_prompt = gr.Text(
|
97 |
+
label="Negative prompt",
|
98 |
+
max_lines=1,
|
99 |
+
placeholder="Enter a negative prompt",
|
100 |
+
)
|
101 |
+
seed = gr.Slider(
|
102 |
+
label="Seed",
|
103 |
+
minimum=0,
|
104 |
+
maximum=MAX_SEED,
|
105 |
+
step=1,
|
106 |
+
value=0,
|
107 |
+
)
|
108 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
109 |
+
width = gr.Slider(
|
110 |
+
label="Width",
|
111 |
+
minimum=256,
|
112 |
+
maximum=MAX_IMAGE_SIZE,
|
113 |
+
step=32,
|
114 |
+
value=1024,
|
115 |
+
)
|
116 |
+
height = gr.Slider(
|
117 |
+
label="Height",
|
118 |
+
minimum=256,
|
119 |
+
maximum=MAX_IMAGE_SIZE,
|
120 |
+
step=32,
|
121 |
+
value=1024,
|
122 |
+
)
|
123 |
+
guidance_scale = gr.Slider(
|
124 |
+
label="Guidance scale",
|
125 |
+
minimum=1,
|
126 |
+
maximum=20,
|
127 |
+
step=0.1,
|
128 |
+
value=3.0,
|
129 |
+
)
|
130 |
+
num_inference_steps = gr.Slider(
|
131 |
+
label="Number of inference steps",
|
132 |
+
minimum=10,
|
133 |
+
maximum=MAX_NUM_INFERENCE_STEPS,
|
134 |
+
step=1,
|
135 |
+
value=25,
|
136 |
+
)
|
137 |
+
|
138 |
+
gr.Examples(
|
139 |
+
examples=examples,
|
140 |
+
inputs=prompt,
|
141 |
+
outputs=result,
|
142 |
+
fn=generate,
|
143 |
+
cache_examples=CACHE_EXAMPLES,
|
144 |
+
)
|
145 |
+
|
146 |
+
gr.on(
|
147 |
+
triggers=[prompt.submit, negative_prompt.submit, run_button.click],
|
148 |
+
fn=randomize_seed_fn,
|
149 |
+
inputs=[seed, randomize_seed],
|
150 |
+
outputs=seed,
|
151 |
+
queue=False,
|
152 |
+
api_name=False,
|
153 |
+
).then(
|
154 |
+
fn=generate,
|
155 |
+
inputs=[
|
156 |
+
prompt,
|
157 |
+
negative_prompt,
|
158 |
+
seed,
|
159 |
+
width,
|
160 |
+
height,
|
161 |
+
guidance_scale,
|
162 |
+
num_inference_steps,
|
163 |
+
],
|
164 |
+
outputs=result,
|
165 |
+
api_name="run",
|
166 |
+
)
|
167 |
+
|
168 |
+
if __name__ == "__main__":
|
169 |
+
demo.queue(max_size=20).launch()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate==0.25.0
|
2 |
+
diffusers==0.24.0
|
3 |
+
gradio==4.8.0
|
4 |
+
sentencepiece==0.1.99
|
5 |
+
spaces==0.18.0
|
6 |
+
torch==2.0.0
|
7 |
+
transformers==4.35.2
|
style.css
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
h1 {
|
2 |
+
text-align: center;
|
3 |
+
}
|
4 |
+
|
5 |
+
.contain {
|
6 |
+
max-width: 730px;
|
7 |
+
margin: auto;
|
8 |
+
padding-top: 1.5rem;
|
9 |
+
}
|
10 |
+
|
11 |
+
#duplicate-button {
|
12 |
+
margin: auto;
|
13 |
+
color: white;
|
14 |
+
background: #1565c0;
|
15 |
+
border-radius: 100vh;
|
16 |
+
}
|