NeuroDonu commited on
Commit
3bf801f
·
verified ·
1 Parent(s): c332e34

Update updater_facefusion.py

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