Spaces:
Running
on
Zero
Running
on
Zero
Update constants.py
Browse files- constants.py +111 -0
constants.py
CHANGED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from huggingface_hub import HfApi
|
| 3 |
+
from pprint import pprint
|
| 4 |
+
from stablepy import BUILTIN_UPSCALERS
|
| 5 |
+
|
| 6 |
+
# Initial UPSCALER_DICT_GUI
|
| 7 |
+
UPSCALER_DICT_GUI = {
|
| 8 |
+
**{bu.replace(" ", "_"): bu for bu in BUILTIN_UPSCALERS[2:]},
|
| 9 |
+
"RealESRNet_x4plus": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.1/RealESRNet_x4plus.pth",
|
| 10 |
+
"4x_foolhardy_Remacri": "https://huggingface.co/FacehugmanIII/4x_foolhardy_Remacri/resolve/main/4x_foolhardy_Remacri.pth",
|
| 11 |
+
"Remacri4xExtraSmoother": "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/Remacri%204x%20ExtraSmoother.pth",
|
| 12 |
+
"Lollypop": "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/lollypop.pth",
|
| 13 |
+
"NickelbackFS4x": "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/NickelbackFS%204x.pth",
|
| 14 |
+
"Real_HAT_GAN_SRx4": "https://huggingface.co/halffried/gyre_upscalers/resolve/main/hat_ganx4/Real_HAT_GAN_SRx4.safetensors",
|
| 15 |
+
"HAT-L_SRx4_ImageNet-pretrain": "https://huggingface.co/halffried/gyre_upscalers/resolve/main/hat_lx4/HAT-L_SRx4_ImageNet-pretrain.safetensors",
|
| 16 |
+
"Real-ESRGAN-Anime-finetuning": "https://huggingface.co/danhtran2mind/Real-ESRGAN-Anime-finetuning/resolve/main/Real-ESRGAN-Anime-finetuning.pth",
|
| 17 |
+
"8x_NMKD-Superscale_150000_G": "https://huggingface.co/lantianhang/8x_NMKD-Superscale_150000_G/resolve/main/8x_NMKD-Superscale_150000_G.pth",
|
| 18 |
+
"4x_Valar_v1": "https://huggingface.co/halffried/gyre_upscalers/resolve/main/esrgan_valar_x4/4x_Valar_v1.pth",
|
| 19 |
+
"Ghibli_Grain": "https://huggingface.co/anonderpling/upscalers/resolve/main/ESRGAN/ghibli_grain.pth",
|
| 20 |
+
"Detoon4x": "https://huggingface.co/anonderpling/upscalers/resolve/main/ESRGAN/4x_detoon_225k.pth",
|
| 21 |
+
"2x_Text2HD_v1-RealPLKSR": "https://github.com/starinspace/StarinspaceUpscale/releases/download/Models/2x_Text2HD_v.1-RealPLKSR.pth",
|
| 22 |
+
"4xNomosWebPhoto_esrgan": "https://github.com/Phhofm/models/releases/download/4xNomosWebPhoto_esrgan/4xNomosWebPhoto_esrgan.pth",
|
| 23 |
+
"4xTextureDAT2_otf": "https://github.com/Phhofm/models/releases/download/4xTextureDAT2_otf/4xTextureDAT2_otf.pth",
|
| 24 |
+
"2xVHS2HD-RealPLKSR": "https://github.com/starinspace/StarinspaceUpscale/releases/download/Models/2xVHS2HD-RealPLKSR.pth",
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
def clean_filename_for_key(filename):
|
| 28 |
+
"""Removes the extension and replaces special characters with underscores."""
|
| 29 |
+
name_without_extension = filename.rsplit('.', 1)[0]
|
| 30 |
+
# Replace common separators and any non-alphanumeric characters (except underscore) with an underscore
|
| 31 |
+
cleaned_name = re.sub(r'[^a-zA-Z0-9_]+', '_', name_without_extension)
|
| 32 |
+
return cleaned_name
|
| 33 |
+
|
| 34 |
+
def add_upscalers_from_author(author_name, upscaler_dict, repos_to_avoid=None):
|
| 35 |
+
"""
|
| 36 |
+
Scans an author's Hugging Face repos and adds model files to the upscaler dictionary.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
author_name (str): The Hugging Face username of the author.
|
| 40 |
+
upscaler_dict (dict): The dictionary of upscalers to update.
|
| 41 |
+
repos_to_avoid (list, optional): A list of repository names to skip. Defaults to None.
|
| 42 |
+
"""
|
| 43 |
+
if repos_to_avoid is None:
|
| 44 |
+
repos_to_avoid = []
|
| 45 |
+
|
| 46 |
+
print(f"--- Processing author: {author_name} ---")
|
| 47 |
+
api = HfApi()
|
| 48 |
+
try:
|
| 49 |
+
models = api.list_models(author=author_name)
|
| 50 |
+
all_repo_ids = [model.modelId for model in models]
|
| 51 |
+
|
| 52 |
+
# Filter out repositories that should be avoided
|
| 53 |
+
repo_ids = [
|
| 54 |
+
repo for repo in all_repo_ids
|
| 55 |
+
if not any(avoid_name in repo for avoid_name in repos_to_avoid)
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
filtered_count = len(all_repo_ids) - len(repo_ids)
|
| 59 |
+
print(f"Found {len(all_repo_ids)} repositories. Skipping {filtered_count}, processing {len(repo_ids)}.")
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"Could not fetch repositories for {author_name}: {e}")
|
| 63 |
+
return
|
| 64 |
+
|
| 65 |
+
for repo_id in repo_ids:
|
| 66 |
+
try:
|
| 67 |
+
files = api.list_repo_files(repo_id)
|
| 68 |
+
# Prioritize .pth files
|
| 69 |
+
pth_files = [f for f in files if f.endswith(".pth")]
|
| 70 |
+
model_files_to_add = []
|
| 71 |
+
|
| 72 |
+
if pth_files:
|
| 73 |
+
model_files_to_add = pth_files
|
| 74 |
+
else:
|
| 75 |
+
# If no .pth files, look for .safetensors
|
| 76 |
+
safetensors_files = [f for f in files if f.endswith(".safetensors")]
|
| 77 |
+
if safetensors_files:
|
| 78 |
+
model_files_to_add = safetensors_files
|
| 79 |
+
|
| 80 |
+
if not model_files_to_add:
|
| 81 |
+
continue
|
| 82 |
+
|
| 83 |
+
print(f"Found {len(model_files_to_add)} model(s) in {repo_id}")
|
| 84 |
+
for file_path in model_files_to_add:
|
| 85 |
+
# Get just the filename from the full path
|
| 86 |
+
filename = file_path.split('/')[-1]
|
| 87 |
+
key = clean_filename_for_key(filename)
|
| 88 |
+
|
| 89 |
+
if key in upscaler_dict:
|
| 90 |
+
print(f" - Skipping duplicate key: {key}")
|
| 91 |
+
continue
|
| 92 |
+
|
| 93 |
+
url = f"https://huggingface.co/{repo_id}/resolve/main/{file_path}"
|
| 94 |
+
upscaler_dict[key] = url
|
| 95 |
+
print(f" + Added: {key}")
|
| 96 |
+
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print(f"Could not process repo {repo_id}: {e}")
|
| 99 |
+
|
| 100 |
+
# --- Main Execution ---
|
| 101 |
+
# Create a copy to avoid modifying the original dict while iterating if needed later
|
| 102 |
+
updated_upscaler_dict = UPSCALER_DICT_GUI.copy()
|
| 103 |
+
|
| 104 |
+
# Define the list of repositories to skip
|
| 105 |
+
REPOS_TO_AVOID = ["BHI_Filtering_Post", "ppo-LunarLander-v2", "dqn-SpaceInvadersNoFrameskip-v4", "dqn-BeamRiderNoFrameskip-v4"]
|
| 106 |
+
|
| 107 |
+
# Add models from Kim2091 (no repos to avoid in this case)
|
| 108 |
+
add_upscalers_from_author("Kim2091", updated_upscaler_dict)
|
| 109 |
+
|
| 110 |
+
# Add models from Phips, passing the list of repos to avoid
|
| 111 |
+
add_upscalers_from_author("Phips", updated_upscaler_dict, repos_to_avoid=REPOS_TO_AVOID)
|