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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
from .install_from_source import get_uv_path
|
| 4 |
+
|
| 5 |
+
git = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'git', 'cmd', 'git.exe')
|
| 6 |
+
ff_obs = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'sources', 'facefusion')
|
| 7 |
+
p = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'python', 'python.exe')
|
| 8 |
+
|
| 9 |
+
files = [
|
| 10 |
+
ff_obs + "\\next" + "\\facefusion\\content_analyser.py",
|
| 11 |
+
ff_obs + "\\master" + "\\facefusion\\content_analyser.py",
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
uv_executable = get_uv_path()
|
| 15 |
+
|
| 16 |
+
def gradio_version(branch):
|
| 17 |
+
if branch=="master":
|
| 18 |
+
subprocess.run([p], ["-m", {uv_executable}, "pip", "install", "gradio==3.50.2"])
|
| 19 |
+
if branch=="next":
|
| 20 |
+
subprocess.run([p], ["-m", {uv_executable}, "pip", "install", "gradio==4.40.0"])
|
| 21 |
+
|
| 22 |
+
def process_file_master(file_path):
|
| 23 |
+
with open(file_path, 'r') as f:
|
| 24 |
+
lines = f.readlines()
|
| 25 |
+
with open(file_path, 'w') as f:
|
| 26 |
+
inside_function = False
|
| 27 |
+
for line in lines:
|
| 28 |
+
if 'def analyse_frame(' in line:
|
| 29 |
+
inside_function = True
|
| 30 |
+
f.write(line)
|
| 31 |
+
f.write(' return False\n')
|
| 32 |
+
elif inside_function:
|
| 33 |
+
if line.startswith('def ') or line.strip() == '':
|
| 34 |
+
inside_function = False
|
| 35 |
+
f.write(line)
|
| 36 |
+
else:
|
| 37 |
+
f.write(line)
|
| 38 |
+
|
| 39 |
+
def process_file_next(file_path):
|
| 40 |
+
with open(file_path, 'r') as f:
|
| 41 |
+
lines = f.readlines()
|
| 42 |
+
|
| 43 |
+
with open(file_path, 'w') as f:
|
| 44 |
+
inside_function = False
|
| 45 |
+
current_function = None
|
| 46 |
+
|
| 47 |
+
for line in lines:
|
| 48 |
+
if 'def analyse_frame(' in line:
|
| 49 |
+
inside_function = True
|
| 50 |
+
current_function = 'analyse_frame'
|
| 51 |
+
f.write(line)
|
| 52 |
+
f.write(' return False\n\n')
|
| 53 |
+
elif 'def forward(' in line:
|
| 54 |
+
inside_function = True
|
| 55 |
+
current_function = 'forward'
|
| 56 |
+
f.write(line)
|
| 57 |
+
f.write(' return 0\n\n')
|
| 58 |
+
elif inside_function:
|
| 59 |
+
if line.startswith('def '):
|
| 60 |
+
inside_function = False
|
| 61 |
+
current_function = None
|
| 62 |
+
f.write(line)
|
| 63 |
+
elif line.strip() == '':
|
| 64 |
+
continue
|
| 65 |
+
else:
|
| 66 |
+
f.write(line)
|
| 67 |
+
|
| 68 |
+
def process_files(files):
|
| 69 |
+
for file_path in files:
|
| 70 |
+
if '\\next\\' in file_path:
|
| 71 |
+
process_file_next(file_path)
|
| 72 |
+
elif '\\master\\' in file_path:
|
| 73 |
+
process_file_master(file_path)
|
| 74 |
+
|
| 75 |
+
def run_git_command(args):
|
| 76 |
+
subprocess.run([git] + args, check=True)
|
| 77 |
+
|
| 78 |
+
def update_branch(branch):
|
| 79 |
+
if branch=="master":
|
| 80 |
+
os.chdir(ff_obs + "\\master")
|
| 81 |
+
if branch=="next":
|
| 82 |
+
os.chdir(ff_obs + "\\next")
|
| 83 |
+
run_git_command(['reset', '--hard'])
|
| 84 |
+
run_git_command(['checkout', branch])
|
| 85 |
+
run_git_command(['pull', 'origin', branch, '--rebase'])
|
| 86 |
+
|
| 87 |
+
def start_ff(branch, webcam_mode=False):
|
| 88 |
+
if branch=="master":
|
| 89 |
+
path_to_branch = os.path.join(ff_obs + "\\master")
|
| 90 |
+
if branch=="next":
|
| 91 |
+
path_to_branch = os.path.join(ff_obs + "\\next")
|
| 92 |
+
|
| 93 |
+
if branch=="next":
|
| 94 |
+
py_files = [f for f in os.listdir(path_to_branch) if f.endswith('.py')]
|
| 95 |
+
if len(py_files) != 2:
|
| 96 |
+
return
|
| 97 |
+
second_file = [f for f in py_files if f != 'installer.py'][0]
|
| 98 |
+
if branch=="master":
|
| 99 |
+
second_file = "run.py"
|
| 100 |
+
|
| 101 |
+
if webcam_mode:
|
| 102 |
+
if branch=="next":
|
| 103 |
+
args_next = ["run"]
|
| 104 |
+
args = ["--open-browser", "--ui-layouts", "webcam"]
|
| 105 |
+
args = args_next + args
|
| 106 |
+
if branch=="master":
|
| 107 |
+
args = ["--open-browser", "--ui-layouts", "webcam"]
|
| 108 |
+
else:
|
| 109 |
+
if branch=="next":
|
| 110 |
+
args_next = ["run"]
|
| 111 |
+
args = ["--open-browser"]
|
| 112 |
+
args = args_next+args
|
| 113 |
+
if branch=="master":
|
| 114 |
+
args = ["--open-browser"]
|
| 115 |
+
|
| 116 |
+
subprocess.run([p, os.path.join(path_to_branch, second_file)] + args)
|
| 117 |
+
|
| 118 |
+
def get_localized_text(language, key):
|
| 119 |
+
texts = {
|
| 120 |
+
"en": {
|
| 121 |
+
"choose_action": "Choose an action:",
|
| 122 |
+
"update_master": "1. Update to the master branch and start it",
|
| 123 |
+
"update_next": "2. Update to the next branch and start it",
|
| 124 |
+
"start_facefusion": "3. Start facefusion",
|
| 125 |
+
"enter_choice": "Enter the number of the action: ",
|
| 126 |
+
"invalid_choice": "Invalid choice, please try again.",
|
| 127 |
+
"choose_language": "Choose a language (en/ru): ",
|
| 128 |
+
"enable_webcam": "Enable webcam mode? (Y/N): ",
|
| 129 |
+
},
|
| 130 |
+
"ru": {
|
| 131 |
+
"choose_action": "Выберите действие:",
|
| 132 |
+
"update_master": "1. Обновить до обычной ветки и запустить ее (master)",
|
| 133 |
+
"update_next": "2. Обновить до ветки next и запустить ее",
|
| 134 |
+
"start_facefusion": "3. Запустить facefusion",
|
| 135 |
+
"enter_choice": "Введите номер действия: ",
|
| 136 |
+
"invalid_choice": "Неверный выбор, попробуйте снова.",
|
| 137 |
+
"choose_language": "Выберите язык (en/ru): ",
|
| 138 |
+
"enable_webcam": "Включить режим вебкамеры? (Y/N): ",
|
| 139 |
+
}
|
| 140 |
+
}
|
| 141 |
+
return texts[language].get(key, "")
|
| 142 |
+
|
| 143 |
+
def read_language_from_file(file_path):
|
| 144 |
+
if os.path.exists(file_path):
|
| 145 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 146 |
+
language = file.read().strip().lower()
|
| 147 |
+
if language in ["en", "ru"]:
|
| 148 |
+
return language
|
| 149 |
+
return None
|
| 150 |
+
|
| 151 |
+
def write_language_to_file(file_path, language):
|
| 152 |
+
with open(file_path, 'w', encoding='utf-8') as file:
|
| 153 |
+
file.write(language)
|
| 154 |
+
|
| 155 |
+
def ask_webcam_mode(language):
|
| 156 |
+
webcam_choice = input(get_localized_text(language, "enable_webcam")).strip().lower()
|
| 157 |
+
return webcam_choice == 'y'
|
| 158 |
+
|
| 159 |
+
def facefusion():
|
| 160 |
+
file_path = 'lang.txt'
|
| 161 |
+
language = read_language_from_file(file_path)
|
| 162 |
+
if not language:
|
| 163 |
+
language = input(get_localized_text("en", "choose_language")).strip().lower()
|
| 164 |
+
if language not in ["en", "ru"]:
|
| 165 |
+
language = "en"
|
| 166 |
+
write_language_to_file(file_path, language)
|
| 167 |
+
|
| 168 |
+
while True:
|
| 169 |
+
print(get_localized_text(language, "choose_action"))
|
| 170 |
+
print(get_localized_text(language, "update_master"))
|
| 171 |
+
print(get_localized_text(language, "update_next"))
|
| 172 |
+
|
| 173 |
+
choice = input(get_localized_text(language, "enter_choice")).strip()
|
| 174 |
+
|
| 175 |
+
if choice == '1':
|
| 176 |
+
update_branch('master')
|
| 177 |
+
process_files(files)
|
| 178 |
+
gradio_version('master')
|
| 179 |
+
webcam_mode = ask_webcam_mode(language)
|
| 180 |
+
start_ff("master", webcam_mode)
|
| 181 |
+
elif choice == '2':
|
| 182 |
+
update_branch('next')
|
| 183 |
+
process_files(files)
|
| 184 |
+
gradio_version('next')
|
| 185 |
+
webcam_mode = ask_webcam_mode(language)
|
| 186 |
+
start_ff("next", webcam_mode)
|
| 187 |
+
else:
|
| 188 |
+
print(get_localized_text(language, "invalid_choice"))
|