fengn commited on
Commit
7f1014c
·
verified ·
1 Parent(s): 6137cc0

Upload folder using huggingface_hub

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. README.md +3 -9
  2. app.py +241 -0
  3. images/0118a123-f782-4065-bcc1-a1effb986124.png +0 -0
  4. images/01769f3d-18a0-423f-b3f6-a97af1c94bf2.png +0 -0
  5. images/01bb3133-e94e-4179-a3c4-b2456e72079b.png +0 -0
  6. images/020d25c7-e77c-42a6-bf0c-67189a68909f.png +0 -0
  7. images/02c34630-1cdf-400c-8125-e02441275657.png +0 -0
  8. images/065490e8-2a6b-4a7a-be86-ba5ebea130e3.png +0 -0
  9. images/0675c336-5d12-4adf-a000-cd41c14cb2ce.png +0 -0
  10. images/0808038d-ac56-4af9-aeea-fb7b7837385f.png +0 -0
  11. images/0a09dc96-247c-4d5c-9079-4b7d407f2fa5.png +0 -0
  12. images/0c8373bb-7af5-47f2-bdd8-0fe80533d46b.png +0 -0
  13. images/0f674c3d-76c3-470c-b9f2-88fd8bd795cd.png +0 -0
  14. images/11fd9c7a-636d-4405-b6a0-2bdd644e0f6b.png +0 -0
  15. images/132e6041-1db8-46ae-a34b-120aa3ea0b77.png +0 -0
  16. images/14e0f180-4f3e-4fc0-99f0-28866c900970.png +0 -0
  17. images/19fe3a25-ee3c-49d7-bd3f-cbd531d7ba60.png +0 -0
  18. images/1b238947-1de5-40d2-a0f2-7e020ebdd075.png +0 -0
  19. images/1d32843e-8406-4fe8-8199-ae9accf748ad.png +0 -0
  20. images/1d3b9603-1820-47a6-8c38-5fe321b36cc6.png +0 -0
  21. images/20915b3d-5168-4a42-ac77-52a442ca8998.png +0 -0
  22. images/20b48cb1-c939-4f19-b240-7ad23c7d5de5.png +0 -0
  23. images/2222b07a-0085-477c-b51b-f35edfd3c08d.png +0 -0
  24. images/23ea2bc1-2539-400b-b76c-f7c6e400ddca.png +0 -0
  25. images/29262f41-37c0-4e12-8eab-252b144afc22.png +0 -0
  26. images/2961c8d0-b781-49be-acfc-fbf2c8147301.png +0 -0
  27. images/2b30cbd0-9685-46b2-95fa-1cd901f538a5.png +0 -0
  28. images/3b80e697-c83a-4fa0-8d65-67b96a244335.png +0 -0
  29. images/3cd0779a-8b86-4fc7-86d4-7a69f3280077.png +0 -0
  30. images/3f234161-51c9-496a-b052-9932223b6d70.png +0 -0
  31. images/440004d2-1295-4601-aee0-21004d2f13e3.png +0 -0
  32. images/44e32e8c-6906-42b3-87cc-48c648e69bc9.png +0 -0
  33. images/48855e8d-eb5a-4965-b037-ebaa1e0c2a0e.png +0 -0
  34. images/503c7f64-c6bf-4f88-abfe-fbaeb0f0b8e4.png +0 -0
  35. images/5c0692b7-2b07-4c66-a85c-aa705ecf8eb3.png +0 -0
  36. images/5dc0bc85-cff9-4c8b-8102-18bfcd30b52b.png +0 -0
  37. images/5f88a975-82e5-4cb0-95bc-dbede0943bbf.png +0 -0
  38. images/612513d8-d317-40c6-9905-791244708670.png +0 -0
  39. images/638dadea-4f5b-434f-b431-9aa09b8d014e.png +0 -0
  40. images/64594ea6-b9ba-4939-a827-8ab10a6cff4a.png +0 -0
  41. images/66ff4bb9-f0a6-4d69-8e3e-c6126982d511.png +0 -0
  42. images/6772a638-9900-4376-a960-b191f6214bb9.png +0 -0
  43. images/6c15bb3a-3343-4cac-9c81-de8daaecebd3.png +0 -0
  44. images/71a3c480-89c2-4303-b59b-6e275e8f886f.png +0 -0
  45. images/7a9fb1df-8481-4630-ab54-43e3c5c3f984.png +0 -0
  46. images/7e80fe32-8c2d-4e56-aca5-a27ab517641f.png +0 -0
  47. images/85086f04-d11e-4f9f-8eb9-565f2b135d12.png +0 -0
  48. images/86ed98c5-d700-4c06-a5d3-7fb7143145be.png +0 -0
  49. images/88ddf880-800d-48af-9e54-2003632dec37.png +0 -0
  50. images/8a0b63d4-dbf1-4573-baa4-991cfa88a629.png +0 -0
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Local Gradio
3
- emoji: 📚
4
- colorFrom: indigo
5
- colorTo: green
6
- sdk: gradio
7
- sdk_version: 4.36.1
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: local_gradio
 
 
 
 
 
3
  app_file: app.py
4
+ sdk: gradio
5
+ sdk_version: 3.48.0
6
  ---
 
 
app.py ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import random
5
+ import time
6
+
7
+ import gradio as gr
8
+ import numpy as np
9
+ import PIL.Image
10
+ import torch
11
+ try:
12
+ import intel_extension_for_pytorch as ipex
13
+ except:
14
+ pass
15
+
16
+
17
+ from diffusers import DiffusionPipeline
18
+ import torch
19
+
20
+ import os
21
+ import torch
22
+ from tqdm import tqdm
23
+
24
+ from concurrent.futures import ThreadPoolExecutor
25
+ import uuid
26
+
27
+ DESCRIPTION = '''# Latent Consistency Model
28
+ Distilled from [Dreamshaper v7](https://huggingface.co/Lykon/dreamshaper-7) fine-tune of [Stable Diffusion v1-5](https://huggingface.co/runwayml/stable-diffusion-v1-5) with only 4,000 training iterations (~32 A100 GPU Hours). [Project page](https://latent-consistency-models.github.io)
29
+ '''
30
+ if torch.cuda.is_available():
31
+ DESCRIPTION += "\n<p>Running on CUDA 😀</p>"
32
+ elif hasattr(torch, 'xpu') and torch.xpu.is_available():
33
+ DESCRIPTION += "\n<p>Running on XPU 🤓</p>"
34
+ else:
35
+ DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
36
+
37
+ MAX_SEED = np.iinfo(np.int32).max
38
+ CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES") == "1"
39
+ MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "768"))
40
+ USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE") == "1"
41
+
42
+
43
+
44
+ """
45
+ Operation System Options:
46
+ If you are using MacOS, please set the following (device="mps") ;
47
+ If you are using Linux & Windows with Nvidia GPU, please set the device="cuda";
48
+ If you are using Linux & Windows with Intel Arc GPU, please set the device="xpu";
49
+ """
50
+ # device = "mps" # MacOS
51
+ #device = "xpu" # Intel Arc GPU
52
+ device = "cuda" # Linux & Windows
53
+
54
+
55
+ """
56
+ DTYPE Options:
57
+ To reduce GPU memory you can set "DTYPE=torch.float16",
58
+ but image quality might be compromised
59
+ """
60
+ DTYPE = torch.float16 # torch.float16 works as well, but pictures seem to be a bit worse
61
+
62
+
63
+ #pipe = DiffusionPipeline.from_pretrained("SimianLuo/LCM_Dreamshaper_v7")
64
+ pipe = DiffusionPipeline.from_pretrained("D:/git-work/LCM_Dreamshaper_v7")
65
+
66
+
67
+ #pipe = DiffusionPipeline.from_pretrained("SimianLuo/LCM_Dreamshaper_v7", custom_pipeline="latent_consistency_txt2img", custom_revision="main")
68
+ pipe.to(torch_device=device, torch_dtype=DTYPE)
69
+
70
+
71
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
72
+ if randomize_seed:
73
+ seed = random.randint(0, MAX_SEED)
74
+ return seed
75
+
76
+ def save_image(img, profile: gr.OAuthProfile | None, metadata: dict, root_path='./'):
77
+ unique_name = str(uuid.uuid4()) + '.png'
78
+ unique_name = os.path.join(root_path, unique_name)
79
+ img.save(unique_name)
80
+ # gr_user_history.save_image(label=metadata["prompt"], image=img, profile=profile, metadata=metadata)
81
+ return unique_name
82
+
83
+ def save_images(image_array, profile: gr.OAuthProfile | None, metadata: dict):
84
+ paths = []
85
+ root_path = './images/'
86
+ os.makedirs(root_path, exist_ok=True)
87
+ with ThreadPoolExecutor() as executor:
88
+ paths = list(executor.map(save_image, image_array, [profile]*len(image_array), [metadata]*len(image_array), [root_path]*len(image_array)))
89
+ return paths
90
+
91
+ def generate(
92
+ prompt: str,
93
+ seed: int = 0,
94
+ width: int = 512,
95
+ height: int = 512,
96
+ guidance_scale: float = 8.0,
97
+ num_inference_steps: int = 4,
98
+ num_images: int = 4,
99
+ randomize_seed: bool = False,
100
+ param_dtype='torch.float16',
101
+ progress = gr.Progress(track_tqdm=True),
102
+ profile: gr.OAuthProfile | None = None,
103
+ ) -> PIL.Image.Image:
104
+ seed = randomize_seed_fn(seed, randomize_seed)
105
+ torch.manual_seed(seed)
106
+ pipe.to(torch_device=device, torch_dtype=torch.float16 if param_dtype == 'torch.float16' else torch.float32)
107
+ start_time = time.time()
108
+ result = pipe(
109
+ prompt=prompt,
110
+ width=width,
111
+ height=height,
112
+ guidance_scale=guidance_scale,
113
+ num_inference_steps=num_inference_steps,
114
+ num_images_per_prompt=num_images,
115
+ lcm_origin_steps=50,
116
+ output_type="pil",
117
+ ).images
118
+ paths = save_images(result, profile, metadata={"prompt": prompt, "seed": seed, "width": width, "height": height, "guidance_scale": guidance_scale, "num_inference_steps": num_inference_steps})
119
+ print(time.time() - start_time)
120
+ return paths, seed
121
+
122
+ examples = [
123
+ "portrait photo of a girl, photograph, highly detailed face, depth of field, moody light, golden hour, style by Dan Winters, Russell James, Steve McCurry, centered, extremely detailed, Nikon D850, award winning photography",
124
+ "Self-portrait oil painting, a beautiful cyborg with golden hair, 8k",
125
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
126
+ "A photo of beautiful mountain with realistic sunset and blue lake, highly detailed, masterpiece",
127
+ ]
128
+
129
+ with gr.Blocks(css="style.css") as demo:
130
+ gr.Markdown(DESCRIPTION)
131
+ gr.DuplicateButton(
132
+ value="Duplicate Space for private use",
133
+ elem_id="duplicate-button",
134
+ visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
135
+ )
136
+ with gr.Group():
137
+ with gr.Row():
138
+ prompt = gr.Text(
139
+ label="Prompt",
140
+ show_label=False,
141
+ max_lines=1,
142
+ placeholder="Enter your prompt",
143
+ container=False,
144
+ )
145
+ run_button = gr.Button("Run", scale=0)
146
+ result = gr.Gallery(
147
+ label="Generated images", show_label=False, elem_id="gallery",
148
+ )
149
+ with gr.Accordion("Advanced options", open=False):
150
+ seed = gr.Slider(
151
+ label="Seed",
152
+ minimum=0,
153
+ maximum=MAX_SEED,
154
+ step=1,
155
+ value=0,
156
+ randomize=True
157
+ )
158
+ randomize_seed = gr.Checkbox(label="Randomize seed across runs", value=True)
159
+ with gr.Row():
160
+ width = gr.Slider(
161
+ label="Width",
162
+ #minimum=256,
163
+ minimum=128,
164
+ maximum=MAX_IMAGE_SIZE,
165
+ step=32,
166
+ value=512,
167
+ )
168
+ height = gr.Slider(
169
+ label="Height",
170
+ minimum=256,
171
+ maximum=MAX_IMAGE_SIZE,
172
+ step=32,
173
+ value=512,
174
+ )
175
+ with gr.Row():
176
+ guidance_scale = gr.Slider(
177
+ label="Guidance scale for base",
178
+ minimum=2,
179
+ maximum=14,
180
+ step=0.1,
181
+ value=8.0,
182
+ )
183
+ num_inference_steps = gr.Slider(
184
+ label="Number of inference steps for base",
185
+ minimum=1,
186
+ maximum=8,
187
+ step=1,
188
+ value=4,
189
+ )
190
+ with gr.Row():
191
+ num_images = gr.Slider(
192
+ label="Number of images",
193
+ minimum=1,
194
+ maximum=8,
195
+ step=1,
196
+ value=1,#生成图片的数量
197
+ visible=True,
198
+ )
199
+ dtype_choices = ['torch.float16','torch.float32']
200
+ param_dtype = gr.Radio(dtype_choices,label='torch.dtype',
201
+ value=dtype_choices[0],
202
+ interactive=True,
203
+ info='To save GPU memory, use torch.float16. For better quality, use torch.float32.')
204
+
205
+ # with gr.Accordion("Past generations", open=False):
206
+ # gr_user_history.render()
207
+
208
+ gr.Examples(
209
+ examples=examples,
210
+ inputs=prompt,
211
+ outputs=result,
212
+ fn=generate,
213
+ cache_examples=CACHE_EXAMPLES,
214
+ )
215
+
216
+ gr.on(
217
+ triggers=[
218
+ prompt.submit,
219
+ run_button.click,
220
+ ],
221
+ fn=generate,
222
+ inputs=[
223
+ prompt,
224
+ seed,
225
+ width,
226
+ height,
227
+ guidance_scale,
228
+ num_inference_steps,
229
+ num_images,
230
+ randomize_seed,
231
+ param_dtype
232
+ ],
233
+ outputs=[result, seed],
234
+ api_name="run",
235
+ )
236
+
237
+ if __name__ == "__main__":
238
+ demo.queue(api_open=False)
239
+ # demo.queue(max_size=20).launch()
240
+ demo.launch(share=True)
241
+ #demo.launch()
images/0118a123-f782-4065-bcc1-a1effb986124.png ADDED
images/01769f3d-18a0-423f-b3f6-a97af1c94bf2.png ADDED
images/01bb3133-e94e-4179-a3c4-b2456e72079b.png ADDED
images/020d25c7-e77c-42a6-bf0c-67189a68909f.png ADDED
images/02c34630-1cdf-400c-8125-e02441275657.png ADDED
images/065490e8-2a6b-4a7a-be86-ba5ebea130e3.png ADDED
images/0675c336-5d12-4adf-a000-cd41c14cb2ce.png ADDED
images/0808038d-ac56-4af9-aeea-fb7b7837385f.png ADDED
images/0a09dc96-247c-4d5c-9079-4b7d407f2fa5.png ADDED
images/0c8373bb-7af5-47f2-bdd8-0fe80533d46b.png ADDED
images/0f674c3d-76c3-470c-b9f2-88fd8bd795cd.png ADDED
images/11fd9c7a-636d-4405-b6a0-2bdd644e0f6b.png ADDED
images/132e6041-1db8-46ae-a34b-120aa3ea0b77.png ADDED
images/14e0f180-4f3e-4fc0-99f0-28866c900970.png ADDED
images/19fe3a25-ee3c-49d7-bd3f-cbd531d7ba60.png ADDED
images/1b238947-1de5-40d2-a0f2-7e020ebdd075.png ADDED
images/1d32843e-8406-4fe8-8199-ae9accf748ad.png ADDED
images/1d3b9603-1820-47a6-8c38-5fe321b36cc6.png ADDED
images/20915b3d-5168-4a42-ac77-52a442ca8998.png ADDED
images/20b48cb1-c939-4f19-b240-7ad23c7d5de5.png ADDED
images/2222b07a-0085-477c-b51b-f35edfd3c08d.png ADDED
images/23ea2bc1-2539-400b-b76c-f7c6e400ddca.png ADDED
images/29262f41-37c0-4e12-8eab-252b144afc22.png ADDED
images/2961c8d0-b781-49be-acfc-fbf2c8147301.png ADDED
images/2b30cbd0-9685-46b2-95fa-1cd901f538a5.png ADDED
images/3b80e697-c83a-4fa0-8d65-67b96a244335.png ADDED
images/3cd0779a-8b86-4fc7-86d4-7a69f3280077.png ADDED
images/3f234161-51c9-496a-b052-9932223b6d70.png ADDED
images/440004d2-1295-4601-aee0-21004d2f13e3.png ADDED
images/44e32e8c-6906-42b3-87cc-48c648e69bc9.png ADDED
images/48855e8d-eb5a-4965-b037-ebaa1e0c2a0e.png ADDED
images/503c7f64-c6bf-4f88-abfe-fbaeb0f0b8e4.png ADDED
images/5c0692b7-2b07-4c66-a85c-aa705ecf8eb3.png ADDED
images/5dc0bc85-cff9-4c8b-8102-18bfcd30b52b.png ADDED
images/5f88a975-82e5-4cb0-95bc-dbede0943bbf.png ADDED
images/612513d8-d317-40c6-9905-791244708670.png ADDED
images/638dadea-4f5b-434f-b431-9aa09b8d014e.png ADDED
images/64594ea6-b9ba-4939-a827-8ab10a6cff4a.png ADDED
images/66ff4bb9-f0a6-4d69-8e3e-c6126982d511.png ADDED
images/6772a638-9900-4376-a960-b191f6214bb9.png ADDED
images/6c15bb3a-3343-4cac-9c81-de8daaecebd3.png ADDED
images/71a3c480-89c2-4303-b59b-6e275e8f886f.png ADDED
images/7a9fb1df-8481-4630-ab54-43e3c5c3f984.png ADDED
images/7e80fe32-8c2d-4e56-aca5-a27ab517641f.png ADDED
images/85086f04-d11e-4f9f-8eb9-565f2b135d12.png ADDED
images/86ed98c5-d700-4c06-a5d3-7fb7143145be.png ADDED
images/88ddf880-800d-48af-9e54-2003632dec37.png ADDED
images/8a0b63d4-dbf1-4573-baa4-991cfa88a629.png ADDED