Spaces:
Runtime error
Runtime error
File size: 8,860 Bytes
2d54438 5285893 32a5d3a b3aad16 32a5d3a 5285893 75d6fdd 5285893 75d6fdd 5285893 69dd450 2d54438 5285893 2d54438 5285893 2d54438 5285893 2d54438 5285893 2d54438 5285893 2d54438 5285893 69dd450 32a5d3a 2d54438 69dd450 2d54438 5285893 2d54438 5285893 2d54438 5285893 2d54438 5285893 2d54438 5285893 2d54438 5285893 69dd450 2d54438 69dd450 32a5d3a 4970ac1 2d54438 5285893 2d54438 5285893 2d54438 5285893 8187725 2d54438 69dd450 32a5d3a 5285893 2d54438 5285893 2d54438 5285893 2d54438 69dd450 2d54438 32a5d3a 2d54438 32a5d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
import os
import gradio as gr
import numpy as np
import random
import torch
import subprocess
import time
import requests
import json
import base64
from io import BytesIO
from PIL import Image
from huggingface_hub import login
from huggingface_hub.utils import (
HfFolder
)
myip = os.environ["myip"]
myport = os.environ["myport"]
url = f"http://{myip}:{myport}"
queue_size = 0
def displayTextBox():
global queue_size
if queue_size > 4:
# return [gr.update(visible=False), gr.update(visible=True)]
return [gr.update(elem_id='txtOrangeStyle')]
# elif queue_size <= 4:
# # return [gr.update(visible=True), gr.update(visible=False)]
# return gr.update(elem_id='txtGreenStyle')
def set_msg():
global queue_size
if queue_size > int(os.environ["max_queue_size"]):
return "The current traffic is high with " + str(queue_size) + " in the queue. Please wait a moment."
else:
return "The current traffic is not high. You can submit your job now."
def img2img_generate(source_img, prompt, steps=25, strength=0.75, seed=42, guidance_scale=7.5):
print('image-to-image')
print("prompt: ", prompt)
print("steps: ", steps)
buffered = BytesIO()
source_img.save(buffered, format="JPEG")
img_b64 = base64.b64encode(buffered.getvalue())
timestamp = int(time.time()*1000)
data = {"source_img": img_b64.decode(), "prompt": prompt, "steps": steps,
"guidance_scale": guidance_scale, "seed": seed, "strength": strength,
"task_type": "1",
"timestamp": timestamp, "user": os.environ.get("token", "")}
start_time = time.time()
global queue_size
queue_size = queue_size + 1
resp = requests.post(url, data=json.dumps(data))
queue_size = queue_size - 1
try:
img_str = json.loads(resp.text)["img_str"]
print("Compute node: ", json.loads(resp.text)["ip"])
except:
print('No inference result. Please check server connection')
return None
img_byte = base64.b64decode(img_str)
img_io = BytesIO(img_byte) # convert image to file-like object
img = Image.open(img_io) # img is now PIL Image object
print("elapsed time: ", time.time() - start_time)
return img
def txt2img_generate(prompt, steps=25, seed=42, guidance_scale=7.5):
print('text-to-image')
print("prompt: ", prompt)
print("steps: ", steps)
timestamp = int(time.time()*1000)
data = {"prompt": prompt,
"steps": steps, "guidance_scale": guidance_scale, "seed": seed,
"task_type": "0",
"timestamp": timestamp, "user": os.environ.get("token", "")}
start_time = time.time()
global queue_size
queue_size = queue_size + 1
resp = requests.post(url, data=json.dumps(data))
queue_size = queue_size - 1
try:
img_str = json.loads(resp.text)["img_str"]
print("Compute node: ", json.loads(resp.text)["ip"])
except:
print('No inference result. Please check server connection')
return None
img_byte = base64.b64decode(img_str)
img_io = BytesIO(img_byte) # convert image to file-like object
img = Image.open(img_io) # img is now PIL Image object
print("elapsed time: ", time.time() - start_time)
return img
md = """
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!
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>.
"""
legal = """
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.
© 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.
"""
details = """
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.
"""
css = '''
.instruction{position: absolute; top: 0;right: 0;margin-top: 0px !important}
.arrow{position: absolute;top: 0;right: -110px;margin-top: -8px !important}
#component-4, #component-3, #component-10{min-height: 0}
.duplicate-button img{margin: 0}
#mdStyle{font-size: 0.6rem}
.generating.svelte-1w9161c { border: none }
#txtGreenStyle {border: 2px solid #32ec48;}
#txtOrangeStyle {border: 2px solid #e77718;}
'''
random_seed = random.randint(0, 2147483647)
with gr.Blocks(css=css) as demo:
gr.Markdown("# Stable Diffusion Inference Demo on 4th Gen Intel Xeon Scalable Processors")
gr.Markdown(md)
textBoxGreen = gr.Textbox(set_msg, every=3, label='Real-time Jobs in Queue', elem_id='txtGreenStyle', visible=True)
textBoxGreen.change(displayTextBox, outputs = [textBoxGreen])
with gr.Tab("Text-to-Image"):
with gr.Row(visible=True) as text_to_image:
with gr.Column():
prompt = gr.inputs.Textbox(label='Prompt', default='a photo of an astronaut riding a horse on mars')
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)
seed = gr.inputs.Slider(0, 2147483647, label='Seed', default=random_seed, step=1)
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)
txt2img_button = gr.Button("Generate Image")
with gr.Column():
result_image = gr.Image()
with gr.Tab("Image-to-Image text-guided generation"):
with gr.Row(visible=True) as image_to_image:
with gr.Column():
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")
# source_img = gr.Image(source="upload", type="pil")
prompt_2 = gr.inputs.Textbox(label='Prompt', default='A fantasy landscape, trending on artstation')
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)
seed_2 = gr.inputs.Slider(0, 2147483647, label='Seed', default=random_seed, step=1)
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)
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)
img2img_button = gr.Button("Generate Image")
with gr.Column():
result_image_2 = gr.Image()
txt2img_button.click(fn=txt2img_generate, inputs=[prompt, inference_steps, seed, guidance_scale], outputs=[result_image])
img2img_button.click(fn=img2img_generate, inputs=[source_img, prompt_2, inference_steps_2, strength, seed_2, guidance_scale_2], outputs=result_image_2)
gr.Markdown("**Additional Test Configuration Details:**", elem_id='mdStyle')
gr.Markdown(details, elem_id='mdStyle')
gr.Markdown("**Notices and Disclaimers:**", elem_id='mdStyle')
gr.Markdown(legal, elem_id='mdStyle')
demo.queue(max_size=int(os.environ["max_job_size"]), concurrency_count=int(os.environ["max_job_size"])).launch(debug=True, show_api=False)
|