imageGen / app.py
ciditel's picture
bugfix
3022b1a verified
raw
history blame
2.73 kB
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()