2022-09-08 22:35:31 +08:00
|
|
|
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):
|
2024-02-13 02:20:46 +08:00
|
|
|
rng = np.random.default_rng()
|
2022-09-08 22:35:31 +08:00
|
|
|
images = []
|
|
|
|
for _ in range(steps):
|
|
|
|
time.sleep(1)
|
2024-02-13 02:20:46 +08:00
|
|
|
image = rng.random((600, 600, 3))
|
2022-09-08 22:35:31 +08:00
|
|
|
images.append(image)
|
2023-09-19 12:17:06 +08:00
|
|
|
yield image, gr.Image(visible=False)
|
2024-01-27 00:52:10 +08:00
|
|
|
|
2022-09-08 22:35:31 +08:00
|
|
|
time.sleep(1)
|
2024-01-27 00:52:10 +08:00
|
|
|
image = "https://gradio-builds.s3.amazonaws.com/diffusion_image/cute_dog.jpg"
|
2022-09-08 22:35:31 +08:00
|
|
|
images.append(image)
|
|
|
|
gif_path = create_gif(images)
|
2024-01-27 00:52:10 +08:00
|
|
|
|
2023-09-19 12:17:06 +08:00
|
|
|
yield image, gr.Image(value=gif_path, visible=True)
|
2022-09-08 22:35:31 +08:00
|
|
|
|
2024-01-27 00:52:10 +08:00
|
|
|
demo = gr.Interface(fake_diffusion,
|
|
|
|
inputs=gr.Slider(1, 10, 3, step=1),
|
2022-09-08 22:35:31 +08:00
|
|
|
outputs=["image", gr.Image(label="All Images", visible=False)])
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
demo.launch()
|