diff --git a/macos-build-script.sh b/macos-build-script.sh index ef30c2f..a8d4cef 100644 --- a/macos-build-script.sh +++ b/macos-build-script.sh @@ -1,10 +1,19 @@ +# Ask user whether to use mirror_head +read -p "Do you want to use mirror_head? (y/n): " use_mirror +if [ "$use_mirror" = "y" ]; then + mirror_head="https://mirror.ghproxy.com/" +else + mirror_head="" +fi + # Clone repo echo "INFO: Clone repo." -git clone https://github.com/TransparentLC/realesrgan-gui.git +repo_url="https://github.com/TransparentLC/realesrgan-gui.git" +git clone $mirror_head$repo_url cd realesrgan-gui # Create and activate Python virtual environment -echo "INFO: ๐Ÿš€ Checking current Python version..." +echo "INFO: ๐Ÿšง Checking current Python version..." python_version=$(python3 -V 2>&1 | cut -d" " -f2 | cut -d"." -f1-2) if ! which python3 >/dev/null 2>&1; then @@ -15,65 +24,124 @@ else echo "INFO: The 'python3' command found." if [ "$python_version" == "3.12" ]; then echo "INFO: โœ… The current Python version is 3.12" - echo "INFO: ๐Ÿš€ Creating Python 3.12 virtual enviroment..." + echo "INFO: ๐Ÿšง Creating Python 3.12 virtual enviroment..." python3 -m venv venv - echo "INFO: ๐Ÿš€ Activating Python virtual enviroment..." + echo "INFO: ๐Ÿšง Activating Python virtual enviroment..." source venv/bin/activate else echo "ERROR: โ›”๏ธ The current Python version is $python_version but 3.12 is required." - echo "INFO: ๐Ÿš€ Installling Python package 'virtualenv'..." + echo "INFO: ๐Ÿšง Installling Python package 'virtualenv'..." pip3 install virtualenv - echo "INFO: ๐Ÿš€ Creating Python 3.12 virtual enviroment..." + echo "INFO: ๐Ÿšง Creating Python 3.12 virtual enviroment..." virtualenv -p python3.12 venv - echo "INFO: ๐Ÿš€ Activating Python virtual enviroment..." + echo "INFO: ๐Ÿšง Activating Python virtual enviroment..." source venv/bin/activate fi fi -# Download required files -echo "INFO: ๐Ÿš€ Downloading realesrgan-ncnn-vulkan executable and models..." -base_url="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0" -source_file="realesrgan-ncnn-vulkan-20220424-macos.zip" -target_file="realesrgan-ncnn-vulkan" -model_folder="models" +# Download extra files +# Function to download and process release asset +download_asset() { + repo=$1 + asset_keyword=$2 + unzip_target=$3 + output_folder=$4 -if command -v wget &> /dev/null; then - echo "INFO: Using wget..." - wget -q --show-progress "$base_url/$source_file" -O "$source_file" -else - echo "INFO: wget not available, using curl..." - curl -L "$base_url/$source_file" -o "$source_file" -fi + # GitHub API endpoint to retrieve releases + api_url="https://api.github.com/repos/$repo/releases/latest" + + # Make a GET request to the GitHub API to retrieve the latest release + response=$(curl -s "$api_url") + + # Check if repo is xinntao/Real-ESRGAN + if [ "$repo" = "xinntao/Real-ESRGAN" ]; then + asset_url="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesrgan-ncnn-vulkan-20220424-macos.zip" + zip_file=$(basename "$asset_url") + folder_name="models" + else + # Parse the response to get the latest release asset URL + asset_url=$(echo "$response" | grep -o "https://.*$asset_keyword*.zip" | head -n 1) + zip_file=$(basename "$asset_url") + folder_name=$(basename -s .zip "$zip_file") + fi -unzip -j "$source_file" "$target_file" -d "." -unzip -j "$source_file" "$model_folder/*" -d "$model_folder" -rm -rf "$source_file" + # Download the latest release asset + if [ -n "$asset_url" ]; then + echo "INFO: ๐Ÿšง Downloading $zip_file..." + curl -LO "$mirror_head$asset_url" + echo "INFO: โœ… Download $zip_file complete." -# Thin fat files to single architecture -arch=$(uname -m) + # Unzip and process the file + echo "INFO: ๐Ÿšง Extracting and processing files..." + unzip -j "$zip_file" "$folder_name/$unzip_target" -d "$output_folder" -echo "INFO: System architecture is $arch." -echo "INFO: Extracting architecture specific libraries..." + # Perform additional actions based on the repo + if [ "$repo" = "upscayl/upscayl-ncnn" ]; then + # Thin file and set permissions + if [ -f "upscayl-bin" ]; then + # Thin file + echo "INFO: ๐Ÿšง Thin fat files to single architecture..." + arch=$(uname -m) + echo "INFO: ๐Ÿ’ฌ System architecture is $arch." + echo "INFO: ๐Ÿšง Extracting architecture specific libraries..." + if [ "$arch" = "arm64" ]; then + ditto --arch arm64 "$target_file" "$target_file_temp" + else + ditto --arch x86_64 "$target_file" "$target_file_temp" + fi + rm -rf "$target_file" + mv "$target_file_temp" "$target_file" + # Add execute permission + echo "INFO: ๐Ÿšง Add execute permission to realesrgan-ncnn-vulkan..." + chmod u+x realesrgan-ncnn-vulkan + fi + elif [ "$repo" = "nihui/realsr-ncnn-vulkan" ]; then + # Rename RealSR models + if [ -f "models/x4.param" ] && [ -f "models/x4.bin" ]; then + echo "INFO: ๐Ÿšง Rename RealSR models..." + mv models/x4.param models/realsr-x4-realworld.param + mv models/x4.bin models/realsr-x4-realworld.bin + fi + elif [ "$repo" = "xinntao/Real-ESRGAN" ]; then + # Models to remove + models_to_remove=( + "realesr-animevideov3-x2" + "realesr-animevideov3-x3" + ) + # Remove models + for model in "${models_to_remove[@]}"; do + param_file="models/$model.param" + bin_file="models/$model.bin" + if [ -f "$param_file" ] && [ -f "$bin_file" ]; then + echo "INFO: ๐Ÿšง Removing $model models..." + rm -rf "$param_file" "$bin_file" + fi + done + fi + echo "INFO: โœ… Processing complete." + else + echo "ERROR: โ›”๏ธ No release asset found for $repo." + fi +} -if [ "$arch" = "arm64" ]; then - ditto --arch arm64 "$target_file" "temp_file" -else - ditto --arch x86_64 "$target_file" "temp_file" -fi +# Download and process assets for upscayl-ncnn +download_asset "upscayl/upscayl-ncnn" "macos" "upscayl-bin" "." + +# Download and process assets for realsr-ncnn-vulkan +download_asset "nihui/realsr-ncnn-vulkan" "macos" "models-DF2K/*" "models" -rm -rf "$target_file" -mv "temp_file" "$target_file" -chmod u+x "$target_file" +# Download and process assets for realesrgan-ncnn-vulkan +download_asset "xinntao/Real-ESRGAN" "macos" "*" "models" # Install dependencies -echo "INFO: ๐Ÿš€ Installing requirements..." +echo "INFO: ๐Ÿšง Installing requirements..." pip3 install -r requirements.txt -echo "INFO: ๐Ÿš€ Installing Python package 'pyinstaller'..." +echo "INFO: ๐Ÿšง Installing Python package 'pyinstaller'..." pip install pyinstaller # Build macOS app -echo "INFO: ๐Ÿš€ Packaging macOS app..." +echo "INFO: ๐Ÿšง Packaging macOS app..." sudo pyinstaller realesrgan-gui-macos.spec # Copy built app to Download directory diff --git a/realesrgan-gui-macos.spec b/realesrgan-gui-macos.spec index 35715a8..4d1f5d8 100644 --- a/realesrgan-gui-macos.spec +++ b/realesrgan-gui-macos.spec @@ -8,14 +8,20 @@ import subprocess commit_hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').strip() version = "0.2.5." + commit_hash +# ๅˆคๆ–ญๆ˜ฏๅฆๅญ˜ๅœจ upscayl-bin ๆ–‡ไปถ +if os.path.exists('upscayl-bin'): + # ๅญ˜ๅœจ upscayl-bin๏ผŒๅช้œ€่ฆๆ‰“ๅŒ… upscayl-bin + bin = [('upscayl-bin', '.')] +else: + # ไธๅญ˜ๅœจ upscayl-bin๏ผŒๆ‰“ๅŒ… realesrgan-ncnn-vulkan + bin = [('realesrgan-ncnn-vulkan', '.')] + # PyInstallerๅˆ†ๆž่„šๆœฌ # ๆŒ‡ๅฎšไธปๅ…ฅๅฃๆ–‡ไปถ a = Analysis( ['main.py'], # ้œ€่ฆๆ‰“ๅŒ…็š„ไบŒ่ฟ›ๅˆถ - binaries=[ - ('realesrgan-ncnn-vulkan', '.'), - ], + binaries=bin, # ้œ€่ฆๆ‰“ๅŒ…็š„ๆ•ฐๆฎๆ–‡ไปถ datas=[ ('models', 'models'),