wenjiao commited on
Commit
5285893
1 Parent(s): 8048fe8
Files changed (1) hide show
  1. app.py +41 -70
app.py CHANGED
@@ -21,34 +21,45 @@ 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)
@@ -60,45 +71,37 @@ def img2img_generate(source_img, prompt, steps=25, strength=0.75, seed=42, guida
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), 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), gr.update(visible=True), gr.update(visible=True)]
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 = """
@@ -107,7 +110,7 @@ Performance varies by use, configuration and other factors. Learn more at www.In
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 = '''
@@ -116,23 +119,21 @@ css = '''
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
- txt_to_img_example = [
122
- ['a photo of an astronaut riding a horse on mars', 20, 929194386, 7.5],
123
- ]
124
- img_to_img_example = [
125
- ["https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg", 'A fantasy landscape, trending on artstation', 25, 0.75, 42, 7.5],
126
- ]
127
-
128
  random_seed = random.randint(0, 2147483647)
129
 
130
  with gr.Blocks(css=css) as demo:
131
  gr.Markdown("# Stable Diffusion Inference Demo on 4th Gen Intel Xeon Scalable Processors")
132
  gr.Markdown(md)
133
 
 
 
134
  with gr.Tab("Text-to-Image"):
135
- with gr.Row(visible=False) as text_to_image:
136
  with gr.Column():
137
  prompt = gr.inputs.Textbox(label='Prompt', default='a photo of an astronaut riding a horse on mars')
138
  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)
@@ -143,11 +144,9 @@ with gr.Blocks(css=css) as demo:
143
  with gr.Column():
144
  result_image = gr.Image()
145
 
146
- with gr.Row(visible=False) as txt_example:
147
- gr.Examples(examples=txt_to_img_example, inputs=[prompt, inference_steps, seed, guidance_scale], outputs=result_image, fn=txt2img_generate, cache_examples=True,)
148
 
149
  with gr.Tab("Image-to-Image text-guided generation"):
150
- with gr.Row(visible=False) as image_to_image:
151
  with gr.Column():
152
  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")
153
  # source_img = gr.Image(source="upload", type="pil")
@@ -160,35 +159,10 @@ with gr.Blocks(css=css) as demo:
160
 
161
  with gr.Column():
162
  result_image_2 = gr.Image()
163
-
164
- with gr.Row(visible=False) as img_example:
165
- gr.Examples(examples=img_to_img_example, inputs=[source_img, prompt_2, inference_steps_2, strength, seed_2, guidance_scale_2], outputs=result_image_2, fn=img2img_generate, cache_examples=True,)
166
-
167
-
168
- with gr.Box(visible=True) as is_login:
169
-
170
- gr.Markdown("""### Login
171
- - Paste your user access tokens from hf.co/settings/tokens. Read access is enough.
172
- - 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>.
173
- - Click **Login** button. """)
174
-
175
- with gr.Row():
176
- hf_token_login = gr.Textbox(label='Hugging Face User Access Tokens', type="password")
177
-
178
- with gr.Row():
179
- confirm = gr.Checkbox(label="Agree")
180
 
181
- with gr.Row():
182
- login_button = gr.Button("Login")
183
 
184
- with gr.Row():
185
- msg = gr.Markdown(label="Message")
186
-
187
- login_button.click(fn=check_login, inputs=[hf_token_login, confirm],
188
- outputs=[msg, is_login, text_to_image, txt_example, image_to_image, img_example], queue=False)
189
-
190
- txt2img_button.click(fn=txt2img_generate, inputs=[prompt, inference_steps, seed, guidance_scale], outputs=result_image, queue=False)
191
- 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)
192
 
193
  gr.Markdown("**Additional Test Configuration Details:**", elem_id='mdStyle')
194
  gr.Markdown(details, elem_id='mdStyle')
@@ -196,7 +170,4 @@ with gr.Blocks(css=css) as demo:
196
  gr.Markdown("**Notices and Disclaimers:**", elem_id='mdStyle')
197
  gr.Markdown(legal, elem_id='mdStyle')
198
 
199
-
200
-
201
- demo.queue(default_enabled=False, api_open=False, max_size=10).launch(debug=True, show_api=False)
202
-
 
21
 
22
  url = f"http://{myip}:{myport}"
23
 
24
+ queue_size = 0
25
+
26
+ def set_msg():
27
+ global queue_size
28
+ if queue_size > int(os.environ["max_queue_size"]):
29
+ return "The current traffic is high with " + str(queue_size) + " in the queue. Please wait a moment."
30
+ else:
31
+ return "The current traffic is not high. You can submit your job now."
32
+
33
+ def execute():
34
+ global queue_size
35
+ queue_size += 1
36
 
37
  def img2img_generate(source_img, prompt, steps=25, strength=0.75, seed=42, guidance_scale=7.5):
38
 
 
 
 
 
39
  print('image-to-image')
40
  print("prompt: ", prompt)
41
  print("steps: ", steps)
42
  buffered = BytesIO()
43
  source_img.save(buffered, format="JPEG")
44
  img_b64 = base64.b64encode(buffered.getvalue())
45
+ timestamp = int(time.time()*1000)
46
 
47
  data = {"source_img": img_b64.decode(), "prompt": prompt, "steps": steps,
48
  "guidance_scale": guidance_scale, "seed": seed, "strength": strength,
49
+ "task_type": "1",
50
+ "timestamp": timestamp, "user": os.environ.get("token", "")}
51
 
52
  start_time = time.time()
53
+ global queue_size
54
+ queue_size = queue_size + 1
55
  resp = requests.post(url, data=json.dumps(data))
56
+ queue_size = queue_size - 1
57
 
58
  try:
59
  img_str = json.loads(resp.text)["img_str"]
60
+ print("Compute node: ", json.loads(resp.text)["ip"])
61
  except:
62
+ print('No inference result. Please check server connection')
63
  return None
64
 
65
  img_byte = base64.b64decode(img_str)
 
71
 
72
  def txt2img_generate(prompt, steps=25, seed=42, guidance_scale=7.5):
73
 
 
 
 
 
74
  print('text-to-image')
75
  print("prompt: ", prompt)
76
  print("steps: ", steps)
77
+ timestamp = int(time.time()*1000)
78
  data = {"prompt": prompt,
79
  "steps": steps, "guidance_scale": guidance_scale, "seed": seed,
80
+ "task_type": "0",
81
+ "timestamp": timestamp, "user": os.environ.get("token", "")}
82
  start_time = time.time()
83
+ global queue_size
84
+ queue_size = queue_size + 1
85
  resp = requests.post(url, data=json.dumps(data))
86
+ queue_size = queue_size - 1
87
  try:
88
  img_str = json.loads(resp.text)["img_str"]
89
+ print("Compute node: ", json.loads(resp.text)["ip"])
90
  except:
91
+ print('No inference result. Please check server connection')
92
  return None
93
 
94
  img_byte = base64.b64decode(img_str)
95
  img_io = BytesIO(img_byte) # convert image to file-like object
96
  img = Image.open(img_io) # img is now PIL Image object
97
  print("elapsed time: ", time.time() - start_time)
 
98
 
99
+ return img
 
 
 
 
100
 
 
 
101
 
102
  md = """
103
+ 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! Please note that the demo is in **preview** under limited HW resources. We are committed to continue improving the demo and happy to hear your feedbacks. Thanks for your trying!
 
104
  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>.
 
105
  """
106
 
107
  legal = """
 
110
  """
111
 
112
  details = """
113
+ 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.
114
  """
115
 
116
  css = '''
 
119
  #component-4, #component-3, #component-10{min-height: 0}
120
  .duplicate-button img{margin: 0}
121
  #mdStyle{font-size: 0.6rem}
122
+ .generating.svelte-1w9161c { border: none }
123
+ #txtGreenStyle {2px solid #32ec48;}
124
+ #txtOrangeStyle {2px solid #e77718;}
125
  '''
126
 
 
 
 
 
 
 
 
127
  random_seed = random.randint(0, 2147483647)
128
 
129
  with gr.Blocks(css=css) as demo:
130
  gr.Markdown("# Stable Diffusion Inference Demo on 4th Gen Intel Xeon Scalable Processors")
131
  gr.Markdown(md)
132
 
133
+ gr.Textbox(set_msg, every=3, label='Real-time Jobs in Queue', elem_id='txtOrangeStyle')
134
+
135
  with gr.Tab("Text-to-Image"):
136
+ with gr.Row(visible=True) as text_to_image:
137
  with gr.Column():
138
  prompt = gr.inputs.Textbox(label='Prompt', default='a photo of an astronaut riding a horse on mars')
139
  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)
 
144
  with gr.Column():
145
  result_image = gr.Image()
146
 
 
 
147
 
148
  with gr.Tab("Image-to-Image text-guided generation"):
149
+ with gr.Row(visible=True) as image_to_image:
150
  with gr.Column():
151
  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")
152
  # source_img = gr.Image(source="upload", type="pil")
 
159
 
160
  with gr.Column():
161
  result_image_2 = gr.Image()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
 
163
+ txt2img_button.click(fn=txt2img_generate, inputs=[prompt, inference_steps, seed, guidance_scale], outputs=[result_image])
 
164
 
165
+ img2img_button.click(fn=img2img_generate, inputs=[source_img, prompt_2, inference_steps_2, strength, seed_2, guidance_scale_2], outputs=result_image_2)
 
 
 
 
 
 
 
166
 
167
  gr.Markdown("**Additional Test Configuration Details:**", elem_id='mdStyle')
168
  gr.Markdown(details, elem_id='mdStyle')
 
170
  gr.Markdown("**Notices and Disclaimers:**", elem_id='mdStyle')
171
  gr.Markdown(legal, elem_id='mdStyle')
172
 
173
+ demo.queue(max_size=int(os.environ["max_job_size"]), concurrency_count=int(os.environ["max_job_size"])).launch(debug=True, show_api=False)