Update app.py
Browse files
app.py
CHANGED
|
@@ -1,126 +1,126 @@
|
|
| 1 |
-
import os, sys
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import torch
|
| 4 |
-
import shutil
|
| 5 |
-
from src.gradio_demo import SadTalker
|
| 6 |
-
|
| 7 |
-
def sadtalker_demo():
|
| 8 |
-
# For Hugging Face, we'll use the current directory structure
|
| 9 |
-
checkpoint_path = 'checkpoints'
|
| 10 |
-
config_path = 'src/config'
|
| 11 |
-
|
| 12 |
-
try:
|
| 13 |
-
sad_talker = SadTalker(checkpoint_path, config_path, lazy_load=True)
|
| 14 |
-
except Exception as e:
|
| 15 |
-
print(f"Warning: Could not initialize SadTalker: {e}")
|
| 16 |
-
sad_talker = None
|
| 17 |
-
|
| 18 |
-
def generate_video(source_image, driven_audio, preprocess_type, is_still_mode, enhancer, batch_size, size_of_image, pose_style):
|
| 19 |
-
if sad_talker is None:
|
| 20 |
-
return "Error: SadTalker not initialized. Please ensure all model files are uploaded."
|
| 21 |
-
|
| 22 |
-
try:
|
| 23 |
-
return sad_talker.test(
|
| 24 |
-
source_image=source_image,
|
| 25 |
-
driven_audio=driven_audio,
|
| 26 |
-
preprocess=preprocess_type,
|
| 27 |
-
still_mode=is_still_mode,
|
| 28 |
-
use_enhancer=enhancer,
|
| 29 |
-
batch_size=batch_size,
|
| 30 |
-
size=size_of_image,
|
| 31 |
-
pose_style=pose_style
|
| 32 |
-
)
|
| 33 |
-
except Exception as e:
|
| 34 |
-
return f"Error generating video: {str(e)}"
|
| 35 |
-
with gr.Row().style(equal_height=False):
|
| 36 |
-
with gr.Column(variant='panel'):
|
| 37 |
-
with gr.Tabs(elem_id="sadtalker_source_image"):
|
| 38 |
-
with gr.TabItem('Upload image'):
|
| 39 |
-
with gr.Row():
|
| 40 |
-
source_image = gr.Image(
|
| 41 |
-
label="Source image",
|
| 42 |
-
source="upload",
|
| 43 |
-
type="filepath",
|
| 44 |
-
elem_id="img2img_image"
|
| 45 |
-
).style(width=512)
|
| 46 |
-
|
| 47 |
-
with gr.Tabs(elem_id="sadtalker_driven_audio"):
|
| 48 |
-
with gr.TabItem('Upload Audio'):
|
| 49 |
-
with gr.Column(variant='panel'):
|
| 50 |
-
driven_audio = gr.Audio(
|
| 51 |
-
label="Input audio",
|
| 52 |
-
source="upload",
|
| 53 |
-
type="filepath"
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
with gr.Column(variant='panel'):
|
| 57 |
-
with gr.Tabs(elem_id="sadtalker_checkbox"):
|
| 58 |
-
with gr.TabItem('Settings'):
|
| 59 |
-
gr.Markdown("""
|
| 60 |
-
Need help? Please visit our [best practice page](https://github.com/OpenTalker/SadTalker/blob/main/docs/best_practice.md) for more details
|
| 61 |
-
""")
|
| 62 |
-
with gr.Column(variant='panel'):
|
| 63 |
-
pose_style = gr.Slider(
|
| 64 |
-
minimum=0,
|
| 65 |
-
maximum=46,
|
| 66 |
-
step=1,
|
| 67 |
-
label="Pose style",
|
| 68 |
-
value=0
|
| 69 |
-
)
|
| 70 |
-
size_of_image = gr.Radio(
|
| 71 |
-
[256, 512],
|
| 72 |
-
value=256,
|
| 73 |
-
label='Face model resolution',
|
| 74 |
-
info="Use 256/512 model?"
|
| 75 |
-
)
|
| 76 |
-
preprocess_type = gr.Radio(
|
| 77 |
-
['crop', 'resize','full', 'extcrop', 'extfull'],
|
| 78 |
-
value='crop',
|
| 79 |
-
label='preprocess',
|
| 80 |
-
info="How to handle input image?"
|
| 81 |
-
)
|
| 82 |
-
is_still_mode = gr.Checkbox(
|
| 83 |
-
label="Still Mode (fewer head motion, works with preprocess `full`)"
|
| 84 |
-
)
|
| 85 |
-
batch_size = gr.Slider(
|
| 86 |
-
label="Batch size in generation",
|
| 87 |
-
step=1,
|
| 88 |
-
maximum=10,
|
| 89 |
-
value=2
|
| 90 |
-
)
|
| 91 |
-
enhancer = gr.Checkbox(
|
| 92 |
-
label="GFPGAN as Face enhancer"
|
| 93 |
-
)
|
| 94 |
-
submit = gr.Button(
|
| 95 |
-
'Generate',
|
| 96 |
-
elem_id="sadtalker_generate",
|
| 97 |
-
variant='primary'
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
with gr.Tabs(elem_id="sadtalker_generated"):
|
| 101 |
-
gen_video = gr.Video(
|
| 102 |
-
label="Generated video",
|
| 103 |
-
format="mp4"
|
| 104 |
-
).style(width=512)
|
| 105 |
-
|
| 106 |
-
submit.click(
|
| 107 |
-
fn=generate_video,
|
| 108 |
-
inputs=[
|
| 109 |
-
source_image,
|
| 110 |
-
driven_audio,
|
| 111 |
-
preprocess_type,
|
| 112 |
-
is_still_mode,
|
| 113 |
-
enhancer,
|
| 114 |
-
batch_size,
|
| 115 |
-
size_of_image,
|
| 116 |
-
pose_style
|
| 117 |
-
],
|
| 118 |
-
outputs=[gen_video]
|
| 119 |
-
)
|
| 120 |
-
|
| 121 |
-
return sadtalker_interface
|
| 122 |
-
|
| 123 |
-
if __name__ == "__main__":
|
| 124 |
-
demo = sadtalker_demo()
|
| 125 |
-
demo.queue()
|
| 126 |
demo.launch()
|
|
|
|
| 1 |
+
import os, sys
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
import shutil
|
| 5 |
+
from src.gradio_demo import SadTalker
|
| 6 |
+
|
| 7 |
+
def sadtalker_demo():
|
| 8 |
+
# For Hugging Face, we'll use the current directory structure
|
| 9 |
+
checkpoint_path = 'checkpoints'
|
| 10 |
+
config_path = 'src/config'
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
sad_talker = SadTalker(checkpoint_path, config_path, lazy_load=True)
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"Warning: Could not initialize SadTalker: {e}")
|
| 16 |
+
sad_talker = None
|
| 17 |
+
|
| 18 |
+
def generate_video(source_image, driven_audio, preprocess_type, is_still_mode, enhancer, batch_size, size_of_image, pose_style):
|
| 19 |
+
if sad_talker is None:
|
| 20 |
+
return "Error: SadTalker not initialized. Please ensure all model files are uploaded."
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
return sad_talker.test(
|
| 24 |
+
source_image=source_image,
|
| 25 |
+
driven_audio=driven_audio,
|
| 26 |
+
preprocess=preprocess_type,
|
| 27 |
+
still_mode=is_still_mode,
|
| 28 |
+
use_enhancer=enhancer,
|
| 29 |
+
batch_size=batch_size,
|
| 30 |
+
size=size_of_image,
|
| 31 |
+
pose_style=pose_style
|
| 32 |
+
)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Error generating video: {str(e)}"
|
| 35 |
+
with gr.Row().style(equal_height=False):
|
| 36 |
+
with gr.Column(variant='panel'):
|
| 37 |
+
with gr.Tabs(elem_id="sadtalker_source_image"):
|
| 38 |
+
with gr.TabItem('Upload image'):
|
| 39 |
+
with gr.Row():
|
| 40 |
+
source_image = gr.Image(
|
| 41 |
+
label="Source image",
|
| 42 |
+
source="upload",
|
| 43 |
+
type="filepath",
|
| 44 |
+
elem_id="img2img_image"
|
| 45 |
+
).style(width=512)
|
| 46 |
+
|
| 47 |
+
with gr.Tabs(elem_id="sadtalker_driven_audio"):
|
| 48 |
+
with gr.TabItem('Upload Audio'):
|
| 49 |
+
with gr.Column(variant='panel'):
|
| 50 |
+
driven_audio = gr.Audio(
|
| 51 |
+
label="Input audio",
|
| 52 |
+
source="upload",
|
| 53 |
+
type="filepath"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
with gr.Column(variant='panel'):
|
| 57 |
+
with gr.Tabs(elem_id="sadtalker_checkbox"):
|
| 58 |
+
with gr.TabItem('Settings'):
|
| 59 |
+
gr.Markdown("""
|
| 60 |
+
Need help? Please visit our [best practice page](https://github.com/OpenTalker/SadTalker/blob/main/docs/best_practice.md) for more details
|
| 61 |
+
""")
|
| 62 |
+
with gr.Column(variant='panel'):
|
| 63 |
+
pose_style = gr.Slider(
|
| 64 |
+
minimum=0,
|
| 65 |
+
maximum=46,
|
| 66 |
+
step=1,
|
| 67 |
+
label="Pose style",
|
| 68 |
+
value=0
|
| 69 |
+
)
|
| 70 |
+
size_of_image = gr.Radio(
|
| 71 |
+
[256, 512],
|
| 72 |
+
value=256,
|
| 73 |
+
label='Face model resolution',
|
| 74 |
+
info="Use 256/512 model?"
|
| 75 |
+
)
|
| 76 |
+
preprocess_type = gr.Radio(
|
| 77 |
+
['crop', 'resize','full', 'extcrop', 'extfull'],
|
| 78 |
+
value='crop',
|
| 79 |
+
label='preprocess',
|
| 80 |
+
info="How to handle input image?"
|
| 81 |
+
)
|
| 82 |
+
is_still_mode = gr.Checkbox(
|
| 83 |
+
label="Still Mode (fewer head motion, works with preprocess `full`)"
|
| 84 |
+
)
|
| 85 |
+
batch_size = gr.Slider(
|
| 86 |
+
label="Batch size in generation",
|
| 87 |
+
step=1,
|
| 88 |
+
maximum=10,
|
| 89 |
+
value=2
|
| 90 |
+
)
|
| 91 |
+
enhancer = gr.Checkbox(
|
| 92 |
+
label="GFPGAN as Face enhancer"
|
| 93 |
+
)
|
| 94 |
+
submit = gr.Button(
|
| 95 |
+
'Generate',
|
| 96 |
+
elem_id="sadtalker_generate",
|
| 97 |
+
variant='primary'
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
with gr.Tabs(elem_id="sadtalker_generated"):
|
| 101 |
+
gen_video = gr.Video(
|
| 102 |
+
label="Generated video",
|
| 103 |
+
format="mp4"
|
| 104 |
+
).style(width=512)
|
| 105 |
+
|
| 106 |
+
submit.click(
|
| 107 |
+
fn=generate_video,
|
| 108 |
+
inputs=[
|
| 109 |
+
source_image,
|
| 110 |
+
driven_audio,
|
| 111 |
+
preprocess_type,
|
| 112 |
+
is_still_mode,
|
| 113 |
+
enhancer,
|
| 114 |
+
batch_size,
|
| 115 |
+
size_of_image,
|
| 116 |
+
pose_style
|
| 117 |
+
],
|
| 118 |
+
outputs=[gen_video]
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
return sadtalker_interface
|
| 122 |
+
|
| 123 |
+
if __name__ == "__main__":
|
| 124 |
+
demo = sadtalker_demo()
|
| 125 |
+
demo.queue()
|
| 126 |
demo.launch()
|