talker / app.py
1dm's picture
desactivation mise en cache example
1392a39 verified
import os, sys
import gradio as gr
from huggingface_hub import snapshot_download
# Assurez-vous que le chemin d'importation de SadTalker est correct
# Cela suppose que 'src' est dans le même répertoire que ce fichier.
from src.gradio_demo import SadTalker
# --- Fonctions de l'interface utilisateur ---
def toggle_audio_file(choice):
"""Bascule l'affichage entre l'audio standard et le mode IDLE (pas d'audio requis)."""
if choice == False:
# L'utilisateur désactive 'Use Idle Animation' -> afficher le champ Audio
return gr.update(visible=True), gr.update(visible=False)
else:
# L'utilisateur active 'Use Idle Animation' -> cacher le champ Audio
return gr.update(visible=False), gr.update(visible=True)
def ref_video_fn(path_of_ref_video):
"""Met à jour la checkbox 'Use Reference Video' si une vidéo de référence est chargée."""
if path_of_ref_video is not None:
return gr.update(value=True)
else:
return gr.update(value=False)
def download_model():
"""Télécharge les checkpoints du modèle SadTalker."""
REPO_ID = 'vinthony/SadTalker-V002rc'
# Utilisation de local_dir='./checkpoints' pour le standard SadTalker
snapshot_download(repo_id=REPO_ID, local_dir='./checkpoints', local_dir_use_symlinks=False)
# --- Fonction principale de l'interface Gradio ---
def sadtalker_demo():
# 1. Téléchargement des modèles
print("Téléchargement des modèles SadTalker...")
download_model()
# 2. Initialisation du modèle (lazy_load=True retarde le chargement des poids)
sad_talker = SadTalker(lazy_load=True)
with gr.Blocks(analytics_enabled=False) as sadtalker_interface:
gr.Markdown("<div align='center'> <h2> 😭 SadTalker: Learning Realistic 3D Motion Coefficients for Stylized Audio-Driven Single Image Talking Face Animation </span> </h2> \
<a style='font-size:18px;color: #efefef' href='https://arxiv.org/abs/2211.12194'>Arxiv</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
<a style='font-size:18px;color: #efefef' href='https://sadtalker.github.io'>Homepage</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
<a style='font-size:18px;color: #efefef' href='https://github.com/Winfredy/SadTalker'> Github </div>")
gr.Markdown("""
<b>🚀 Dupliquez l'espace et mettez à niveau vers un GPU pour de meilleures performances et une inférence plus rapide sans attendre dans la file d'attente. <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> \
<br/><b>Alternativement, essayez notre <a href=https://github.com/Winfredy/SadTalker> code GitHub </a> sur votre propre 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> \
""")
with gr.Row():
with gr.Column(variant='panel'):
with gr.Tabs(elem_id="sadtalker_source_image"):
with gr.TabItem('Source image'):
with gr.Row():
source_image = gr.Image(label="Source image", source="upload", type="filepath", elem_id="img2img_image")
with gr.Tabs(elem_id="sadtalker_driven_audio"):
with gr.TabItem('Driving Methods'):
gr.Markdown("Combinaisons possibles : <br> 1. Audio seul 2. Audio/IDLE Mode + Ref Video 3. IDLE Mode seul 4. Ref Video seul ")
with gr.Row():
driven_audio = gr.Audio(label="Input audio", source="upload", type="filepath", max_length=180)
# Audio invisible pour le mode IDLE
driven_audio_no = gr.Audio(label="Use IDLE mode, no audio is required", source="upload", type="filepath", visible=False)
with gr.Column():
use_idle_mode = gr.Checkbox(label="Use Idle Animation")
length_of_audio = gr.Number(value=5, label="The length(seconds) of the generated video.")
# Action pour basculer les champs audio
use_idle_mode.change(toggle_audio_file, inputs=use_idle_mode, outputs=[driven_audio, driven_audio_no])
with gr.Row():
ref_video = gr.Video(label="Reference Video", source="upload", type="filepath", elem_id="vidref")
with gr.Column():
use_ref_video = gr.Checkbox(label="Use Reference Video")
ref_info = gr.Radio(['pose', 'blink','pose+blink', 'all'], value='pose', label='Reference Video',info="Comment emprunter de la vidéo de référence ?")
ref_video.change(ref_video_fn, inputs=ref_video, outputs=[use_ref_video])
with gr.Column(variant='panel'):
with gr.Tabs(elem_id="sadtalker_checkbox"):
with gr.TabItem('Settings'):
gr.Markdown("Aide ? Visitez notre [[page de meilleures pratiques](https://github.com/OpenTalker/SadTalker/blob/main/docs/best_practice.md)]")
with gr.Column(variant='panel'):
with gr.Row():
pose_style = gr.Slider(minimum=0, maximum=45, step=1, label="Pose style", value=0)
exp_weight = gr.Slider(minimum=0, maximum=3, step=0.1, label="expression scale", value=1)
blink_every = gr.Checkbox(label="use eye blink", value=True)
with gr.Row():
size_of_image = gr.Radio([256, 512], value=256, label='face model resolution', info="Utiliser le modèle 256/512 ?")
preprocess_type = gr.Radio(['crop', 'resize','full', 'extcrop', 'extfull'], value='crop', label='preprocess', info="Comment traiter l'image source?")
with gr.Row():
is_still_mode = gr.Checkbox(label="Still Mode (moins de mouvement de tête, fonctionne avec `full`)")
facerender = gr.Radio(['facevid2vid','pirender'], value='facevid2vid', label='facerender', info="Quel moteur de rendu de visage ?")
with gr.Row():
batch_size = gr.Slider(label="batch size in generation", step=1, maximum=10, value=1)
enhancer = gr.Checkbox(label="GFPGAN as Face enhancer")
submit = gr.Button('Generate', elem_id="sadtalker_generate", variant='primary')
with gr.Tabs(elem_id="sadtalker_genearted"):
gen_video = gr.Video(label="Generated video", format="mp4", scale=1)
# --- Événement de soumission ---
submit.click(
fn=sad_talker.test,
inputs=[source_image,
driven_audio,
preprocess_type,
is_still_mode,
enhancer,
batch_size,
size_of_image,
pose_style,
facerender,
exp_weight,
use_ref_video,
ref_video,
ref_info,
use_idle_mode,
length_of_audio,
blink_every
],
outputs=[gen_video],
)
# --- Exemples ---
with gr.Row():
examples = [
[
'examples/source_image/full_body_1.png',
'examples/driven_audio/bus_chinese.wav',
'crop',
True,
False
],
# ... (le reste des exemples est maintenu tel quel)
[
'examples/source_image/art_5.png',
'examples/driven_audio/RD_Radio31_000.wav',
'resize',
True,
True
],
]
gr.Examples(examples=examples,
inputs=[
source_image,
driven_audio,
preprocess_type,
is_still_mode,
enhancer],
outputs=[gen_video],
fn=sad_talker.test,
# MODIFICATION CLÉ ICI : Desactiver la mise en cache
cache_examples=False)
return sadtalker_interface
if __name__ == "__main__":
demo = sadtalker_demo()
# Configuration cruciale pour l'API (n8n ou autre) et la file d'attente
# api_open=True permet de créer automatiquement une API REST pour l'endpoint 'test' (sad_talker.test)
demo.queue(max_size=10, api_open=True)
demo.launch(debug=True)