Certainly! I'll help you create a Python GUI application using PyQt5 (or any GUI library you prefer) that allows you to:
Select between the next or main branches of the FaceFusion repository.
Install or reinstall the selected version, including setting up or redoing the Conda environment.
Update FFmpeg to the latest version.
Select the preferred accelerator (NVIDIA CUDA/TensorRT, OpenVINO, or DirectML).
Execute the installation steps accordingly.
This GUI will streamline the setup process for FaceFusion on Windows systems.
Overview
We'll build a GUI application with the following features:
Branch Selection : A dropdown to select either the main or next branch.
Accelerator Selection : Radio buttons or a dropdown to select between NVIDIA (CUDA/TensorRT), OpenVINO, or DirectML.
Buttons :
Install/Reinstall : Installs or reinstalls the selected branch and accelerator, setting up the Conda environment.
Update FFmpeg : Updates FFmpeg to the latest version.
Run FaceFusion : Runs FaceFusion with the selected configuration.
Steps to Build the GUI Application
1. Setup the Python Environment
First, ensure you have Python installed on your Windows system. We'll use PyQt5 for the GUI.
bash Copy code
pip install PyQt5
2. Import Necessary Libraries
python Copy code
import sys
import subprocess
from PyQt5.QtWidgets import (
QApplication, QWidget, QLabel, QPushButton, QComboBox, QVBoxLayout,
QHBoxLayout, QGroupBox, QRadioButton, QMessageBox
)
from PyQt5.QtCore import Qt
3. Define the Main Window Class
python Copy code
class FaceFusionInstaller(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# Set window title
self.setWindowTitle('FaceFusion Installer')
# Branch selection
branch_label = QLabel('Select Branch:')
self.branch_combo = QComboBox()
self.branch_combo.addItems(['main', 'next'])
branch_layout = QHBoxLayout()
branch_layout.addWidget(branch_label)
branch_layout.addWidget(self.branch_combo)
# Accelerator selection
accelerator_label = QLabel('Select Accelerator:')
self.accelerator_combo = QComboBox()
self.accelerator_combo.addItems(['NVIDIA (CUDA/TensorRT)', 'OpenVINO (Intel Arc)', 'DirectML (AMD)'])
accelerator_layout = QHBoxLayout()
accelerator_layout.addWidget(accelerator_label)
accelerator_layout.addWidget(self.accelerator_combo)
# Buttons
self.install_button = QPushButton('Install/Reinstall')
self.install_button.clicked.connect(self.install_reinstall)
self.update_ffmpeg_button = QPushButton('Update FFmpeg')
self.update_ffmpeg_button.clicked.connect(self.update_ffmpeg)
self.run_button = QPushButton('Run FaceFusion')
self.run_button.clicked.connect(self.run_facefusion)
buttons_layout = QHBoxLayout()
buttons_layout.addWidget(self.install_button)
buttons_layout.addWidget(self.update_ffmpeg_button)
buttons_layout.addWidget(self.run_button)
# Main layout
main_layout = QVBoxLayout()
main_layout.addLayout(branch_layout)
main_layout.addLayout(accelerator_layout)
main_layout.addLayout(buttons_layout)
self.setLayout(main_layout)
4. Implement the Button Functions
a. Install/Reinstall Function
python Copy code
def install_reinstall(self):
branch = self.branch_combo.currentText()
accelerator = self.accelerator_combo.currentText()
reply = QMessageBox.question(
self, 'Confirmation',
f'This will install/reinstall the "{branch}" branch with "{accelerator}". Continue?',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No
)
if reply == QMessageBox.Yes:
try:
self.install_dependencies()
self.install_ffmpeg()
self.install_accelerator(accelerator)
self.clone_facefusion(branch)
QMessageBox.information(self, 'Success', 'Installation completed successfully.')
except Exception as e:
QMessageBox.critical(self, 'Error', f'An error occurred: {e}')
b. Update FFmpeg Function
python Copy code
def update_ffmpeg(self):
reply = QMessageBox.question(
self, 'Confirmation',
'This will update FFmpeg to the latest version. Continue?',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No
)
if reply == QMessageBox.Yes:
try:
self.install_ffmpeg()
QMessageBox.information(self, 'Success', 'FFmpeg updated successfully.')
except Exception as e:
QMessageBox.critical(self, 'Error', f'An error occurred: {e}')
c. Run FaceFusion Function
python Copy code
def run_facefusion(self):
branch = self.branch_combo.currentText()
env_name = 'facefusionnext' if branch == 'next' else 'facefusion'
try:
# Activate the conda environment and run FaceFusion
subprocess.run(f'''
conda deactivate
conda activate {env_name}
python facefusion.py run --open-browser
''', shell=True, check=True)
except Exception as e:
QMessageBox.critical(self, 'Error', f'An error occurred: {e}')
5. Implement Helper Functions
a. Install Dependencies
python Copy code
def install_dependencies(self):
commands = [
'winget install -e --id Git.Git',
'winget install -e --id Anaconda.Miniconda3 --override "/AddToPath=1"',
'winget install -e --id CodecGuide.K-LiteCodecPack.Basic'
]
for cmd in commands:
subprocess.run(cmd, shell=True, check=True)
b. Install FFmpeg
python Copy code
def install_ffmpeg(self):
commands = [
'rmdir /S /Q "C:\\ffmpeg"',
'mkdir "C:\\ffmpeg"',
'''
powershell -Command "& {
$url = (Invoke-RestMethod 'https://api.github.com/repos/jellyfin/jellyfin-ffmpeg/releases/latest').assets |
Where-Object { $_.name -eq 'portable_win64.zip' } |
Select-Object -ExpandProperty browser_download_url;
Invoke-WebRequest -Uri $url -OutFile 'C:\\portable_win64.zip'
}"
''',
'''
powershell -Command "& {
Expand-Archive -Path 'C:\\portable_win64.zip' -DestinationPath 'C:\\temp_ffmpeg' -Force;
Get-ChildItem -Path 'C:\\temp_ffmpeg\\portable_win64\\' -Recurse -File |
ForEach-Object { Move-Item -Path $_.FullName -Destination 'C:\\ffmpeg' };
Remove-Item -Recurse -Force 'C:\\temp_ffmpeg'
}"
''',
'''
powershell -Command "& {
[Environment]::SetEnvironmentVariable('Path', [Environment]::GetEnvironmentVariable('Path', 'Machine') + ';C:\\ffmpeg', 'Machine')
}"
''',
'ffmpeg -version'
]
for cmd in commands:
subprocess.run(cmd, shell=True, check=True)
c. Install Accelerator
python Copy code
def install_accelerator(self, accelerator):
if 'NVIDIA' in accelerator:
# Install CUDA and cuDNN
subprocess.run('conda install -y conda-forge::cuda-runtime=12.4.1 conda-forge::cudnn=9.2.1.18', shell=True, check=True)
# Install TensorRT
subprocess.run('pip install tensorrt==10.5.0 --extra-index-url https://pypi.nvidia.com', shell=True, check=True)
elif 'OpenVINO' in accelerator:
# Install OpenVINO
subprocess.run('conda install -y conda-forge::openvino=2024.3.0', shell=True, check=True)
elif 'DirectML' in accelerator:
# Install CUDA runtime and cuDNN (if required for DirectML)
subprocess.run('conda install -y conda-forge::cuda-runtime=12.4.1 conda-forge::cudnn=9.2.1.18', shell=True, check=True)
d. Clone FaceFusion Repository
python Copy code
def clone_facefusion(self, branch):
repo_dir = 'facefusion'
env_name = 'facefusionnext' if branch == 'next' else 'facefusion'
# Remove existing directory if it exists
subprocess.run(f'rmdir /S /Q "{repo_dir}"', shell=True)
# Clone the repository with the selected branch
subprocess.run(f'git clone --branch {branch} https://github.com/facefusion/facefusion', shell=True, check=True)
subprocess.run(f'cd {repo_dir}', shell=True)
# Choose and install the appropriate accelerator
accelerator = self.accelerator_combo.currentText()
if 'DirectML' in accelerator:
install_cmd = 'python install.py --onnxruntime directml'
elif 'OpenVINO' in accelerator:
install_cmd = 'python install.py --onnxruntime openvino'
elif 'NVIDIA' in accelerator:
install_cmd = 'python install.py --onnxruntime tensorrt'
subprocess.run(install_cmd, shell=True, check=True)
6. Run the Application
python Copy code
if __name__ == '__main__':
app = QApplication(sys.argv)
installer = FaceFusionInstaller()
installer.show()
sys.exit(app.exec_())
Notes and Considerations
Administrative Privileges : Some commands may require administrative privileges. Ensure the application is run with sufficient permissions.
Error Handling : The above code includes basic error handling with try-except blocks and will display error messages using QMessageBox.
Environment Activation : The run_facefusion function assumes that the conda environment activation command works within subprocess.run. Depending on your setup, you may need to adjust how the environment is activated.
Testing : Before deploying the GUI, test each function individually to ensure it works as expected in your environment.
Dependencies : Ensure that winget is installed and available in your system's PATH for the dependency installation commands to work.
Permissions for Environment Variables : Modifying system environment variables requires administrative rights.
Conclusion
This GUI application provides a user-friendly interface to manage the installation of FaceFusion, select the desired branch and accelerator, update FFmpeg, and run the application. It abstracts the command-line complexity and helps users set up FaceFusion according to their preferences.
Disclaimer : The above code is a basic implementation and may require adjustments based on your specific environment and requirements. Always back up important data before running installation scripts, and ensure you understand each command's effects on your system.