Skip to content

Commit

Permalink
added cache and ui improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Inzaniak committed Feb 16, 2024
1 parent 5e9064c commit 5b1b9f5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ The extension is now divided into two main functionalities that can be used toge
### Ranbooru
This is the main part of the extension. It gets a random set of tags from boorus pictures.
Here's an explanation of all the parameters:
- **Enabled**: Enables the Ranbooru part of the extension
- **Booru**: The booru to get the tags from. Right now Gelbooru, Rule34, Safebooru, yande.re, konachan, aibooru, danbooru and xbooru are implemented. You can easily add more creating a class for the booru and adding it to the booru list in the script.
- **Max Pages**: The maximum amount of pages to get the tags from. The extension will get a random page from the booru and then get the tags from one or more random pictures from that page.
- **Post ID**: Here you can specify the ID of the post to get the tags from. If you leave it blank, the extension will get a random post (or more than one) from the random page.
Expand Down Expand Up @@ -45,11 +44,11 @@ Here's an explanation of all the parameters:
- **Chaos Amount**: This sets the percentage of tags to move to the negative prompt.
- **Negative Mode**: This moves all the tags to the negative prompt.
- **Use Same Seed**: This uses the same seed for all the generations in the same batch.
- **Use Cache**: This uses the cached version of the page once it has been used. This improves the speed of the search, but it can return the same results if the cache is not cleared.

### LoRAnado
This is a newer experimental function that enables you to pick random LoRAs from a folder and add them to the prompt. This can lead to interesting results.
Here's an explanation of all the parameters:
- **Use LoRAnado**: Enables the LoRAnado part of the extension
- **Lock Previous LoRAs**: Uses the same LoRAs of the previous generation. This is useful if you've found an interesting combination and you want to test it with different tags.
- **LoRAs Subfolder**: The subfolder of the LoRAs folder to use. This is required.
- **LoRAs Amount**: The amount of LoRAs to use.
Expand Down
4 changes: 4 additions & 0 deletions install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import launch

if not launch.is_installed("requests-cache"):
launch.run_pip("install requests-cache", "requests-cache")
22 changes: 15 additions & 7 deletions scripts/ranbooru.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
from PIL import Image
import numpy as np
import importlib
import requests_cache

from modules import images
from modules.processing import process_images, StableDiffusionProcessingImg2Img
from modules import shared
from modules.img2img import img2img
from modules.sd_hijack import model_hijack
from modules.deepbooru import DeepDanbooru
from modules.ui_components import InputAccordion

extensions_root = scripts.basedir()
user_dir = os.path.join(extensions_root, 'user')
Expand Down Expand Up @@ -358,8 +360,7 @@ def show(self, is_img2img):

def ui(self, is_img2img):
with gr.Group():
with gr.Accordion("Ranbooru", open = False):
enabled = gr.Checkbox(label="Enable")
with InputAccordion(False, label="Ranbooru", elem_id=self.elem_id("ra_enable")) as enabled:
booru = gr.Dropdown(["gelbooru","rule34","safebooru","danbooru","konachan",'yande.re','aibooru','xbooru','e621'], label="Booru", value="gelbooru")
max_pages = gr.Slider(label="Max Pages", minimum=1, maximum=100,value=100, step=1)
gr.Markdown("""## Post""")
Expand Down Expand Up @@ -408,18 +409,19 @@ def ui(self, is_img2img):
with gr.Box():
negative_mode = gr.Radio(["None","Negative"], label="Negative Mode", value="None")
use_same_seed = gr.Checkbox(label="Use same seed for all pictures", value=False)
with gr.Box():
use_cache = gr.Checkbox(label="Use cache", value=True)
with gr.Group():
with gr.Accordion("LoRAnado", open = False):
with InputAccordion(False, label="LoRAnado", elem_id=self.elem_id("lo_enable")) as lora_enabled:
with gr.Box():
lora_enabled = gr.Checkbox(label="Use LoRAnado", value=False)
lora_lock_prev = gr.Checkbox(label="Lock previous LoRAs", value=False)
lora_folder = gr.Textbox(lines=1, label="LoRAs Subfolder")
lora_amount = gr.Slider(default=1, label="LoRAs Amount", minimum=1, maximum=10, step=1)
with gr.Box():
lora_min = gr.Slider(value=-1.0, label="Min LoRAs Weight", minimum=-1.0, maximum=1, step=0.1)
lora_max = gr.Slider(value=1.0, label="Max LoRAs Weight", minimum=-1.0, maximum=1.0, step=0.1)
lora_custom_weights = gr.Textbox(lines=1, label="LoRAs Custom Weights")
return [enabled,tags,booru,remove_bad_tags,max_pages,change_dash,same_prompt,fringe_benefits,remove_tags,use_img2img,denoising,use_last_img,change_background,change_color,shuffle_tags,post_id,mix_prompt,mix_amount,chaos_mode,negative_mode,chaos_amount,limit_tags,max_tags,sorting_order,mature_rating,lora_folder,lora_amount,lora_min,lora_max,lora_enabled,lora_custom_weights,lora_lock_prev,use_ip,use_search_txt,use_remove_txt,choose_search_txt,choose_remove_txt,crop_center, use_deepbooru, use_same_seed]
return [enabled,tags,booru,remove_bad_tags,max_pages,change_dash,same_prompt,fringe_benefits,remove_tags,use_img2img,denoising,use_last_img,change_background,change_color,shuffle_tags,post_id,mix_prompt,mix_amount,chaos_mode,negative_mode,chaos_amount,limit_tags,max_tags,sorting_order,mature_rating,lora_folder,lora_amount,lora_min,lora_max,lora_enabled,lora_custom_weights,lora_lock_prev,use_ip,use_search_txt,use_remove_txt,choose_search_txt,choose_remove_txt,crop_center, use_deepbooru, use_same_seed,use_cache]

def check_orientation(self, img):
"""Check if image is portrait, landscape or square"""
Expand Down Expand Up @@ -457,7 +459,13 @@ def loranado(self,lora_enabled,lora_folder,lora_amount,lora_min,lora_max,lora_cu
p.prompt = f'{lora_prompt} {p.prompt}'
return p

def before_process(self, p, enabled, tags, booru, remove_bad_tags,max_pages,change_dash,same_prompt,fringe_benefits,remove_tags,use_img2img,denoising,use_last_img,change_background,change_color,shuffle_tags,post_id,mix_prompt,mix_amount,chaos_mode,negative_mode,chaos_amount,limit_tags,max_tags,sorting_order,mature_rating,lora_folder,lora_amount,lora_min,lora_max,lora_enabled,lora_custom_weights,lora_lock_prev,use_ip,use_search_txt,use_remove_txt,choose_search_txt,choose_remove_txt,crop_center,use_deepbooru,use_same_seed):
def before_process(self, p, enabled, tags, booru, remove_bad_tags,max_pages,change_dash,same_prompt,fringe_benefits,remove_tags,use_img2img,denoising,use_last_img,change_background,change_color,shuffle_tags,post_id,mix_prompt,mix_amount,chaos_mode,negative_mode,chaos_amount,limit_tags,max_tags,sorting_order,mature_rating,lora_folder,lora_amount,lora_min,lora_max,lora_enabled,lora_custom_weights,lora_lock_prev,use_ip,use_search_txt,use_remove_txt,choose_search_txt,choose_remove_txt,crop_center,use_deepbooru,use_same_seed,use_cache):
if use_cache:
if not requests_cache.patcher.is_installed():
requests_cache.install_cache('ranbooru_cache', backend='sqlite', expire_after=3600)
else:
if requests_cache.patcher.is_installed():
requests_cache.uninstall_cache()
if enabled:
# Initialize APIs
booru_apis = {
Expand Down Expand Up @@ -733,7 +741,7 @@ def before_process(self, p, enabled, tags, booru, remove_bad_tags,max_pages,chan
else:
pass

def postprocess(self, p, processed, enabled, tags, booru, remove_bad_tags,max_pages,change_dash,same_prompt,fringe_benefits,remove_tags,use_img2img,denoising,use_last_img,change_background,change_color,shuffle_tags,post_id,mix_prompt,mix_amount,chaos_mode,negative_mode,chaos_amount,limit_tags,max_tags,sorting_order,mature_rating,lora_folder,lora_amount,lora_min,lora_max,lora_enabled,lora_custom_weights,lora_lock_prev,use_ip,use_search_txt,use_remove_txt,choose_search_txt,choose_remove_txt,crop_center,use_deepbooru,use_same_seed):
def postprocess(self, p, processed, enabled, tags, booru, remove_bad_tags,max_pages,change_dash,same_prompt,fringe_benefits,remove_tags,use_img2img,denoising,use_last_img,change_background,change_color,shuffle_tags,post_id,mix_prompt,mix_amount,chaos_mode,negative_mode,chaos_amount,limit_tags,max_tags,sorting_order,mature_rating,lora_folder,lora_amount,lora_min,lora_max,lora_enabled,lora_custom_weights,lora_lock_prev,use_ip,use_search_txt,use_remove_txt,choose_search_txt,choose_remove_txt,crop_center,use_deepbooru,use_same_seed,use_cache):
if use_img2img and not use_ip and enabled:
print('Using pictures')
if crop_center:
Expand Down

0 comments on commit 5b1b9f5

Please sign in to comment.