Code Monkey home page Code Monkey logo

lora-wildcard-maker's People

Contributors

dogmantc avatar

Stargazers

 avatar  avatar

Watchers

 avatar

lora-wildcard-maker's Issues

Not an issue, but a thank you for giving me a kick to work on this

I was really looking for a way to make a wildcard file of all the lora/hypernetworks/etc...
I really appreciate you creating this, it got me excited to expand itr.

I wanted to give you my modified version, in case you found it useful.
It will get the first sample image prompt from the info file, if it exists, and create a line for it.
If not, it will create a prompt using lora:filename:0.8 plus the trained words, if they exist.
If there are no trained words, you will at least get a prompt with the lora call.
(I also added some character stripping for things that were making SD stop)
(I replace lyco with lora because i keep them all in the lora folder, but you may not want that )

I'm sure there is a more efficient way to do things, but I just got this fully working and wanted to share it with you.

import os
import json

# Define directories 
directory = os.path.join(os.getcwd(),"models")
output_dir = os.path.join(os.getcwd(), "extensions", "sd-dynamic-prompts", "wildcards")

# Get words and loras from .civitai.info files
words = {}
subdirectories = set()  # Store the subdirectories

for root, dirs, files in os.walk(directory):
    origin = os.path.basename(root)  # Get the subdirectory name as the origin
    subdirectories.add(origin[0:200])  # Add the subdirectory to the set
    for filename in files:
        if filename.endswith(".civitai.info"):
            file_path = os.path.join(root, filename)
            with open(file_path, "r") as file:
                twords = "" #Base value
                file_contents = file.read()
                json_data = json.loads(file_contents)
                LoraPrompt = f"<lora:{filename.replace('.civitai.info','')}"
                trained_words = json_data.get("trainedWords", [])
                if trained_words:
                    twords = ",".join([str(item.strip().strip("\"'[{(").strip("\"' ]})")) for item in trained_words]) #add trained words
                if "images" in json_data:
                    Imagelist = json_data.get("images",[])
                    if Imagelist:
                        Meta = json_data['images'][0]['meta']
                        if Meta is not None:
                            if "prompt" in Meta:
                                prompt = json_data['images'][0]['meta']['prompt'] #get first image test prompt
                                prompt = prompt.replace('BREAK','')
                                prompt = prompt.replace('[','{')
                                prompt = prompt.replace(']','}')
                                prompt = prompt.replace('\n','')
                                prompt = prompt.replace('','')
                                prompt = prompt.replace('lyco','lora')
                                NumLora = prompt.find(LoraPrompt)
                                if NumLora < 1:
                                    LoraPrompt = f"<lora:{filename.replace('.civitai.info','')}:0.8>"
                                else:
                                    LoraPrompt = ""
                                words[file_path] = f"<REM:{filename.replace('.civitai.info','')}>, {LoraPrompt}, {prompt}%{origin}"  # Append origin to the lora
                            else:
                                LoraPrompt = f"<lora:{filename.replace('.civitai.info','')}:0.8>, {twords}"
                                words[file_path] = f"<REM:{filename.replace('.civitai.info','')}>, {LoraPrompt}%{origin}"  # Append origin to the lora
                        else:
                            LoraPrompt = f"<lora:{filename.replace('.civitai.info','')}:0.8>, {twords}"
                            words[file_path] = f"<REM:{filename.replace('.civitai.info','')}>, {LoraPrompt}%{origin}"  # Append origin to the lora
                    else:
                        LoraPrompt = f"<lora:{filename.replace('.civitai.info','')}:0.8>, {twords}"
                        words[file_path] = f"<REM:{filename.replace('.civitai.info','')}>, {LoraPrompt}%{origin}"  # Append origin to the lora
                else:
                    LoraPrompt = f"<lora:{filename.replace('.civitai.info','')}:0.8>, {twords}"
                    words[file_path] = f"<REM:{filename.replace('.civitai.info','')}>, {LoraPrompt}%{origin}"  # Append origin to the lora
# Write Overall txt file
loras_file_path = os.path.join(output_dir, "AllPrompts.txt")
with open(loras_file_path, "w", encoding="utf-8") as file:
    for word, thing in words.items():
        prompt_without_origin = thing.rsplit('%', 1)[0]  # Remove the "-{origin}" from the line
        file.write(f"{prompt_without_origin}\n")
# Write subdir Prompt_{subdirectory}.txt files and remove "-{origin}" from lines
for subdir in subdirectories:
    if "lora" in subdir:
        sub_output_file = os.path.join(output_dir, f"Prompts_{subdir}.txt")
        with open(sub_output_file, "w", encoding="utf-8") as file:
            for word, thing in words.items():
                if thing.endswith(f"%{subdir}"):  # Check if the line belongs to the current subdirectory
                    prompt_without_origin = thing.rsplit('%', 1)[0]  # Remove the "-{origin}" from the line
                    file.write(f"{prompt_without_origin}\n")
    if "hypernetwork" in subdir:
        sub_output_file = os.path.join(output_dir, f"Prompts_{subdir}.txt")
        with open(sub_output_file, "w", encoding="utf-8") as file:
            for word, thing in words.items():
                if thing.endswith(f"%{subdir}"):  # Check if the line belongs to the current subdirectory
                    prompt_without_origin = thing.rsplit('%', 1)[0]  # Remove the "-{origin}" from the line
                    file.write(f"{prompt_without_origin}\n")
    if "Stable-diffusion" in subdir:
        sub_output_file = os.path.join(output_dir, f"Prompts_{subdir}.txt")
        with open(sub_output_file, "w", encoding="utf-8") as file:
            for word, thing in words.items():
                if thing.endswith(f"%{subdir}"):  # Check if the line belongs to the current subdirectory
                    prompt_without_origin = thing.rsplit('%', 1)[0]  # Remove the "-{origin}" from the line
                    file.write(f"{prompt_without_origin}\n")


Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.