Create app_hf.py
Browse files
app_hf.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import yaml
|
4 |
+
import os
|
5 |
+
import cv2
|
6 |
+
|
7 |
+
def get_video_frame_count(video_path):
|
8 |
+
if video_path is None:
|
9 |
+
return None
|
10 |
+
|
11 |
+
video = cv2.VideoCapture(video_path)
|
12 |
+
frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
13 |
+
video.release()
|
14 |
+
return frame_count
|
15 |
+
|
16 |
+
def initialize_yaml(config_path):
|
17 |
+
try:
|
18 |
+
with open(config_path, 'r') as file:
|
19 |
+
config = yaml.safe_load(file)
|
20 |
+
if config is None:
|
21 |
+
print("YAML file is empty or contains invalid content. Initializing...")
|
22 |
+
config = {'test_cases': {}}
|
23 |
+
with open(config_path, 'w') as outfile:
|
24 |
+
yaml.safe_dump(config, outfile)
|
25 |
+
else:
|
26 |
+
print("YAML file is valid.")
|
27 |
+
except FileNotFoundError:
|
28 |
+
print("YAML file not found. Creating a new one...")
|
29 |
+
config = {'test_cases': {}}
|
30 |
+
with open(config_path, 'w') as outfile:
|
31 |
+
yaml.safe_dump(config, outfile)
|
32 |
+
|
33 |
+
def run_vid2vid(image_path, video_path, width, height, length, seed, cfg, steps):
|
34 |
+
# Assuming the rest of the configuration remains constant
|
35 |
+
config_path = './configs/prompts/animation_facereenac.yaml'
|
36 |
+
|
37 |
+
# Ensure paths are strings
|
38 |
+
image_path_str = str(image_path)
|
39 |
+
video_path_str = str(video_path)
|
40 |
+
|
41 |
+
# Load the existing configuration
|
42 |
+
with open(config_path, 'r') as file:
|
43 |
+
config = yaml.safe_load(file)
|
44 |
+
|
45 |
+
# If the file is somehow still empty or invalid, initialize config
|
46 |
+
if config is None:
|
47 |
+
config = {'test_cases': {}}
|
48 |
+
|
49 |
+
# Update the configuration using string paths
|
50 |
+
config['test_cases'] = {image_path_str: [video_path_str]}
|
51 |
+
|
52 |
+
# Save the updated configuration
|
53 |
+
with open(config_path, 'w') as file:
|
54 |
+
yaml.safe_dump(config, file, default_flow_style=False)
|
55 |
+
|
56 |
+
# Construct the command with all the additional arguments
|
57 |
+
command = [
|
58 |
+
"python", "-m", "scripts.vid2vid_gr",
|
59 |
+
"--config", config_path,
|
60 |
+
"-W", str(width),
|
61 |
+
"-H", str(height),
|
62 |
+
# "-L", str(length),
|
63 |
+
"--seed", str(seed),
|
64 |
+
"--cfg", str(cfg),
|
65 |
+
"--steps", str(steps)
|
66 |
+
]
|
67 |
+
# Conditionally add the "-L" argument if a meaningful value is provided
|
68 |
+
if length is not None:
|
69 |
+
command.extend(["-L", str(length)])
|
70 |
+
|
71 |
+
# Only add "--fps" argument if it is provided (not None)
|
72 |
+
# if fps is not None:
|
73 |
+
# command.extend(["--fps", str(fps)])
|
74 |
+
|
75 |
+
# Run the vid2vid script with the specified parameters
|
76 |
+
subprocess.run(command)
|
77 |
+
|
78 |
+
# Assuming the script saves its output to a known, fixed path
|
79 |
+
output_file_path = "output/result/result.mp4"
|
80 |
+
|
81 |
+
# Return this path for Gradio to display the video
|
82 |
+
return output_file_path
|
83 |
+
|
84 |
+
with gr.Blocks(title="AniPortrait") as blocks_app:
|
85 |
+
gr.Markdown("# Gradio UI for AniPortrait vid2vid")
|
86 |
+
gr.Markdown("Audio-Driven Synthesis of Photorealistic Portrait Animation")
|
87 |
+
gr.Markdown("Original Project: https://github.com/Zejun-Yang/AniPortrait")
|
88 |
+
|
89 |
+
with gr.Row():
|
90 |
+
image_input = gr.Image(type="filepath", label="Upload Image")
|
91 |
+
video_input = gr.Video(label="Upload Video")
|
92 |
+
|
93 |
+
with gr.Row():
|
94 |
+
width = gr.Slider(minimum=256, maximum=1024, step=8, value=512, label="Width (-W)")
|
95 |
+
height = gr.Slider(minimum=256, maximum=1024, step=8, value=512, label="Height (-H)")
|
96 |
+
|
97 |
+
with gr.Row():
|
98 |
+
length = gr.Number(value=None, label="Length (-L)")
|
99 |
+
seed = gr.Number(value=42, label="Seed (--seed)")
|
100 |
+
video_input.change(get_video_frame_count, inputs=video_input, outputs=length)
|
101 |
+
|
102 |
+
with gr.Row():
|
103 |
+
cfg = gr.Slider(minimum=0.1, maximum=10.0, step=0.1, value=3.5, label="Cfg (--cfg)")
|
104 |
+
steps = gr.Slider(minimum=1, maximum=100.0, step=1, value=25, label="Steps (--steps)")
|
105 |
+
|
106 |
+
# with gr.Row():
|
107 |
+
# fps = gr.Number(label="FPS (--fps)")
|
108 |
+
|
109 |
+
output_video = gr.PlayableVideo(label="Result")
|
110 |
+
|
111 |
+
def process_and_display(image_path, video_path, width, height, length, seed, cfg, steps):
|
112 |
+
return run_vid2vid(image_path, video_path, width, height, length, seed, cfg, steps)
|
113 |
+
|
114 |
+
run_button = gr.Button("Run")
|
115 |
+
run_button.click(
|
116 |
+
process_and_display,
|
117 |
+
inputs=[image_input, video_input, width, height, length, seed, cfg, steps],
|
118 |
+
outputs=output_video
|
119 |
+
)
|
120 |
+
|
121 |
+
if __name__ == "__main__":
|
122 |
+
blocks_app.queue(max_size=3).launch(share=False, show_error=True, show_api=False)
|
123 |
+
|