Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273import osimport argparsefrom PIL import Imageimport numpy as npimport torchfrom transformers import CLIPImageProcessor, CLIPVisionModelWithProjectionfrom diffusers import AutoencoderKL, EulerDiscreteSchedulerfrom diffusers.utils import export_to_giffrom diffusers_sv3d import SV3DUNetSpatioTemporalConditionModel, StableVideo3DDiffusionPipelineSV3D_DIFFUSERS = "chenguolin/sv3d-diffusers"# os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"os.environ["HF_HOME"] = "~/.cache/huggingface"def main():parser = argparse.ArgumentParser()parser.add_argument("--output_dir", default="out/", type=str, help="Output filepath")parser.add_argument("--image_path", default="assets/img1.jpg", type=str, help="Image filepath")parser.add_argument("--elevation", default=10, type=float, help="Camera elevation of the input image")parser.add_argument("--half_precision", action="store_true", help="Use fp16 half precision")parser.add_argument("--seed", default=-1, type=int, help="Random seed")args = parser.parse_args()unet = SV3DUNetSpatioTemporalConditionModel.from_pretrained(SV3D_DIFFUSERS, subfolder="unet")vae = AutoencoderKL.from_pretrained(SV3D_DIFFUSERS, subfolder="vae")scheduler = EulerDiscreteScheduler.from_pretrained(SV3D_DIFFUSERS, subfolder="scheduler")image_encoder = CLIPVisionModelWithProjection.from_pretrained(SV3D_DIFFUSERS, subfolder="image_encoder")feature_extractor = CLIPImageProcessor.from_pretrained(SV3D_DIFFUSERS, subfolder="feature_extractor")pipeline = StableVideo3DDiffusionPipeline(image_encoder=image_encoder, feature_extractor=feature_extractor,unet=unet, vae=vae,scheduler=scheduler,)num_frames, sv3d_res = 21, 576elevations_deg = [args.elevation] * num_framespolars_rad = [np.deg2rad(90 - e) for e in elevations_deg]azimuths_deg = np.linspace(0, 360, num_frames + 1)[1:] % 360azimuths_rad = [np.deg2rad((a - azimuths_deg[-1]) % 360) for a in azimuths_deg]azimuths_rad[:-1].sort()pipeline = pipeline.to("cuda")with torch.no_grad():with torch.autocast("cuda", dtype=torch.float16 if args.half_precision else torch.float32, enabled=True):image = Image.open(args.image_path)image.load() # required for `.split()`if len(image.split()) == 4: # RGBAinput_image = Image.new("RGB", image.size, (255, 255, 255)) # pure white bginput_image.paste(image, mask=image.split()[3]) # 3rd is the alpha channelelse:input_image = imagevideo_frames = pipeline(input_image.resize((sv3d_res, sv3d_res)),height=sv3d_res,width=sv3d_res,num_frames=num_frames,decode_chunk_size=8, # smaller to save memorypolars_rad=polars_rad,azimuths_rad=azimuths_rad,generator=torch.manual_seed(args.seed) if args.seed >= 0 else None,).frames[0]os.makedirs(args.output_dir, exist_ok=True)name = os.path.basename(args.image_path).split(".")[0]export_to_gif(video_frames, f"{args.output_dir}/{name}.gif", fps=7)if __name__ == "__main__":main()12345678910111213141516171819import gradio as grfrom infer import generate_videodef gradio_generate_video(image, elevation=10):image_path = image.name # Save the image to a temporary fileimage.save(image_path) # Save uploaded image to diskoutput_gif = generate_video(image_path=image_path, output_dir="out/", elevation=elevation)return output_gif # Return the generated video pathiface = gr.Interface(fn=gradio_generate_video,inputs=[gr.inputs.Image(type="filepath"),gr.inputs.Slider(minimum=0, maximum=90, default=10, label="Camera Elevation"),],outputs=gr.outputs.Video(),)iface.launch()
app.py+19-73