Spaces:
Runtime error
Runtime error
File size: 8,860 Bytes
49c356f caf0602 49c356f e445523 49c356f e4b5df5 49c356f e4b5df5 49c356f e445523 49c356f e445523 49c356f e445523 49c356f e445523 49c356f e445523 49c356f e445523 49c356f e445523 49c356f e445523 49c356f e445523 49c356f e445523 49c356f e445523 49c356f e445523 49c356f e445523 49c356f bc3aa41 49c356f e445523 e0aecb6 e445523 e0aecb6 caf0602 49c356f e445523 e0aecb6 49c356f e445523 49c356f e445523 49c356f |
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
import os
os.system("pip install gradio==3.28.0")
import gradio as gr
import numpy as np
import random
import torch
import subprocess
import time
import requests
import json
import threading
import base64
from io import BytesIO
from PIL import Image
from huggingface_hub import login
myip_spr = os.environ["myip_spr"]
myport = os.environ["myport"]
SPR = f"http://{myip_spr}:{myport}"
print('=='*20)
print(os.system("hostname -i"))
print(SPR)
prompt_examples_list = [
['A cascading waterfall tumbles down moss-covered rocks, surrounded by a lush and vibrant forest.'],
['In a serene garden, delicate cherry blossoms fall like pink snowflakes.'],
['A breathtaking mountain range towers above a picturesque valley, with a winding river reflecting the surrounding beauty.'],
['A serene beach scene with turquoise waters, palm trees swaying in the breeze, and a radiant sunset painting the sky in hues of orange and pink.'],
['After the rain, sunlight breaks through the clouds, illuminating the verdant fields.']
]
def update_language(value):
if value == "zh-CN":
return [gr.update(visible=False), gr.update(visible=True)]
else:
return [gr.update(visible=True), gr.update(visible=False)]
def url_requests(url, data):
resp = requests.post(url, data=json.dumps(data))
img_str = json.loads(resp.text)["img_str"]
location = json.loads(resp.text)["ip"]
img_byte = base64.b64decode(img_str)
img_io = BytesIO(img_byte)
img = Image.open(img_io)
return img, location
def img2img_generate(url, source_img, prompt, steps=25, strength=0.75, seed=42, guidance_scale=7.5):
print('=*'*20)
print(type(source_img))
print("prompt: ", prompt)
buffered = BytesIO()
source_img.save(buffered, format="JPEG")
img_b64 = base64.b64encode(buffered.getvalue())
data = {"source_img": img_b64.decode(), "prompt": prompt, "steps": steps,
"guidance_scale": guidance_scale, "seed": seed, "strength": strength}
start_time = time.time()
img, location = url_requests(url, data)
print("*="*20)
print("location: ", location)
print("cost: ", time.time() - start_time)
return img
def txt2img_example_input(value):
print('6/12/2023', value)
return value
def txt2img_generate(url, prompt, steps=25, seed=42, guidance_scale=7.5):
print("prompt: ", prompt)
print("steps: ", steps)
print("url: ", url)
data = {"prompt": prompt, "steps": steps,
"guidance_scale": guidance_scale, "seed": seed}
start_time = time.time()
img, location = url_requests(url, data)
print("*="*20)
print("location: ", location)
print("cost: ", time.time() - start_time)
return img
title = """
# Stable Diffusion Inference Acceleration Comparison
"""
subtitle = """
# between 4th Gen and 3rd Gen Intel Xeon Scalable Processor
"""
md = """
Have fun and try your own prompts and see a up to 9x performance acceleration on the new 4th Gen Intel Xeon using <a href=\"https://github.com/intel/intel-extension-for-transformers\">**Intel Extension for Transformers**</a>. 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 10/06/2023. Ubuntu 22.04.1 LTS, Intel Extension for Transformers(1.1.dev154+g448cc17e), Transformers 4.28.1, Diffusers 0.12.1, oneDNN v2.7.4.
"""
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}
#img_1, #img_2, #img_3, #img_4{height:15rem}
#mdStyle{font-size: 0.7rem}
#titleCenter {text-align:center}
'''
random_seed = random.randint(0, 2147483647)
with gr.Blocks(css=css) as demo:
with gr.Box(visible=True) as Eng:
gr.Markdown(title)
gr.Markdown(subtitle)
gr.Markdown(md)
with gr.Tab("Text-to-Image"):
with gr.Row(visible=True) as text_to_image:
with gr.Column(visible=True):
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", variant="primary")
url_SPR_txt = gr.Textbox(label='url_SPR_txt', value=SPR, visible=False)
with gr.Column():
result_image_1 = gr.Image(label="4th Gen Intel Xeon Scalable Processors (SPR)", elem_id="img_1")
txt2img_input = gr.Textbox(visible=False)
gr.Examples(
examples=prompt_examples_list,
inputs=txt2img_input,
outputs=prompt,
fn=txt2img_example_input,
cache_examples=True,
)
with gr.Tab("Image-to-Image text-guided generation"):
with gr.Row(visible=True) as image_to_image:
with gr.Column(visible=True):
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")
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", variant="primary")
url_SPR = gr.Textbox(label='url_SPR', value=SPR, visible=False)
with gr.Column():
result_image_3 = gr.Image(label="4th Gen Intel Xeon Scalable Processors (SPR)", elem_id="img_3")
with gr.Accordion("Additional Info", open=False) as area_crazy_fn:
gr.Markdown("**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')
txt2img_button.click(fn=txt2img_generate, inputs=[url_SPR_txt, prompt, inference_steps, seed, guidance_scale], outputs=result_image_1, queue=False)
img2img_button.click(fn=img2img_generate, inputs=[url_SPR, source_img, prompt_2, inference_steps_2, strength, seed_2, guidance_scale_2], outputs=result_image_3, queue=False)
dt = gr.Textbox(label="Current language", visible=False)
demo.load(None, inputs=None, outputs=dt, _js="() => navigator.language")
demo.queue(default_enabled=False, api_open=False, max_size=5).launch(debug=True, show_api=False)
|