ruslanmv commited on
Commit
d2a27fd
·
1 Parent(s): 7f1fa19

First commit

Browse files
Files changed (9) hide show
  1. .gitignore +162 -0
  2. .pre-commit-config.yaml +36 -0
  3. .style.yapf +5 -0
  4. .vscode/settings.json +18 -0
  5. LICENSE +21 -0
  6. README.md +9 -4
  7. app.py +144 -0
  8. requirements.txt +7 -0
  9. style.css +16 -0
.gitignore ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio_cached_examples/
2
+
3
+ # Byte-compiled / optimized / DLL files
4
+ __pycache__/
5
+ *.py[cod]
6
+ *$py.class
7
+
8
+ # C extensions
9
+ *.so
10
+
11
+ # Distribution / packaging
12
+ .Python
13
+ build/
14
+ develop-eggs/
15
+ dist/
16
+ downloads/
17
+ eggs/
18
+ .eggs/
19
+ lib/
20
+ lib64/
21
+ parts/
22
+ sdist/
23
+ var/
24
+ wheels/
25
+ share/python-wheels/
26
+ *.egg-info/
27
+ .installed.cfg
28
+ *.egg
29
+ MANIFEST
30
+
31
+ # PyInstaller
32
+ # Usually these files are written by a python script from a template
33
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
34
+ *.manifest
35
+ *.spec
36
+
37
+ # Installer logs
38
+ pip-log.txt
39
+ pip-delete-this-directory.txt
40
+
41
+ # Unit test / coverage reports
42
+ htmlcov/
43
+ .tox/
44
+ .nox/
45
+ .coverage
46
+ .coverage.*
47
+ .cache
48
+ nosetests.xml
49
+ coverage.xml
50
+ *.cover
51
+ *.py,cover
52
+ .hypothesis/
53
+ .pytest_cache/
54
+ cover/
55
+
56
+ # Translations
57
+ *.mo
58
+ *.pot
59
+
60
+ # Django stuff:
61
+ *.log
62
+ local_settings.py
63
+ db.sqlite3
64
+ db.sqlite3-journal
65
+
66
+ # Flask stuff:
67
+ instance/
68
+ .webassets-cache
69
+
70
+ # Scrapy stuff:
71
+ .scrapy
72
+
73
+ # Sphinx documentation
74
+ docs/_build/
75
+
76
+ # PyBuilder
77
+ .pybuilder/
78
+ target/
79
+
80
+ # Jupyter Notebook
81
+ .ipynb_checkpoints
82
+
83
+ # IPython
84
+ profile_default/
85
+ ipython_config.py
86
+
87
+ # pyenv
88
+ # For a library or package, you might want to ignore these files since the code is
89
+ # intended to run in multiple environments; otherwise, check them in:
90
+ # .python-version
91
+
92
+ # pipenv
93
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
94
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
95
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
96
+ # install all needed dependencies.
97
+ #Pipfile.lock
98
+
99
+ # poetry
100
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
101
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
102
+ # commonly ignored for libraries.
103
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
104
+ #poetry.lock
105
+
106
+ # pdm
107
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
108
+ #pdm.lock
109
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
110
+ # in version control.
111
+ # https://pdm.fming.dev/#use-with-ide
112
+ .pdm.toml
113
+
114
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
115
+ __pypackages__/
116
+
117
+ # Celery stuff
118
+ celerybeat-schedule
119
+ celerybeat.pid
120
+
121
+ # SageMath parsed files
122
+ *.sage.py
123
+
124
+ # Environments
125
+ .env
126
+ .venv
127
+ env/
128
+ venv/
129
+ ENV/
130
+ env.bak/
131
+ venv.bak/
132
+
133
+ # Spyder project settings
134
+ .spyderproject
135
+ .spyproject
136
+
137
+ # Rope project settings
138
+ .ropeproject
139
+
140
+ # mkdocs documentation
141
+ /site
142
+
143
+ # mypy
144
+ .mypy_cache/
145
+ .dmypy.json
146
+ dmypy.json
147
+
148
+ # Pyre type checker
149
+ .pyre/
150
+
151
+ # pytype static type analyzer
152
+ .pytype/
153
+
154
+ # Cython debug symbols
155
+ cython_debug/
156
+
157
+ # PyCharm
158
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
159
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
161
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
162
+ #.idea/
.pre-commit-config.yaml ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.2.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: double-quote-string-fixer
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.4
19
+ hooks:
20
+ - id: docformatter
21
+ args: ['--in-place']
22
+ - repo: https://github.com/pycqa/isort
23
+ rev: 5.12.0
24
+ hooks:
25
+ - id: isort
26
+ - repo: https://github.com/pre-commit/mirrors-mypy
27
+ rev: v0.991
28
+ hooks:
29
+ - id: mypy
30
+ args: ['--ignore-missing-imports']
31
+ additional_dependencies: ['types-python-slugify']
32
+ - repo: https://github.com/google/yapf
33
+ rev: v0.32.0
34
+ hooks:
35
+ - id: yapf
36
+ args: ['--parallel', '--in-place']
.style.yapf ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
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
.vscode/settings.json ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "python.linting.enabled": true,
3
+ "python.linting.flake8Enabled": true,
4
+ "python.linting.pylintEnabled": false,
5
+ "python.linting.lintOnSave": true,
6
+ "python.formatting.provider": "yapf",
7
+ "python.formatting.yapfArgs": [
8
+ "--style={based_on_style: pep8, indent_width: 4, blank_line_before_nested_class_or_def: false, spaces_before_comment: 2, split_before_logical_operator: true}"
9
+ ],
10
+ "[python]": {
11
+ "editor.formatOnType": true,
12
+ "editor.codeActionsOnSave": {
13
+ "source.organizeImports": true
14
+ }
15
+ },
16
+ "editor.formatOnSave": true,
17
+ "files.insertFinalNewline": true
18
+ }
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
@@ -1,12 +1,17 @@
1
  ---
2
  title: Ai Fast Image Server
3
- emoji: 😻
4
- colorFrom: purple
5
- colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 4.15.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: Ai Fast Image Server
3
+ emoji: 🌍
4
+ colorFrom: gray
5
+ colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 3.39.0
8
  app_file: app.py
9
+ license: mit
10
  pinned: false
11
+ suggested_hardware: a10g-small
12
+ duplicated_from: hysts/SD-XL
13
+ load_balancing_strategy: random
14
  ---
15
 
16
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
17
+
app.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #!/usr/bin/env python
3
+
4
+ import os
5
+ import gradio as gr
6
+ import numpy as np
7
+ import PIL
8
+ import base64
9
+ import io
10
+ import torch
11
+
12
+ # SSD-1B
13
+ #from diffusers import LCMScheduler, AutoPipelineForText2Image
14
+
15
+ # SDXL
16
+ from diffusers import UNet2DConditionModel, DiffusionPipeline, LCMScheduler
17
+
18
+ MAX_SEED = np.iinfo(np.int32).max
19
+ MAX_IMAGE_SIZE = int(os.getenv('MAX_IMAGE_SIZE', '1024'))
20
+ SECRET_TOKEN = os.getenv('SECRET_TOKEN', 'default_secret')
21
+
22
+ #device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
23
+ if torch.cuda.is_available():
24
+
25
+ #pipe = AutoPipelineForText2Image.from_pretrained("segmind/SSD-1B", torch_dtype=torch.float16, variant="fp16")
26
+ #pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
27
+ #pipe.to("cuda")
28
+
29
+ # load and fuse
30
+ #pipe.load_lora_weights("latent-consistency/lcm-lora-ssd-1b")
31
+ #pipe.fuse_lora()
32
+
33
+ unet = UNet2DConditionModel.from_pretrained("latent-consistency/lcm-sdxl", torch_dtype=torch.float16, variant="fp16")
34
+ pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", unet=unet, torch_dtype=torch.float16, variant="fp16")
35
+
36
+ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
37
+ pipe.to('cuda')
38
+
39
+ else:
40
+ pipe = None
41
+
42
+ def generate(prompt: str,
43
+ negative_prompt: str = '',
44
+ seed: int = 0,
45
+ width: int = 1024,
46
+ height: int = 1024,
47
+ guidance_scale: float = 0.0,
48
+ num_inference_steps: int = 4,
49
+ secret_token: str = '') -> PIL.Image.Image:
50
+ if secret_token != SECRET_TOKEN:
51
+ raise gr.Error(
52
+ f'Invalid secret token. Please fork the original space if you want to use it for yourself.')
53
+
54
+ generator = torch.Generator().manual_seed(seed)
55
+
56
+ image = pipe(prompt=prompt,
57
+ negative_prompt=negative_prompt,
58
+ width=width,
59
+ height=height,
60
+ guidance_scale=guidance_scale,
61
+ num_inference_steps=num_inference_steps,
62
+ generator=generator,
63
+ output_type='pil').images[0]
64
+
65
+ return image
66
+
67
+ with gr.Blocks() as demo:
68
+ gr.HTML("""
69
+ <div style="z-index: 100; position: fixed; top: 0px; right: 0px; left: 0px; bottom: 0px; width: 100%; height: 100%; background: white; display: flex; align-items: center; justify-content: center; color: black;">
70
+ <div style="text-align: center; color: black;">
71
+ <p style="color: black;">This space is a REST API to programmatically generate images using LCM LoRA SSD-1B.</p>
72
+ <p style="color: black;">It is not meant to be directly used through a user interface, but using code and an access key.</p>
73
+ </div>
74
+ </div>""")
75
+ secret_token = gr.Text(
76
+ label='Secret Token',
77
+ max_lines=1,
78
+ placeholder='Enter your secret token',
79
+ )
80
+ prompt = gr.Text(
81
+ label='Prompt',
82
+ show_label=False,
83
+ max_lines=1,
84
+ placeholder='Enter your prompt',
85
+ container=False,
86
+ )
87
+ result = gr.Image(label='Result', show_label=False)
88
+ negative_prompt = gr.Text(
89
+ label='Negative prompt',
90
+ max_lines=1,
91
+ placeholder='Enter a negative prompt',
92
+ visible=True,
93
+ )
94
+ seed = gr.Slider(label='Seed',
95
+ minimum=0,
96
+ maximum=MAX_SEED,
97
+ step=1,
98
+ value=0)
99
+
100
+ width = gr.Slider(
101
+ label='Width',
102
+ minimum=256,
103
+ maximum=MAX_IMAGE_SIZE,
104
+ step=32,
105
+ value=1024,
106
+ )
107
+ height = gr.Slider(
108
+ label='Height',
109
+ minimum=256,
110
+ maximum=MAX_IMAGE_SIZE,
111
+ step=32,
112
+ value=1024,
113
+ )
114
+ guidance_scale = gr.Slider(
115
+ label='Guidance scale',
116
+ minimum=0,
117
+ maximum=2,
118
+ step=0.1,
119
+ value=0.0)
120
+ num_inference_steps = gr.Slider(
121
+ label='Number of inference steps',
122
+ minimum=1,
123
+ maximum=8,
124
+ step=1,
125
+ value=4)
126
+
127
+ inputs = [
128
+ prompt,
129
+ negative_prompt,
130
+ seed,
131
+ width,
132
+ height,
133
+ guidance_scale,
134
+ num_inference_steps,
135
+ secret_token,
136
+ ]
137
+ prompt.submit(
138
+ fn=generate,
139
+ inputs=inputs,
140
+ outputs=result,
141
+ api_name='run',
142
+ )
143
+
144
+ demo.queue(max_size=32).launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ accelerate==0.24.1
2
+ diffusers==0.23.0
3
+ gradio==3.39.0
4
+ invisible-watermark==0.2.0
5
+ Pillow==10.1.0
6
+ torch==2.1.0
7
+ transformers==4.35.0
style.css ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ }
4
+
5
+ #duplicate-button {
6
+ margin: auto;
7
+ color: #fff;
8
+ background: #1565c0;
9
+ border-radius: 100vh;
10
+ }
11
+
12
+ #component-0 {
13
+ max-width: 730px;
14
+ margin: auto;
15
+ padding-top: 1.5rem;
16
+ }