jeremyrmanning's picture
change default prompt
967d5a3
raw
history blame
2.31 kB
import warnings
import gradio as gr
from transformers import pipeline
import io, base64
from PIL import Image
import numpy as np
import tensorflow as tf
import mediapy
import os
import sys
from huggingface_hub import snapshot_download
#CREDIT: this demo is based *heavily* on https://huggingface.co/spaces/osanseviero/latent-video
with warnings.catch_warnings():
warnings.simplefilter('ignore')
image_gen = gr.Interface.load("spaces/multimodalart/latentdiffusion")
os.system("git clone https://github.com/google-research/frame-interpolation")
sys.path.append("frame-interpolation")
from eval import interpolator, util
ffmpeg_path = util.get_ffmpeg_path()
mediapy.set_ffmpeg(ffmpeg_path)
model = snapshot_download(repo_id="akhaliq/frame-interpolation-film-style")
interpolator = interpolator.Interpolator(model, None)
def generate_images(text, width=256, height=256, steps=50, num_images=2,
diversity=4):
image_bytes = image_gen(text, steps, width, height, num_images, diversity)
# Algo from spaces/Gradio-Blocks/latent_gpt2_story/blob/main/app.py
generated_images = []
for image in image_bytes[1]:
image_str = image[0]
image_str = image_str.replace("data:image/png;base64,","")
decoded_bytes = base64.decodebytes(bytes(image_str, "utf-8"))
img = Image.open(io.BytesIO(decoded_bytes))
generated_images.append(img)
return generated_images
def generate_interpolation(text, fps=7, steps=4):
images = []
frames = []
for i, t in enumerate(text.split(', ')):
print(f'image {i}: {t.lower().strip()}', end='...')
images.extend(generate_images(t.lower().strip()))
print('done!')
frames.append(f'frame_{i}.png')
images[-1].save(frames[-1])
vid = list(util.interpolate_recursively_from_files(frames, steps, interpolator))
mediapy.write_video("out.mp4", vid, fps=fps)
return "out.mp4"
demo = gr.Blocks()
with demo:
text = gr.Textbox(placeholder='human, human brain, brain in a computer, humanoid robot', label='Input a comma-separated list of terms:')
button = gr.Button("Generate Video")
output = gr.Video(label="Generated Video")
button.click(fn=generate_interpolation, inputs=text, outputs=output)
demo.launch(debug=True, enable_queue=True)