Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,81 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import subprocess
|
| 4 |
import uuid
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
from pydub import AudioSegment
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
output_path = f"{uid}_output.mp4"
|
| 24 |
|
| 25 |
-
#
|
| 26 |
image = image.resize((int(image.width * 256 / image.height), 256), Image.Resampling.LANCZOS)
|
| 27 |
-
image.save(
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
return
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
return
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
fn=generate,
|
| 53 |
-
inputs=[
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import uuid
|
| 3 |
+
import subprocess
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
from PIL import Image
|
| 8 |
from pydub import AudioSegment
|
| 9 |
|
| 10 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 11 |
+
# 1. Download model checkpoint once
|
| 12 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 13 |
+
MODEL_PATH = Path("wav2lip_gan.pth")
|
| 14 |
+
MODEL_URL = (
|
| 15 |
+
"https://huggingface.co/spaces/fffiloni/wav2lip/resolve/main/wav2lip_gan.pth"
|
| 16 |
+
) # public mirror
|
| 17 |
+
|
| 18 |
+
if not MODEL_PATH.exists():
|
| 19 |
+
os.system(f"wget -q {MODEL_URL} -O {MODEL_PATH}")
|
| 20 |
|
| 21 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 22 |
+
# 2. Helper: resize image + convert audio β 16 kHz mono WAV
|
| 23 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 24 |
+
def preprocess(image, audio_file):
|
| 25 |
+
if image is None or audio_file is None:
|
| 26 |
+
raise ValueError("Both an image and an audio file are required.")
|
| 27 |
|
| 28 |
+
uid = uuid.uuid4().hex
|
| 29 |
+
img_path = f"{uid}.jpg"
|
| 30 |
+
wav_path = f"{uid}.wav"
|
| 31 |
+
out_path = f"{uid}_result.mp4"
|
|
|
|
| 32 |
|
| 33 |
+
# resize image to 256 px height (keeps aspect ratio)
|
| 34 |
image = image.resize((int(image.width * 256 / image.height), 256), Image.Resampling.LANCZOS)
|
| 35 |
+
image.save(img_path)
|
| 36 |
|
| 37 |
+
# convert audio to 16 kHz mono WAV
|
| 38 |
+
seg = AudioSegment.from_file(audio_file)
|
| 39 |
+
seg = seg.set_frame_rate(16_000).set_channels(1)
|
| 40 |
+
seg.export(wav_path, format="wav")
|
| 41 |
|
| 42 |
+
return img_path, wav_path, out_path
|
| 43 |
|
| 44 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 45 |
+
# 3. Main inference wrapper
|
| 46 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 47 |
+
def generate(image, audio):
|
| 48 |
+
try:
|
| 49 |
+
img, wav, out_vid = preprocess(image, audio)
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"β {e}"
|
| 52 |
|
| 53 |
+
subprocess.run(
|
| 54 |
+
[
|
| 55 |
+
"python", "inference.py",
|
| 56 |
+
"--checkpoint_path", str(MODEL_PATH),
|
| 57 |
+
"--face", img,
|
| 58 |
+
"--audio", wav,
|
| 59 |
+
"--outfile", out_vid,
|
| 60 |
+
],
|
| 61 |
+
check=True,
|
| 62 |
+
)
|
| 63 |
|
| 64 |
+
return out_vid if Path(out_vid).exists() else "β Generation failed."
|
| 65 |
|
| 66 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 67 |
+
# 4. Gradio UI
|
| 68 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 69 |
+
demo = gr.Interface(
|
| 70 |
fn=generate,
|
| 71 |
+
inputs=[gr.Image(type="pil", label="Image"),
|
| 72 |
+
gr.Audio(type="filepath", label="Audio (any format)")],
|
| 73 |
+
outputs=gr.Video(label="Talking-head MP4"),
|
| 74 |
+
title="π£οΈ Wav2Lip CPU Demo",
|
| 75 |
+
description="Upload a single face image and an audio clip to create a lip-synced video (runs on free CPU tier).",
|
| 76 |
+
allow_flagging="never",
|
| 77 |
+
live=True,
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
demo.launch()
|