gradio/demo/fake_diffusion_with_gif/run.py
aliabid94 9b42ba8f10
Update guides esp plots (#8907)
* changes

* changes

* revert changes

* changes

* add changeset

* notebooks script

* changes

* changes

---------

Co-authored-by: Ali Abid <aliabid94@gmail.com>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>
2024-07-29 22:08:51 -07:00

46 lines
1.3 KiB
Python

import gradio as gr
import numpy as np
import time
import os
from PIL import Image
import requests
from io import BytesIO
def create_gif(images):
pil_images = []
for image in images:
if isinstance(image, str):
response = requests.get(image)
image = Image.open(BytesIO(response.content))
else:
image = Image.fromarray((image * 255).astype(np.uint8))
pil_images.append(image)
fp_out = os.path.join(os.path.dirname(__file__), "image.gif")
img = pil_images.pop(0)
img.save(fp=fp_out, format='GIF', append_images=pil_images,
save_all=True, duration=400, loop=0)
return fp_out
def fake_diffusion(steps):
rng = np.random.default_rng()
images = []
for _ in range(steps):
time.sleep(1)
image = rng.random((600, 600, 3))
images.append(image)
yield image, gr.Image(visible=False)
time.sleep(1)
image = "https://gradio-builds.s3.amazonaws.com/diffusion_image/cute_dog.jpg"
images.append(image)
gif_path = create_gif(images)
yield image, gr.Image(value=gif_path, visible=True)
demo = gr.Interface(fake_diffusion,
inputs=gr.Slider(1, 10, 3, step=1),
outputs=["image", gr.Image(label="All Images", visible=False)])
if __name__ == "__main__":
demo.launch()