wenjiao's picture
test
8187725
raw
history blame
8.62 kB
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 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 execute():
global queue_size
queue_size += 1
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)
textBoxOrange = gr.Textbox(set_msg, every=3, label='Real-time Jobs in Queue', elem_id='txtOrangeStyle', visible=False)
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)