Spaces:
Runtime error
Runtime error
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) | |