CoolFace
Apppublic

Shellbrady/magicanimate

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py115 linesDownload Raw Back to root
1# Copyright 2023 ByteDance and/or its affiliates.2#3# Copyright (2023) MagicAnimate Authors4#5# ByteDance, its affiliates and licensors retain all intellectual6# property and proprietary rights in and to this material, related7# documentation and any modifications thereto. Any use, reproduction,8# disclosure or distribution of this material and related documentation9# without an express license agreement from ByteDance or10# its affiliates is strictly prohibited.11import argparse12import imageio13import numpy as np14import gradio as gr15import os16from PIL import Image17from subprocess import PIPE, run18 19from demo.animate import MagicAnimate20 21from huggingface_hub import snapshot_download22 23snapshot_download(repo_id="runwayml/stable-diffusion-v1-5", local_dir="./stable-diffusion-v1-5")24snapshot_download(repo_id="stabilityai/sd-vae-ft-mse", local_dir="./sd-vae-ft-mse")25snapshot_download(repo_id="zcxu-eric/MagicAnimate", local_dir="./MagicAnimate")26 27is_spaces = True if "SPACE_ID" in os.environ else False28true_for_shared_ui = False #This will be true only if you are in a shared UI29if(is_spaces):30    true_for_shared_ui = True if "zcxu-eric/magicanimate" in os.environ['SPACE_ID'] else False31 32 33animator = MagicAnimate()34 35def animate(reference_image, motion_sequence_state, seed=1, steps=25, guidance_scale=7.5):36    return animator(reference_image, motion_sequence_state, seed, steps, guidance_scale)37 38with gr.Blocks() as demo:39 40    gr.HTML(41        """42        <div style="display: flex; justify-content: center; align-items: center; text-align: center;">43        <a href="https://github.com/magic-research/magic-animate" style="margin-right: 20px; text-decoration: none; display: flex; align-items: center;">44        </a>45        <div>46            <h1 >MagicAnimate: Temporally Consistent Human Image Animation using Diffusion Model</h1>47            <h5 style="margin: 0;">If you like our project, please give us a star ✨ on Github for the latest update.</h5>48            <div style="display: flex; justify-content: center; align-items: center; text-align: center;>49                <a href="https://arxiv.org/abs/2311.16498"><img src="https://img.shields.io/badge/Arxiv-2311.16498-red"></a>50                <a href='https://showlab.github.io/magicanimate'><img src='https://img.shields.io/badge/Project_Page-MagicAnimate-green' alt='Project Page'></a>51                <a href='https://github.com/magic-research/magic-animate'><img src='https://img.shields.io/badge/Github-Code-blue'></a>52            </div>53        </div>54        </div>55        """)56    animation = gr.Video(format="mp4", label="Animation Results", autoplay=True)57 58    with gr.Row():59        reference_image  = gr.Image(label="Reference Image")60        motion_sequence  = gr.Video(format="mp4", label="Motion Sequence",max_length=5)61 62        with gr.Column():63            random_seed         = gr.Textbox(label="Random seed", value=1, info="default: -1")64            sampling_steps      = gr.Textbox(label="Sampling steps", value=25, info="default: 25")65            guidance_scale      = gr.Textbox(label="Guidance scale", value=7.5, info="default: 7.5")66            submit              = gr.Button("Animate")67 68    def read_video(video):69        reader = imageio.get_reader(video)70        fps = reader.get_meta_data()['fps']71        return video72 73    def read_image(image, size=512):74        return np.array(Image.fromarray(image).resize((size, size)))75 76    # when user uploads a new video77    motion_sequence.upload(78        read_video,79        motion_sequence,80        motion_sequence,81        queue=False82    )83    # when `first_frame` is updated84    reference_image.upload(85        read_image,86        reference_image,87        reference_image,88        queue=False89    )90    # when the `submit` button is clicked91    submit.click(92        animate,93        [reference_image, motion_sequence, random_seed, sampling_steps, guidance_scale],94        animation95    )96 97    # Examples98    gr.Markdown("## Examples")99    gr.Examples(100        fn=animate,101        examples=[102            ["inputs/applications/source_image/monalisa.png", "inputs/applications/driving/densepose/running.mp4"],103            ["inputs/applications/source_image/demo4.png", "inputs/applications/driving/densepose/demo4.mp4"],104            ["inputs/applications/source_image/dalle2.jpeg", "inputs/applications/driving/densepose/running2.mp4"],105            ["inputs/applications/source_image/dalle8.jpeg", "inputs/applications/driving/densepose/dancing2.mp4"],106            ["inputs/applications/source_image/multi1_source.png", "inputs/applications/driving/densepose/multi_dancing.mp4"],107        ],108        inputs=[reference_image, motion_sequence],109        outputs=animation,110        cache_examples=true_for_shared_ui111    )112 113# demo.queue(max_size=15, api_open=False)114demo.launch(share=True, show_api=False)115