Spaces:
Runtime error
Runtime error
Duplicate from Intel/Stable-Diffusion
Browse filesCo-authored-by: lvkaokao <lvkaokao@users.noreply.huggingface.co>
- .gitattributes +34 -0
- README.md +14 -0
- app.py +187 -0
- requirements.txt +5 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Stable Diffusion on SPR
|
3 |
+
emoji: 🏢
|
4 |
+
colorFrom: pink
|
5 |
+
colorTo: purple
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.15.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: apache-2.0
|
11 |
+
duplicated_from: Intel/Stable-Diffusion
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import random
|
5 |
+
import torch
|
6 |
+
import subprocess
|
7 |
+
import time
|
8 |
+
import requests
|
9 |
+
import json
|
10 |
+
|
11 |
+
import base64
|
12 |
+
from io import BytesIO
|
13 |
+
from PIL import Image
|
14 |
+
from huggingface_hub import login
|
15 |
+
from huggingface_hub.utils import (
|
16 |
+
HfFolder
|
17 |
+
)
|
18 |
+
|
19 |
+
myip = os.environ["myip"]
|
20 |
+
myport = os.environ["myport"]
|
21 |
+
|
22 |
+
url = f"http://{myip}:{myport}"
|
23 |
+
|
24 |
+
# print('=='*20)
|
25 |
+
# print(os.system("hostname -i"))
|
26 |
+
|
27 |
+
def img2img_generate(source_img, prompt, steps=25, strength=0.75, seed=42, guidance_scale=7.5):
|
28 |
+
|
29 |
+
if not isinstance(steps, int):
|
30 |
+
return None
|
31 |
+
# cpu info
|
32 |
+
# print(subprocess.check_output(["cat /proc/cpuinfo | grep 'model name' |uniq"], stderr=subprocess.STDOUT).decode("utf8"))
|
33 |
+
print('image-to-image')
|
34 |
+
print("prompt: ", prompt)
|
35 |
+
print("steps: ", steps)
|
36 |
+
buffered = BytesIO()
|
37 |
+
source_img.save(buffered, format="JPEG")
|
38 |
+
img_b64 = base64.b64encode(buffered.getvalue())
|
39 |
+
|
40 |
+
data = {"source_img": img_b64.decode(), "prompt": prompt, "steps": steps,
|
41 |
+
"guidance_scale": guidance_scale, "seed": seed, "strength": strength,
|
42 |
+
"token": os.environ["access_token"]}
|
43 |
+
|
44 |
+
start_time = time.time()
|
45 |
+
resp = requests.post(url, data=json.dumps(data))
|
46 |
+
|
47 |
+
try:
|
48 |
+
img_str = json.loads(resp.text)["img_str"]
|
49 |
+
print("compute node: ", json.loads(resp.text)["ip"])
|
50 |
+
except:
|
51 |
+
print('no inference result. please check server connection')
|
52 |
+
return None
|
53 |
+
|
54 |
+
img_byte = base64.b64decode(img_str)
|
55 |
+
img_io = BytesIO(img_byte) # convert image to file-like object
|
56 |
+
img = Image.open(img_io) # img is now PIL Image object
|
57 |
+
print("elapsed time: ", time.time() - start_time)
|
58 |
+
return img
|
59 |
+
|
60 |
+
|
61 |
+
def txt2img_generate(prompt, steps=25, seed=42, guidance_scale=7.5):
|
62 |
+
|
63 |
+
if not isinstance(steps, int):
|
64 |
+
return None
|
65 |
+
# cpu info
|
66 |
+
# print(subprocess.check_output(["cat /proc/cpuinfo | grep 'model name' |uniq"], stderr=subprocess.STDOUT).decode("utf8"))
|
67 |
+
print('text-to-image')
|
68 |
+
print("prompt: ", prompt)
|
69 |
+
print("steps: ", steps)
|
70 |
+
data = {"prompt": prompt,
|
71 |
+
"steps": steps, "guidance_scale": guidance_scale, "seed": seed,
|
72 |
+
"token": os.environ["access_token"]}
|
73 |
+
start_time = time.time()
|
74 |
+
resp = requests.post(url, data=json.dumps(data))
|
75 |
+
try:
|
76 |
+
img_str = json.loads(resp.text)["img_str"]
|
77 |
+
print("compute node: ", json.loads(resp.text)["ip"])
|
78 |
+
except:
|
79 |
+
print('no inference result. please check server connection')
|
80 |
+
return None
|
81 |
+
|
82 |
+
img_byte = base64.b64decode(img_str)
|
83 |
+
img_io = BytesIO(img_byte) # convert image to file-like object
|
84 |
+
img = Image.open(img_io) # img is now PIL Image object
|
85 |
+
print("elapsed time: ", time.time() - start_time)
|
86 |
+
return img
|
87 |
+
|
88 |
+
def check_login(hf_token="", gr1=None, gr2=None, gr3=None):
|
89 |
+
try:
|
90 |
+
login(token=hf_token)
|
91 |
+
|
92 |
+
return [f"### Success 🔥", gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)]
|
93 |
+
|
94 |
+
except:
|
95 |
+
return [f"### Error 😢😢😢", gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)]
|
96 |
+
|
97 |
+
md = """
|
98 |
+
This demo shows the accelerated inference performance of a Stable Diffusion model on **Intel Xeon Gold 64xx (4th Gen Intel Xeon Scalable Processors codenamed Sapphire Rapids)**. Try it and generate photorealistic images from text!
|
99 |
+
|
100 |
+
You may also want to try creating your own Stable Diffusion with few-shot fine-tuning. Please refer to our <a href=\"https://medium.com/intel-analytics-software/personalized-stable-diffusion-with-few-shot-fine-tuning-on-a-single-cpu-f01a3316b13\">blog</a> and <a href=\"https://github.com/intel/neural-compressor/tree/master/examples/pytorch/diffusion_model/diffusers/textual_inversion\">code</a> available in <a href=\"https://github.com/intel/neural-compressor\">**Intel Neural Compressor**</a> and <a href=\"https://github.com/huggingface/diffusers\">**Hugging Face Diffusers**</a>.
|
101 |
+
|
102 |
+
"""
|
103 |
+
|
104 |
+
legal = """
|
105 |
+
Performance varies by use, configuration and other factors. Learn more at www.Intel.com/PerformanceIndex. Performance results are based on testing as of dates shown in configurations and may not reflect all publicly available updates. See backup for configuration details. No product or component can be absolutely secure.
|
106 |
+
© Intel Corporation. Intel, the Intel logo, and other Intel marks are trademarks of Intel Corporation or its subsidiaries. Other names and brands may be claimed as the property of others.
|
107 |
+
"""
|
108 |
+
|
109 |
+
details = """
|
110 |
+
4th Gen Intel Xeon Scalable Processor Inference. Test by Intel on 01/06/2023. 1 node, 1S, Intel(R) Xeon(R) Gold 64xx CPU @ 3.0GHz 32 cores and software with 512GB (8x64GB DDR5 4800 MT/s [4800 MT/s]), microcode 0x2a000080, HT on, Turbo on, Ubuntu 22.04.1 LTS, 5.15.0-1026-aws, 200G Amazon Elastic Block Store. Multiple nodes connected with Elastic Network Adapter (ENA). PyTorch Nightly build (2.0.0.dev20230105+cpu), Transformers 4.25.1, Diffusers 0.11.1, oneDNN v2.7.2.
|
111 |
+
"""
|
112 |
+
|
113 |
+
css = '''
|
114 |
+
.instruction{position: absolute; top: 0;right: 0;margin-top: 0px !important}
|
115 |
+
.arrow{position: absolute;top: 0;right: -110px;margin-top: -8px !important}
|
116 |
+
#component-4, #component-3, #component-10{min-height: 0}
|
117 |
+
.duplicate-button img{margin: 0}
|
118 |
+
#mdStyle{font-size: 0.6rem}
|
119 |
+
'''
|
120 |
+
|
121 |
+
random_seed = random.randint(0, 2147483647)
|
122 |
+
|
123 |
+
with gr.Blocks(css=css) as demo:
|
124 |
+
gr.Markdown("# Stable Diffusion Inference Demo on 4th Gen Intel Xeon Scalable Processors")
|
125 |
+
gr.Markdown(md)
|
126 |
+
|
127 |
+
with gr.Tab("Text-to-Image"):
|
128 |
+
with gr.Row(visible=False) as text_to_image:
|
129 |
+
with gr.Column():
|
130 |
+
prompt = gr.inputs.Textbox(label='Prompt', default='a photo of an astronaut riding a horse on mars')
|
131 |
+
inference_steps = gr.inputs.Slider(1, 100, label='Inference Steps - increase the steps for better quality (e.g., avoiding black image) ', default=20, step=1)
|
132 |
+
seed = gr.inputs.Slider(0, 2147483647, label='Seed', default=random_seed, step=1)
|
133 |
+
guidance_scale = gr.inputs.Slider(1.0, 20.0, label='Guidance Scale - how much the prompt will influence the results', default=7.5, step=0.1)
|
134 |
+
txt2img_button = gr.Button("Generate Image")
|
135 |
+
|
136 |
+
with gr.Column():
|
137 |
+
result_image = gr.Image()
|
138 |
+
|
139 |
+
|
140 |
+
with gr.Tab("Image-to-Image text-guided generation"):
|
141 |
+
with gr.Row(visible=False) as image_to_image:
|
142 |
+
with gr.Column():
|
143 |
+
source_img = gr.Image(source="upload", type="pil", value="https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg")
|
144 |
+
# source_img = gr.Image(source="upload", type="pil")
|
145 |
+
prompt_2 = gr.inputs.Textbox(label='Prompt', default='A fantasy landscape, trending on artstation')
|
146 |
+
inference_steps_2 = gr.inputs.Slider(1, 100, label='Inference Steps - increase the steps for better quality (e.g., avoiding black image) ', default=20, step=1)
|
147 |
+
seed_2 = gr.inputs.Slider(0, 2147483647, label='Seed', default=random_seed, step=1)
|
148 |
+
guidance_scale_2 = gr.inputs.Slider(1.0, 20.0, label='Guidance Scale - how much the prompt will influence the results', default=7.5, step=0.1)
|
149 |
+
strength = gr.inputs.Slider(0.0, 1.0, label='Strength - adding more noise to it the larger the strength', default=0.75, step=0.01)
|
150 |
+
img2img_button = gr.Button("Generate Image")
|
151 |
+
|
152 |
+
with gr.Column():
|
153 |
+
result_image_2 = gr.Image()
|
154 |
+
|
155 |
+
with gr.Box(visible=True) as is_login:
|
156 |
+
|
157 |
+
gr.Markdown("""### Login
|
158 |
+
- Paste your user access tokens from hf.co/settings/tokens. Read access is enough.
|
159 |
+
- Click **Agree** that authorizes us to use your access tokens to access Stable Diffusion from <a href=\"https://huggingface.co/models\">**Hugging Face model hub**</a>.
|
160 |
+
- Click **Login** button. """)
|
161 |
+
|
162 |
+
with gr.Row():
|
163 |
+
hf_token_login = gr.Textbox(label='Hugging Face User Access Tokens', type="password")
|
164 |
+
|
165 |
+
with gr.Row():
|
166 |
+
confirm = gr.Checkbox(label="Agree")
|
167 |
+
|
168 |
+
with gr.Row():
|
169 |
+
login_button = gr.Button("Login")
|
170 |
+
|
171 |
+
with gr.Row():
|
172 |
+
msg = gr.Markdown(label="Message")
|
173 |
+
|
174 |
+
login_button.click(fn=check_login, inputs=[hf_token_login, confirm],
|
175 |
+
outputs=[msg, is_login, text_to_image, image_to_image], queue=False)
|
176 |
+
|
177 |
+
txt2img_button.click(fn=txt2img_generate, inputs=[prompt, inference_steps, seed, guidance_scale], outputs=result_image, queue=False)
|
178 |
+
img2img_button.click(fn=img2img_generate, inputs=[source_img, prompt_2, inference_steps_2, strength, seed_2, guidance_scale_2], outputs=result_image_2, queue=False)
|
179 |
+
|
180 |
+
gr.Markdown("**Additional Test Configuration Details:**", elem_id='mdStyle')
|
181 |
+
gr.Markdown(details, elem_id='mdStyle')
|
182 |
+
|
183 |
+
gr.Markdown("**Notices and Disclaimers:**", elem_id='mdStyle')
|
184 |
+
gr.Markdown(legal, elem_id='mdStyle')
|
185 |
+
|
186 |
+
demo.queue(default_enabled=False, api_open=False, max_size=10).launch(debug=True, show_api=False)
|
187 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
diffusers==0.8.1
|
3 |
+
transformers==4.21.2
|
4 |
+
requests
|
5 |
+
huggingface_hub
|