File size: 2,731 Bytes
c259297
 
 
18fa0fe
3022b1a
c259297
a645f56
eea9420
 
 
 
 
c259297
a645f56
c259297
18fa0fe
8a3da89
eea9420
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c259297
 
 
 
 
64cfc02
a645f56
 
 
 
 
c259297
 
 
 
 
8d51740
c259297
 
 
 
a645f56
8d51740
a645f56
 
 
 
eea9420
a645f56
 
 
c259297
eea9420
 
 
 
 
 
 
 
 
 
 
 
c259297
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from PIL import Image
import torch
import re
import gradio as gr
import random
from diffusers import AutoPipelineForText2Image
from diffusers import AutoPipelineForImage2Image
from diffusers.utils import load_image, export_to_video
from diffusers import StableVideoDiffusionPipeline


pipelineVideo = StableVideoDiffusionPipeline.from_pretrained("stabilityai/stable-video-diffusion-img2vid-xt",).to("cuda")
pipeline_text2image = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16")
pipeline_image2image = AutoPipelineForImage2Image.from_pipe(pipeline_text2image).to("cuda")
pipeline_text2image = pipeline_text2image.to("cuda")

def image2video(image,seed="",fps=7,outfile=""):
    if seed=="":
        seed=random.randint(0, 5000)
    
    else:
        try:
            seed=int(seed)
        except:
            seed=random.randint(0, 5000)
    if outfile=="":
        outfile=str(seed)+".mp4"
    image = load_image(image)
    image = image.resize((1024, 576))
    generator = torch.manual_seed(seed)
    frames = pipeline(image, decode_chunk_size=8, generator=generator).frames[0]
    export_to_video(frames, outfile, fps=fps)
    return outfile

def text2img(prompt = "A cinematic shot of a baby racoon wearing an intricate italian priest robe.",guidance_scale=0.0, num_inference_steps=1):
    image = pipeline_text2image(prompt=prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps).images[0]
    return image

def img2img(image,prompt="A cinematic shot of a baby racoon wearing an intricate italian priest robe.", guidance_scale=0.0, num_inference_steps=1,strength=0.5):
    init_image = load_image(image)
    init_image = init_image.resize((512, 512))
    image = pipeline_image2image(prompt, image=init_image, strength=strength, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps).images[0]
    return image

gradio_app_text2img = gr.Interface(
    fn=text2img,
    inputs=[
        gr.Text(),
        gr.Slider(0.0, 10.0, value=1,step=0.1),
        gr.Slider(0.0, 100.0, value=1,step=1)
    ],
    outputs="image",
)

gradio_app_img2img = gr.Interface(
    fn=img2img,
    inputs=[
        gr.Image(type='filepath'),
        gr.Text(),
        gr.Slider(0.0, 10.0, value=1,step=0.1),
        gr.Text()
    ],
    outputs="image",
)

gradio_app_img2video = gr.Interface(
    fn=img2img,
    inputs=[
        gr.Image(type='filepath'),
        gr.Text(),
        gr.Slider(0.0, 40.0, value=9,step=1),
        gr.Text()
    ],
    outputs="video",
)

demo = gr.TabbedInterface([gradio_app_text2img,gradio_app_img2img,gradio_app_img2video], ["text2img","img2img","img2video"])

if __name__ == "__main__":
    demo.launch()