Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
-
import torch
|
| 4 |
import subprocess
|
| 5 |
import os
|
|
|
|
| 6 |
import ffmpeg
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
@spaces.GPU
|
| 12 |
-
def greet(n):
|
| 13 |
-
print(zero.device) # <-- 'cuda:0' 🤗
|
| 14 |
-
return f"Hello {zero + n} Tensor"
|
| 15 |
|
| 16 |
def audio_video():
|
| 17 |
-
print("started =========================")
|
| 18 |
input_video = ffmpeg.input('results/result_voice.mp4')
|
| 19 |
-
|
| 20 |
-
input_audio = ffmpeg.input('sample_data/sir.mp3')
|
| 21 |
os.system(f"rm -rf results/final_output.mp4")
|
| 22 |
ffmpeg.concat(input_video, input_audio, v=1, a=1).output('results/final_output.mp4').run()
|
| 23 |
|
| 24 |
return "results/final_output.mp4"
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
command = f'python3 inference.py --checkpoint_path checkpoints/wav2lip_gan.pth --face sample_data/spark.png --audio sample_data/sir.mp3'
|
| 30 |
-
print("running ")
|
| 31 |
-
# Execute the command
|
| 32 |
-
process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
output, error = process.communicate()
|
| 36 |
|
| 37 |
return audio_video()
|
|
@@ -40,17 +51,14 @@ def run():
|
|
| 40 |
with gr.Blocks(css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}") as demo:
|
| 41 |
gr.Markdown("<h1 style='text-align: center;'>"+ "One Shot Talking Face from Text" + "</h1><br/><br/>")
|
| 42 |
with gr.Group():
|
| 43 |
-
# with gr.Box():
|
| 44 |
with gr.Row():
|
| 45 |
-
|
| 46 |
-
input_video = gr.Video(label="Input Video")
|
| 47 |
input_audio = gr.Audio(label="Input Audio")
|
| 48 |
video_out = gr.Video(show_label=True,label="Output")
|
| 49 |
with gr.Row():
|
| 50 |
btn = gr.Button("Generate")
|
| 51 |
-
|
| 52 |
-
btn.click(run_infrence,inputs=[
|
| 53 |
-
# btn.click(run_infrence,inputs=[input_video,input_audio])
|
| 54 |
demo.queue()
|
| 55 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 56 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
|
|
|
| 3 |
import subprocess
|
| 4 |
import os
|
| 5 |
+
from PIL import Image
|
| 6 |
import ffmpeg
|
| 7 |
+
from pydub import AudioSegment
|
| 8 |
|
| 9 |
+
import numpy as np
|
| 10 |
+
import soundfile as sf
|
| 11 |
+
|
| 12 |
+
def save_audio_mp3(audio_tuple, filename):
|
| 13 |
+
sampling_rate, audio_data = audio_tuple
|
| 14 |
+
audio_bytes = np.array(audio_data, dtype=np.int16).tobytes()
|
| 15 |
+
audio_segment = AudioSegment(audio_bytes, sample_width=2, frame_rate=sampling_rate, channels=1)
|
| 16 |
+
audio_segment.export(filename, format="mp3")
|
| 17 |
+
|
| 18 |
+
return f"Audio saved successfully as {filename}"
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def audio_video():
|
|
|
|
| 22 |
input_video = ffmpeg.input('results/result_voice.mp4')
|
| 23 |
+
input_audio = ffmpeg.input('sample_data/uploaded_audio.mp3')
|
|
|
|
| 24 |
os.system(f"rm -rf results/final_output.mp4")
|
| 25 |
ffmpeg.concat(input_video, input_audio, v=1, a=1).output('results/final_output.mp4').run()
|
| 26 |
|
| 27 |
return "results/final_output.mp4"
|
| 28 |
|
| 29 |
+
@spaces.GPU
|
| 30 |
+
def run_infrence(input_image,input_audio):
|
| 31 |
+
pil_image = Image.fromarray(input_image.astype(np.uint8))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
save_dir = "sample_data"
|
| 34 |
+
if not os.path.exists(save_dir):
|
| 35 |
+
os.makedirs(save_dir)
|
| 36 |
+
|
| 37 |
+
# Save input image
|
| 38 |
+
filename = os.path.join(save_dir, "uploaded_image.png")
|
| 39 |
+
pil_image.save(filename)
|
| 40 |
+
|
| 41 |
+
#Save input audio
|
| 42 |
+
save_audio_mp3(input_audio, "sample_data/uploaded_audio.mp3")
|
| 43 |
+
|
| 44 |
+
command = f'python3 inference.py --checkpoint_path checkpoints/wav2lip_gan.pth --face sample_data/uploaded_image.png --audio sample_data/uploaded_audio.mp3'
|
| 45 |
+
process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
|
| 46 |
output, error = process.communicate()
|
| 47 |
|
| 48 |
return audio_video()
|
|
|
|
| 51 |
with gr.Blocks(css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}") as demo:
|
| 52 |
gr.Markdown("<h1 style='text-align: center;'>"+ "One Shot Talking Face from Text" + "</h1><br/><br/>")
|
| 53 |
with gr.Group():
|
|
|
|
| 54 |
with gr.Row():
|
| 55 |
+
input_image = gr.Image(label="Input Image")
|
|
|
|
| 56 |
input_audio = gr.Audio(label="Input Audio")
|
| 57 |
video_out = gr.Video(show_label=True,label="Output")
|
| 58 |
with gr.Row():
|
| 59 |
btn = gr.Button("Generate")
|
| 60 |
+
|
| 61 |
+
btn.click(run_infrence,inputs=[input_image,input_audio], outputs=[video_out])
|
|
|
|
| 62 |
demo.queue()
|
| 63 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 64 |
|