Spaces:
Sleeping
Sleeping
File size: 4,516 Bytes
e42eb07 887ffa4 2ad7c2a 887ffa4 8b76f07 e42eb07 887ffa4 4f41d98 e3173c4 4cdd06e e3173c4 4cdd06e e3173c4 4cdd06e e3173c4 08ecbc4 d6f88c7 08ecbc4 d6f88c7 08ecbc4 d6f88c7 08ecbc4 d6f88c7 08ecbc4 d6f88c7 08ecbc4 d6f88c7 08ecbc4 d6f88c7 e3173c4 4cdd06e e3173c4 08ecbc4 e3173c4 4cdd06e e3173c4 4cdd06e 08ecbc4 e3173c4 08ecbc4 b74f017 e3173c4 fe2a84a 4f41d98 e3173c4 39d810d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | import gradio as gr
import fastapi
import starlette
import pydantic
import huggingface_hub
print("GRADIO =", gr.__version__)
print("FASTAPI =", fastapi.__version__)
print("STARLETTE =", starlette.__version__)
print("PYDANTIC =", pydantic.__version__)
print("HF HUB =", huggingface_hub.__version__)
import subprocess
import os
from PIL import Image
import numpy as np
from pydub import AudioSegment
# ----------------------------
# Save audio (numpy -> mp3)
# ----------------------------
def save_audio_mp3(audio_tuple, filename):
sampling_rate, audio_data = audio_tuple
audio_bytes = np.array(audio_data, dtype=np.int16).tobytes()
audio_segment = AudioSegment(
audio_bytes,
sample_width=2,
frame_rate=sampling_rate,
channels=1
)
audio_segment.export(filename, format="mp3")
# ----------------------------
# Merge video + audio (ffmpeg)
# ----------------------------
def merge_audio_video(video_path, audio_path, output_path):
if os.path.exists(output_path):
os.remove(output_path)
cmd = [
"ffmpeg",
"-y",
"-i", video_path,
"-i", audio_path,
"-c:v", "copy",
"-c:a", "aac",
"-map", "0:v:0",
"-map", "1:a:0",
output_path
]
subprocess.run(cmd, check=True)
return output_path
# ----------------------------
# Inference function
# ----------------------------
def run_inference(input_image, input_audio):
if input_image is None:
raise gr.Error("Please upload an image.")
if input_audio is None:
raise gr.Error("Please upload audio.")
os.makedirs("sample_data", exist_ok=True)
os.makedirs("results", exist_ok=True)
# Save image
image_path = "sample_data/uploaded_image.png"
Image.fromarray(input_image.astype(np.uint8)).save(image_path)
# Save audio
audio_path = "sample_data/uploaded_audio.mp3"
save_audio_mp3(input_audio, audio_path)
# Run Wav2Lip
cmd = [
"python3",
"inference.py",
"--checkpoint_path", "checkpoints/wav2lip_gan.pth",
"--face", image_path,
"--audio", audio_path
]
result = subprocess.run(
cmd,
capture_output=True,
text=True
)
if result.returncode != 0:
# نجمع stdout و stderr لأن بعض الرسائل قد تظهر في أي منهما
error = (result.stderr or "") + (result.stdout or "")
# رسالة عدم اكتشاف الوجه
if "Face not detected!" in error or "No face detected" in error:
raise gr.Error(
"❌ No face detected. Please upload a clear front-facing image."
)
# رسالة الصوت غير الصالح
if "Mel contains nan" in error:
raise gr.Error(
"❌ Invalid audio file. Please upload another audio."
)
# أي خطأ آخر
raise gr.Error("❌ Failed to generate video.")
wav2lip_video = "results/result_voice.mp4"
if not os.path.exists(wav2lip_video):
raise gr.Error("Wav2Lip output not found!")
# merge audio + video
final_video = merge_audio_video(
wav2lip_video,
audio_path,
"results/final_output.mp4"
)
return final_video
# ----------------------------
# UI
# ----------------------------
def create_demo():
with gr.Blocks() as demo:
gr.Markdown("# 🎤 Wav2Lip Demo")
with gr.Row():
input_image = gr.Image(
type="numpy",
label="Input Image"
)
input_audio = gr.Audio(
type="numpy",
label="Input Audio"
)
output_video = gr.Video(
label="Output Video"
)
btn = gr.Button("Generate Video")
btn.click(
fn=run_inference,
inputs=[input_image, input_audio],
outputs=output_video
)
gr.Markdown("## Sample")
with gr.Row():
gr.Image(
"sample/spark.png",
label="Sample Image"
)
gr.Audio(
"sample/spark_1.1.mp3",
label="Sample Audio"
)
gr.Video(
"sample/final_output.mp4",
label="Sample Output"
)
return demo
if __name__ == "__main__":
demo = create_demo()
demo.queue()
demo.launch(show_api=True) |