Spaces:
Running on Zero
Running on Zero
Upload 23 files
Browse files- README.md +0 -1
- app.py +33 -1
- constants.py +1 -1
- env.py +1 -0
- modutils.py +0 -0
- packages.txt +2 -1
- requirements.txt +4 -6
- utils.py +35 -20
README.md
CHANGED
|
@@ -13,7 +13,6 @@ duplicated_from: r3gm/DiffuseCraft
|
|
| 13 |
short_description: Stunning images using stable diffusion.
|
| 14 |
preload_from_hub:
|
| 15 |
- madebyollin/sdxl-vae-fp16-fix config.json,diffusion_pytorch_model.safetensors
|
| 16 |
-
hf_oauth: true
|
| 17 |
---
|
| 18 |
|
| 19 |
## Using this Space programmatically
|
|
|
|
| 13 |
short_description: Stunning images using stable diffusion.
|
| 14 |
preload_from_hub:
|
| 15 |
- madebyollin/sdxl-vae-fp16-fix config.json,diffusion_pytorch_model.safetensors
|
|
|
|
| 16 |
---
|
| 17 |
|
| 18 |
## Using this Space programmatically
|
app.py
CHANGED
|
@@ -199,6 +199,11 @@ class GuiSD:
|
|
| 199 |
# Avoid duplicate downloads
|
| 200 |
self.active_downloads = set()
|
| 201 |
self.download_lock = threading.Lock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
def update_storage_models(self, storage_floor_gb=24, required_inventory_for_purge=3):
|
| 203 |
while get_used_storage_gb() > storage_floor_gb:
|
| 204 |
if len(self.inventory) < required_inventory_for_purge:
|
|
@@ -223,6 +228,34 @@ class GuiSD:
|
|
| 223 |
print(self.inventory)
|
| 224 |
|
| 225 |
def load_new_model(self, model_name, vae_model, task, controlnet_model, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
lock_key = model_name
|
| 227 |
|
| 228 |
while True:
|
|
@@ -2255,7 +2288,6 @@ with gr.Blocks(theme=args.theme, elem_id="main", fill_width=True, fill_height=Fa
|
|
| 2255 |
gr.api(generate_image, api_name="generate_image", show_api=True, queue=True, concurrency_id="gpu")
|
| 2256 |
gr.api(generate_image_stream, api_name="generate_image_stream", show_api=True, queue=True, concurrency_id="gpu")
|
| 2257 |
|
| 2258 |
-
gr.LoginButton()
|
| 2259 |
gr.DuplicateButton(value="Duplicate Space for private use (This demo does not work on CPU. Requires GPU Space)")
|
| 2260 |
|
| 2261 |
|
|
|
|
| 199 |
# Avoid duplicate downloads
|
| 200 |
self.active_downloads = set()
|
| 201 |
self.download_lock = threading.Lock()
|
| 202 |
+
|
| 203 |
+
# Anti-abuse: track new model requests.
|
| 204 |
+
self.used_models = []
|
| 205 |
+
self.new_model_history = []
|
| 206 |
+
|
| 207 |
def update_storage_models(self, storage_floor_gb=24, required_inventory_for_purge=3):
|
| 208 |
while get_used_storage_gb() > storage_floor_gb:
|
| 209 |
if len(self.inventory) < required_inventory_for_purge:
|
|
|
|
| 228 |
print(self.inventory)
|
| 229 |
|
| 230 |
def load_new_model(self, model_name, vae_model, task, controlnet_model, progress=gr.Progress(track_tqdm=True)):
|
| 231 |
+
|
| 232 |
+
if model_name != model_list[0]:
|
| 233 |
+
# --- Anti-Abuse Check Start ---
|
| 234 |
+
if model_name in self.used_models:
|
| 235 |
+
# Move to the end to mark as the most recently used.
|
| 236 |
+
self.used_models.remove(model_name)
|
| 237 |
+
self.used_models.append(model_name)
|
| 238 |
+
else:
|
| 239 |
+
current_time = datetime.now()
|
| 240 |
+
# Retain history of new model requests from the last 20 minutes.
|
| 241 |
+
self.new_model_history = [
|
| 242 |
+
t for t in self.new_model_history
|
| 243 |
+
if (current_time - t).total_seconds() < 1200
|
| 244 |
+
]
|
| 245 |
+
|
| 246 |
+
# Allow a maximum of 5 new model requests per 20 minutes.
|
| 247 |
+
if len(self.new_model_history) >= 5:
|
| 248 |
+
yield "Rate limit exceeded: Too many new models requested."
|
| 249 |
+
raise gr.Error("Too many new models requested. Please reuse your previously loaded models or wait a few minutes before trying new ones.")
|
| 250 |
+
|
| 251 |
+
self.new_model_history.append(current_time)
|
| 252 |
+
self.used_models.append(model_name)
|
| 253 |
+
|
| 254 |
+
# Cap the reuse list to the 5 most recent models.
|
| 255 |
+
if len(self.used_models) > 5:
|
| 256 |
+
self.used_models.pop(0)
|
| 257 |
+
# --- Anti-Abuse Check End ---
|
| 258 |
+
|
| 259 |
lock_key = model_name
|
| 260 |
|
| 261 |
while True:
|
|
|
|
| 2288 |
gr.api(generate_image, api_name="generate_image", show_api=True, queue=True, concurrency_id="gpu")
|
| 2289 |
gr.api(generate_image_stream, api_name="generate_image_stream", show_api=True, queue=True, concurrency_id="gpu")
|
| 2290 |
|
|
|
|
| 2291 |
gr.DuplicateButton(value="Duplicate Space for private use (This demo does not work on CPU. Requires GPU Space)")
|
| 2292 |
|
| 2293 |
|
constants.py
CHANGED
|
@@ -19,7 +19,7 @@ DOWNLOAD_MODEL = "https://huggingface.co/zuv0/test/resolve/main/milkyWonderland_
|
|
| 19 |
DOWNLOAD_VAE = "https://huggingface.co/Anzhc/Anzhcs-VAEs/resolve/main/SDXL%20Anime%20VAE%20Dec-only%20B3.safetensors, https://huggingface.co/fp16-guy/anything_kl-f8-anime2_vae-ft-mse-840000-ema-pruned_blessed_clearvae_fp16_cleaned/resolve/main/vae-ft-mse-840000-ema-pruned_fp16.safetensors?download=true"
|
| 20 |
|
| 21 |
# - **Download LoRAs**
|
| 22 |
-
DOWNLOAD_LORA = "https://huggingface.co/Leopain/color/resolve/main/Coloring_book_-_LineArt.safetensors, https://civitai.com/api/download/models/135867, https://huggingface.co/Linaqruf/anime-detailer-xl-lora/resolve/main/anime-detailer-xl.safetensors?download=true, https://huggingface.co/Linaqruf/style-enhancer-xl-lora/resolve/main/style-enhancer-xl.safetensors?download=true
|
| 23 |
|
| 24 |
LOAD_DIFFUSERS_FORMAT_MODEL = [
|
| 25 |
'TestOrganizationPleaseIgnore/potato_quality_anime_plzwork_sdxl',
|
|
|
|
| 19 |
DOWNLOAD_VAE = "https://huggingface.co/Anzhc/Anzhcs-VAEs/resolve/main/SDXL%20Anime%20VAE%20Dec-only%20B3.safetensors, https://huggingface.co/fp16-guy/anything_kl-f8-anime2_vae-ft-mse-840000-ema-pruned_blessed_clearvae_fp16_cleaned/resolve/main/vae-ft-mse-840000-ema-pruned_fp16.safetensors?download=true"
|
| 20 |
|
| 21 |
# - **Download LoRAs**
|
| 22 |
+
DOWNLOAD_LORA = "https://huggingface.co/Leopain/color/resolve/main/Coloring_book_-_LineArt.safetensors, https://civitai.com/api/download/models/135867, https://huggingface.co/Linaqruf/anime-detailer-xl-lora/resolve/main/anime-detailer-xl.safetensors?download=true, https://huggingface.co/Linaqruf/style-enhancer-xl-lora/resolve/main/style-enhancer-xl.safetensors?download=true"
|
| 23 |
|
| 24 |
LOAD_DIFFUSERS_FORMAT_MODEL = [
|
| 25 |
'TestOrganizationPleaseIgnore/potato_quality_anime_plzwork_sdxl',
|
env.py
CHANGED
|
@@ -49,6 +49,7 @@ LOAD_DIFFUSERS_FORMAT_MODEL = [
|
|
| 49 |
'Raelina/Raehoshi-illust-XL-8',
|
| 50 |
'Raelina/Raehoshi-illust-XL-8.1',
|
| 51 |
'Raelina/Raehoshi-illust-XL-9',
|
|
|
|
| 52 |
'Raelina/Raehoshi-illust-vpred',
|
| 53 |
'camenduru/FLUX.1-dev-diffusers',
|
| 54 |
'black-forest-labs/FLUX.1-schnell',
|
|
|
|
| 49 |
'Raelina/Raehoshi-illust-XL-8',
|
| 50 |
'Raelina/Raehoshi-illust-XL-8.1',
|
| 51 |
'Raelina/Raehoshi-illust-XL-9',
|
| 52 |
+
'Raelina/Raehoshi-illust-XL-9.1',
|
| 53 |
'Raelina/Raehoshi-illust-vpred',
|
| 54 |
'camenduru/FLUX.1-dev-diffusers',
|
| 55 |
'black-forest-labs/FLUX.1-schnell',
|
modutils.py
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
packages.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
git-lfs
|
|
|
|
|
|
| 1 |
+
git-lfs
|
| 2 |
+
ffmpeg
|
requirements.txt
CHANGED
|
@@ -3,10 +3,8 @@ diffusers
|
|
| 3 |
transformers
|
| 4 |
accelerate
|
| 5 |
huggingface_hub
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
torch==2.5.1
|
| 9 |
-
torchvision
|
| 10 |
numpy<2
|
| 11 |
gdown
|
| 12 |
opencv-python
|
|
@@ -20,5 +18,5 @@ sentencepiece
|
|
| 20 |
unidecode
|
| 21 |
matplotlib-inline
|
| 22 |
mediapipe==0.10.5
|
| 23 |
-
|
| 24 |
-
#pydantic==2.10.6
|
|
|
|
| 3 |
transformers
|
| 4 |
accelerate
|
| 5 |
huggingface_hub
|
| 6 |
+
spaces
|
| 7 |
+
torch==2.8.0
|
|
|
|
|
|
|
| 8 |
numpy<2
|
| 9 |
gdown
|
| 10 |
opencv-python
|
|
|
|
| 18 |
unidecode
|
| 19 |
matplotlib-inline
|
| 20 |
mediapipe==0.10.5
|
| 21 |
+
einops
|
| 22 |
+
# pydantic==2.10.6
|
utils.py
CHANGED
|
@@ -275,11 +275,11 @@ def civ_redirect_down(url, dir_, civitai_api_key, romanize, alternative_name):
|
|
| 275 |
elif os.path.exists(os.path.join(dir_, filename_base)):
|
| 276 |
return os.path.join(dir_, filename_base), filename_base
|
| 277 |
|
| 278 |
-
|
| 279 |
-
f'
|
| 280 |
-
f'-
|
| 281 |
)
|
| 282 |
-
r_code = os.system(
|
| 283 |
|
| 284 |
# if r_code != 0:
|
| 285 |
# raise RuntimeError(f"Failed to download file: {filename_base}. Error code: {r_code}")
|
|
@@ -293,27 +293,32 @@ def civ_redirect_down(url, dir_, civitai_api_key, romanize, alternative_name):
|
|
| 293 |
|
| 294 |
def civ_api_down(url, dir_, civitai_api_key, civ_filename):
|
| 295 |
"""
|
| 296 |
-
This method is susceptible to being blocked because it generates a lot of temp redirect links with
|
| 297 |
-
If an API key limit is reached, generating a new API key and using it can fix the issue.
|
| 298 |
"""
|
| 299 |
output_path = None
|
| 300 |
-
|
| 301 |
url_dl = url + f"?token={civitai_api_key}"
|
|
|
|
| 302 |
if not civ_filename:
|
| 303 |
-
|
| 304 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
else:
|
| 306 |
output_path = os.path.join(dir_, civ_filename)
|
|
|
|
| 307 |
if not os.path.exists(output_path):
|
| 308 |
-
|
| 309 |
-
f'
|
| 310 |
-
f'-
|
| 311 |
)
|
| 312 |
-
os.system(
|
| 313 |
-
|
| 314 |
return output_path
|
| 315 |
|
| 316 |
-
|
| 317 |
def drive_down(url, dir_):
|
| 318 |
import gdown
|
| 319 |
|
|
@@ -354,10 +359,16 @@ def hf_down(url, dir_, hf_token, romanize):
|
|
| 354 |
url = url.replace("/blob/", "/resolve/")
|
| 355 |
|
| 356 |
if hf_token:
|
| 357 |
-
|
| 358 |
-
|
|
|
|
|
|
|
|
|
|
| 359 |
else:
|
| 360 |
-
os.system(
|
|
|
|
|
|
|
|
|
|
| 361 |
|
| 362 |
return output_path
|
| 363 |
|
|
@@ -370,7 +381,8 @@ def download_things(directory, url, hf_token="", civitai_api_key="", romanize=Fa
|
|
| 370 |
downloaded_file_path = drive_down(url, directory)
|
| 371 |
elif "huggingface.co" in url:
|
| 372 |
downloaded_file_path = hf_down(url, directory, hf_token, romanize)
|
| 373 |
-
elif "civitai.
|
|
|
|
| 374 |
if not civitai_api_key:
|
| 375 |
msg = "You need an API key to download Civitai models."
|
| 376 |
print(f"\033[91m{msg}\033[0m")
|
|
@@ -393,7 +405,10 @@ def download_things(directory, url, hf_token="", civitai_api_key="", romanize=Fa
|
|
| 393 |
gr.Warning(msg)
|
| 394 |
downloaded_file_path = civ_api_down(url, directory, civitai_api_key, civ_filename)
|
| 395 |
else:
|
| 396 |
-
os.system(
|
|
|
|
|
|
|
|
|
|
| 397 |
|
| 398 |
return downloaded_file_path
|
| 399 |
|
|
|
|
| 275 |
elif os.path.exists(os.path.join(dir_, filename_base)):
|
| 276 |
return os.path.join(dir_, filename_base), filename_base
|
| 277 |
|
| 278 |
+
wget_command = (
|
| 279 |
+
f'wget -c -nv '
|
| 280 |
+
f'-O "{os.path.join(dir_, filename_base)}" "{redirect_url}"'
|
| 281 |
)
|
| 282 |
+
r_code = os.system(wget_command) # noqa
|
| 283 |
|
| 284 |
# if r_code != 0:
|
| 285 |
# raise RuntimeError(f"Failed to download file: {filename_base}. Error code: {r_code}")
|
|
|
|
| 293 |
|
| 294 |
def civ_api_down(url, dir_, civitai_api_key, civ_filename):
|
| 295 |
"""
|
| 296 |
+
This method is susceptible to being blocked because it generates a lot of temp redirect links with wget.
|
| 297 |
+
If an API key limit is reached, generating a new API key and using it can fix the issue. no
|
| 298 |
"""
|
| 299 |
output_path = None
|
| 300 |
+
|
| 301 |
url_dl = url + f"?token={civitai_api_key}"
|
| 302 |
+
|
| 303 |
if not civ_filename:
|
| 304 |
+
wget_command = (
|
| 305 |
+
f'wget -c -nv '
|
| 306 |
+
f'-P "{dir_}" "{url_dl}"'
|
| 307 |
+
)
|
| 308 |
+
os.system(wget_command)
|
| 309 |
+
|
| 310 |
else:
|
| 311 |
output_path = os.path.join(dir_, civ_filename)
|
| 312 |
+
|
| 313 |
if not os.path.exists(output_path):
|
| 314 |
+
wget_command = (
|
| 315 |
+
f'wget -c -nv '
|
| 316 |
+
f'-O "{output_path}" "{url_dl}"'
|
| 317 |
)
|
| 318 |
+
os.system(wget_command)
|
| 319 |
+
|
| 320 |
return output_path
|
| 321 |
|
|
|
|
| 322 |
def drive_down(url, dir_):
|
| 323 |
import gdown
|
| 324 |
|
|
|
|
| 359 |
url = url.replace("/blob/", "/resolve/")
|
| 360 |
|
| 361 |
if hf_token:
|
| 362 |
+
os.system(
|
| 363 |
+
f'wget -c -nv '
|
| 364 |
+
f'--header="Authorization: Bearer {hf_token}" '
|
| 365 |
+
f'-O "{os.path.join(dir_, filename)}" "{url}"'
|
| 366 |
+
)
|
| 367 |
else:
|
| 368 |
+
os.system(
|
| 369 |
+
f'wget -c -nv '
|
| 370 |
+
f'-O "{os.path.join(dir_, filename)}" "{url}"'
|
| 371 |
+
)
|
| 372 |
|
| 373 |
return output_path
|
| 374 |
|
|
|
|
| 381 |
downloaded_file_path = drive_down(url, directory)
|
| 382 |
elif "huggingface.co" in url:
|
| 383 |
downloaded_file_path = hf_down(url, directory, hf_token, romanize)
|
| 384 |
+
elif "civitai." in url:
|
| 385 |
+
url = url.replace("civitai.red", "civitai.com")
|
| 386 |
if not civitai_api_key:
|
| 387 |
msg = "You need an API key to download Civitai models."
|
| 388 |
print(f"\033[91m{msg}\033[0m")
|
|
|
|
| 405 |
gr.Warning(msg)
|
| 406 |
downloaded_file_path = civ_api_down(url, directory, civitai_api_key, civ_filename)
|
| 407 |
else:
|
| 408 |
+
os.system(
|
| 409 |
+
f'wget -c -nv '
|
| 410 |
+
f'-P "{directory}" "{url}"'
|
| 411 |
+
)
|
| 412 |
|
| 413 |
return downloaded_file_path
|
| 414 |
|