Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,74 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
from pathlib import Path
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
-
|
| 10 |
-
return None, "β Please upload both files!"
|
| 11 |
|
| 12 |
-
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
video_path=None,
|
| 22 |
-
bbox_shift=0,
|
| 23 |
-
api_name="/predict"
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
# Result is a file path
|
| 27 |
-
if result and Path(result).exists():
|
| 28 |
-
# Copy to local output
|
| 29 |
-
output = "result.mp4"
|
| 30 |
-
shutil.copy(result, output)
|
| 31 |
-
return output, "β
Video generated successfully!"
|
| 32 |
else:
|
| 33 |
-
return None, "β
|
| 34 |
|
| 35 |
except Exception as e:
|
| 36 |
return None, f"β Error: {str(e)}"
|
| 37 |
|
| 38 |
-
# Gradio UI
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
btn.click(generate_video, [img, aud], [vid, status])
|
| 54 |
-
|
| 55 |
-
gr.Markdown("""
|
| 56 |
-
### π‘ Notes:
|
| 57 |
-
- Uses MuseTalk API (no local installation needed)
|
| 58 |
-
- Processing time: 30-90 seconds
|
| 59 |
-
- Best with clear front-facing images
|
| 60 |
-
""")
|
| 61 |
|
| 62 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
from pathlib import Path
|
| 5 |
|
| 6 |
+
def setup():
|
| 7 |
+
"""One-time setup"""
|
| 8 |
+
if Path("setup_done.txt").exists():
|
| 9 |
+
return
|
| 10 |
+
|
| 11 |
+
print("Installing dependencies...")
|
| 12 |
+
os.system("pip install -q gfpgan realesrgan basicsr")
|
| 13 |
+
|
| 14 |
+
print("Downloading model...")
|
| 15 |
+
os.system("git clone https://github.com/vinthony/video-retalking.git vrt")
|
| 16 |
+
|
| 17 |
+
# Download checkpoints
|
| 18 |
+
os.chdir("vrt")
|
| 19 |
+
os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/30_net_gen.pth -P checkpoints/")
|
| 20 |
+
os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/BFM.zip -P checkpoints/")
|
| 21 |
+
os.system("unzip -q checkpoints/BFM.zip -d checkpoints/")
|
| 22 |
+
os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/DNet.pt -P checkpoints/")
|
| 23 |
+
os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/ENet.pth -P checkpoints/")
|
| 24 |
+
os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/expression.mat -P checkpoints/")
|
| 25 |
+
os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/face3d_pretrain_epoch_20.pth -P checkpoints/")
|
| 26 |
+
os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/GFPGANv1.3.pth -P checkpoints/")
|
| 27 |
+
os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/GPEN-BFR-512.pth -P checkpoints/")
|
| 28 |
+
os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/LNet.pth -P checkpoints/")
|
| 29 |
+
os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/ParseNet-latest.pth -P checkpoints/")
|
| 30 |
+
os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/RetinaFace-R50.pth -P checkpoints/")
|
| 31 |
+
os.system("wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/shape_predictor_68_face_landmarks.dat -P checkpoints/")
|
| 32 |
+
os.chdir("..")
|
| 33 |
+
|
| 34 |
+
Path("setup_done.txt").touch()
|
| 35 |
+
print("β
Setup complete!")
|
| 36 |
+
|
| 37 |
+
def generate(image, audio):
|
| 38 |
+
if not image or not audio:
|
| 39 |
+
return None, "Upload both files!"
|
| 40 |
+
|
| 41 |
try:
|
| 42 |
+
setup()
|
|
|
|
| 43 |
|
| 44 |
+
output = "result.mp4"
|
| 45 |
|
| 46 |
+
# Run inference
|
| 47 |
+
cmd = f"cd vrt && python inference.py --face ../{image} --audio ../{audio} --outfile ../{output}"
|
| 48 |
|
| 49 |
+
result = os.system(cmd)
|
| 50 |
|
| 51 |
+
if Path(output).exists():
|
| 52 |
+
return output, "β
Done!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
else:
|
| 54 |
+
return None, "β Failed to generate"
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
return None, f"β Error: {str(e)}"
|
| 58 |
|
| 59 |
+
# Simple Gradio UI
|
| 60 |
+
demo = gr.Interface(
|
| 61 |
+
fn=generate,
|
| 62 |
+
inputs=[
|
| 63 |
+
gr.Image(type="filepath", label="Face Image"),
|
| 64 |
+
gr.Audio(type="filepath", label="Audio File")
|
| 65 |
+
],
|
| 66 |
+
outputs=[
|
| 67 |
+
gr.Video(label="Result"),
|
| 68 |
+
gr.Textbox(label="Status")
|
| 69 |
+
],
|
| 70 |
+
title="AI Lip Sync Generator",
|
| 71 |
+
description="Upload a face image and audio to generate lip-synced video"
|
| 72 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
demo.launch()
|