RabbitRUI/ruispace
0
1import os, sys2import tempfile3import gradio as gr4from src.gradio_demo import SadTalker 5from src.utils.text2speech import TTSTalker6from huggingface_hub import snapshot_download7 8def get_source_image(image): 9 return image10 11def download_model():12 REPO_ID = 'vinthony/SadTalker'13 snapshot_download(repo_id=REPO_ID, local_dir='./checkpoints', local_dir_use_symlinks=True)14 15def sadtalker_demo():16 17 download_model()18 19 sad_talker = SadTalker(lazy_load=True)20 tts_talker = TTSTalker()21 22 with gr.Blocks(analytics_enabled=False) as sadtalker_interface:23 gr.Markdown("<div align='center'> <h2> 😭 SadTalker: Learning Realistic 3D Motion Coefficients for Stylized Audio-Driven Single Image Talking Face Animation (CVPR 2023) </span> </h2> \24 <a style='font-size:18px;color: #efefef' href='https://arxiv.org/abs/2211.12194'>Arxiv</a> \25 <a style='font-size:18px;color: #efefef' href='https://sadtalker.github.io'>Homepage</a> \26 <a style='font-size:18px;color: #efefef' href='https://github.com/Winfredy/SadTalker'> Github </div>")27 28 29 gr.Markdown("""30 <b>You may duplicate the space and upgrade to GPU in settings for better performance and faster inference without waiting in the queue. <a style='display:inline-block' href="https://huggingface.co/spaces/vinthony/SadTalker?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a></b> \31 <br/><b>Alternatively, try our GitHub <a href=https://github.com/Winfredy/SadTalker> code </a> on your own GPU. </b> <a style='display:inline-block' href="https://github.com/Winfredy/SadTalker"><img src="https://img.shields.io/github/stars/Winfredy/SadTalker?style=social"/></a> \32 """)33 34 with gr.Row().style(equal_height=False):35 with gr.Column(variant='panel'):36 with gr.Tabs(elem_id="sadtalker_source_image"):37 with gr.TabItem('Upload image'):38 with gr.Row():39 source_image = gr.Image(label="Source image", source="upload", type="filepath").style(height=256,width=256)40 41 with gr.Tabs(elem_id="sadtalker_driven_audio"):42 with gr.TabItem('Upload or Generating from TTS'):43 with gr.Column(variant='panel'):44 driven_audio = gr.Audio(label="Input audio(.wav/.mp3)", source="upload", type="filepath")45 46 with gr.Column(variant='panel'):47 input_text = gr.Textbox(label="Generating audio from text", lines=5, placeholder="Alternatively, you can genreate the audio from text using @Coqui.ai TTS.")48 tts = gr.Button('Generate audio',elem_id="sadtalker_audio_generate", variant='primary')49 tts.click(fn=tts_talker.test, inputs=[input_text], outputs=[driven_audio])50 51 52 with gr.Column(variant='panel'): 53 with gr.Tabs(elem_id="sadtalker_checkbox"):54 with gr.TabItem('Settings'):55 with gr.Column(variant='panel'):56 preprocess_type = gr.Radio(['crop','resize','full'], value='crop', label='preprocess', info="How to handle input image?")57 is_still_mode = gr.Checkbox(label="w/ Still Mode (fewer hand motion, works with preprocess `full`)")58 enhancer = gr.Checkbox(label="w/ GFPGAN as Face enhancer")59 submit = gr.Button('Generate', elem_id="sadtalker_generate", variant='primary')60 61 with gr.Tabs(elem_id="sadtalker_genearted"):62 gen_video = gr.Video(label="Generated video", format="mp4").style(width=256)63 64 with gr.Row():65 examples = [66 [67 'examples/source_image/full_body_1.png',68 'examples/driven_audio/bus_chinese.wav',69 'crop',70 True,71 False72 ],73 [74 'examples/source_image/full_body_2.png',75 'examples/driven_audio/japanese.wav',76 'crop',77 False,78 False79 ],80 [81 'examples/source_image/full3.png',82 'examples/driven_audio/deyu.wav',83 'crop',84 False,85 True86 ],87 [88 'examples/source_image/full4.jpeg',89 'examples/driven_audio/eluosi.wav',90 'full',91 False,92 True93 ],94 [95 'examples/source_image/full4.jpeg',96 'examples/driven_audio/imagine.wav',97 'full',98 True,99 True100 ],101 [102 'examples/source_image/full_body_1.png',103 'examples/driven_audio/bus_chinese.wav',104 'full',105 True,106 False107 ],108 [109 'examples/source_image/art_13.png',110 'examples/driven_audio/fayu.wav',111 'resize',112 True,113 False114 ],115 [116 'examples/source_image/art_5.png',117 'examples/driven_audio/chinese_news.wav',118 'resize',119 False,120 False121 ],122 [123 'examples/source_image/art_5.png',124 'examples/driven_audio/RD_Radio31_000.wav',125 'resize',126 True,127 True128 ],129 ]130 gr.Examples(examples=examples,131 inputs=[132 source_image,133 driven_audio,134 preprocess_type,135 is_still_mode,136 enhancer], 137 outputs=[gen_video],138 fn=sad_talker.test,139 cache_examples=os.getenv('SYSTEM') == 'spaces') # 140 141 submit.click(142 fn=sad_talker.test, 143 inputs=[source_image,144 driven_audio,145 preprocess_type,146 is_still_mode,147 enhancer], 148 outputs=[gen_video]149 )150 151 return sadtalker_interface152 153 154if __name__ == "__main__":155 156 demo = sadtalker_demo()157 demo.queue(max_size=10)158 demo.launch(debug=True)159 160 161 