Spaces:
Running
Running
Update externalmod.py
Browse files- externalmod.py +27 -0
externalmod.py
CHANGED
@@ -583,3 +583,30 @@ def find_model_list(author: str="", tags: list[str]=[], not_tag="", sort: str="l
|
|
583 |
models.append(model.id)
|
584 |
if len(models) == limit: break
|
585 |
return models
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
583 |
models.append(model.id)
|
584 |
if len(models) == limit: break
|
585 |
return models
|
586 |
+
|
587 |
+
|
588 |
+
def save_image(image, savefile, modelname, prompt, nprompt, height=0, width=0, steps=0, cfg=0, seed=-1):
|
589 |
+
from PIL import Image, PngImagePlugin
|
590 |
+
import json
|
591 |
+
try:
|
592 |
+
metadata = {"prompt": prompt, "negative_prompt": nprompt, "Model": {"Model": modelname.split("/")[-1]}}
|
593 |
+
if steps > 0: metadata["num_inference_steps"] = steps
|
594 |
+
if cfg > 0: metadata["guidance_scale"] = cfg
|
595 |
+
if seed != -1: metadata["seed"] = seed
|
596 |
+
if width > 0 and height > 0: metadata["resolution"] = f"{width} x {height}"
|
597 |
+
metadata_str = json.dumps(metadata)
|
598 |
+
info = PngImagePlugin.PngInfo()
|
599 |
+
info.add_text("metadata", metadata_str)
|
600 |
+
image.save(savefile, "PNG", pnginfo=info)
|
601 |
+
return str(Path(savefile).resolve())
|
602 |
+
except Exception as e:
|
603 |
+
print(f"Failed to save image file: {e}")
|
604 |
+
raise Exception(f"Failed to save image file:") from e
|
605 |
+
|
606 |
+
|
607 |
+
def randomize_seed():
|
608 |
+
from random import seed, randint
|
609 |
+
MAX_SEED = 2**32-1
|
610 |
+
seed()
|
611 |
+
rseed = randint(0, MAX_SEED)
|
612 |
+
return rseed
|