Skip to content

Update to genai usage #2714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions notebooks/stable-diffusion-xl/gradio_helper.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import gradio as gr
from diffusers.utils import load_image
import numpy as np
from PIL import Image

import openvino as ov
import openvino_genai as ov_genai


# TODO Consider reusing make_demo_segmind_vegart
def make_demo_sd_xl_text2image(pipeline):
def generate_from_text(text, seed, num_steps):
result = pipeline(
image_tensor = pipeline.generate(
text,
num_inference_steps=num_steps,
generator=np.random.RandomState(seed),
height=512,
width=512,
).images[0]
return result
generator=ov_genai.TorchGenerator(seed),
)
image = Image.fromarray(image_tensor.data[0])

return image

with gr.Blocks() as demo:
with gr.Column():
Expand Down Expand Up @@ -59,13 +65,21 @@ def make_demo_sd_xl_image2image(pipeline):
)

def generate_from_image(text, image, seed, num_steps):
result = pipeline(
def image_to_tensor(image: Image) -> ov.Tensor:
pic = image.convert("RGB")
image_data = np.array(pic.getdata()).reshape(1, pic.size[1], pic.size[0], 3).astype(np.uint8)
return ov.Tensor(image_data)

init_image = image_to_tensor(image)
photo_image_tensor = pipeline.generate(
text,
image=image,
image=init_image,
num_inference_steps=num_steps,
generator=np.random.RandomState(seed),
).images[0]
return result
generator=ov_genai.TorchGenerator(seed),
)
photo_image = Image.fromarray(photo_image_tensor.data[0])

return photo_image

with gr.Blocks() as demo:
with gr.Column():
Expand Down
159 changes: 79 additions & 80 deletions notebooks/stable-diffusion-xl/stable-diffusion-xl.ipynb

Large diffs are not rendered by default.

25 changes: 18 additions & 7 deletions notebooks/tiny-sd-image-generation/gradio_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import gradio as gr
import requests

sample_image_name = "tower.jpg"
sample_image_1 = ["tower.jpg", "https://storage.openvinotoolkit.org/repositories/openvino_notebooks/data/data/image/tower.jpg"]
sample_image_2 = ["picture.jpg", "https://user-images.githubusercontent.com/29454499/260418860-69cc443a-9ee6-493c-a393-3a97af080be7.jpg"]

if not Path(sample_image_name).exists():
r = requests.get("https://storage.openvinotoolkit.org/repositories/openvino_notebooks/data/data/image/tower.jpg")
with open(sample_image_name, "wb") as f:
f.write(r.content)
for sample_image in [sample_image_1, sample_image_2]:
if not Path(sample_image[0]).exists():
r = requests.get(sample_image[1])
with open(sample_image[0], "wb") as f:
f.write(r.content)


def make_demo(text_to_text_fn: Callable, image_to_image_fn: Callable):
Expand Down Expand Up @@ -49,7 +51,6 @@ def make_demo(text_to_text_fn: Callable, image_to_image_fn: Callable):
strength_input = gr.Slider(0, 1, value=0.5, label="Strength")
i2i_output = gr.Image(label="Result", type="pil")
i2i_btn = gr.Button()
sample_i2i_text = "amazing watercolor painting"
i2i_btn.click(
fn=image_to_image_fn,
inputs=[
Expand All @@ -63,7 +64,17 @@ def make_demo(text_to_text_fn: Callable, image_to_image_fn: Callable):
outputs=i2i_output,
)
gr.Examples(
[[sample_image_name, sample_i2i_text, "", 6400023, 40, 0.3]],
[
[
sample_image_2[0],
"professional photo portrait of woman, highly detailed, hyper realistic, cinematic effects, soft lighting",
"blurry, poor quality, low res, worst quality, cropped, ugly, poorly drawn face, without eyes, mutation, unreal, animate, poorly drawn eyes",
8098234,
40,
0.68,
],
[sample_image_1[0], "amazing watercolor painting", "", 6400023, 40, 0.3],
],
[
i2i_input,
i2i_text_input,
Expand Down
Loading
Loading