Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify requirement install and add manually load previews #44

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion civitai/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#region Utils
def log(message):
"""Log a message to the console."""
if not shared.opts.data.get('civitai_link_logging', True): return
print(f'Civitai: {message}')

def download_file(url, dest, on_progress=None):
Expand All @@ -45,7 +46,7 @@ def download_file(url, dest, on_progress=None):

try:
current = 0
with tqdm(total=total, unit='B', unit_scale=True, unit_divisor=1024) as bar:
with tqdm(total=total, unit='B', unit_scale=True, unit_divisor=1024, disable=not shared.opts.data.get('civitai_link_logging', True)) as bar:
for data in response.iter_content(chunk_size=download_chunk_size):
current += len(data)
pos = f.write(data)
Expand Down
81 changes: 30 additions & 51 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import git

from launch import run
import launch
from modules import shared

req_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "requirements.txt")

Expand All @@ -23,57 +24,35 @@ def is_package_installed(package_name, version):
return True
return False

def check_versions():
global req_file
reqs = open(req_file, 'r')
lines = reqs.readlines()
reqs_dict = {}
for line in lines:
splits = line.split("==")
if len(splits) == 2:
key = splits[0]
reqs_dict[key] = splits[1].replace("\n", "").strip()
# Loop through reqs and check if installed
for req in reqs_dict:
available = is_package_installed(req, reqs_dict[req])
if available: print(f"[+] {req} version {reqs_dict[req]} installed.")
else : print(f"[!] {req} version {reqs_dict[req]} NOT installed.")

base_dir = os.path.dirname(os.path.realpath(__file__))
revision = ""
app_revision = ""

try:
repo = git.Repo(base_dir)
revision = repo.rev_parse("HEAD")
app_repo = git.Repo(os.path.join(base_dir, "..", ".."))
app_revision = app_repo.rev_parse("HEAD")
except:
pass

print("")
print("#######################################################################################################")
print("Initializing Civitai Link")
print("If submitting an issue on github, please provide the below text for debugging purposes:")
print("")
print(f"Python revision: {sys.version}")
print(f"Civitai Link revision: {revision}")
print(f"SD-WebUI revision: {app_revision}")
print("")
civitai_skip_install = os.environ.get('CIVITAI_SKIP_INSTALL', False)

try:
requirements_file = os.environ.get('REQS_FILE', "requirements_versions.txt")
if requirements_file == req_file:
civitai_skip_install = True
except:
pass

if not civitai_skip_install:
name = "Civitai Link"
run(f'"{sys.executable}" -m pip install -r "{req_file}"', f"Checking {name} requirements...",
f"Couldn't install {name} requirements.")
with open(req_file) as file:
for lib in file:
lib = lib.strip()
if not launch.is_installed(lib):
launch.run_pip(f"install {lib}", f"Civitai Link requirement: {lib}")

if shared.opts.data.get('civitai_link_logging', True):
base_dir = os.path.dirname(os.path.realpath(__file__))
revision = ""
app_revision = ""

check_versions()
print("")
print("#######################################################################################################")
try:
repo = git.Repo(base_dir)
revision = repo.rev_parse("HEAD")
app_repo = git.Repo(os.path.join(base_dir, "..", ".."))
app_revision = app_repo.rev_parse("HEAD")
except:
pass
print("")
print("#######################################################################################################")
print("Initializing Civitai Link")
print("If submitting an issue on github, please provide the below text for debugging purposes:")
print("")
print(f"Python revision: {sys.version}")
print(f"Civitai Link revision: {revision}")
print(f"SD-WebUI revision: {app_revision}")
print("")
print("")
print("#######################################################################################################")
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
python-socketio[client]==5.7.2
python-socketio[client]>=5.7.2
18 changes: 14 additions & 4 deletions scripts/previews.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List
from datetime import datetime
import gradio as gr
import threading

Expand All @@ -7,9 +8,10 @@
from modules import script_callbacks, shared

previewable_types = ['LORA', 'Hypernetwork', 'TextualInversion', 'Checkpoint']
previews_update_info = ["Press Refresh button on the right to manually load previews"]

def load_previews():
download_missing_previews = shared.opts.data.get('civitai_download_previews', True)
if not download_missing_previews: return
previews_update_info[0] = datetime.now().strftime("%Y/%m/%d/ %H:%M:%S")
nsfw_previews = shared.opts.data.get('civitai_nsfw_previews', True)

civitai.log(f"Check resources for missing preview images")
Expand All @@ -29,10 +31,12 @@ def load_previews():
results.extend(civitai.get_all_by_hash(batch))
except:
civitai.log("Failed to fetch preview images from Civitai")
previews_update_info[0] += " - Failed to fetch preview images from Civitai"
return

if len(results) == 0:
civitai.log("No preview images found on Civitai")
civitai.log("No new preview image found on Civitai")
previews_update_info[0] += " - No new preview image found on Civitai"
return

civitai.log(f"Found {len(results)} hash matches")
Expand All @@ -54,10 +58,16 @@ def load_previews():
updated += 1

civitai.log(f"Updated {updated} preview images")
previews_update_info[0] += f" - Found and updated {updated} of {len(missing_previews)} missing preview images"

def load_previews_on_startup():
download_missing_previews = shared.opts.data.get('civitai_on_startup_download_previews', False)
if not download_missing_previews: return
load_previews()

# Automatically pull model with corresponding hash from Civitai
def start_load_previews(demo: gr.Blocks, app):
thread = threading.Thread(target=load_previews)
thread = threading.Thread(target=load_previews_on_startup)
thread.start()

script_callbacks.on_app_started(start_load_previews)
17 changes: 14 additions & 3 deletions scripts/settings.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import sys
import os
import gradio as gr

cwd = os.getcwd()
utils_dir = os.path.join(cwd, 'extensions', 'sd_civitai_extension', 'scripts')
sys.path.extend([utils_dir])

from civitai.link import on_civitai_link_key_changed
from previews import load_previews,previews_update_info

from modules import shared, script_callbacks


def on_ui_settings():
section = ('civitai_link', "Civitai")
shared.opts.add_option("civitai_link_key", shared.OptionInfo("", "Your Civitai Link Key", section=section, onchange=on_civitai_link_key_changed))
shared.opts.add_option("civitai_link_logging", shared.OptionInfo(True, "Show Civitai Link events in the console", section=section))
shared.opts.add_option("civitai_api_key", shared.OptionInfo("", "Your Civitai API Key", section=section))
shared.opts.add_option("civitai_download_previews", shared.OptionInfo(True, "Download missing preview images on startup", section=section))
shared.opts.add_option("civitai_on_startup_download_previews", shared.OptionInfo(False, "Download missing preview images on startup", section=section))
shared.opts.add_option("civitai_nsfw_previews", shared.OptionInfo(False, "Download NSFW (adult) preview images", section=section))
shared.opts.add_option("civitai_download_missing_models", shared.OptionInfo(True, "Download missing models upon reading generation parameters from prompt", section=section))
shared.opts.add_option("civitai_hashify_resources", shared.OptionInfo(True, "Include resource hashes in image metadata (for resource auto-detection on Civitai)", section=section))
shared.opts.add_option("civitai_folder_lora", shared.OptionInfo("", "LoRA directory (if not default)", section=section))
shared.opts.add_option("civitai_manually_download_previews", shared.OptionInfo(previews_update_info[0], "Last manually load time", gr.Dropdown, lambda: {"choices": previews_update_info},refresh=load_previews,section=section))


script_callbacks.on_ui_settings(on_ui_settings)
script_callbacks.on_ui_settings(on_ui_settings)