Spaces:
Runtime error
Runtime error
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import io, base64
|
4 |
+
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
+
import tensorflow as tf
|
7 |
+
import mediapy
|
8 |
+
import os
|
9 |
+
import sys
|
10 |
+
from huggingface_hub import snapshot_download
|
11 |
+
|
12 |
+
image_gen = gr.Interface.load("spaces/multimodalart/latentdiffusion")
|
13 |
+
|
14 |
+
os.system("git clone https://github.com/google-research/frame-interpolation")
|
15 |
+
sys.path.append("frame-interpolation")
|
16 |
+
from eval import interpolator, util
|
17 |
+
|
18 |
+
ffmpeg_path = util.get_ffmpeg_path()
|
19 |
+
mediapy.set_ffmpeg(ffmpeg_path)
|
20 |
+
|
21 |
+
model = snapshot_download(repo_id="akhaliq/frame-interpolation-film-style")
|
22 |
+
interpolator = interpolator.Interpolator(model, None)
|
23 |
+
|
24 |
+
def generate_story(choice, input_text):
|
25 |
+
query = "<BOS> <{0}> {1}".format(choice, input_text)
|
26 |
+
|
27 |
+
print(query)
|
28 |
+
generated_text = story_gen(query)
|
29 |
+
generated_text = generated_text[0]['generated_text']
|
30 |
+
generated_text = generated_text.split('> ')[2]
|
31 |
+
|
32 |
+
return generated_text
|
33 |
+
|
34 |
+
def generate_images(text):
|
35 |
+
steps=50
|
36 |
+
width=256
|
37 |
+
height=256
|
38 |
+
num_images=4
|
39 |
+
diversity=4
|
40 |
+
image_bytes = image_gen(text, steps, width, height, num_images, diversity)
|
41 |
+
|
42 |
+
# Algo from spaces/Gradio-Blocks/latent_gpt2_story/blob/main/app.py
|
43 |
+
generated_images = []
|
44 |
+
for image in image_bytes[1]:
|
45 |
+
image_str = image[0]
|
46 |
+
image_str = image_str.replace("data:image/png;base64,","")
|
47 |
+
decoded_bytes = base64.decodebytes(bytes(image_str, "utf-8"))
|
48 |
+
img = Image.open(io.BytesIO(decoded_bytes))
|
49 |
+
generated_images.append(img)
|
50 |
+
|
51 |
+
return generated_images
|
52 |
+
|
53 |
+
def generate_interpolation(text):
|
54 |
+
times_to_interpolate = 4
|
55 |
+
|
56 |
+
generated_images = generate_images(text)
|
57 |
+
|
58 |
+
generated_images[0].save('frame_0.png')
|
59 |
+
generated_images[1].save('frame_1.png')
|
60 |
+
generated_images[2].save('frame_2.png')
|
61 |
+
generated_images[3].save('frame_3.png')
|
62 |
+
|
63 |
+
input_frames = ["frame_0.png", "frame_1.png", "frame_2.png", "frame_3.png"]
|
64 |
+
|
65 |
+
frames = list(util.interpolate_recursively_from_files(input_frames, times_to_interpolate, interpolator))
|
66 |
+
|
67 |
+
mediapy.write_video("out.mp4", frames, fps=7)
|
68 |
+
|
69 |
+
return "out.mp4"
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
demo = gr.Blocks()
|
74 |
+
|
75 |
+
with demo:
|
76 |
+
input_start_text = gr.Textbox(placeholder='A yellow face amazon parrot saddles up his horse and goes for a horseback ride across the Amazon river', label="Starting Text")
|
77 |
+
button_gen_video = gr.Button("Generate Video")
|
78 |
+
output_interpolation = gr.Video(label="Generated Video")
|
79 |
+
|
80 |
+
button_gen_video.click(fn=generate_interpolation, inputs=input_start_text, outputs=output_interpolation)
|
81 |
+
|
82 |
+
demo.launch(debug=True, enable_queue=True)
|