├── .python-version ├── venv.cmd ├── AI_Assistant_ReadMe.txt ├── AI_Assistant_install.ps1 ├── AI_Assistant_modules ├── config_states.py ├── ui_extensions.py ├── launch_utils_AI_Assistant.py ├── gitpython_hack.py ├── shared_cmd_options.py ├── application_config.py ├── prompt_analysis.py ├── sd2_clip.py ├── output_image_gui.py ├── actions │ ├── resize.py │ ├── color_scheme.py │ ├── stick2body.py │ ├── normal_map.py │ ├── anime_shadow.py │ ├── line_drawing_cutout.py │ ├── coloring.py │ ├── lighting.py │ ├── line_drawing.py │ └── i2i.py ├── sdxl_clip.py ├── tab_gui.py └── sd1_clip.py ├── AI_Assistant_exUI.bat ├── utils ├── prompt_utils.py ├── lang_util.py ├── tagger.py ├── request_api.py └── img_utils.py ├── AI_Assistant.py ├── .gitignore ├── languages ├── language_zh_CN.properties ├── language_jp.properties └── language_en.properties ├── AI_Assistant_gui.py ├── README_zh_CN.md ├── README.md ├── README_en.md ├── AI_Assistant_setup.py ├── AI_Assistant_model_DL.cmd └── LICENSE.txt /.python-version: -------------------------------------------------------------------------------- 1 | 3.10.11 2 | -------------------------------------------------------------------------------- /venv.cmd: -------------------------------------------------------------------------------- 1 | cmd /k venv\Scripts\activate -------------------------------------------------------------------------------- /AI_Assistant_ReadMe.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tori29umai0123/AI-Assistant/HEAD/AI_Assistant_ReadMe.txt -------------------------------------------------------------------------------- /AI_Assistant_install.ps1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tori29umai0123/AI-Assistant/HEAD/AI_Assistant_install.ps1 -------------------------------------------------------------------------------- /AI_Assistant_modules/config_states.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tori29umai0123/AI-Assistant/HEAD/AI_Assistant_modules/config_states.py -------------------------------------------------------------------------------- /AI_Assistant_modules/ui_extensions.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tori29umai0123/AI-Assistant/HEAD/AI_Assistant_modules/ui_extensions.py -------------------------------------------------------------------------------- /AI_Assistant_exUI.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | start /d "%~dp0" AI_Assistant.exe --nowebui --xformers --skip-python-version-check --skip-torch-cuda-test --exui 3 | -------------------------------------------------------------------------------- /AI_Assistant_modules/launch_utils_AI_Assistant.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from modules import cmd_args 3 | 4 | 5 | args, _ = cmd_args.parser.parse_known_args() 6 | 7 | 8 | 9 | def start(): 10 | print(f"Launching {'API server' if '--nowebui' in sys.argv else 'Web UI'} with arguments: {' '.join(sys.argv[1:])}") 11 | import AI_Assistant_gui 12 | AI_Assistant_gui.api_only() 13 | 14 | from modules_forge import main_thread 15 | 16 | main_thread.loop() 17 | return 18 | 19 | 20 | def dump_sysinfo(): 21 | from modules import sysinfo 22 | import datetime 23 | 24 | text = sysinfo.get() 25 | filename = f"sysinfo-{datetime.datetime.utcnow().strftime('%Y-%m-%d-%H-%M')}.json" 26 | 27 | with open(filename, "w", encoding="utf8") as file: 28 | file.write(text) 29 | 30 | return filename 31 | -------------------------------------------------------------------------------- /AI_Assistant_modules/gitpython_hack.py: -------------------------------------------------------------------------------- 1 | import io 2 | from typing import Tuple 3 | 4 | import pygit2 5 | 6 | 7 | class Git: 8 | """ 9 | Git wrapper class. 10 | """ 11 | 12 | def __init__(self, repo_path: str): 13 | self.repo_path = repo_path 14 | 15 | def get_object_header(self, ref: str) -> Tuple[str, str, int]: 16 | repo = pygit2.Repository(self.repo_path) 17 | obj = repo.revparse_single(ref) 18 | return obj.hex, obj.type, obj.size 19 | 20 | def stream_object_data(self, ref: str) -> Tuple[str, str, int, io.BytesIO]: 21 | repo = pygit2.Repository(self.repo_path) 22 | obj = repo.revparse_single(ref) 23 | data = obj.data 24 | bio = io.BytesIO(data) 25 | return obj.hex, obj.type, obj.size, bio 26 | 27 | 28 | class Repo: 29 | def __init__(self, repo_path: str): 30 | self.repo_path = repo_path 31 | self.git = Git(repo_path) 32 | -------------------------------------------------------------------------------- /AI_Assistant_modules/shared_cmd_options.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import launch 4 | from modules import cmd_args, script_loading 5 | from modules.paths_internal import models_path, script_path, data_path, sd_configs_path, sd_default_config, sd_model_file, default_sd_model_file, extensions_dir, extensions_builtin_dir # noqa: F401 6 | 7 | parser = cmd_args.parser 8 | 9 | script_loading.preload_extensions(extensions_dir, parser, extension_list=launch.list_extensions(launch.args.ui_settings_file)) 10 | script_loading.preload_extensions(extensions_builtin_dir, parser) 11 | 12 | if os.environ.get('IGNORE_CMD_ARGS_ERRORS', None) is None: 13 | cmd_opts, _ = parser.parse_known_args() 14 | else: 15 | cmd_opts, _ = parser.parse_known_args() 16 | 17 | cmd_opts.nowebui = True 18 | 19 | cmd_opts.webui_is_non_local = any([cmd_opts.share, cmd_opts.listen, cmd_opts.ngrok, cmd_opts.server_name]) 20 | cmd_opts.disable_extension_access = cmd_opts.webui_is_non_local and not cmd_opts.enable_insecure_extension_access -------------------------------------------------------------------------------- /utils/prompt_utils.py: -------------------------------------------------------------------------------- 1 | def remove_duplicates(base_prompt): 2 | # タグの重複を取り除く 3 | prompt_list = base_prompt.split(", ") 4 | seen = set() 5 | unique_tags = [] 6 | for tag in prompt_list : 7 | tag_clean = tag.lower().strip() 8 | if tag_clean not in seen and tag_clean != "": 9 | unique_tags.append(tag) 10 | seen.add(tag_clean) 11 | return ", ".join(unique_tags) 12 | 13 | 14 | def remove_color(base_prompt): 15 | # タグの色情報を取り除く 16 | prompt_list = base_prompt.split(", ") 17 | color_list = ["pink", "red", "orange", "brown", "yellow", "green", "blue", "purple", "blonde", "colored skin", "white hair"] 18 | # カラータグを除去します。 19 | cleaned_tags = [tag for tag in prompt_list if all(color.lower() not in tag.lower() for color in color_list)] 20 | return ", ".join(cleaned_tags) 21 | 22 | 23 | def execute_prompt(execute_tags, base_prompt): 24 | prompt_list = base_prompt.split(", ") 25 | # execute_tagsを除去 26 | filtered_tags = [tag for tag in prompt_list if tag not in execute_tags] 27 | # 最終的なプロンプトを生成 28 | return ", ".join(filtered_tags) 29 | -------------------------------------------------------------------------------- /AI_Assistant_modules/application_config.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import os 3 | import sys 4 | 5 | from gradio.utils import colab_check, is_zero_gpu_space 6 | 7 | 8 | class ApplicationConfig: 9 | def __init__(self, lang_util, dpath): 10 | self.lang_util = lang_util 11 | self.fastapi_url = None 12 | self.dpath = dpath 13 | self.output_dir = os.path.join(dpath, "output") 14 | self.exui = False 15 | 16 | device_mapping = { 17 | 'darwin': 'mac', 18 | 'linux': 'linux', 19 | 'win32': 'windows' 20 | } 21 | if colab_check() or is_zero_gpu_space() or os.environ.get("GRADIO_CLOUD") == "1": 22 | self.device = "cloud" 23 | elif os.path.exists('/.dockerenv'): 24 | self.device = "docker" 25 | else: 26 | self.device = device_mapping.get(sys.platform.split()[0], 'unknown') 27 | 28 | def set_fastapi_url(self, url): 29 | self.fastapi_url = url 30 | 31 | def make_output_path(self, filename=None): 32 | if filename is None: 33 | filename = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") 34 | 35 | if not os.path.exists(self.output_dir): 36 | os.makedirs(self.output_dir) 37 | return os.path.join(self.output_dir, filename + ".png") 38 | -------------------------------------------------------------------------------- /utils/lang_util.py: -------------------------------------------------------------------------------- 1 | import configparser 2 | import os 3 | import sys 4 | 5 | 6 | def _get_appropriate_file_path(): 7 | if getattr(sys, 'frozen', False): 8 | return os.path.dirname(sys.executable) 9 | else: 10 | return os.path.dirname(os.path.dirname(__file__)) 11 | 12 | 13 | def get_language_argument(default='jp'): 14 | for arg in sys.argv: 15 | if arg.startswith('--lang='): 16 | return arg.split('=')[1] 17 | return default 18 | 19 | 20 | class LangUtil: 21 | def __init__(self, language_code='en'): 22 | self.language_code = language_code 23 | self.config = configparser.ConfigParser() 24 | self.appropriate_file_path = _get_appropriate_file_path() 25 | self._load_language() 26 | 27 | def _load_language(self): 28 | try: 29 | language_file = os.path.join(self.appropriate_file_path, 'languages', 30 | f'language_{self.language_code}.properties') 31 | 32 | with open(language_file, 'r', encoding='utf-8') as f: 33 | self.config.read_file(f) 34 | except Exception as e: 35 | print(f"Error loading language file: {e}") 36 | 37 | def get_text(self, key): 38 | try: 39 | return self.config.get('LANGUAGE', key) 40 | except Exception as e: 41 | print(f"Error getting text: {e}") 42 | return key 43 | -------------------------------------------------------------------------------- /AI_Assistant.py: -------------------------------------------------------------------------------- 1 | import locale 2 | import sys 3 | 4 | from modules import launch_utils_AI_Assistant 5 | 6 | # Default arguments 7 | default_args = ["--nowebui", "--xformers", "--skip-python-version-check", "--skip-torch-cuda-test", "--skip-torch-cuda-test"] 8 | 9 | # Check if custom arguments are provided; if not, append default arguments 10 | if len(sys.argv) == 1: 11 | sys.argv.extend(default_args) 12 | else: 13 | # 独自の引数がある場合、default_argsの中で未指定の引数のみを追加する 14 | # 引数を解析しやすくするため、setを使用 15 | provided_args_set = set(sys.argv) 16 | for arg in default_args: 17 | # "--"で始まるオプションのみを考慮する 18 | if arg.startswith("--"): 19 | option = arg.split("=")[0] if "=" in arg else arg 20 | if option not in provided_args_set and "--no-" + option.removeprefix('--') not in provided_args_set: 21 | sys.argv.append(arg) 22 | else: 23 | # "--"で始まらないオプションは直接追加 24 | sys.argv.append(arg) 25 | 26 | if "--lang" not in sys.argv: 27 | system_locale = locale.getdefaultlocale()[0] 28 | if system_locale.startswith("ja"): 29 | sys.argv.append("--lang=jp") 30 | elif system_locale.startswith("zh"): 31 | sys.argv.append("--lang=zh_CN") 32 | else: 33 | sys.argv.append("--lang=en") 34 | 35 | args = launch_utils_AI_Assistant.args 36 | 37 | start = launch_utils_AI_Assistant.start 38 | 39 | def main(): 40 | start() 41 | 42 | if __name__ == "__main__": 43 | main() -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /models/ 2 | /venv/ 3 | /.idea/ 4 | 5 | # WebUI Forge 6 | /javascript/ 7 | /ldm_patched/ 8 | /localizations/ 9 | /modules/ 10 | /modules_forge/ 11 | /configs/ 12 | /extensions-builtin/ 13 | /html/ 14 | 15 | __pycache__ 16 | *.ckpt 17 | *.safetensors 18 | *.pth 19 | /ESRGAN/* 20 | /SwinIR/* 21 | /repositories 22 | /venv 23 | /tmp 24 | /output 25 | /model.ckpt 26 | /models/**/* 27 | /GFPGANv1.3.pth 28 | /gfpgan/weights/*.pth 29 | /ui-config.json 30 | /outputs 31 | /config.json 32 | /log 33 | /webui.settings.bat 34 | /embeddings 35 | /styles.csv 36 | /params.txt 37 | /styles.csv.bak 38 | /webui-user.bat 39 | /webui-user.sh 40 | /interrogate 41 | /user.css 42 | /.idea 43 | notification.mp3 44 | /SwinIR 45 | /textual_inversion 46 | .vscode 47 | /extensions 48 | /test/stdout.txt 49 | /test/stderr.txt 50 | /cache.json* 51 | /config_states/ 52 | /node_modules 53 | /package-lock.json 54 | /.coverage* 55 | /test/test_outputs 56 | /test/results.xml 57 | coverage.xml 58 | **/tests/**/expectations 59 | /.eslintignore 60 | /.eslintrc.js 61 | /.git-blame-ignore-revs 62 | /.pylintrc 63 | /CHANGELOG.md 64 | /CITATION.cff 65 | /CODEOWNERS 66 | /environment-wsl2.yaml 67 | /launch.py 68 | /LICENSE.txt 69 | /package.json 70 | /pyproject.toml 71 | /requirements.txt 72 | /requirements-test.txt 73 | /requirements_npu.txt 74 | /requirements_versions.txt 75 | /script.js 76 | /style.css 77 | /webui.bat 78 | /webui.pys 79 | /webui.sh 80 | /webui-macos-env.sh 81 | /build/AI_Assistant/ 82 | /dist/AI_Assistant/ 83 | /webui.py 84 | -------------------------------------------------------------------------------- /AI_Assistant_modules/prompt_analysis.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import gradio as gr 4 | 5 | from utils.prompt_utils import remove_color 6 | from utils.tagger import modelLoad, analysis 7 | 8 | 9 | class PromptAnalysis: 10 | def __init__(self, app_config, post_filter=True, 11 | default_nagative_prompt="lowres, error, extra digit, fewer digits, cropped, worst quality, " 12 | "low quality, normal quality, jpeg artifacts, blurry"): 13 | self.default_nagative_prompt = default_nagative_prompt 14 | self.post_filter = post_filter 15 | self.model = None 16 | self.model_dir = os.path.join(app_config.dpath, 'models/tagger') 17 | 18 | def layout(self, lang_util, input_image): 19 | with gr.Column(): 20 | with gr.Row(): 21 | self.prompt_analysis_button = gr.Button(lang_util.get_text("analyze_prompt")) 22 | with gr.Row(): 23 | self.prompt = gr.Textbox(label=lang_util.get_text("prompt"), lines=3) 24 | with gr.Row(): 25 | self.negative_prompt = gr.Textbox(label=lang_util.get_text("negative_prompt"), lines=3, value=self.default_nagative_prompt) 26 | 27 | 28 | self.prompt_analysis_button.click( 29 | self.process_prompt_analysis, 30 | inputs=[input_image], 31 | outputs=self.prompt 32 | ) 33 | return [self.prompt, self.negative_prompt] 34 | 35 | def process_prompt_analysis(self, input_image_path): 36 | if self.model is None: 37 | self.model = modelLoad(self.model_dir) 38 | tags = analysis(input_image_path, self.model_dir, self.model) 39 | tags_list = tags 40 | if self.post_filter: 41 | tags_list = remove_color(tags) 42 | return tags_list 43 | -------------------------------------------------------------------------------- /languages/language_zh_CN.properties: -------------------------------------------------------------------------------- 1 | [LANGUAGE] 2 | analyze_prompt=分析提示 3 | anime_coloring=动漫着色 4 | anime_shadow=动画阴影 5 | anime_shadow_input_image=动画阴影输入线稿 6 | anime_shadow_tab_transfer=转移到动画阴影标签 7 | anytest_choice=Anytest選擇 8 | anytest_fidelity=Anytest保真度 9 | anytest_image=Anytest图像 10 | canny_creation=生成Canny 11 | canny_image=Canny图像 12 | clipboard=剪贴板 13 | create=创建 14 | coloring=着色 15 | color_scheme=顏色方案 16 | coloring_choice=着色类型 17 | delete=删除 18 | drag_drop_image=将图片拖拽到此处 19 | generate=生成 20 | generate_normalmap=生成法线贴图 21 | image_fidelity=图像保真度 22 | img2img=img2img 23 | input_color=输入颜色 24 | input_image=输入图像 25 | input_lineart=输入线稿 26 | light_pitch=光源俯仰 27 | light_source_directly_above=光源:正上方 28 | light_source_directly_below=光源:正下方 29 | light_source_directly_left=光源:左侧 30 | light_source_directly_right=光源:右侧 31 | light_source_upper_left_diagonal=光源:左上方 32 | light_source_upper_right_diagonal=光源:右上方 33 | light_yaw=光源偏航 34 | lighting=灯光 35 | lineart=线稿 36 | lineart2=线条的粗细变化 37 | lineart_bold=线稿加粗 38 | lineart_fidelity=线稿保真度 39 | line_image=线稿 40 | lora_models = Lora列表 41 | Lora_update = Lora列表更新 42 | mask_image=遮罩图像 43 | max_length=最大长度 44 | negative_prompt=反向提示 45 | normal_diffuse_strength=普通扩散强度 46 | normalmap=法线贴图 47 | normalmap_fidelity=法线贴图保真度 48 | normalmap_negative_prompt=法线贴图反向提示 49 | normalmap_prompt=法线贴图提示 50 | output_destination=输出目标 51 | output_image=输出图像 52 | output_normalmap_image=输出法线贴图 53 | pose_fidelity=姿势保真度 54 | pose_image=姿势图像 55 | prompt=提示 56 | resize=調整大小 57 | save=保存 58 | save_image=保存图像 59 | screenshot=截屏 60 | shadow_choices=陰影型 61 | shadow_image=阴影图像 62 | specular_highlights_strength=高光强度 63 | specular_power=高光强度 64 | stick2body=姿勢娃娃 65 | total_gain=总增益 66 | transfer_to_coloring=转移到着色标签 67 | transfer_to_lighting=转移到灯光标签 68 | transfer_to_lineart=转移到线稿标签 69 | transfer_to_normalmap=转移到法线贴图标签 70 | thick_coating=厚涂层 71 | invert_image=反转图像 72 | invert_creation=生成invert 73 | watercolor=水彩 74 | -------------------------------------------------------------------------------- /languages/language_jp.properties: -------------------------------------------------------------------------------- 1 | [LANGUAGE] 2 | analyze_prompt=prompt分析 3 | anime_coloring=アニメ塗り 4 | anime_shadow=アニメ影 5 | anime_shadow_input_image=入力線画 6 | anime_shadow_tab_transfer=アニメ影タブ転送 7 | anytest_choice=Anytest選択 8 | anytest_fidelity=Anytest忠実度 9 | anytest_image=Anytest画像 10 | canny_creation=canny作成 11 | canny_image=canny画像 12 | clipboard=クリップボード 13 | create=作成 14 | coloring=着色 15 | coloring_choice=着色タイプ 16 | color_scheme=色見本 17 | delete=削除 18 | drag_drop_image=画像をここにドラッグ&ドロップ 19 | generate=生成 20 | generate_normalmap=生成 21 | image_fidelity=画像再現度 22 | img2img=img2img 23 | input_color=カラー画像入力 24 | input_image=入力画像 25 | input_lineart=入力線画 26 | light_pitch=Light Pitch 27 | light_source_directly_above=光源:真上 28 | light_source_directly_below=光源:真下 29 | light_source_directly_left=光源:左横 30 | light_source_directly_right=光源:右横 31 | light_source_upper_left_diagonal=光源:左斜め上 32 | light_source_upper_right_diagonal=光源:右斜め上 33 | light_yaw=Light Yaw 34 | lighting=ライティング 35 | lineart=線画化 36 | lineart2=線画入り抜き 37 | lineart_bold=線画太さ 38 | lineart_fidelity=線画忠実度 39 | line_image=線画 40 | lora_models = Loraモデル一覧 41 | lora_update = Loraモデル一覧更新 42 | mask_image=mask画像 43 | max_length=長辺の長さ 44 | negative_prompt=negative prompt 45 | normal_diffuse_strength=Normal Diffuse Strength 46 | normalmap=ノーマルマップ 47 | normalmap_fidelity=線画忠実度 48 | normalmap_negative_prompt=negative prompt 49 | normalmap_prompt=prompt 50 | output_destination=出力先 51 | output_image=出力画像 52 | output_normalmap_image=出力画像 53 | pose_fidelity=ポーズ忠実度 54 | pose_image=ポーズ画像 55 | prompt=prompt 56 | resize=リサイズ 57 | save=保存 58 | save_image=画像保存 59 | screenshot=画面キャプチャ 60 | shadow_choices=影タイプ 61 | shadow_image=陰影画像 62 | specular_highlights_strength=Specular Highlights Strength 63 | specular_power=Specular Power 64 | stick2body=ポーズ人形 65 | total_gain=Total Gain 66 | transfer_to_coloring=着色タブ転送 67 | transfer_to_lighting=ライティングタブ転送 68 | transfer_to_lineart=線画タブ転送 69 | transfer_to_normalmap=Normalmapタブ転送 70 | thick_coating=厚塗り 71 | watercolor=水彩 -------------------------------------------------------------------------------- /languages/language_en.properties: -------------------------------------------------------------------------------- 1 | [LANGUAGE] 2 | analyze_prompt=Analyze Prompt 3 | anime_coloring=Anime Coloring 4 | anime_shadow=Anime Shadow 5 | anime_shadow_input_image=Input Lineart for Anime Shadow 6 | anime_shadow_tab_transfer=Transfer to Anime Shadow Tab 7 | anytest_choice=Anytest Choice 8 | anytest_fidelity=Anytest Fidelity 9 | anytest_image=Anytest Image 10 | canny_creation=Create Canny 11 | canny_image=Canny Image 12 | clipboard=Clipboard 13 | create=Create 14 | coloring=Coloring 15 | color_scheme=Color Scheme 16 | coloring_choice=Coloring Type 17 | delete=Delete 18 | drag_drop_image=Drag & Drop Image Here 19 | generate=Generate 20 | generate_normalmap=Generate Normalmap 21 | image_fidelity=Image Fidelity 22 | img2img=img2img 23 | input_color=Input Color 24 | input_image=Input Image 25 | input_lineart=Input Lineart 26 | light_pitch=Light Pitch 27 | light_source_directly_above=Light Source: Directly Above 28 | light_source_directly_below=Light Source: Directly Below 29 | light_source_directly_left=Light Source: Directly Left 30 | light_source_directly_right=Light Source: Directly Right 31 | light_source_upper_left_diagonal=Light Source: Upper Left Diagonal 32 | light_source_upper_right_diagonal=Light Source: Upper Right Diagonal 33 | light_yaw=Light Yaw 34 | lighting=Lighting 35 | lineart=Lineart 36 | lineart2=Line thickness 37 | lineart_bold=Lineart Boldness 38 | lineart_fidelity=Lineart Fidelity 39 | line_image=Line Image 40 | lora_models = Lora Model List 41 | lora_update = Lora Model List Updated 42 | mask_image=Mask Image 43 | max_length=Max Length 44 | negative_prompt=Negative Prompt 45 | normal_diffuse_strength=Normal Diffuse Strength 46 | normalmap=Normal Map 47 | normalmap_fidelity=Normalmap Fidelity 48 | normalmap_negative_prompt=Negative Prompt for Normalmap 49 | normalmap_prompt=Prompt for Normalmap 50 | output_destination=Output Destination 51 | output_image=Output Image 52 | output_normalmap_image=Output Normalmap Image 53 | pose_fidelity=Pose Fidelity 54 | pose_image=Pose Image 55 | prompt=Prompt 56 | resize=Resize 57 | save=Save 58 | save_image=Save Image 59 | screenshot=Screenshot 60 | shadow_choices=Shadow Type 61 | shadow_image=Shadow Image 62 | specular_highlights_strength=Specular Highlights Strength 63 | specular_power=Specular Power 64 | stick2body=Pose Doll 65 | total_gain=Total Gain 66 | transfer_to_coloring=Transfer to Coloring Tab 67 | transfer_to_lighting=Transfer to Lighting Tab 68 | transfer_to_lineart=Transfer to Lineart Tab 69 | transfer_to_normalmap=Transfer to Normalmap Tab 70 | thick_coating=Thick Coating 71 | invert_image=invert image 72 | invert_creation=invert creation 73 | watercolor=Watercolor 74 | -------------------------------------------------------------------------------- /AI_Assistant_modules/sd2_clip.py: -------------------------------------------------------------------------------- 1 | import sys 2 | # 'frozen' 状態に応じて適切なファイルパスを取得する関数 3 | def get_appropriate_file_path(): 4 | if getattr(sys, 'frozen', False): 5 | return sys.executable + "/Line2Normalmap/" 6 | else: 7 | return __file__ 8 | appropriate_file_path = get_appropriate_file_path() 9 | import sys 10 | # 'frozen'状態に応じて適切なファイルパスを取得する関数 11 | def get_appropriate_file_path(): 12 | if getattr(sys, 'frozen', False): 13 | # ビルドされたアプリケーションの場合、sys.executableのパスを使用 14 | return sys.executable + "/Line2Normalmap/" 15 | else: 16 | # そうでない場合は、従来通りappropriate_file_pathを使用 17 | return appropriate_file_path 18 | 19 | # 適切なファイルパスを取得 20 | appropriate_file_path = get_appropriate_file_path() 21 | 22 | import sys 23 | import os 24 | 25 | # 'frozen'状態に応じて適切なファイルパスを取得する関数 26 | def get_appropriate_file_path(): 27 | if getattr(sys, 'frozen', False): 28 | # ビルドされたアプリケーションの場合、os.path.dirname(sys.executable)のパスを使用 29 | return os.path.dirname(sys.executable) + "/ldm_patched/modules" 30 | else: 31 | # そうでない場合は、従来通りappropriate_file_pathを使用 32 | return os.path.dirname(appropriate_file_path) 33 | 34 | # 適切なファイルパスを取得 35 | appropriate_file_path = get_appropriate_file_path() 36 | 37 | # Taken from https://github.com/comfyanonymous/ComfyUI 38 | # This file is only for reference, and not used in the backend or runtime. 39 | 40 | 41 | from ldm_patched.modules import sd1_clip 42 | import torch 43 | import os 44 | 45 | class SD2ClipHModel(sd1_clip.SDClipModel): 46 | def __init__(self, arch="ViT-H-14", device="cpu", max_length=77, freeze=True, layer="penultimate", layer_idx=None, dtype=None): 47 | if layer == "penultimate": 48 | layer="hidden" 49 | layer_idx=-2 50 | 51 | textmodel_json_config = os.path.join(appropriate_file_path, "sd2_clip_config.json") 52 | super().__init__(device=device, freeze=freeze, layer=layer, layer_idx=layer_idx, textmodel_json_config=textmodel_json_config, dtype=dtype, special_tokens={"start": 49406, "end": 49407, "pad": 0}) 53 | 54 | class SD2ClipHTokenizer(sd1_clip.SDTokenizer): 55 | def __init__(self, tokenizer_path=None, embedding_directory=None): 56 | super().__init__(tokenizer_path, pad_with_end=False, embedding_directory=embedding_directory, embedding_size=1024) 57 | 58 | class SD2Tokenizer(sd1_clip.SD1Tokenizer): 59 | def __init__(self, embedding_directory=None): 60 | super().__init__(embedding_directory=embedding_directory, clip_name="h", tokenizer=SD2ClipHTokenizer) 61 | 62 | class SD2ClipModel(sd1_clip.SD1ClipModel): 63 | def __init__(self, device="cpu", dtype=None, **kwargs): 64 | super().__init__(device=device, dtype=dtype, clip_name="h", clip_model=SD2ClipHModel, **kwargs) 65 | -------------------------------------------------------------------------------- /AI_Assistant_modules/output_image_gui.py: -------------------------------------------------------------------------------- 1 | import gradio as gr 2 | 3 | javascript = """ 4 | function copyToClipboard() { 5 | var img = Array.from(document.querySelectorAll('.output-image img') || []).filter(img => img.offsetParent)[0]; 6 | if (!img) { 7 | return; 8 | } 9 | fetch(img.src) 10 | .then(response => response.blob()) 11 | .then(blob => { 12 | const item = new ClipboardItem({ "image/png": blob }); 13 | navigator.clipboard.write([item]); 14 | }) 15 | .catch(console.error); 16 | } 17 | """ 18 | 19 | class OutputImage: 20 | def __init__(self, app_config, transfer_target_lang_key=None): 21 | self.app_config = app_config 22 | self.transfer_button = None 23 | self.output_image = None 24 | self.output_image_path = None 25 | self.transfer_target_lang_key = transfer_target_lang_key 26 | 27 | def layout(self): 28 | lang_util = self.app_config.lang_util 29 | if self.transfer_target_lang_key == "noline": 30 | output_image = gr.Image(label=lang_util.get_text("noline_image"), interactive=False, type="filepath", 31 | elem_classes=["output_image"]) 32 | else: 33 | output_image = gr.Image(label=lang_util.get_text("output_image"), interactive=False, type="filepath", 34 | elem_classes=["output-image"]) 35 | 36 | output_image.change(self._set_output_image, inputs=[output_image]) 37 | clipboard_button = gr.Button("" + lang_util.get_text("clipboard"), elem_classes=["clipboard"], 38 | interactive=False) 39 | clipboard_button.click(self._notify, _js=javascript, queue=True) 40 | if self.transfer_target_lang_key is not None: 41 | if self.transfer_target_lang_key != "noline": 42 | self.transfer_button = gr.Button(lang_util.get_text(self.transfer_target_lang_key), interactive=False) 43 | output_image.change(lambda x: gr.update(interactive=x is not None), inputs=[output_image], 44 | outputs=[self.transfer_button]) 45 | if self.app_config.device != "cloud" and self.app_config.device != "docker": 46 | if self.transfer_target_lang_key != "noline": 47 | gr.Button(lang_util.get_text("output_destination")).click(self._open_output_folder) 48 | 49 | self.output_image = output_image 50 | output_image.change(lambda x: gr.update(interactive=x is not None), inputs=[output_image], 51 | outputs=[clipboard_button]) 52 | 53 | return output_image 54 | 55 | def _set_output_image(self, output_image_path): 56 | self.output_image_path = output_image_path 57 | 58 | def _notify(self): 59 | if self.output_image_path is None: 60 | gr.Warning("Please Image Select") 61 | else: 62 | gr.Info("Image Copied to Clipboard") 63 | 64 | def _open_output_folder(self): 65 | import subprocess 66 | if self.app_config.device == "windows": 67 | subprocess.Popen(["explorer", self.app_config.output_dir]) 68 | elif self.app_config.device == "mac": 69 | subprocess.Popen(["open", self.app_config.output_dir]) 70 | else: 71 | subprocess.Popen(["xdg-open", self.app_config.output_dir]) 72 | -------------------------------------------------------------------------------- /AI_Assistant_gui.py: -------------------------------------------------------------------------------- 1 | from __future__ import annotations 2 | 3 | import asyncio 4 | import atexit 5 | import os 6 | import socket 7 | import sys 8 | import webbrowser 9 | from threading import Event 10 | 11 | from fastapi.responses import RedirectResponse 12 | 13 | from AI_Assistant_modules.application_config import ApplicationConfig 14 | from AI_Assistant_modules.tab_gui import gradio_tab_gui 15 | from modules import initialize 16 | from modules import initialize_util 17 | from modules import timer 18 | from modules_forge import main_thread 19 | from modules_forge.initialization import initialize_forge 20 | from utils.lang_util import LangUtil, get_language_argument 21 | 22 | startup_timer = timer.startup_timer 23 | startup_timer.record("launcher") 24 | 25 | initialize_forge() 26 | initialize.imports() 27 | initialize.check_versions() 28 | initialize.initialize() 29 | 30 | shutdown_event = Event() 31 | 32 | def create_api(app): 33 | from modules.api.api import Api 34 | from modules.call_queue import queue_lock 35 | 36 | api = Api(app, queue_lock) 37 | return api 38 | 39 | def is_port_in_use(port): 40 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: 41 | return s.connect_ex(('localhost', port)) == 0 42 | 43 | def find_available_port(starting_port): 44 | port = starting_port 45 | while is_port_in_use(port): 46 | print(f"Port {port} is in use, trying next one.") 47 | port += 1 48 | return port 49 | 50 | async def api_only_worker(shutdown_event: Event): 51 | from fastapi import FastAPI 52 | import uvicorn 53 | 54 | app = FastAPI() 55 | 56 | # 言語設定の取得 57 | lang_util = LangUtil(get_language_argument()) 58 | # 基準ディレクトリの取得 59 | if getattr(sys, 'frozen', False): 60 | # PyInstaller でビルドされた場合 61 | dpath = os.path.dirname(sys.executable) 62 | else: 63 | # 通常の Python スクリプトとして実行された場合 64 | dpath = os.path.dirname(sys.argv[0]) 65 | app_config = ApplicationConfig(lang_util, dpath) 66 | 67 | #sys.argvの中に--exuiがある場合、app_configにexuiを設定する 68 | if "--exui" in sys.argv: 69 | app_config.exui = True 70 | 71 | is_share = False 72 | if "--share" in sys.argv: 73 | is_share = True 74 | 75 | # Gradioインターフェースの設定 76 | _, gradio_url, _ = gradio_tab_gui(app_config).queue().launch(share=is_share, prevent_thread_lock=True) 77 | 78 | # FastAPIのルートにGradioのURLへのリダイレクトを設定 79 | @app.get("/", response_class=RedirectResponse) 80 | async def read_root(): 81 | return RedirectResponse(url=gradio_url) 82 | 83 | initialize_util.setup_middleware(app) 84 | api = create_api(app) 85 | 86 | from modules import script_callbacks 87 | script_callbacks.before_ui_callback() 88 | script_callbacks.app_started_callback(None, app) 89 | 90 | print(f"Startup time: {startup_timer.summary()}.") 91 | print(f"Web UI is running at {gradio_url}.") 92 | webbrowser.open(gradio_url) 93 | 94 | starting_port = 7861 95 | port = find_available_port(starting_port) 96 | app_config.set_fastapi_url(f"http://127.0.0.1:{port}") 97 | 98 | config = uvicorn.Config(app=app, host="127.0.0.1", port=port, log_level="info") 99 | server = uvicorn.Server(config=config) 100 | 101 | loop = asyncio.get_event_loop() 102 | shutdown_event.set() 103 | await loop.create_task(server.serve()) 104 | 105 | def api_only(): 106 | loop = asyncio.get_event_loop() 107 | loop.run_until_complete(api_only_worker(shutdown_event)) 108 | 109 | def on_exit(): 110 | print("Cleaning up...") 111 | shutdown_event.set() 112 | 113 | if __name__ == "__main__": 114 | atexit.register(on_exit) 115 | api_only() 116 | main_thread.loop() -------------------------------------------------------------------------------- /AI_Assistant_modules/actions/resize.py: -------------------------------------------------------------------------------- 1 | import gradio as gr 2 | from PIL import Image 3 | 4 | from AI_Assistant_modules.output_image_gui import OutputImage 5 | from AI_Assistant_modules.prompt_analysis import PromptAnalysis 6 | from utils.prompt_utils import execute_prompt, remove_duplicates 7 | from utils.request_api import upscale_and_save_images 8 | 9 | LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS) 10 | 11 | 12 | class ImageResize: 13 | def __init__(self, app_config): 14 | self.app_config = app_config 15 | self.input_image = None 16 | self.output = None 17 | 18 | def layout(self, transfer_target_lang_key=None): 19 | lang_util = self.app_config.lang_util 20 | with gr.Row() as self.block: 21 | with gr.Column(): 22 | with gr.Row(): 23 | with gr.Column(): 24 | self.input_image = gr.Image(label=lang_util.get_text("input_image"), 25 | source="upload", 26 | type='filepath', interactive=True) 27 | with gr.Column(): 28 | pass 29 | with gr.Row(): 30 | prompt_analysis = PromptAnalysis(app_config=self.app_config, post_filter=False) 31 | [prompt, nega] = PromptAnalysis(self.app_config).layout(lang_util, self.input_image) 32 | with gr.Row(): 33 | max_length_scale = gr.Slider(minimum=1600, maximum=2880, step=1, interactive=True, 34 | label=lang_util.get_text("max_length")) 35 | with gr.Row(): 36 | generate_button = gr.Button(lang_util.get_text("generate"), interactive=False) 37 | with gr.Column(): 38 | self.output = OutputImage(self.app_config, transfer_target_lang_key) 39 | output_image = self.output.layout() 40 | 41 | self.input_image.change(lambda x: gr.update(interactive=x is not None), inputs=[self.input_image], 42 | outputs=[generate_button]) 43 | 44 | generate_button.click(self._process, inputs=[ 45 | self.input_image, 46 | prompt, 47 | nega, 48 | max_length_scale, 49 | ], outputs=[output_image]) 50 | 51 | def _process(self, input_image_path, prompt_text, negative_prompt_text, max_length_scale): 52 | prompt = "masterpiece, best quality " + prompt_text.strip() 53 | execute_tags = ["transparent background"] 54 | prompt =execute_prompt(execute_tags, prompt) 55 | prompt = remove_duplicates(prompt) 56 | nega = negative_prompt_text.strip() 57 | base_pil = Image.open(input_image_path).convert("RGBA") 58 | white_bg = Image.new("RGBA", base_pil.size, "WHITE") 59 | white_bg.paste(base_pil, mask=base_pil) 60 | base_pil = white_bg.convert("RGB") 61 | max_length = float(max_length_scale) 62 | # 元の画像サイズを取得 63 | original_width, original_height = base_pil.size 64 | # アスペクト比を計算 65 | aspect_ratio = original_width / original_height 66 | # 長辺がmax_lengthになるように新しいサイズを計算 67 | if original_width > original_height: 68 | new_width = int(max_length) 69 | new_height = int(round(max_length / aspect_ratio)) 70 | else: 71 | new_height = int(max_length) 72 | new_width = int(round(max_length * aspect_ratio)) 73 | image_size = [new_width, new_height] 74 | resize_output_path = self.app_config.make_output_path() 75 | output_pil = upscale_and_save_images(self.app_config.fastapi_url, prompt, nega, base_pil, resize_output_path, 76 | image_size) 77 | return output_pil 78 | -------------------------------------------------------------------------------- /AI_Assistant_modules/actions/color_scheme.py: -------------------------------------------------------------------------------- 1 | import gradio as gr 2 | from PIL import Image 3 | 4 | from AI_Assistant_modules.output_image_gui import OutputImage 5 | from AI_Assistant_modules.prompt_analysis import PromptAnalysis 6 | from utils.img_utils import make_base_pil, base_generation, resize_image_aspect_ratio, invert_process 7 | from utils.prompt_utils import execute_prompt, remove_duplicates 8 | from utils.request_api import create_and_save_images 9 | 10 | LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS) 11 | 12 | 13 | class ColorScheme: 14 | def __init__(self, app_config): 15 | self.app_config = app_config 16 | self.input_image = None 17 | self.output = None 18 | 19 | def layout(self, transfer_target_lang_key=None): 20 | lang_util = self.app_config.lang_util 21 | with gr.Row() as self.block: 22 | with gr.Column(): 23 | with gr.Row(): 24 | with gr.Column(): 25 | self.input_image = gr.Image(label=lang_util.get_text("input_lineart"), 26 | source="upload", 27 | type='filepath', interactive=True) 28 | with gr.Row(): 29 | prompt_analysis = PromptAnalysis(app_config=self.app_config, post_filter=False) 30 | [prompt, nega] = prompt_analysis.layout(lang_util=lang_util, input_image=self.input_image) 31 | with gr.Row(): 32 | generate_button = gr.Button(lang_util.get_text("generate"), interactive=False) 33 | with gr.Column(): 34 | self.output = OutputImage(self.app_config, transfer_target_lang_key) 35 | output_image = self.output.layout() 36 | 37 | self.input_image.change(lambda x: gr.update(interactive=x is not None), inputs=[self.input_image], 38 | outputs=[generate_button]) 39 | 40 | generate_button.click(self._process, inputs=[ 41 | self.input_image, 42 | prompt, 43 | nega, 44 | ], outputs=[output_image]) 45 | 46 | def _process(self, input_image_path, prompt_text, negative_prompt_text): 47 | prompt = "masterpiece, best quality, (flat color:1.4), " + prompt_text.strip() 48 | execute_tags = ["monochrome", "greyscale", "lineart", "sketch", "transparent background"] 49 | prompt =execute_prompt(execute_tags, prompt) 50 | prompt = remove_duplicates(prompt) 51 | nega = negative_prompt_text.strip() 52 | base_pil = make_base_pil(input_image_path) 53 | base_pil = resize_image_aspect_ratio(base_pil) 54 | base_pil = base_generation(base_pil.size, (255, 255, 255, 255)).convert("RGB") 55 | image_size = Image.open(input_image_path).size 56 | invert_pil = invert_process(input_image_path).resize(base_pil.size, LANCZOS).convert("RGB") 57 | mask_pil = base_generation(base_pil.size, (255, 255, 255, 255)).convert("RGB") 58 | image_fidelity = 1.0 59 | lineart_fidelity = 1.25 60 | output_path = self.app_config.make_output_path() 61 | output_pil = create_and_save_images(self.app_config.fastapi_url, prompt, nega, base_pil, mask_pil, 62 | image_size, output_path , image_fidelity, 63 | self._make_cn_args(invert_pil, lineart_fidelity)) 64 | return output_pil 65 | 66 | 67 | def _make_cn_args(self, invert_pil, lineart_fidelity): 68 | unit1 = { 69 | "image": invert_pil, 70 | "mask_image": None, 71 | "control_mode": "Balanced", 72 | "enabled": True, 73 | "guidance_end": 1.0, 74 | "guidance_start": 0, 75 | "pixel_perfect": True, 76 | "processor_res": 512, 77 | "resize_mode": "Just Resize", 78 | "weight": lineart_fidelity, 79 | "module": "None", 80 | "model": "Kataragi_lineartXL-lora128 [0598262f]", 81 | "save_detected_map": None, 82 | "hr_option": "Both" 83 | } 84 | unit2 = None 85 | return [unit1] 86 | -------------------------------------------------------------------------------- /AI_Assistant_modules/sdxl_clip.py: -------------------------------------------------------------------------------- 1 | import sys 2 | # 'frozen' 状態に応じて適切なファイルパスを取得する関数 3 | def get_appropriate_file_path(): 4 | if getattr(sys, 'frozen', False): 5 | return sys.executable + "/Line2Normalmap/" 6 | else: 7 | return __file__ 8 | appropriate_file_path = get_appropriate_file_path() 9 | import sys 10 | # 'frozen'状態に応じて適切なファイルパスを取得する関数 11 | def get_appropriate_file_path(): 12 | if getattr(sys, 'frozen', False): 13 | # ビルドされたアプリケーションの場合、sys.executableのパスを使用 14 | return sys.executable + "/Line2Normalmap/" 15 | else: 16 | # そうでない場合は、従来通りappropriate_file_pathを使用 17 | return appropriate_file_path 18 | 19 | # 適切なファイルパスを取得 20 | appropriate_file_path = get_appropriate_file_path() 21 | 22 | import sys 23 | import os 24 | 25 | # 'frozen'状態に応じて適切なファイルパスを取得する関数 26 | def get_appropriate_file_path(): 27 | if getattr(sys, 'frozen', False): 28 | # ビルドされたアプリケーションの場合、os.path.dirname(sys.executable)のパスを使用 29 | return os.path.dirname(sys.executable) + "/ldm_patched/modules" 30 | else: 31 | # そうでない場合は、従来通りappropriate_file_pathを使用 32 | return os.path.dirname(appropriate_file_path) 33 | 34 | 35 | # 適切なファイルパスを取得 36 | appropriate_file_path = get_appropriate_file_path() 37 | 38 | 39 | # Taken from https://github.com/comfyanonymous/ComfyUI 40 | # This file is only for reference, and not used in the backend or runtime. 41 | 42 | 43 | from ldm_patched.modules import sd1_clip 44 | import torch 45 | import os 46 | 47 | class SDXLClipG(sd1_clip.SDClipModel): 48 | def __init__(self, device="cpu", max_length=77, freeze=True, layer="penultimate", layer_idx=None, dtype=None): 49 | if layer == "penultimate": 50 | layer="hidden" 51 | layer_idx=-2 52 | 53 | textmodel_json_config = os.path.join(appropriate_file_path, "clip_config_bigg.json") 54 | super().__init__(device=device, freeze=freeze, layer=layer, layer_idx=layer_idx, textmodel_json_config=textmodel_json_config, dtype=dtype, 55 | special_tokens={"start": 49406, "end": 49407, "pad": 0}, layer_norm_hidden_state=False) 56 | 57 | def load_sd(self, sd): 58 | return super().load_sd(sd) 59 | 60 | class SDXLClipGTokenizer(sd1_clip.SDTokenizer): 61 | def __init__(self, tokenizer_path=None, embedding_directory=None): 62 | super().__init__(tokenizer_path, pad_with_end=False, embedding_directory=embedding_directory, embedding_size=1280, embedding_key='clip_g') 63 | 64 | 65 | class SDXLTokenizer: 66 | def __init__(self, embedding_directory=None): 67 | self.clip_l = sd1_clip.SDTokenizer(embedding_directory=embedding_directory) 68 | self.clip_g = SDXLClipGTokenizer(embedding_directory=embedding_directory) 69 | 70 | def tokenize_with_weights(self, text:str, return_word_ids=False): 71 | out = {} 72 | out["g"] = self.clip_g.tokenize_with_weights(text, return_word_ids) 73 | out["l"] = self.clip_l.tokenize_with_weights(text, return_word_ids) 74 | return out 75 | 76 | def untokenize(self, token_weight_pair): 77 | return self.clip_g.untokenize(token_weight_pair) 78 | 79 | class SDXLClipModel(torch.nn.Module): 80 | def __init__(self, device="cpu", dtype=None): 81 | super().__init__() 82 | self.clip_l = sd1_clip.SDClipModel(layer="hidden", layer_idx=-2, device=device, dtype=dtype, layer_norm_hidden_state=False) 83 | self.clip_g = SDXLClipG(device=device, dtype=dtype) 84 | 85 | def clip_layer(self, layer_idx): 86 | self.clip_l.clip_layer(layer_idx) 87 | self.clip_g.clip_layer(layer_idx) 88 | 89 | def reset_clip_layer(self): 90 | self.clip_g.reset_clip_layer() 91 | self.clip_l.reset_clip_layer() 92 | 93 | def encode_token_weights(self, token_weight_pairs): 94 | token_weight_pairs_g = token_weight_pairs["g"] 95 | token_weight_pairs_l = token_weight_pairs["l"] 96 | g_out, g_pooled = self.clip_g.encode_token_weights(token_weight_pairs_g) 97 | l_out, l_pooled = self.clip_l.encode_token_weights(token_weight_pairs_l) 98 | return torch.cat([l_out, g_out], dim=-1), g_pooled 99 | 100 | def load_sd(self, sd): 101 | if "text_model.encoder.layers.30.mlp.fc1.weight" in sd: 102 | return self.clip_g.load_sd(sd) 103 | else: 104 | return self.clip_l.load_sd(sd) 105 | 106 | class SDXLRefinerClipModel(sd1_clip.SD1ClipModel): 107 | def __init__(self, device="cpu", dtype=None): 108 | super().__init__(device=device, dtype=dtype, clip_name="g", clip_model=SDXLClipG) 109 | -------------------------------------------------------------------------------- /AI_Assistant_modules/tab_gui.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import gradio as gr 4 | 5 | from AI_Assistant_modules.actions.anime_shadow import AnimeShadow 6 | from AI_Assistant_modules.actions.color_scheme import ColorScheme 7 | from AI_Assistant_modules.actions.coloring import Coloring 8 | from AI_Assistant_modules.actions.i2i import Img2Img 9 | from AI_Assistant_modules.actions.lighting import Lighting 10 | from AI_Assistant_modules.actions.line_drawing import LineDrawing 11 | from AI_Assistant_modules.actions.line_drawing_cutout import LineDrawingCutOut 12 | from AI_Assistant_modules.actions.normal_map import NormalMap 13 | from AI_Assistant_modules.actions.resize import ImageResize 14 | from AI_Assistant_modules.actions.stick2body import Stick2Body 15 | 16 | # class base_gui: 17 | # def layout(self, lang_util, transfer_target=None): 18 | 19 | def _set_transfer_button(main_tab, target_tab_item, from_tab, transfer_target_tab): 20 | from_tab.output.transfer_button.click(fn=lambda x: [x, gr.Tabs.update(selected=target_tab_item.id)], 21 | inputs=[from_tab.output.output_image], 22 | outputs=[transfer_target_tab.input_image, main_tab]) 23 | 24 | def _open_outputdir(app_config): 25 | dir = os.path.join(app_config.dpath, "output") 26 | os.makedirs(dir, exist_ok=True) 27 | # image list 28 | image_list = [] 29 | for file in os.listdir(dir): 30 | if file.endswith(".png"): 31 | image_list.append(file) 32 | image_list.sort(reverse=True) 33 | for i, file in enumerate(image_list): 34 | image_list[i] = (os.path.join(dir, file), file) 35 | return image_list 36 | 37 | 38 | def gradio_tab_gui(app_config): 39 | lang_util = app_config.lang_util 40 | 41 | with gr.Blocks(title="AI_Assistant") as main_block: 42 | with gr.Tabs() as main_tab: 43 | with gr.TabItem(lang_util.get_text("img2img")): 44 | img_2_img = Img2Img(app_config) 45 | img_2_img.layout("transfer_to_lineart") 46 | with gr.TabItem(lang_util.get_text("stick2body")): 47 | stick_2_body = Stick2Body(app_config) 48 | stick_2_body.layout() 49 | with gr.TabItem(lang_util.get_text("lineart"), id="lineart") as line_drawing_tab_item: 50 | line_drawing_tab = LineDrawing(app_config) 51 | line_drawing_tab.layout("transfer_to_normalmap") 52 | with gr.TabItem(lang_util.get_text("lineart2")): 53 | line_drawing_cutout_tab = LineDrawingCutOut(app_config) 54 | line_drawing_cutout_tab.layout("transfer_to_normalmap") 55 | with gr.TabItem(lang_util.get_text("normalmap"), id="normalmap") as normal_map_tab_item: 56 | normal_map = NormalMap(app_config) 57 | normal_map.layout("transfer_to_lighting") 58 | with gr.TabItem(lang_util.get_text("lighting"), id="lighting") as lighting_tab_item: 59 | lighting = Lighting(app_config) 60 | lighting.layout("anime_shadow_tab_transfer") 61 | with gr.TabItem(lang_util.get_text("anime_shadow"), id="anime_shadow") as anime_shadow_tab_item: 62 | anime_shadow = AnimeShadow(app_config) 63 | anime_shadow.layout() 64 | with gr.TabItem(lang_util.get_text("color_scheme")): 65 | color_scheme = ColorScheme(app_config) 66 | color_scheme.layout() 67 | with gr.TabItem(lang_util.get_text("coloring")): 68 | coloring = Coloring(app_config) 69 | coloring.layout() 70 | with gr.TabItem(lang_util.get_text("resize")): 71 | ImageResize(app_config).layout() 72 | if app_config.device == "cloud" or app_config.device == "docker": 73 | with gr.TabItem(lang_util.get_text("output_destination")) as output_tab_item: 74 | gallery = gr.Gallery([], label=lang_util.get_text("output_destination"), interactive=False, 75 | height="85vh") 76 | output_tab_item.select(fn=lambda: _open_outputdir(app_config), outputs=[gallery]) 77 | 78 | # タブ間転送の動作設定 79 | _set_transfer_button(main_tab, line_drawing_tab_item, img_2_img, line_drawing_tab) 80 | _set_transfer_button(main_tab, normal_map_tab_item, line_drawing_tab, normal_map) 81 | _set_transfer_button(main_tab, normal_map_tab_item, line_drawing_cutout_tab, normal_map) 82 | _set_transfer_button(main_tab, lighting_tab_item, normal_map, lighting) 83 | _set_transfer_button(main_tab, anime_shadow_tab_item, lighting, anime_shadow) 84 | 85 | return main_block 86 | -------------------------------------------------------------------------------- /AI_Assistant_modules/actions/stick2body.py: -------------------------------------------------------------------------------- 1 | import gradio as gr 2 | from PIL import Image, ImageOps 3 | 4 | from AI_Assistant_modules.output_image_gui import OutputImage 5 | from AI_Assistant_modules.prompt_analysis import PromptAnalysis 6 | from utils.img_utils import make_base_pil, base_generation, resize_image_aspect_ratio, noline_process 7 | from utils.prompt_utils import execute_prompt, remove_duplicates 8 | from utils.request_api import create_and_save_images, get_lora_model 9 | 10 | LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS) 11 | 12 | 13 | class Stick2Body: 14 | def __init__(self, app_config): 15 | self.app_config = app_config 16 | self.input_image = None 17 | self.output = None 18 | 19 | def layout(self, transfer_target_lang_key=None): 20 | lang_util = self.app_config.lang_util 21 | exui = self.app_config.exui 22 | with gr.Row() as self.block: 23 | with gr.Column(): 24 | with gr.Row(): 25 | with gr.Column(): 26 | self.input_image = gr.Image(label=lang_util.get_text("pose_image"), 27 | source="upload", 28 | type='filepath', interactive=True) 29 | with gr.Row(): 30 | prompt_analysis = PromptAnalysis(app_config=self.app_config, post_filter=False) 31 | [prompt, nega] = prompt_analysis.layout(lang_util=lang_util, input_image=self.input_image) 32 | with gr.Row(): 33 | fidelity = gr.Slider(minimum=0.5, maximum=2.0, value=1.04, step=0.01, interactive=True, label=lang_util.get_text("pose_fidelity")) 34 | with gr.Row(): 35 | generate_button = gr.Button(lang_util.get_text("generate"), interactive=False) 36 | with gr.Column(): 37 | self.output = OutputImage(self.app_config, transfer_target_lang_key) 38 | output_image = self.output.layout() 39 | 40 | 41 | self.input_image.change(lambda x: gr.update(interactive=x is not None), inputs=[self.input_image], 42 | outputs=[generate_button]) 43 | 44 | 45 | 46 | generate_button.click(self._process, inputs=[ 47 | self.input_image, 48 | prompt, 49 | nega, 50 | fidelity, 51 | ], outputs=[output_image]) 52 | 53 | 54 | 55 | def _process(self, input_image_path, prompt_text, negative_prompt_text, fidelity, coloring_choice): 56 | prompt = "masterpiece, best quality, , simple background, white background, bald, nude, " + prompt_text.strip() 57 | execute_tags = ["NSFW", "monochrome", "greyscale", "pokemon (creature)", "no humans", "lineart", "triangle"] 58 | prompt =execute_prompt(execute_tags, prompt) 59 | prompt = remove_duplicates(prompt) 60 | nega = "nsfw, nipples, bad anatomy, liquid fingers, low quality, worst quality, out of focus, ugly, error, jpeg artifacts, lowers, blurry, bokeh" + negative_prompt_text.strip() 61 | nega = remove_duplicates(nega) 62 | base_pil = make_base_pil(input_image_path) 63 | base_pil = resize_image_aspect_ratio(base_pil) 64 | image_size = Image.open(input_image_path).size 65 | noline_pil = noline_process(input_image_path).resize(base_pil.size, LANCZOS).convert("RGB") 66 | mask_pil = base_generation(base_pil.size, (255, 255, 255, 255)).convert("RGB") 67 | pose_fidelity = float(fidelity) 68 | image_fidelity = 1.0 69 | output_path = self.app_config.make_output_path() 70 | output_pil = create_and_save_images(self.app_config.fastapi_url, prompt, nega, noline_pil, mask_pil, 71 | image_size, output_path, image_fidelity, 72 | self._make_cn_args(base_pil, pose_fidelity)) 73 | return output_pil 74 | 75 | 76 | def _make_cn_args(self, image_pil, pose_fidelity ): 77 | unit1 = { 78 | "image": image_pil, 79 | "mask_image": None, 80 | "control_mode": "ControlNet is more important", 81 | "enabled": True, 82 | "guidance_end": 0.4, 83 | "guidance_start": 0, 84 | "pixel_perfect": True, 85 | "processor_res": 512, 86 | "resize_mode": "Just Resize", 87 | "weight": pose_fidelity, 88 | "module": "None", 89 | "model": "CL_am31_pose3D_V7_marged_rank256 [af7c88a9]", 90 | "save_detected_map": None, 91 | "hr_option": "Both" 92 | } 93 | unit2 = None 94 | return [unit1] 95 | -------------------------------------------------------------------------------- /README_zh_CN.md: -------------------------------------------------------------------------------- 1 | [日本語](README.md) | [EN](README_en.md) | [中文](README_zh_CN.md) 2 | # AI-Assistant 3 | 这是一个绘图辅助AI的GUI应用程序,集成 [stable-diffusion-webui-forge](https://github.com/lllyasviel/stable-diffusion-webui-forge/tree/main) 作为后端。 4 | ![01](https://github.com/tori29umai0123/AI-Assistant/assets/1675141/07ea96a5-d9d0-4b87-a8f6-ba41b4680f33) 5 | 6 | # 启动方法 7 | ## exe文件 8 | 你可以直接双击exe文件来启动应用程序。 9 | 10 | 通过指定以下参数,你可以在启动时设定语言。 11 | ``` 12 | AI_Assistant.exe --lang=jp 13 | AI_Assistant.exe --lang=en 14 | AI_Assistant.exe --lang=zh_CN 15 | ``` 16 | 通过添加更多参数,您可以为Stable Diffusion Web UI添加选项(适用于高级用户)。 17 | 默认情况下,如下所示: 18 | AI_Assistant.exe --lang=ja --nowebui --xformers --skip-python-version-check --skip-torch-cuda-test --skip-torch-cuda-test 19 | 20 | 此外,通过添加以下参数,您可以显示扩展UI(目前,在i2i标签中可以加载LoRA): 21 | ``` 22 | --exui 23 | ``` 24 | ## 针对专家 25 | 通过运行以下类似的bat文件,您可以轻松指定参数。
26 | AI_Assistant.bat 27 | ``` 28 | @echo off 29 | start /d "%~dp0" AI_Assistant.exe --nowebui --xformers --skip-python-version-check --skip-torch-cuda-test --exui 30 | ``` 31 | 32 | AI_Assistant_lowVRAM.bat 33 | ``` 34 | @echo off 35 | start /d "%~dp0" AI_Assistant.exe --nowebui --xformers --skip-python-version-check --skip-torch-cuda-test --unet-in-fp8-e4m3fn 36 | ``` 37 | 38 | ## 开发者 39 | 配置好构建设置后,请运行`python AI_Assistant.py`。 40 | 41 | # 构建设置(开发者) 42 | 开发使用Python 3.10.x。 43 | 由于使用了webui-forge库,因此在构建时需要匹配版本并执行以下步骤: 44 | 1. 运行 AI_Assistant_install.ps1 进行安装。 45 | 2. 在安全软件设置中,将文件夹和可执行文件名添加到排除列表中。 46 | 示例:对于Windows Defender,前往 Windows 安全性 → 病毒与威胁防护 → 病毒与威胁防护设置 → 管理设置 → 排除。如下指定: 47 | AI_Assistant.exe(进程) 48 | C:\AI_Assistant(文件夹) 49 | 50 | ## 生成执行文件 51 | 运行 venv.cmd,然后输入以下命令: 52 | ``` 53 | pyinstaller "AI_Assistant.py" ^ 54 | --clean ^ 55 | --copy-metadata rich ^ 56 | --add-data "javascript;.\javascript" ^ 57 | --add-data "ldm_patched;.\ldm_patched" ^ 58 | --add-data "localizations;.\localizations" ^ 59 | --add-data "modules;.\modules" ^ 60 | --add-data "modules_forge;.\modules_forge" ^ 61 | --add-data "repositories;.\repositories" ^ 62 | --add-data "cache.json;." ^ 63 | --add-data "script.js;." ^ 64 | --add-data "ui-config.json;." ^ 65 | --add-data "config_states;.\config_states" ^ 66 | --add-data "configs;.\configs" ^ 67 | --add-data "extensions-builtin;.\extensions-builtin" ^ 68 | --add-data "html;.\html" 69 | 70 | xcopy /E /I /Y venv\Lib\site-packages\xformers dist\AI_Assistant\_internal\xformers 71 | xcopy /E /I /Y venv\Lib\site-packages\pytorch_lightning dist\AI_Assistant\_internal\pytorch_lightning 72 | xcopy /E /I /Y venv\Lib\site-packages\lightning_fabric dist\AI_Assistant\_internal\lightning_fabric 73 | xcopy /E /I /Y venv\Lib\site-packages\gradio dist\AI_Assistant\_internal\gradio 74 | xcopy /E /I /Y venv\Lib\site-packages\gradio_client dist\AI_Assistant\_internal\gradio_client 75 | xcopy /E /I /Y venv\Lib\site-packages\kornia dist\AI_Assistant\_internal\kornia 76 | xcopy /E /I /Y venv\Lib\site-packages\open_clip dist\AI_Assistant\_internal\open_clip 77 | xcopy /E /I /Y venv\Lib\site-packages\jsonmerge dist\AI_Assistant\_internal\jsonmerge 78 | xcopy /E /I /Y venv\Lib\site-packages\torchdiffeq dist\AI_Assistant\_internal\torchdiffeq 79 | xcopy /E /I /Y venv\Lib\site-packages\cleanfid dist\AI_Assistant\_internal\cleanfid 80 | xcopy /E /I /Y venv\Lib\site-packages\clip dist\AI_Assistant\_internal\clip 81 | xcopy /E /I /Y venv\Lib\site-packages\resize_right dist\AI_Assistant\_internal\resize_right 82 | xcopy /E /I /Y venv\Lib\site-packages\diffusers dist\AI_Assistant\_internal\diffusers 83 | xcopy /E /I /Y venv\Lib\site-packages\onnx dist\AI_Assistant\_internal\onnx 84 | xcopy /E /I /Y venv\Lib\site-packages\onnxruntime dist\AI_Assistant\_internal\onnxruntime 85 | xcopy /E /I /Y venv\Lib\site-packages\scipy dist\AI_Assistant\_internal\scipy 86 | xcopy /E /I /Y config_states dist\AI_Assistant\config_states 87 | xcopy /E /I /Y configs dist\AI_Assistant\configs 88 | xcopy /E /I /Y embeddings dist\AI_Assistant\embeddings 89 | xcopy /E /I /Y extensions-builtin dist\AI_Assistant\extensions-builtin 90 | xcopy /E /I /Y html dist\AI_Assistant\html 91 | xcopy /E /I /Y javascript dist\AI_Assistant\javascript 92 | xcopy /E /I /Y ldm_patched dist\AI_Assistant\ldm_patched 93 | xcopy /E /I /Y localizations dist\AI_Assistant\localizations 94 | xcopy /E /I /Y modules dist\AI_Assistant\modules 95 | xcopy /E /I /Y modules_forge dist\AI_Assistant\modules_forge 96 | xcopy /E /I /Y repositories dist\AI_Assistant\repositories 97 | xcopy /E /I /Y scripts dist\AI_Assistant\scripts 98 | xcopy /E /I /Y languages dist\AI_Assistant\languages 99 | copy script.js dist\AI_Assistant\script.js 100 | copy AI_Assistant_model_DL.cmd dist\AI_Assistant\AI_Assistant_model_DL.cmd 101 | copy AI_Assistant_ReadMe.txt dist\AI_Assistant\AI_Assistant_ReadMe.txt 102 | copy AI_Assistant_exUI.bat dist\AI_Assistant\AI_Assistant_exUI.bat 103 | ``` 104 | -------------------------------------------------------------------------------- /AI_Assistant_modules/actions/normal_map.py: -------------------------------------------------------------------------------- 1 | import gradio as gr 2 | from PIL import Image 3 | 4 | from AI_Assistant_modules.output_image_gui import OutputImage 5 | from AI_Assistant_modules.prompt_analysis import PromptAnalysis 6 | from utils.img_utils import base_generation, canny_process, resize_image_aspect_ratio, invert_process, make_base_pil 7 | from utils.prompt_utils import execute_prompt, remove_color, remove_duplicates 8 | from utils.request_api import create_and_save_images 9 | 10 | LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS) 11 | 12 | 13 | class NormalMap: 14 | def __init__(self, app_config): 15 | self.app_config = app_config 16 | self.input_image = None 17 | self.output = None 18 | 19 | def layout(self, transfer_target_lang_key=None): 20 | lang_util = self.app_config.lang_util 21 | with gr.Row() as self.block: 22 | with gr.Column(): 23 | with gr.Row(): 24 | with gr.Column(): 25 | self.input_image = gr.Image(label=lang_util.get_text("input_lineart"), 26 | source="upload", 27 | type='filepath', interactive=True) 28 | with gr.Column(): 29 | pass 30 | with gr.Row(): 31 | [prompt, nega] = PromptAnalysis(self.app_config).layout(lang_util, self.input_image) 32 | with gr.Row(): 33 | fidelity = gr.Slider(minimum=0.75, maximum=1.25, value=1.25, step=0.01, interactive=True, 34 | label=lang_util.get_text("lineart_fidelity")) 35 | with gr.Row(): 36 | generate_button = gr.Button(lang_util.get_text("generate"), interactive=False) 37 | with gr.Column(): 38 | self.output = OutputImage(self.app_config, transfer_target_lang_key) 39 | output_image = self.output.layout() 40 | 41 | self.input_image.change(lambda x: gr.update(interactive=x is not None), inputs=[self.input_image], 42 | outputs=[generate_button]) 43 | 44 | generate_button.click(self._process, inputs=[ 45 | self.input_image, 46 | prompt, 47 | nega, 48 | fidelity, 49 | ], outputs=[output_image]) 50 | 51 | def _process(self, input_image_path, prompt_text, negative_prompt_text, fidelity): 52 | prompt = "masterpiece, best quality, normal map, " + prompt_text.strip() 53 | execute_tags = ["monochrome", "greyscale", "lineart", "white background", "sketch", "transparent background"] 54 | prompt = execute_prompt(execute_tags, prompt) 55 | prompt = remove_duplicates(prompt) 56 | prompt = remove_color(prompt) 57 | nega = negative_prompt_text.strip() 58 | base_pil = make_base_pil(input_image_path) 59 | base_pil = resize_image_aspect_ratio(base_pil) 60 | image_size = Image.open(input_image_path).size 61 | base_pil = base_generation(base_pil.size, (150, 110, 255, 255)).convert("RGB") 62 | invert_pil = invert_process(input_image_path).resize(base_pil.size, LANCZOS).convert("RGB") 63 | mask_pil = base_generation(base_pil.size, (255, 255, 255, 255)).convert("RGB") 64 | image_fidelity = 1.0 65 | lineart_fidelity = float(fidelity) 66 | normalmap_output_path = self.app_config.make_output_path() 67 | output_pil = create_and_save_images(self.app_config.fastapi_url, prompt, nega, base_pil, mask_pil, 68 | image_size, normalmap_output_path, image_fidelity, 69 | self._make_cn_args(invert_pil, lineart_fidelity)) 70 | return output_pil 71 | 72 | def _make_canny(self, canny_img_path, canny_threshold1, canny_threshold2): 73 | threshold1 = int(canny_threshold1) 74 | threshold2 = int(canny_threshold2) 75 | return canny_process(canny_img_path, threshold1, threshold2) 76 | 77 | def _make_cn_args(self, invert_pil, lineart_fidelity): 78 | unit1 = { 79 | "image": invert_pil, 80 | "mask_image": None, 81 | "control_mode": "Balanced", 82 | "enabled": True, 83 | "guidance_end": 1.0, 84 | "guidance_start": 0, 85 | "pixel_perfect": True, 86 | "processor_res": 512, 87 | "resize_mode": "Just Resize", 88 | "weight": lineart_fidelity, 89 | "module": "None", 90 | "model": "Kataragi_lineartXL-lora128 [0598262f]", 91 | "save_detected_map": None, 92 | "hr_option": "Both" 93 | } 94 | unit2 = None 95 | return [unit1] 96 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [日本語](README.md) | [EN](README_en.md) | [中文](README_zh_CN.md) 2 | # AI-Assistant 3 | [stable-diffusion-webui-forge](https://github.com/lllyasviel/stable-diffusion-webui-forge/tree/main) をバックエンドに組み込んだ、お絵描き補助AIのGUIアプリです。 4 | ![01](https://github.com/tori29umai0123/AI-Assistant/assets/1675141/07ea96a5-d9d0-4b87-a8f6-ba41b4680f33) 5 | 6 | # 起動方法 7 | ## exeファイル 8 | exeファイルをそのままダブルクリックで起動できます。 9 | 10 | 以下の引数を指定することで、起動時の言語を指定できます。 11 | ``` 12 | AI_Assistant.exe --lang=jp 13 | AI_Assistant.exe --lang=en 14 | AI_Assistant.exe --lang=zh_CN 15 | ``` 16 | さらに引数を追加することで、Stable Diffusion Web UIに対するオプションを追加できます(上級者向け) 17 | デフォルトではこのように指定されています。 18 | AI_Assistant.exe --lang=ja --nowebui --xformers --skip-python-version-check --skip-torch-cuda-test --skip-torch-cuda-test 19 | 20 | また、以下の引数を追加することで拡張UIを表示できます(現在、i2iタブでLoRAを読み込めるようになりました) 21 | ``` 22 | --exui 23 | ``` 24 | ## わかっている人向け 25 | 以下のようなbatファイルを実行することで、引数を簡単に指定できます。
26 | AI_Assistant.bat 27 | ``` 28 | @echo off 29 | start /d "%~dp0" AI_Assistant.exe --nowebui --xformers --skip-python-version-check --skip-torch-cuda-test --exui 30 | ``` 31 | 32 | AI_Assistant_lowVRAM.bat 33 | ``` 34 | @echo off 35 | start /d "%~dp0" AI_Assistant.exe --nowebui --xformers --skip-python-version-check --skip-torch-cuda-test --unet-in-fp8-e4m3fn 36 | ``` 37 | 38 | ## 開発者向け 39 | ビルド設定を行った上で、`python AI_Assistant.py`を実行してください。 40 | 41 | # ビルド設定(開発者向け) 42 | python 3.10.xで開発されています。 43 | webui-forgeのライブラリを使用しているため、ビルド時にはバージョンを合わせた上で以下の手順が必要です。 44 | 45 | ①AI_Assistant_install.ps1を実行してインストール
46 | ②セキュリティーソフトの設定で、フォルダと実行ファイル名を除外リストに追加する。
47 | 例:Windows Defenderの場合、Windows セキュリティ→ウイルスと脅威の防止→ウイルスと脅威の防止の設定→設定の管理→除外
48 | AI_Assistant.exe(プロセス)
49 | C:\AI_Assistant(フォルダ)
50 | のように指定する。
51 | 52 | ## 実行ファイル生成 53 | venv.cmdを実行し、以下のコマンドを入力 54 | ``` 55 | pyinstaller "AI_Assistant.py" ^ 56 | --clean ^ 57 | --copy-metadata rich ^ 58 | --add-data "javascript;.\javascript" ^ 59 | --add-data "ldm_patched;.\ldm_patched" ^ 60 | --add-data "localizations;.\localizations" ^ 61 | --add-data "modules;.\modules" ^ 62 | --add-data "modules_forge;.\modules_forge" ^ 63 | --add-data "repositories;.\repositories" ^ 64 | --add-data "cache.json;." ^ 65 | --add-data "script.js;." ^ 66 | --add-data "ui-config.json;." ^ 67 | --add-data "config_states;.\config_states" ^ 68 | --add-data "configs;.\configs" ^ 69 | --add-data "extensions-builtin;.\extensions-builtin" ^ 70 | --add-data "html;.\html" 71 | 72 | xcopy /E /I /Y venv\Lib\site-packages\xformers dist\AI_Assistant\_internal\xformers 73 | xcopy /E /I /Y venv\Lib\site-packages\pytorch_lightning dist\AI_Assistant\_internal\pytorch_lightning 74 | xcopy /E /I /Y venv\Lib\site-packages\lightning_fabric dist\AI_Assistant\_internal\lightning_fabric 75 | xcopy /E /I /Y venv\Lib\site-packages\gradio dist\AI_Assistant\_internal\gradio 76 | xcopy /E /I /Y venv\Lib\site-packages\gradio_client dist\AI_Assistant\_internal\gradio_client 77 | xcopy /E /I /Y venv\Lib\site-packages\kornia dist\AI_Assistant\_internal\kornia 78 | xcopy /E /I /Y venv\Lib\site-packages\open_clip dist\AI_Assistant\_internal\open_clip 79 | xcopy /E /I /Y venv\Lib\site-packages\jsonmerge dist\AI_Assistant\_internal\jsonmerge 80 | xcopy /E /I /Y venv\Lib\site-packages\torchdiffeq dist\AI_Assistant\_internal\torchdiffeq 81 | xcopy /E /I /Y venv\Lib\site-packages\cleanfid dist\AI_Assistant\_internal\cleanfid 82 | xcopy /E /I /Y venv\Lib\site-packages\clip dist\AI_Assistant\_internal\clip 83 | xcopy /E /I /Y venv\Lib\site-packages\resize_right dist\AI_Assistant\_internal\resize_right 84 | xcopy /E /I /Y venv\Lib\site-packages\diffusers dist\AI_Assistant\_internal\diffusers 85 | xcopy /E /I /Y venv\Lib\site-packages\onnx dist\AI_Assistant\_internal\onnx 86 | xcopy /E /I /Y venv\Lib\site-packages\onnxruntime dist\AI_Assistant\_internal\onnxruntime 87 | xcopy /E /I /Y venv\Lib\site-packages\scipy dist\AI_Assistant\_internal\scipy 88 | xcopy /E /I /Y config_states dist\AI_Assistant\config_states 89 | xcopy /E /I /Y configs dist\AI_Assistant\configs 90 | xcopy /E /I /Y embeddings dist\AI_Assistant\embeddings 91 | xcopy /E /I /Y extensions-builtin dist\AI_Assistant\extensions-builtin 92 | xcopy /E /I /Y html dist\AI_Assistant\html 93 | xcopy /E /I /Y javascript dist\AI_Assistant\javascript 94 | xcopy /E /I /Y ldm_patched dist\AI_Assistant\ldm_patched 95 | xcopy /E /I /Y localizations dist\AI_Assistant\localizations 96 | xcopy /E /I /Y modules dist\AI_Assistant\modules 97 | xcopy /E /I /Y modules_forge dist\AI_Assistant\modules_forge 98 | xcopy /E /I /Y repositories dist\AI_Assistant\repositories 99 | xcopy /E /I /Y scripts dist\AI_Assistant\scripts 100 | xcopy /E /I /Y languages dist\AI_Assistant\languages 101 | copy script.js dist\AI_Assistant\script.js 102 | copy AI_Assistant_model_DL.cmd dist\AI_Assistant\AI_Assistant_model_DL.cmd 103 | copy AI_Assistant_ReadMe.txt dist\AI_Assistant\AI_Assistant_ReadMe.txt 104 | copy AI_Assistant_exUI.bat dist\AI_Assistant\AI_Assistant_exUI.bat 105 | copy LICENSE.txt dist\AI_Assistant\LICENSE.txt 106 | ``` 107 | -------------------------------------------------------------------------------- /README_en.md: -------------------------------------------------------------------------------- 1 | [日本語](README.md) | [EN](README_en.md) | [中文](README_zh_CN.md) 2 | # AI-Assistant 3 | This is a GUI application for drawing assistance AI, which integrates the [stable-diffusion-webui-forge](https://github.com/lllyasviel/stable-diffusion-webui-forge/tree/main) as its backend. 4 | ![01](https://github.com/tori29umai0123/AI-Assistant/assets/1675141/07ea96a5-d9d0-4b87-a8f6-ba41b4680f33) 5 | 6 | # Startup 7 | ## exe 8 | You can start the application by double-clicking the exe file directly. 9 | 10 | You can specify the language at startup by providing the following arguments. 11 | ``` 12 | AI_Assistant.exe --lang=jp 13 | AI_Assistant.exe --lang=en 14 | AI_Assistant.exe --lang=zh_CN 15 | ``` 16 | Additionally, by adding more arguments, you can add options for the Stable Diffusion Web UI (for advanced users). 17 | By default, it is specified as follows: 18 | AI_Assistant.exe --lang=ja --nowebui --xformers --skip-python-version-check --skip-torch-cuda-test --skip-torch-cuda-test 19 | 20 | Furthermore, by adding the following arguments, you can display an extended UI (currently, you can load LoRA in the i2i tab) 21 | ``` 22 | --exui 23 | ``` 24 | ## For those who are familiar 25 | You can easily specify arguments by running a bat file like the following.
26 | AI_Assistant.bat 27 | ``` 28 | @echo off 29 | start /d "%~dp0" AI_Assistant.exe --nowebui --xformers --skip-python-version-check --skip-torch-cuda-test --exui 30 | ``` 31 | 32 | AI_Assistant_lowVRAM.bat 33 | ``` 34 | @echo off 35 | start /d "%~dp0" AI_Assistant.exe --nowebui --xformers --skip-python-version-check --skip-torch-cuda-test --unet-in-fp8-e4m3fn 36 | ``` 37 | 38 | ## Developer 39 | After configuring the build settings, please run `python AI_Assistant.py`. 40 | 41 | # Build Settings(Developer) 42 | Developed with Python 3.10.x. 43 | Due to the use of the webui-forge library, the following steps are necessary during the build to match the version 44 | 45 | 1. Run AI_Assistant_install.ps1 to install. 46 | 2. In the settings of the security software, add the folder and executable name to the exclusion list. 47 | Example: For Windows Defender, go to Windows Security → Virus & threat protection → Virus & threat protection settings → Manage settings → Exclusions. Specify like this: 48 | AI_Assistant.exe (process) 49 | C:\AI_Assistant (folder) 50 | 51 | ## Executable Generation 52 | Run venv.cmd, then enter the following command: 53 | ``` 54 | pyinstaller "AI_Assistant.py" ^ 55 | --clean ^ 56 | --copy-metadata rich ^ 57 | --add-data "javascript;.\javascript" ^ 58 | --add-data "ldm_patched;.\ldm_patched" ^ 59 | --add-data "localizations;.\localizations" ^ 60 | --add-data "modules;.\modules" ^ 61 | --add-data "modules_forge;.\modules_forge" ^ 62 | --add-data "repositories;.\repositories" ^ 63 | --add-data "cache.json;." ^ 64 | --add-data "script.js;." ^ 65 | --add-data "ui-config.json;." ^ 66 | --add-data "config_states;.\config_states" ^ 67 | --add-data "configs;.\configs" ^ 68 | --add-data "extensions-builtin;.\extensions-builtin" ^ 69 | --add-data "html;.\html" 70 | 71 | xcopy /E /I /Y venv\Lib\site-packages\xformers dist\AI_Assistant\_internal\xformers 72 | xcopy /E /I /Y venv\Lib\site-packages\pytorch_lightning dist\AI_Assistant\_internal\pytorch_lightning 73 | xcopy /E /I /Y venv\Lib\site-packages\lightning_fabric dist\AI_Assistant\_internal\lightning_fabric 74 | xcopy /E /I /Y venv\Lib\site-packages\gradio dist\AI_Assistant\_internal\gradio 75 | xcopy /E /I /Y venv\Lib\site-packages\gradio_client dist\AI_Assistant\_internal\gradio_client 76 | xcopy /E /I /Y venv\Lib\site-packages\kornia dist\AI_Assistant\_internal\kornia 77 | xcopy /E /I /Y venv\Lib\site-packages\open_clip dist\AI_Assistant\_internal\open_clip 78 | xcopy /E /I /Y venv\Lib\site-packages\jsonmerge dist\AI_Assistant\_internal\jsonmerge 79 | xcopy /E /I /Y venv\Lib\site-packages\torchdiffeq dist\AI_Assistant\_internal\torchdiffeq 80 | xcopy /E /I /Y venv\Lib\site-packages\cleanfid dist\AI_Assistant\_internal\cleanfid 81 | xcopy /E /I /Y venv\Lib\site-packages\clip dist\AI_Assistant\_internal\clip 82 | xcopy /E /I /Y venv\Lib\site-packages\resize_right dist\AI_Assistant\_internal\resize_right 83 | xcopy /E /I /Y venv\Lib\site-packages\diffusers dist\AI_Assistant\_internal\diffusers 84 | xcopy /E /I /Y venv\Lib\site-packages\onnx dist\AI_Assistant\_internal\onnx 85 | xcopy /E /I /Y venv\Lib\site-packages\onnxruntime dist\AI_Assistant\_internal\onnxruntime 86 | xcopy /E /I /Y venv\Lib\site-packages\scipy dist\AI_Assistant\_internal\scipy 87 | xcopy /E /I /Y config_states dist\AI_Assistant\config_states 88 | xcopy /E /I /Y configs dist\AI_Assistant\configs 89 | xcopy /E /I /Y embeddings dist\AI_Assistant\embeddings 90 | xcopy /E /I /Y extensions-builtin dist\AI_Assistant\extensions-builtin 91 | xcopy /E /I /Y html dist\AI_Assistant\html 92 | xcopy /E /I /Y javascript dist\AI_Assistant\javascript 93 | xcopy /E /I /Y ldm_patched dist\AI_Assistant\ldm_patched 94 | xcopy /E /I /Y localizations dist\AI_Assistant\localizations 95 | xcopy /E /I /Y modules dist\AI_Assistant\modules 96 | xcopy /E /I /Y modules_forge dist\AI_Assistant\modules_forge 97 | xcopy /E /I /Y repositories dist\AI_Assistant\repositories 98 | xcopy /E /I /Y scripts dist\AI_Assistant\scripts 99 | xcopy /E /I /Y languages dist\AI_Assistant\languages 100 | copy script.js dist\AI_Assistant\script.js 101 | copy AI_Assistant_model_DL.cmd dist\AI_Assistant\AI_Assistant_model_DL.cmd 102 | copy AI_Assistant_ReadMe.txt dist\AI_Assistant\AI_Assistant_ReadMe.txt 103 | copy AI_Assistant_exUI.bat dist\AI_Assistant\AI_Assistant_exUI.bat 104 | ``` 105 | -------------------------------------------------------------------------------- /utils/tagger.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # https://github.com/kohya-ss/sd-scripts/blob/main/finetune/tag_images_by_wd14_tagger.py 3 | 4 | import csv 5 | import os 6 | os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true' 7 | 8 | from PIL import Image 9 | import cv2 10 | import numpy as np 11 | from pathlib import Path 12 | import onnx 13 | import onnxruntime as ort 14 | 15 | # from wd14 tagger 16 | IMAGE_SIZE = 448 17 | 18 | model = None # Initialize model variable 19 | 20 | 21 | def convert_array_to_bgr(array): 22 | """ 23 | Convert a NumPy array image to BGR format regardless of its original format. 24 | 25 | Parameters: 26 | - array: NumPy array of the image. 27 | 28 | Returns: 29 | - A NumPy array representing the image in BGR format. 30 | """ 31 | # グレースケール画像(2次元配列) 32 | if array.ndim == 2: 33 | # グレースケールをBGRに変換(3チャンネルに拡張) 34 | bgr_array = np.stack((array,) * 3, axis=-1) 35 | # RGBAまたはRGB画像(3次元配列) 36 | elif array.ndim == 3: 37 | # RGBA画像の場合、アルファチャンネルを削除 38 | if array.shape[2] == 4: 39 | array = array[:, :, :3] 40 | # RGBをBGRに変換 41 | bgr_array = array[:, :, ::-1] 42 | else: 43 | raise ValueError("Unsupported array shape.") 44 | 45 | return bgr_array 46 | 47 | 48 | def preprocess_image(image): 49 | image = np.array(image) 50 | image = convert_array_to_bgr(image) 51 | 52 | size = max(image.shape[0:2]) 53 | pad_x = size - image.shape[1] 54 | pad_y = size - image.shape[0] 55 | pad_l = pad_x // 2 56 | pad_t = pad_y // 2 57 | image = np.pad(image, ((pad_t, pad_y - pad_t), (pad_l, pad_x - pad_l), (0, 0)), mode="constant", constant_values=255) 58 | 59 | interp = cv2.INTER_AREA if size > IMAGE_SIZE else cv2.INTER_LANCZOS4 60 | image = cv2.resize(image, (IMAGE_SIZE, IMAGE_SIZE), interpolation=interp) 61 | 62 | image = image.astype(np.float32) 63 | return image 64 | 65 | def modelLoad(model_dir): 66 | onnx_path = os.path.join(model_dir, "model.onnx") 67 | # 実行プロバイダーをCPUのみに指定 68 | providers = ['CPUExecutionProvider'] 69 | # InferenceSessionの作成時にプロバイダーのリストを指定 70 | ort_session = ort.InferenceSession(onnx_path, providers=providers) 71 | input_name = ort_session.get_inputs()[0].name 72 | 73 | # 実際に使用されているプロバイダーを取得して表示 74 | actual_provider = ort_session.get_providers()[0] # 使用されているプロバイダー 75 | print(f"Using provider: {actual_provider}") 76 | 77 | return [ort_session, input_name] 78 | 79 | def analysis(image_path, model_dir, model): 80 | ort_session = model[0] 81 | input_name = model[1] 82 | 83 | with open(os.path.join(model_dir, "selected_tags.csv"), "r", encoding="utf-8") as f: 84 | reader = csv.reader(f) 85 | l = [row for row in reader] 86 | header = l[0] # tag_id,name,category,count 87 | rows = l[1:] 88 | assert header[0] == "tag_id" and header[1] == "name" and header[2] == "category", f"unexpected csv format: {header}" 89 | 90 | general_tags = [row[1] for row in rows[1:] if row[2] == "0"] 91 | character_tags = [row[1] for row in rows[1:] if row[2] == "4"] 92 | 93 | tag_freq = {} 94 | undesired_tags = ["transparent background"] 95 | 96 | # 画像をロードして前処理する 97 | if image_path: 98 | # 画像を開き、RGBA形式に変換して透過情報を保持 99 | img = Image.open(image_path) 100 | img = img.convert("RGBA") 101 | 102 | # 透過部分を白色で塗りつぶすキャンバスを作成 103 | canvas_image = Image.new('RGBA', img.size, (255, 255, 255, 255)) 104 | # 画像をキャンバスにペーストし、透過部分が白色になるように設定 105 | canvas_image.paste(img, (0, 0), img) 106 | 107 | # RGBAからRGBに変換し、透過部分を白色にする 108 | image_pil = canvas_image.convert("RGB") 109 | image_preprocessed = preprocess_image(image_pil) 110 | image_preprocessed = np.expand_dims(image_preprocessed, axis=0) 111 | 112 | # 推論を実行 113 | prob = ort_session.run(None, {input_name: image_preprocessed})[0][0] 114 | # タグを生成 115 | combined_tags = [] 116 | general_tag_text = "" 117 | character_tag_text = "" 118 | remove_underscore = True 119 | caption_separator = ", " 120 | general_threshold = 0.35 121 | character_threshold = 0.35 122 | 123 | for i, p in enumerate(prob[4:]): 124 | if i < len(general_tags) and p >= general_threshold: 125 | tag_name = general_tags[i] 126 | if remove_underscore and len(tag_name) > 3: # ignore emoji tags like >_< and ^_^ 127 | tag_name = tag_name.replace("_", " ") 128 | 129 | if tag_name not in undesired_tags: 130 | tag_freq[tag_name] = tag_freq.get(tag_name, 0) + 1 131 | general_tag_text += caption_separator + tag_name 132 | combined_tags.append(tag_name) 133 | elif i >= len(general_tags) and p >= character_threshold: 134 | tag_name = character_tags[i - len(general_tags)] 135 | if remove_underscore and len(tag_name) > 3: 136 | tag_name = tag_name.replace("_", " ") 137 | 138 | if tag_name not in undesired_tags: 139 | tag_freq[tag_name] = tag_freq.get(tag_name, 0) + 1 140 | character_tag_text += caption_separator + tag_name 141 | combined_tags.append(tag_name) 142 | 143 | # 先頭のカンマを取る 144 | if len(general_tag_text) > 0: 145 | general_tag_text = general_tag_text[len(caption_separator) :] 146 | if len(character_tag_text) > 0: 147 | character_tag_text = character_tag_text[len(caption_separator) :] 148 | tag_text = caption_separator.join(combined_tags) 149 | return tag_text -------------------------------------------------------------------------------- /AI_Assistant_modules/actions/anime_shadow.py: -------------------------------------------------------------------------------- 1 | import gradio as gr 2 | from PIL import Image 3 | 4 | from AI_Assistant_modules.output_image_gui import OutputImage 5 | from AI_Assistant_modules.prompt_analysis import PromptAnalysis 6 | from utils.img_utils import make_base_pil, invert_process, multiply_images, resize_image_aspect_ratio 7 | from utils.prompt_utils import execute_prompt, remove_color, remove_duplicates 8 | from utils.request_api import create_and_save_images 9 | 10 | LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS) 11 | 12 | 13 | class AnimeShadow: 14 | def __init__(self, app_config): 15 | self.app_config = app_config 16 | self.input_image = None 17 | self.output = None 18 | 19 | def accept_transfer(self, image): 20 | pass 21 | 22 | def layout(self, transfer_target_lang_key=None): 23 | lang_util = self.app_config.lang_util 24 | with gr.Row(): 25 | with gr.Column(): 26 | with gr.Row(): 27 | with gr.Column(): 28 | input_image = gr.Image(label=lang_util.get_text("anime_shadow_input_image"), 29 | source='upload', 30 | type='filepath', interactive=True) 31 | with gr.Column(): 32 | shadow_image = gr.Image(label=lang_util.get_text("shadow_image"), type='pil', interactive=True) 33 | # ライティングタブからの転送先 34 | self.input_image = shadow_image 35 | with gr.Row(): 36 | [prompt, nega] = PromptAnalysis(self.app_config).layout(lang_util, input_image) 37 | with gr.Row(): 38 | shadow_choice = gr.Dropdown(label=lang_util.get_text('shadow_choices'), value='anime01', 39 | choices=['anime01', 'anime02', 'anime03', 'plainmaterial'], interactive=True) 40 | with gr.Row(): 41 | generate_button = gr.Button(lang_util.get_text('generate'), interactive=False) 42 | with gr.Column(): 43 | self.output = OutputImage(self.app_config, transfer_target_lang_key) 44 | output_image = self.output.layout() 45 | 46 | input_image.change(lambda x, y: gr.update(interactive=x is not None and y is not None), 47 | inputs=[input_image, shadow_image], outputs=[generate_button]) 48 | shadow_image.change(lambda x, y: gr.update(interactive=x is not None and y is not None), 49 | inputs=[input_image, shadow_image], outputs=[generate_button]) 50 | 51 | generate_button.click(self._process, inputs=[ 52 | input_image, 53 | shadow_image, 54 | prompt, 55 | nega, 56 | shadow_choice, 57 | ], outputs=[output_image]) 58 | 59 | def _process(self, input_image_path, shadow_image_pil, prompt_text, negative_prompt_text, shadow_choice): 60 | if shadow_choice == 'plainmaterial': 61 | shadow_choice = 'sdxl-testlora-plainmaterial' 62 | prompt = f"masterpiece, best quality, , monochrome, greyscale, normal map, 3D" + prompt_text.strip() 63 | else: 64 | prompt = f"masterpiece, best quality, , monochrome, greyscale, " + prompt_text.strip() 65 | execute_tags = ["lineart", "sketch", "transparent background"] 66 | prompt = execute_prompt(execute_tags, prompt) 67 | prompt = remove_duplicates(prompt) 68 | prompt = remove_color(prompt) 69 | nega = negative_prompt_text.strip() 70 | base_pil = make_base_pil(input_image_path) 71 | base_pil = resize_image_aspect_ratio(base_pil) 72 | image_size = Image.open(input_image_path).size 73 | invert_pil = invert_process(input_image_path).convert("RGB") 74 | shadow_pil = shadow_image_pil.resize(base_pil.size, LANCZOS) 75 | shadow_line_pil = multiply_images(base_pil, shadow_pil).convert("RGB") 76 | image_fidelity = 1.0 77 | lineart_fidelity = 0.2 78 | anime_shadow_output_path = self.app_config.make_output_path() 79 | cn_args = self._make_cn_args(base_pil, invert_pil, lineart_fidelity) 80 | output_pil = create_and_save_images(self.app_config.fastapi_url, prompt, nega, shadow_pil, shadow_line_pil, 81 | image_size, anime_shadow_output_path, image_fidelity, cn_args) 82 | 83 | return output_pil 84 | 85 | def _make_cn_args(self, base_pil, invert_pil, lineart_fidelity): 86 | unit1 = { 87 | "image": base_pil, 88 | "mask_image": None, 89 | "control_mode": "Balanced", 90 | "enabled": True, 91 | "guidance_end": 1, 92 | "guidance_start": 0, 93 | "pixel_perfect": True, 94 | "processor_res": 512, 95 | "resize_mode": "Just Resize", 96 | "weight": 1.0, 97 | "module": "None", 98 | "model": "CN-anytest_v3-50000_am_dim256 [dbecc0f9]", 99 | "save_detected_map": None, 100 | "hr_option": "Both" 101 | } 102 | unit2 = { 103 | "image": invert_pil, 104 | "mask_image": None, 105 | "control_mode": "Balanced", 106 | "enabled": True, 107 | "guidance_end": 1, 108 | "guidance_start": 0, 109 | "pixel_perfect": True, 110 | "processor_res": 512, 111 | "resize_mode": "Just Resize", 112 | "weight": lineart_fidelity, 113 | "module": "None", 114 | "model": "Kataragi_lineartXL-lora128 [0598262f]", 115 | "save_detected_map": None, 116 | "hr_option": "Both" 117 | } 118 | return [unit1, unit2] 119 | -------------------------------------------------------------------------------- /AI_Assistant_modules/actions/line_drawing_cutout.py: -------------------------------------------------------------------------------- 1 | import gradio as gr 2 | from PIL import Image 3 | 4 | from AI_Assistant_modules.output_image_gui import OutputImage 5 | from AI_Assistant_modules.prompt_analysis import PromptAnalysis 6 | from utils.img_utils import make_base_pil, base_generation, resize_image_aspect_ratio 7 | from utils.prompt_utils import execute_prompt, remove_color, remove_duplicates 8 | from utils.request_api import create_and_save_images, get_lora_model 9 | 10 | LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS) 11 | 12 | 13 | class LineDrawingCutOut: 14 | def __init__(self, app_config): 15 | self.app_config = app_config 16 | self.input_image = None 17 | self.output = None 18 | 19 | def layout(self, transfer_target_lang_key=None): 20 | lang_util = self.app_config.lang_util 21 | exui = self.app_config.exui 22 | with gr.Row() as self.block: 23 | with gr.Column(): 24 | with gr.Row(): 25 | with gr.Column(): 26 | self.input_image = gr.Image(label=lang_util.get_text("input_image"), 27 | source="upload", 28 | type='filepath', interactive=True) 29 | if exui: 30 | with gr.Row(): 31 | lora_model_dropdown = gr.Dropdown(label=lang_util.get_text("lora_models"), choices=[]) 32 | load_lora_models_button = gr.Button(lang_util.get_text("lora_update")) 33 | with gr.Row(): 34 | [prompt, nega] = PromptAnalysis(self.app_config).layout(lang_util, self.input_image) 35 | with gr.Row(): 36 | fidelity = gr.Slider(minimum=0.5, maximum=1.25, value=1.0, step=0.01, interactive=True, 37 | label=lang_util.get_text("lineart_fidelity")) 38 | bold = gr.Slider(minimum=0.0, maximum=1.0, value=0.0, step=0.01, interactive=True, 39 | label=lang_util.get_text("lineart_bold")) 40 | with gr.Row(): 41 | generate_button = gr.Button(lang_util.get_text("generate"), interactive=False) 42 | with gr.Column(): 43 | self.output = OutputImage(self.app_config, transfer_target_lang_key) 44 | output_image = self.output.layout() 45 | 46 | self.input_image.change(lambda x: gr.update(interactive=x is not None), inputs=[self.input_image], 47 | outputs=[generate_button]) 48 | 49 | if exui: 50 | load_lora_models_button.click(self.load_lora_models, inputs=[], outputs=[lora_model_dropdown]) 51 | lora_model_dropdown.change(self.update_prompt_with_lora, inputs=[lora_model_dropdown, prompt], outputs=[prompt, lora_model_dropdown]) 52 | 53 | generate_button.click(self._process, inputs=[ 54 | self.input_image, 55 | prompt, 56 | nega, 57 | fidelity, 58 | bold, 59 | ], outputs=[output_image]) 60 | 61 | def _process(self, input_image_path, prompt_text, negative_prompt_text, fidelity, bold): 62 | lineart2 = 1 - bold 63 | prompt = "masterpiece, best quality, , , monochrome, lineart, white background, " + prompt_text.strip() 65 | execute_tags = ["sketch", "transparent background"] 66 | prompt = execute_prompt(execute_tags, prompt) 67 | prompt = remove_duplicates(prompt) 68 | prompt = remove_color(prompt) 69 | nega = negative_prompt_text.strip() 70 | base_pil = make_base_pil(input_image_path) 71 | base_pil = resize_image_aspect_ratio(base_pil) 72 | image_size = Image.open(input_image_path).size 73 | mask_pil = base_generation(base_pil.size, (255, 255, 255, 255)).convert("RGB") 74 | white_base_pil = base_generation(base_pil.size, (255, 255, 255, 255)).convert("RGB") 75 | image_fidelity = 1.0 76 | lineart2_fidelity = float(fidelity) 77 | lineart2_output_path = self.app_config.make_output_path() 78 | output_pil = create_and_save_images(self.app_config.fastapi_url, prompt, nega, white_base_pil, mask_pil, 79 | image_size, lineart2_output_path, image_fidelity, 80 | self._make_cn_args(base_pil, lineart2_fidelity)) 81 | return output_pil 82 | 83 | 84 | def load_lora_models(self): 85 | model_names, model_aliases = get_lora_model(self.app_config.fastapi_url) 86 | model_options = [f"{name} ({alias})" for name, alias in zip(model_names, model_aliases)] 87 | return gr.Dropdown.update(choices=model_options, interactive=True) 88 | 89 | 90 | def update_prompt_with_lora(self, lora_model_selection, existing_prompt): 91 | if '(' in lora_model_selection and ')' in lora_model_selection: 92 | alias = lora_model_selection.split('(')[-1].split(')')[0].strip() 93 | else: 94 | alias = lora_model_selection 95 | 96 | lora_tag = f"" 97 | updated_prompt = existing_prompt + ", " + lora_tag if existing_prompt else lora_tag 98 | 99 | #仮に『, 』という文字列が含まれていたら除去する 100 | updated_prompt = updated_prompt.replace(", ", "").strip() 101 | 102 | return updated_prompt, [] 103 | 104 | def handle_lora_model_update(result): 105 | updated_prompt, reset_choices = result 106 | return gr.update(value=updated_prompt), gr.Dropdown.update(choices=reset_choices, interactive=True) 107 | 108 | 109 | def _make_cn_args(self, base_pil, lineart_fidelity): 110 | unit1 = { 111 | "image": base_pil, 112 | "mask_image": None, 113 | "control_mode": "Balanced", 114 | "enabled": True, 115 | "guidance_end": 1.0, 116 | "guidance_start": 0, 117 | "pixel_perfect": True, 118 | "processor_res": 512, 119 | "resize_mode": "Just Resize", 120 | "weight": lineart_fidelity, 121 | "module": "None", 122 | "model": "CN-anytest_v4-marged_am_dim256 [49b6c950]", 123 | "save_detected_map": None, 124 | "hr_option": "Both" 125 | } 126 | unit2 = None 127 | return [unit1] 128 | -------------------------------------------------------------------------------- /AI_Assistant_setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | import re 3 | import shutil 4 | 5 | # 絶対パスで作業ディレクトリを指定 6 | script_path = os.path.abspath(__file__) 7 | target_directory = os.path.dirname(script_path) 8 | 9 | # 追加するコードの定義 10 | prepend_code = """import sys 11 | # 'frozen' 状態に応じて適切なファイルパスを取得する関数 12 | def get_appropriate_file_path(): 13 | if getattr(sys, 'frozen', False): 14 | return sys.executable + "/AI_Assistant/" 15 | else: 16 | return __file__ 17 | appropriate_file_path = get_appropriate_file_path() 18 | """ 19 | 20 | ldm_special_prepend_code = """import sys 21 | import os 22 | def get_appropriate_file_path(): 23 | if getattr(sys, 'frozen', False): 24 | return os.path.dirname(sys.executable) + "/ldm_patched/modules" 25 | else: 26 | return os.path.dirname(__file__) 27 | appropriate_file_path = get_appropriate_file_path() 28 | """ 29 | 30 | exclude_files = [] 31 | 32 | ldm_patched_files = [ 33 | os.path.join(target_directory, "ldm_patched/modules/sd1_clip.py"), 34 | os.path.join(target_directory, "ldm_patched/modules/sd2_clip.py"), 35 | os.path.join(target_directory, "ldm_patched/modules/sdxl_clip.py"), 36 | ] 37 | 38 | exclude_folders = [ 39 | os.path.join(target_directory, "venv"), 40 | os.path.join(target_directory, "AI_Assistant_modules"), 41 | os.path.join(target_directory, "utils"), 42 | ] 43 | 44 | # 新しいファイルの追加に加えて既存のコピー機能 45 | files_to_copy = { 46 | os.path.join(target_directory, "AI_Assistant_modules/config_states.py"): os.path.join(target_directory, "modules/config_states.py"), 47 | os.path.join(target_directory, "AI_Assistant_modules/gitpython_hack.py"): os.path.join(target_directory, "modules/gitpython_hack.py"), 48 | os.path.join(target_directory, "AI_Assistant_modules/launch_utils_AI_Assistant.py"): os.path.join(target_directory, "modules/launch_utils_AI_Assistant.py"), 49 | os.path.join(target_directory, "AI_Assistant_modules/sdxl_clip.py"): os.path.join(target_directory, "ldm_patched/modules/sdxl_clip.py"), 50 | os.path.join(target_directory, "AI_Assistant_modules/sd1_clip.py"): os.path.join(target_directory, "ldm_patched/modules/sd1_clip.py"), 51 | os.path.join(target_directory, "AI_Assistant_modules/sd2_clip.py"): os.path.join(target_directory, "ldm_patched/modules/sd2_clip.py"), 52 | os.path.join(target_directory, "AI_Assistant_modules/shared_cmd_options.py"): os.path.join(target_directory, "modules/shared_cmd_options.py"), 53 | os.path.join(target_directory, "AI_Assistant_modules/ui_extensions.py"): os.path.join(target_directory, "modules/ui_extensions.py"), 54 | } 55 | 56 | def file_needs_update(filepath): 57 | try: 58 | with open(filepath, 'r', encoding='utf-8') as file: 59 | content = file.read() 60 | return re.search(r'(?, " + prompt_text.strip() 71 | elif coloring_choice == "Thick Coating" or coloring_choice == "厚塗り" or coloring_choice == "厚涂层": 72 | prompt = "masterpiece, best quality, , " + prompt_text.strip() 73 | elif coloring_choice == "Anime Coloring" or coloring_choice == "アニメ塗り" or coloring_choice == "动漫着色": 74 | prompt = "masterpiece, best quality, " + prompt_text.strip() 75 | else: 76 | prompt = "masterpiece, best quality, " + prompt_text.strip() 77 | execute_tags = ["flat color", "transparent background"] 78 | prompt =execute_prompt(execute_tags, prompt) 79 | prompt = remove_duplicates(prompt) 80 | nega = "(flat color:1.2), " + negative_prompt_text.strip() 81 | base_pil = make_base_pil(input_image_path) 82 | base_pil = resize_image_aspect_ratio(base_pil) 83 | image_size = Image.open(input_image_path).size 84 | noline_pil = noline_process(input_image_path).resize(base_pil.size, LANCZOS).convert("RGB") 85 | mask_pil = base_generation(base_pil.size, (255, 255, 255, 255)).convert("RGB") 86 | image_fidelity = 1 - float(fidelity) 87 | lineart_fidelity = 1.25 88 | output_path = self.app_config.make_output_path() 89 | output_pil = create_and_save_images(self.app_config.fastapi_url, prompt, nega, noline_pil, mask_pil, 90 | image_size, output_path, image_fidelity, 91 | self._make_cn_args(base_pil, lineart_fidelity)) 92 | return output_pil 93 | 94 | 95 | def load_lora_models(self): 96 | model_names, model_aliases = get_lora_model(self.app_config.fastapi_url) 97 | model_options = [f"{name} ({alias})" for name, alias in zip(model_names, model_aliases)] 98 | return gr.Dropdown.update(choices=model_options, interactive=True) 99 | 100 | 101 | def update_prompt_with_lora(self, lora_model_selection, existing_prompt): 102 | if '(' in lora_model_selection and ')' in lora_model_selection: 103 | alias = lora_model_selection.split('(')[-1].split(')')[0].strip() 104 | else: 105 | alias = lora_model_selection 106 | 107 | lora_tag = f"" 108 | updated_prompt = existing_prompt + ", " + lora_tag if existing_prompt else lora_tag 109 | 110 | #仮に『, 』という文字列が含まれていたら除去する 111 | updated_prompt = updated_prompt.replace(", ", "").strip() 112 | 113 | return updated_prompt, [] 114 | 115 | def handle_lora_model_update(result): 116 | updated_prompt, reset_choices = result 117 | return gr.update(value=updated_prompt), gr.Dropdown.update(choices=reset_choices, interactive=True) 118 | 119 | 120 | def _make_cn_args(self, base_line_pil, lineart_fidelity): 121 | unit1 = { 122 | "image": base_line_pil, 123 | "mask_image": None, 124 | "control_mode": "Balanced", 125 | "enabled": True, 126 | "guidance_end": 1.0, 127 | "guidance_start": 0, 128 | "pixel_perfect": True, 129 | "processor_res": 512, 130 | "resize_mode": "Just Resize", 131 | "weight": lineart_fidelity, 132 | "module": "lineart_realistic", 133 | "model": "Kataragi_lineartXL-lora128 [0598262f]", 134 | "save_detected_map": None, 135 | "hr_option": "Both" 136 | } 137 | unit2 = None 138 | return [unit1] 139 | -------------------------------------------------------------------------------- /AI_Assistant_modules/actions/lighting.py: -------------------------------------------------------------------------------- 1 | import gradio as gr 2 | import numpy as np 3 | import torch 4 | from PIL import Image 5 | from torchvision.transforms.functional import to_pil_image, to_tensor 6 | 7 | from AI_Assistant_modules.output_image_gui import OutputImage 8 | 9 | LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS) 10 | 11 | 12 | class Lighting: 13 | def __init__(self, app_config): 14 | self.app_config = app_config 15 | self.input_image = None 16 | self.output = None 17 | 18 | def layout(self, transfer_target_lang_key=None): 19 | lang_util = self.app_config.lang_util 20 | with gr.Row(): 21 | with gr.Column(): 22 | with gr.Row(): 23 | with gr.Column(): 24 | self.input_image = gr.Image(label=lang_util.get_text("input_image"), 25 | source="upload", 26 | type='filepath', interactive=True) 27 | with gr.Column(): 28 | pass 29 | with gr.Row(): 30 | lighting_option = gr.Dropdown( 31 | show_label=False, 32 | type='index', 33 | choices=[ 34 | lang_util.get_text("light_source_directly_above"), 35 | lang_util.get_text("light_source_upper_left_diagonal"), 36 | lang_util.get_text("light_source_upper_right_diagonal"), 37 | lang_util.get_text("light_source_directly_left"), 38 | lang_util.get_text("light_source_directly_right"), 39 | lang_util.get_text("light_source_directly_below"), 40 | ], 41 | ) 42 | with gr.Row(): 43 | light_yaw = gr.Slider(label=lang_util.get_text("light_yaw"), minimum=-180, maximum=180, value=60, 44 | step=0.1) 45 | light_pitch = gr.Slider(label=lang_util.get_text("light_pitch"), minimum=-90, maximum=90, value=-60, 46 | step=0.1) 47 | specular_power = gr.Slider(label=lang_util.get_text("specular_power"), minimum=10, maximum=100, 48 | value=30, step=0.01) 49 | normal_diffuse_strength = gr.Slider(label=lang_util.get_text("normal_diffuse_strength"), minimum=0, 50 | maximum=5.0, value=1.00, step=0.01) 51 | specular_highlights_strength = gr.Slider(label=lang_util.get_text("specular_highlights_strength"), 52 | minimum=0, maximum=5.0, value=0.80, step=0.01) 53 | total_gain = gr.Slider(label=lang_util.get_text("total_gain"), minimum=-0, maximum=1.0, value=0.60, 54 | step=0.01) 55 | with gr.Row(): 56 | generate_button = gr.Button(lang_util.get_text("generate"), interactive=False) 57 | with gr.Column(): 58 | self.output = OutputImage(self.app_config, transfer_target_lang_key) 59 | output_image = self.output.layout() 60 | 61 | self.input_image.change(lambda x: gr.update(interactive=x is not None), inputs=[self.input_image], 62 | outputs=[generate_button]) 63 | lighting_option.change(self._select_lighting_option, inputs=[lighting_option], outputs=[light_yaw, light_pitch]) 64 | 65 | generate_button.click(self._process, inputs=[ 66 | self.input_image, 67 | light_yaw, 68 | light_pitch, 69 | specular_power, 70 | normal_diffuse_strength, 71 | specular_highlights_strength, 72 | total_gain 73 | ], outputs=[output_image]) 74 | 75 | def _select_lighting_option(self, select_option_index): 76 | if select_option_index == 0: 77 | # 光源:真上 78 | return [60, -60] 79 | if select_option_index == 1: 80 | # 光源:左斜め上 81 | return [40, -60] 82 | if select_option_index == 2: 83 | # 光源:右斜め上 84 | return [60, -40] 85 | if select_option_index == 3: 86 | # 光源:左横 87 | return [0, 0] 88 | if select_option_index == 4: 89 | # 光源:右横 90 | return [90, 0] 91 | if select_option_index == 5: 92 | # 光源:真下 93 | return [45, 0] 94 | 95 | def _process(self, input_image_path, light_yaw, light_pitch, specular_power, normal_diffuse_strength, 96 | specular_highlights_strength, total_gain): 97 | # ライティング効果を適用するために関数を呼び出します 98 | output_tensor = self.apply_lighting_effects( 99 | to_tensor(Image.open(input_image_path)), 100 | light_yaw, 101 | light_pitch, 102 | specular_power, 103 | normal_diffuse_strength, 104 | specular_highlights_strength, 105 | total_gain 106 | ) 107 | 108 | # テンソルから画像へ変換 109 | output_pil: Image = to_pil_image(output_tensor.squeeze(0)) 110 | output_pil.save(self.app_config.make_output_path()) 111 | return output_pil 112 | 113 | def apply_lighting_effects(self, input_tensor, light_yaw, light_pitch, specular_power, normal_diffuse_strength, 114 | specular_highlights_strength, total_gain): 115 | # 元の次元数を記録する 116 | original_dim = input_tensor.dim() 117 | 118 | # 入力テンソルが最低でも4次元であることを保証する(バッチ次元がなければ追加する) 119 | if original_dim == 3: 120 | input_tensor = input_tensor.unsqueeze(0) # バッチ次元を追加 121 | 122 | # 入力テンソルに3つ以上のチャンネルがある場合、追加のチャンネルをアルファと仮定し削除する 123 | if input_tensor.shape[1] > 3: 124 | input_tensor = input_tensor[:, :3, :, :] # 最初の3チャンネルのみを保持 125 | 126 | # 入力テンソルを正規化して法線ベクトルを取得する 127 | normal_tensor = torch.nn.functional.normalize(input_tensor, dim=1) # チャンネル次元に沿って正規化 128 | 129 | # 光の方向ベクトルを計算する 130 | light_direction = self.euler_to_vector(light_yaw, light_pitch, 0) 131 | light_direction = light_direction.to(input_tensor.device) # 同じデバイスを保証する 132 | 133 | # 光の方向とカメラの方向を入力テンソルの次元に合わせて拡張する 134 | batch_size, _, height, width = input_tensor.shape 135 | light_direction = light_direction.view(1, 3, 1, 1).expand(batch_size, -1, height, width) 136 | camera_direction = torch.tensor([0, 0, 1], dtype=torch.float32, device=input_tensor.device) 137 | camera_direction = camera_direction.view(1, 3, 1, 1).expand(batch_size, -1, height, width) 138 | 139 | # 拡散成分を計算する 140 | diffuse = torch.sum(normal_tensor * light_direction, dim=1, keepdim=True) 141 | diffuse = torch.clamp(diffuse, 0, 1) 142 | 143 | # 鏡面成分を計算する 144 | half_vector = torch.nn.functional.normalize(light_direction + camera_direction, dim=1) 145 | specular = torch.sum(normal_tensor * half_vector, dim=1, keepdim=True) 146 | specular = torch.pow(torch.clamp(specular, 0, 1), specular_power) 147 | 148 | # 拡散成分と鏡面成分を組み合わせて、強度とゲインを適用する 149 | output_tensor = (diffuse * normal_diffuse_strength + specular * specular_highlights_strength) * total_gain 150 | output_tensor = output_tensor.squeeze(1) # keepdim=Trueで追加されたチャンネル次元を削除 151 | 152 | # 初めに追加されたバッチ次元があれば削除する 153 | if original_dim == 3: 154 | output_tensor = output_tensor.squeeze(0) 155 | 156 | return output_tensor 157 | 158 | def euler_to_vector(self, yaw, pitch, roll): 159 | # オイラー角から方向ベクトルを計算 160 | yaw_rad = np.radians(yaw) 161 | pitch_rad = np.radians(pitch) 162 | roll_rad = np.radians(roll) 163 | 164 | cos_pitch = np.cos(pitch_rad) 165 | sin_pitch = np.sin(pitch_rad) 166 | cos_yaw = np.cos(yaw_rad) 167 | sin_yaw = np.sin(yaw_rad) 168 | 169 | direction = np.array([ 170 | sin_yaw * cos_pitch, 171 | -sin_pitch, 172 | cos_yaw * cos_pitch 173 | ]) 174 | return torch.from_numpy(direction).float() 175 | -------------------------------------------------------------------------------- /AI_Assistant_modules/actions/line_drawing.py: -------------------------------------------------------------------------------- 1 | import gradio as gr 2 | from PIL import Image 3 | 4 | from AI_Assistant_modules.output_image_gui import OutputImage 5 | from AI_Assistant_modules.prompt_analysis import PromptAnalysis 6 | from utils.img_utils import make_base_pil, base_generation, canny_process, resize_image_aspect_ratio 7 | from utils.prompt_utils import execute_prompt, remove_color, remove_duplicates 8 | from utils.request_api import create_and_save_images, get_lora_model 9 | 10 | LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS) 11 | 12 | 13 | class LineDrawing: 14 | def __init__(self, app_config): 15 | self.app_config = app_config 16 | self.input_image = None 17 | self.output = None 18 | 19 | def layout(self, transfer_target_lang_key=None): 20 | lang_util = self.app_config.lang_util 21 | exui = self.app_config.exui 22 | with gr.Row() as self.block: 23 | with gr.Column(): 24 | with gr.Row(): 25 | with gr.Column(): 26 | self.input_image = gr.Image(label=lang_util.get_text("input_image"), 27 | source="upload", 28 | type='filepath', interactive=True) 29 | with gr.Column(): 30 | with gr.Row(): 31 | canny_image = gr.Image(label=lang_util.get_text("canny_image"), type="pil", 32 | interactive=False) 33 | with gr.Row(): 34 | canny_threshold1 = gr.Slider(minimum=0, value=20, maximum=253, show_label=False) 35 | gr.HTML(value="/", show_label=False) 36 | canny_threshold2 = gr.Slider(minimum=0, value=120, maximum=254, show_label=False) 37 | canny_generate_button = gr.Button(lang_util.get_text("generate"), interactive=False) 38 | if exui: 39 | with gr.Row(): 40 | lora_model_dropdown = gr.Dropdown(label=lang_util.get_text("lora_models"), choices=[]) 41 | load_lora_models_button = gr.Button(lang_util.get_text("lora_update")) 42 | with gr.Row(): 43 | [prompt, nega] = PromptAnalysis(self.app_config).layout(lang_util, self.input_image) 44 | with gr.Row(): 45 | fidelity = gr.Slider(minimum=0.5, maximum=1.25, value=1.0, step=0.01, interactive=True, 46 | label=lang_util.get_text("lineart_fidelity")) 47 | bold = gr.Slider(minimum=0.0, maximum=1.0, value=0.0, step=0.01, interactive=True, 48 | label=lang_util.get_text("lineart_bold")) 49 | with gr.Row(): 50 | generate_button = gr.Button(lang_util.get_text("generate"), interactive=False) 51 | with gr.Column(): 52 | self.output = OutputImage(self.app_config, transfer_target_lang_key) 53 | output_image = self.output.layout() 54 | 55 | self.input_image.change(lambda x, y: gr.update(interactive=x is not None and y is not None), 56 | inputs=[self.input_image, canny_image], outputs=[generate_button]) 57 | canny_image.change(lambda x, y: gr.update(interactive=x is not None and y is not None), 58 | inputs=[self.input_image, canny_image], outputs=[generate_button]) 59 | self.input_image.change(lambda x: gr.update(interactive=x is not None), inputs=[self.input_image], 60 | outputs=[canny_generate_button]) 61 | 62 | canny_generate_button.click(self._make_canny, inputs=[self.input_image, canny_threshold1, canny_threshold2], 63 | outputs=[canny_image]) 64 | 65 | if exui: 66 | load_lora_models_button.click(self.load_lora_models, inputs=[], outputs=[lora_model_dropdown]) 67 | lora_model_dropdown.change(self.update_prompt_with_lora, inputs=[lora_model_dropdown, prompt], outputs=[prompt, lora_model_dropdown]) 68 | 69 | generate_button.click(self._process, inputs=[ 70 | self.input_image, 71 | canny_image, 72 | prompt, 73 | nega, 74 | fidelity, 75 | bold, 76 | ], outputs=[output_image]) 77 | 78 | def _process(self, input_image_path, canny_image_pil, prompt_text, negative_prompt_text, fidelity, bold): 79 | lineart_bold = float(bold) 80 | lineart = 1 - lineart_bold 81 | prompt = "masterpiece, best quality, , , monochrome, lineart, white background, " + prompt_text.strip() 83 | execute_tags = ["sketch", "transparent background"] 84 | prompt = execute_prompt(execute_tags, prompt) 85 | prompt = remove_duplicates(prompt) 86 | prompt = remove_color(prompt) 87 | nega = negative_prompt_text.strip() 88 | base_pil = make_base_pil(input_image_path) 89 | base_pil = resize_image_aspect_ratio(base_pil) 90 | image_size = Image.open(input_image_path).size 91 | canny_pil = canny_image_pil.resize(base_pil.size, LANCZOS).convert("RGB") 92 | mask_pil = base_generation(base_pil.size, (255, 255, 255, 255)).convert("RGB") 93 | white_base_pil = base_generation(base_pil.size, (255, 255, 255, 255)).convert("RGB") 94 | image_fidelity = 1.0 95 | lineart_fidelity = float(fidelity) 96 | lineart_output_path = self.app_config.make_output_path() 97 | output_pil = create_and_save_images(self.app_config.fastapi_url, 98 | prompt, nega, 99 | white_base_pil, mask_pil, image_size, 100 | lineart_output_path, 101 | image_fidelity, 102 | self._make_cn_args(canny_pil, lineart_fidelity) 103 | ) 104 | return output_pil 105 | 106 | 107 | def load_lora_models(self): 108 | model_names, model_aliases = get_lora_model(self.app_config.fastapi_url) 109 | model_options = [f"{name} ({alias})" for name, alias in zip(model_names, model_aliases)] 110 | return gr.Dropdown.update(choices=model_options, interactive=True) 111 | 112 | 113 | def update_prompt_with_lora(self, lora_model_selection, existing_prompt): 114 | if '(' in lora_model_selection and ')' in lora_model_selection: 115 | alias = lora_model_selection.split('(')[-1].split(')')[0].strip() 116 | else: 117 | alias = lora_model_selection 118 | 119 | lora_tag = f"" 120 | updated_prompt = existing_prompt + ", " + lora_tag if existing_prompt else lora_tag 121 | 122 | #仮に『, 』という文字列が含まれていたら除去する 123 | updated_prompt = updated_prompt.replace(", ", "").strip() 124 | 125 | return updated_prompt, [] 126 | 127 | def handle_lora_model_update(result): 128 | updated_prompt, reset_choices = result 129 | return gr.update(value=updated_prompt), gr.Dropdown.update(choices=reset_choices, interactive=True) 130 | 131 | 132 | def _make_canny(self, canny_img_path, canny_threshold1, canny_threshold2): 133 | threshold1 = int(canny_threshold1) 134 | threshold2 = int(canny_threshold2) 135 | return canny_process(canny_img_path, threshold1, threshold2) 136 | 137 | def _make_cn_args(self, canny_image_pil, lineart_fidelity): 138 | unit1 = { 139 | "image": canny_image_pil, 140 | "mask_image": None, 141 | "control_mode": "Balanced", 142 | "enabled": True, 143 | "guidance_end": 1, 144 | "guidance_start": 0, 145 | "pixel_perfect": True, 146 | "processor_res": 512, 147 | "resize_mode": "Just Resize", 148 | "weight": lineart_fidelity, 149 | "module": "None", 150 | "model": "control-lora-canny-rank256 [ec2dbbe4]", 151 | "save_detected_map": None, 152 | "hr_option": "Both" 153 | } 154 | unit2 = None 155 | return [unit1] 156 | -------------------------------------------------------------------------------- /AI_Assistant_modules/actions/i2i.py: -------------------------------------------------------------------------------- 1 | import gradio as gr 2 | from PIL import Image 3 | 4 | from AI_Assistant_modules.output_image_gui import OutputImage 5 | from AI_Assistant_modules.prompt_analysis import PromptAnalysis 6 | from utils.img_utils import make_base_pil, base_generation, mask_process, resize_image_aspect_ratio 7 | from utils.prompt_utils import execute_prompt, remove_duplicates 8 | from utils.request_api import create_and_save_images, get_lora_model 9 | 10 | LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS) 11 | 12 | class Img2Img: 13 | def __init__(self, app_config): 14 | self.app_config = app_config 15 | self.input_image = None 16 | self.output = None 17 | 18 | 19 | def layout(self, transfer_target_lang_key=None): 20 | lang_util = self.app_config.lang_util 21 | exui = self.app_config.exui 22 | 23 | with gr.Row(): 24 | with gr.Column(): 25 | with gr.Row(): 26 | with gr.Column(): 27 | self.input_image = gr.Image(label=lang_util.get_text("input_image"), source="upload", type='filepath', interactive=True) 28 | with gr.Column(): 29 | with gr.Row(): 30 | mask_image = gr.Image(label=lang_util.get_text("mask_image"), type="pil") 31 | with gr.Row(): 32 | mask_generate_button = gr.Button(lang_util.get_text("create"), interactive=False) 33 | with gr.Column(): 34 | with gr.Row(): 35 | anytest_image = gr.Image(label=lang_util.get_text("anytest_image"), type="pil") 36 | with gr.Row(): 37 | anytest_choice_button = gr.Radio(["none", "anytestV3", "anytestV4"], value="none", label=lang_util.get_text("anytest_choice")) 38 | if exui: 39 | with gr.Row(): 40 | lora_model_dropdown = gr.Dropdown(label=lang_util.get_text("lora_models"), choices=[]) 41 | load_lora_models_button = gr.Button(lang_util.get_text("lora_update")) 42 | with gr.Row(): 43 | prompt_analysis = PromptAnalysis(app_config=self.app_config, post_filter=False) 44 | [prompt, nega] = prompt_analysis.layout(lang_util=lang_util, input_image=self.input_image) 45 | with gr.Row(): 46 | fidelity = gr.Slider(minimum=0.0, maximum=0.9, value=0.35, step=0.01, interactive=True, label=lang_util.get_text("image_fidelity")) 47 | with gr.Row(): 48 | anytest_fidelity = gr.Slider(minimum=0.35, maximum=1.25, value=1.0, step=0.01, interactive=True, label=lang_util.get_text("anytest_fidelity")) 49 | with gr.Row(): 50 | generate_button = gr.Button(lang_util.get_text("generate"), interactive=False) 51 | with gr.Column(): 52 | self.output = OutputImage(self.app_config, transfer_target_lang_key) 53 | output_image = self.output.layout() 54 | 55 | self.input_image.change(lambda x: gr.update(interactive=x is not None), inputs=[self.input_image], outputs=[generate_button]) 56 | self.input_image.change(lambda x: gr.update(interactive=x is not None), inputs=[self.input_image], outputs=[mask_generate_button]) 57 | 58 | mask_generate_button.click(mask_process, inputs=[self.input_image], outputs=[mask_image]) 59 | 60 | if exui: 61 | load_lora_models_button.click(self.load_lora_models, inputs=[], outputs=[lora_model_dropdown]) 62 | lora_model_dropdown.change(self.update_prompt_with_lora, inputs=[lora_model_dropdown, prompt], outputs=[prompt, lora_model_dropdown]) 63 | 64 | generate_button.click(self._process, inputs=[ 65 | self.input_image, 66 | mask_image, 67 | anytest_image, 68 | anytest_choice_button, 69 | anytest_fidelity, 70 | prompt, 71 | nega, 72 | fidelity, 73 | ], outputs=[output_image]) 74 | 75 | def _process(self, input_image_path, mask_image_pil, anytest_image, anytest_choice_button, anytest_fidelity, prompt_text, negative_prompt_text, fidelity): 76 | prompt = "masterpiece, best quality, " + prompt_text.strip() 77 | execute_tags = ["transparent background"] 78 | prompt =execute_prompt(execute_tags, prompt) 79 | prompt = remove_duplicates(prompt) 80 | nega = negative_prompt_text.strip() 81 | base_pil = make_base_pil(input_image_path) 82 | base_pil = resize_image_aspect_ratio(base_pil) 83 | image_size = Image.open(input_image_path).size 84 | if mask_image_pil is None: 85 | mask_pil = base_generation(base_pil.size, (255, 255, 255, 255)).convert("RGB") 86 | else: 87 | mask_pil = mask_image_pil.resize(base_pil.size, LANCZOS).convert("RGB") 88 | image_fidelity = 1 - float(fidelity) 89 | img2img_output_path = self.app_config.make_output_path() 90 | 91 | if anytest_choice_button == "none": 92 | output_pil = create_and_save_images(self.app_config.fastapi_url, prompt, nega, base_pil, mask_pil, 93 | image_size, img2img_output_path, image_fidelity, None, { 94 | "mask": mask_pil, 95 | "mask_blur": 4, 96 | "inpainting_fill": 1, 97 | "inpaint_full_res":0, 98 | }) 99 | return output_pil 100 | else: 101 | output_pil = create_and_save_images(self.app_config.fastapi_url, prompt, nega, base_pil, mask_pil, 102 | image_size, img2img_output_path, image_fidelity, self._make_cn_args(anytest_image, anytest_fidelity, anytest_choice_button), { 103 | "mask": mask_pil, 104 | "mask_blur": 4, 105 | "inpainting_fill": 1, 106 | "inpaint_full_res": 0, 107 | }) 108 | return output_pil 109 | 110 | 111 | def load_lora_models(self): 112 | model_names, model_aliases = get_lora_model(self.app_config.fastapi_url) 113 | model_options = [f"{name} ({alias})" for name, alias in zip(model_names, model_aliases)] 114 | return gr.Dropdown.update(choices=model_options, interactive=True) 115 | 116 | 117 | def update_prompt_with_lora(self, lora_model_selection, existing_prompt): 118 | if '(' in lora_model_selection and ')' in lora_model_selection: 119 | alias = lora_model_selection.split('(')[-1].split(')')[0].strip() 120 | else: 121 | alias = lora_model_selection 122 | 123 | lora_tag = f"" 124 | updated_prompt = existing_prompt + ", " + lora_tag if existing_prompt else lora_tag 125 | 126 | #仮に『, 』という文字列が含まれていたら除去する 127 | updated_prompt = updated_prompt.replace(", ", "").strip() 128 | 129 | return updated_prompt, [] 130 | 131 | def handle_lora_model_update(result): 132 | updated_prompt, reset_choices = result 133 | return gr.update(value=updated_prompt), gr.Dropdown.update(choices=reset_choices, interactive=True) 134 | 135 | 136 | def _make_cn_args(self, anytest_image, anytest_fidelity, anytest_choice_button): 137 | if anytest_choice_button == "anytestV3": 138 | model = "CN-anytest_v3-50000_am_dim256 [dbecc0f9]" 139 | else: 140 | model = "CN-anytest_v4-marged_am_dim256 [49b6c950]" 141 | 142 | unit1 = { 143 | "image": anytest_image, 144 | "mask_image": None, 145 | "control_mode": "Balanced", 146 | "enabled": True, 147 | "guidance_end": 1, 148 | "guidance_start": 0, 149 | "pixel_perfect": True, 150 | "processor_res": 512, 151 | "resize_mode": "Just Resize", 152 | "weight": anytest_fidelity, 153 | "module": "None", 154 | "model": model, 155 | "save_detected_map": None, 156 | "hr_option": "Both" 157 | } 158 | unit2 = None 159 | return [unit1] 160 | 161 | -------------------------------------------------------------------------------- /utils/img_utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from PIL import Image, ImageOps, ImageFilter 3 | import cv2 4 | from collections import defaultdict 5 | from skimage.color import rgb2lab, deltaE_ciede2000 6 | 7 | 8 | def canny_process(image_path, threshold1, threshold2): 9 | # 画像を開き、RGBA形式に変換して透過情報を保持 10 | img = Image.open(image_path) 11 | img = img.convert("RGBA") 12 | 13 | canvas_image = Image.new('RGBA', img.size, (255, 255, 255, 255)) 14 | 15 | # 画像をキャンバスにペーストし、透過部分が白色になるように設定 16 | canvas_image.paste(img, (0, 0), img) 17 | 18 | # RGBAからRGBに変換し、透過部分を白色にする 19 | image_pil = canvas_image.convert("RGB") 20 | image_np = np.array(image_pil) 21 | 22 | # グレースケール変換 23 | gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY) 24 | # Cannyエッジ検出 25 | edges = cv2.Canny(gray, threshold1, threshold2) 26 | 27 | canny = Image.fromarray(edges) 28 | 29 | 30 | return canny 31 | 32 | def invert_process(image_path): 33 | # 画像を開き、RGBA形式に変換して透過情報を保持 34 | img = Image.open(image_path) 35 | img = img.convert("RGBA") 36 | 37 | canvas_image = Image.new('RGBA', img.size, (255, 255, 255, 255)) 38 | 39 | # 画像をキャンバスにペーストし、透過部分が白色になるように設定 40 | canvas_image.paste(img, (0, 0), img) 41 | 42 | # RGBAからRGBに変換し、透過部分を白色にする 43 | image_pil = canvas_image.convert("RGB") 44 | 45 | # image_pilを白黒反転する 46 | invert = ImageOps.invert(image_pil) 47 | 48 | return invert 49 | 50 | 51 | def mask_process(image_path): 52 | # 画像をRGBAで読み込み、アルファチャンネルを考慮 53 | img = Image.open(image_path).convert("RGBA") 54 | img_rgba = img.convert("RGBA") 55 | canvas = Image.new("RGBA", img_rgba.size, (255, 255, 255, 255)) # 白背景のキャンバスを作成 56 | img_with_background = Image.alpha_composite(canvas, img_rgba) 57 | 58 | # OpenCVが扱える形式に変換 59 | img_with_background = cv2.cvtColor(np.array(img_with_background), cv2.COLOR_RGBA2BGR) 60 | img = cv2.bitwise_not(img_with_background) 61 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 62 | 63 | ret, img_binary = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY) 64 | contours, _ = cv2.findContours(img_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 65 | contour = max(contours, key=lambda x: cv2.contourArea(x)) 66 | mask = np.zeros_like(img_with_background) 67 | cv2.drawContours(mask, [contour], -1, (255, 255, 255), thickness=cv2.FILLED) 68 | mask = Image.fromarray(mask) 69 | return mask 70 | 71 | def multiply_images(line_pil, shadow_pil): 72 | # 画像のモードを確認し、必要に応じてRGBAに変換 73 | if line_pil.mode != 'RGBA': 74 | line_pil = line_pil.convert('RGBA') 75 | if shadow_pil.mode != 'RGBA': 76 | shadow_pil = shadow_pil.convert('RGBA') 77 | 78 | # 画像のサイズを確認し、異なる場合はエラーを投げる 79 | if line_pil.size != shadow_pil.size: 80 | raise ValueError("Images must have the same dimensions") 81 | 82 | # 乗算処理を行う 83 | result_image = Image.new('RGBA', line_pil.size) 84 | for x in range(line_pil.width): 85 | for y in range(line_pil.height): 86 | pixel_line = line_pil.getpixel((x, y)) 87 | pixel_shadow = shadow_pil.getpixel((x, y)) 88 | # 各チャンネルごとに乗算し、255で割って正規化 89 | r = (pixel_line[0] * pixel_shadow[0]) // 255 90 | g = (pixel_line[1] * pixel_shadow[1]) // 255 91 | b = (pixel_line[2] * pixel_shadow[2]) // 255 92 | a = (pixel_line[3] * pixel_shadow[3]) // 255 93 | result_image.putpixel((x, y), (r, g, b, a)) 94 | 95 | return result_image 96 | 97 | def resize_image_aspect_ratio(image): 98 | # 元の画像サイズを取得 99 | original_width, original_height = image.size 100 | 101 | # アスペクト比を計算 102 | aspect_ratio = original_width / original_height 103 | 104 | # 標準のアスペクト比サイズを定義 105 | sizes = { 106 | 1: (1024, 1024), # 正方形 107 | 4/3: (1152, 896), # 横長画像 108 | 3/2: (1216, 832), 109 | 16/9: (1344, 768), 110 | 21/9: (1568, 672), 111 | 3/1: (1728, 576), 112 | 1/4: (512, 2048), # 縦長画像 113 | 1/3: (576, 1728), 114 | 9/16: (768, 1344), 115 | 2/3: (832, 1216), 116 | 3/4: (896, 1152) 117 | } 118 | 119 | # 最も近いアスペクト比を見つける 120 | closest_aspect_ratio = min(sizes.keys(), key=lambda x: abs(x - aspect_ratio)) 121 | target_width, target_height = sizes[closest_aspect_ratio] 122 | 123 | # リサイズ処理 124 | resized_image = image.resize((target_width, target_height), Image.ANTIALIAS) 125 | 126 | return resized_image 127 | 128 | def base_generation(size, color): 129 | canvas = Image.new("RGBA", size, color) 130 | return canvas 131 | 132 | 133 | def make_base_pil(image_path): 134 | # 処理対象の基本画像を生成する 135 | # 画像を開き、白色背景に変換した上でアスペクト比に基づくリサイズを行う 136 | base_pil = Image.open(image_path).convert("RGBA") 137 | base_pil = resize_image_aspect_ratio(base_pil) 138 | white_bg = Image.new("RGBA", base_pil.size, "WHITE") 139 | white_bg.paste(base_pil, mask=base_pil) 140 | base_pil = resize_image_aspect_ratio(white_bg).convert("RGB") 141 | return base_pil 142 | 143 | def noline_process(input_image_path): 144 | def get_major_colors(image, threshold_percentage): 145 | if image.mode != 'RGB': 146 | image = image.convert('RGB') 147 | color_count = defaultdict(int) 148 | for pixel in image.getdata(): 149 | color_count[pixel] += 1 150 | total_pixels = image.width * image.height 151 | return [(color, count) for color, count in color_count.items() if (count / total_pixels) >= threshold_percentage] 152 | 153 | def consolidate_colors(major_colors, threshold): 154 | colors_lab = [rgb2lab(np.array([[color]], dtype=np.float32)/255.0).reshape(3) for color, _ in major_colors] 155 | i = 0 156 | while i < len(colors_lab): 157 | j = i + 1 158 | while j < len(colors_lab): 159 | if deltaE_ciede2000(colors_lab[i], colors_lab[j]) < threshold: 160 | if major_colors[i][1] >= major_colors[j][1]: 161 | major_colors[i] = (major_colors[i][0], major_colors[i][1] + major_colors[j][1]) 162 | major_colors.pop(j) 163 | colors_lab.pop(j) 164 | else: 165 | major_colors[j] = (major_colors[j][0], major_colors[j][1] + major_colors[i][1]) 166 | major_colors.pop(i) 167 | colors_lab.pop(i) 168 | continue 169 | j += 1 170 | i += 1 171 | return major_colors 172 | 173 | def generate_distant_colors(consolidated_colors, distance_threshold): 174 | consolidated_lab = [rgb2lab(np.array([color], dtype=np.float32) / 255.0).reshape(3) for color, _ in consolidated_colors] 175 | max_attempts = 10000 176 | for _ in range(max_attempts): 177 | random_rgb = np.random.randint(0, 256, size=3) 178 | random_lab = rgb2lab(np.array([random_rgb], dtype=np.float32) / 255.0).reshape(3) 179 | if all(deltaE_ciede2000(base_color_lab, random_lab) > distance_threshold for base_color_lab in consolidated_lab): 180 | return tuple(random_rgb) 181 | return (128, 128, 128) 182 | 183 | def line_color(image, mask, new_color): 184 | data = np.array(image) 185 | data[mask, :3] = new_color 186 | return Image.fromarray(data) 187 | 188 | def replace_color(image, color_1, blur_radius=2): 189 | data = np.array(image) 190 | original_shape = data.shape 191 | channels = original_shape[2] if len(original_shape) > 2 else 1 192 | data = data.reshape(-1, channels) 193 | color_1 = np.array(color_1) 194 | matches = np.all(data[:, :3] == color_1, axis=1) 195 | mask = np.zeros(data.shape[0], dtype=bool) 196 | 197 | while np.any(matches): 198 | new_matches = np.zeros_like(matches) 199 | for i in range(len(data)): 200 | if matches[i]: 201 | x, y = divmod(i, original_shape[1]) 202 | neighbors = [(x, y-1), (x, y+1), (x-1, y), (x+1, y)] 203 | valid_neighbors = [data[nx * original_shape[1] + ny, :3] for nx, ny in neighbors if 0 <= nx < original_shape[0] and 0 <= ny < original_shape[1] and not matches[nx * original_shape[1] + ny]] 204 | if valid_neighbors: 205 | new_color = np.mean(valid_neighbors, axis=0).astype(np.uint8) 206 | data[i, :3] = new_color 207 | mask[i] = True 208 | else: 209 | new_matches[i] = True 210 | matches = new_matches 211 | if not np.any(matches): 212 | break 213 | 214 | data = data.reshape(original_shape) 215 | mask = mask.reshape(original_shape[:2]) 216 | result_image = Image.fromarray(data, 'RGBA') 217 | blurred_image = result_image.filter(ImageFilter.GaussianBlur(radius=blur_radius)) 218 | blurred_data = np.array(blurred_image) 219 | np.copyto(data, blurred_data, where=mask[..., None]) 220 | return Image.fromarray(data, 'RGBA') 221 | 222 | def DoG_filter(image, kernel_size=0, sigma=1.0, k_sigma=2.0, gamma=1.5): 223 | g1 = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigma) 224 | g2 = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigma * k_sigma) 225 | return g1 - gamma * g2 226 | 227 | def XDoG_filter(image, kernel_size=0, sigma=1.4, k_sigma=1.6, epsilon=0, phi=10, gamma=0.98): 228 | epsilon /= 255 229 | dog = DoG_filter(image, kernel_size, sigma, k_sigma, gamma) 230 | dog /= dog.max() 231 | e = 1 + np.tanh(phi * (dog - epsilon)) 232 | e[e >= 1] = 1 233 | return (e * 255).astype('uint8') 234 | 235 | def binarize_image(image): 236 | _, binarized = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) 237 | return binarized 238 | 239 | 240 | def process_XDoG(image_path): 241 | kernel_size=0 242 | sigma=1.4 243 | k_sigma=1.6 244 | epsilon=0 245 | phi=10 246 | gamma=0.98 247 | 248 | image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) 249 | xdog_image = XDoG_filter(image, kernel_size, sigma, k_sigma, epsilon, phi, gamma) 250 | binarized_image = binarize_image(xdog_image) 251 | final_image = Image.fromarray(binarized_image) 252 | return final_image 253 | 254 | rgb_image = Image.open(input_image_path).convert("RGBA") 255 | lineart = process_XDoG(input_image_path).convert('L') 256 | lineart = lineart.point(lambda x: 0 if x < 200 else 255) 257 | lineart = ImageOps.invert(lineart) 258 | kernel = np.ones((3, 3), np.uint8) 259 | lineart = cv2.dilate(np.array(lineart), kernel, iterations=1) 260 | lineart = Image.fromarray(lineart) 261 | mask = np.array(lineart) == 255 262 | major_colors = get_major_colors(rgb_image, threshold_percentage=0.05) 263 | major_colors = consolidate_colors(major_colors, 10) 264 | new_color_1 = generate_distant_colors(major_colors, 100) 265 | filled_image = line_color(rgb_image, mask, new_color_1) 266 | replace_color_image = replace_color(filled_image, new_color_1, 2).convert('RGB') 267 | return replace_color_image 268 | -------------------------------------------------------------------------------- /AI_Assistant_model_DL.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | 4 | REM Set the base path for models 5 | set "dpath=%~dp0models" 6 | echo Base directory set to: %dpath% 7 | 8 | goto :main 9 | 10 | :download_and_verify 11 | set "FILE_PATH=%~1" 12 | set "DOWNLOAD_URL=%~2" 13 | set "EXPECTED_HASH=%~3" 14 | set "MAX_ATTEMPTS=3" 15 | 16 | for /L %%i in (1,1,%MAX_ATTEMPTS%) do ( 17 | echo Attempt %%i of %MAX_ATTEMPTS% 18 | curl -L "!DOWNLOAD_URL!" -o "!FILE_PATH!" 19 | 20 | REM Calculate SHA-256 hash 21 | for /f "skip=1 tokens=* delims=" %%# in ('certutil -hashfile "!FILE_PATH!" SHA256') do ( 22 | set "ACTUAL_HASH=%%#" 23 | goto :hash_calculated 24 | ) 25 | :hash_calculated 26 | 27 | if "!ACTUAL_HASH!"=="!EXPECTED_HASH!" ( 28 | echo Hash verification successful. 29 | exit /b 0 30 | ) else ( 31 | echo Hash mismatch. Retrying... 32 | if %%i equ %MAX_ATTEMPTS% ( 33 | echo Warning: Failed to download file with matching hash after %MAX_ATTEMPTS% attempts. 34 | exit /b 1 35 | ) 36 | ) 37 | ) 38 | exit /b 39 | 40 | :verify_hash 41 | set "FILE_PATH=%~1" 42 | set "EXPECTED_HASH=%~2" 43 | 44 | for /f "skip=1 tokens=* delims=" %%# in ('certutil -hashfile "%FILE_PATH%" SHA256') do ( 45 | set "ACTUAL_HASH=%%#" 46 | goto :hash_calculated_verify 47 | ) 48 | :hash_calculated_verify 49 | 50 | if "%ACTUAL_HASH%"=="%EXPECTED_HASH%" ( 51 | echo Hash verification successful for %FILE_PATH% 52 | exit /b 0 53 | ) else ( 54 | echo Hash mismatch for %FILE_PATH% 55 | echo Expected: %EXPECTED_HASH% 56 | echo Actual: %ACTUAL_HASH% 57 | exit /b 1 58 | ) 59 | 60 | :download_files_custom 61 | if "%~1"=="" ( 62 | echo No arguments provided to download_files_custom 63 | exit /b 1 64 | ) 65 | echo Downloading files to "%~1" from "%~2" with custom path "%~4" 66 | set "MODEL_DIR=%dpath%\%~1" 67 | set "MODEL_ID=%~2" 68 | set "FILES=%~3" 69 | set "CUSTOM_PATH=%~4" 70 | echo MODEL_DIR: !MODEL_DIR! 71 | echo MODEL_ID: !MODEL_ID! 72 | echo FILES: !FILES! 73 | echo CUSTOM_PATH: !CUSTOM_PATH! 74 | 75 | if not exist "!MODEL_DIR!" ( 76 | echo Creating directory !MODEL_DIR! 77 | mkdir "!MODEL_DIR!" 78 | ) 79 | 80 | for %%f in (%FILES%) do ( 81 | set "FILE_PATH=!MODEL_DIR!\%%f" 82 | set "EXPECTED_HASH=!%~1_%%~nf_hash!" 83 | set "RETRY_COUNT=0" 84 | :retry_download_custom 85 | if not exist "!FILE_PATH!" ( 86 | echo Downloading %%f... 87 | curl -L "https://huggingface.co/!MODEL_ID!/resolve/main/!CUSTOM_PATH!/%%f" -o "!FILE_PATH!" 88 | if !errorlevel! neq 0 ( 89 | echo Error downloading %%f 90 | ) else ( 91 | echo Downloaded %%f 92 | call :verify_hash "!FILE_PATH!" "!EXPECTED_HASH!" 93 | if !errorlevel! neq 0 ( 94 | echo Hash verification failed for %%f 95 | set /a RETRY_COUNT+=1 96 | if !RETRY_COUNT! lss 3 ( 97 | echo Retry !RETRY_COUNT!/3 98 | del "!FILE_PATH!" 99 | goto :retry_download_custom 100 | ) else ( 101 | echo Hash verification failed after 3 retries. Deleting %%f 102 | del "!FILE_PATH!" 103 | ) 104 | ) 105 | ) 106 | ) else ( 107 | echo %%f already exists. Verifying hash... 108 | call :verify_hash "!FILE_PATH!" "!EXPECTED_HASH!" 109 | if !errorlevel! neq 0 ( 110 | echo Hash verification failed for existing file %%f 111 | del "!FILE_PATH!" 112 | set "RETRY_COUNT=0" 113 | goto :retry_download_custom 114 | ) 115 | ) 116 | ) 117 | exit /b 0 118 | 119 | :download_files_default 120 | if "%~1"=="" ( 121 | echo No arguments provided to download_files_default 122 | exit /b 1 123 | ) 124 | set "MODEL_DIR=%dpath%\%~1" 125 | set "MODEL_ID=%~2" 126 | set "FILES=%~3" 127 | 128 | echo MODEL_DIR: !MODEL_DIR! 129 | echo MODEL_ID: !MODEL_ID! 130 | echo FILES: !FILES! 131 | 132 | if not exist "!MODEL_DIR!" ( 133 | echo Creating directory !MODEL_DIR! 134 | mkdir "!MODEL_DIR!" 135 | ) 136 | 137 | for %%f in (%FILES%) do ( 138 | set "FILE_PATH=!MODEL_DIR!\%%f" 139 | set "EXPECTED_HASH=!%~1_%%~nf_hash!" 140 | set "RETRY_COUNT=0" 141 | :retry_download_default 142 | if not exist "!FILE_PATH!" ( 143 | echo Downloading %%f... 144 | curl -L "https://huggingface.co/!MODEL_ID!/resolve/main/%%f" -o "!FILE_PATH!" 145 | if !errorlevel! neq 0 ( 146 | echo Error downloading %%f 147 | ) else ( 148 | echo Downloaded %%f 149 | call :verify_hash "!FILE_PATH!" "!EXPECTED_HASH!" 150 | if !errorlevel! neq 0 ( 151 | echo Hash verification failed for %%f 152 | set /a RETRY_COUNT+=1 153 | if !RETRY_COUNT! lss 3 ( 154 | echo Retry !RETRY_COUNT!/3 155 | del "!FILE_PATH!" 156 | goto :retry_download_default 157 | ) else ( 158 | echo Hash verification failed after 3 retries. Deleting %%f 159 | del "!FILE_PATH!" 160 | ) 161 | ) 162 | ) 163 | ) else ( 164 | echo %%f already exists. Verifying hash... 165 | call :verify_hash "!FILE_PATH!" "!EXPECTED_HASH!" 166 | if !errorlevel! neq 0 ( 167 | echo Hash verification failed for existing file %%f 168 | del "!FILE_PATH!" 169 | set "RETRY_COUNT=0" 170 | goto :retry_download_default 171 | ) 172 | ) 173 | ) 174 | exit /b 0 175 | 176 | :main 177 | echo Starting main execution 178 | 179 | REM Define hashes 180 | set "ControlNet_CN-anytest_v3-50000_am_dim256_hash=9c022669c9225a926c9cbca9baaf40387f2a6d579ea004cd15b2d84b7d130052" 181 | set "ControlNet_CN-anytest_v4-marged_am_dim256_hash=62a63fb885caa1aff54cbceceb0a20968922f45b2d5a370e64b156a982132ffb" 182 | set "ControlNet_control-lora-canny-rank256_hash=21f79f7368eff07f57bcd507ca91c0fc89070d7da182960ff24ed1d58310c3a7" 183 | set "ControlNet_controlnet852AClone_v10_hash=58bae8a373d6a39b33a5d110c5b22894fc86b7b1e189b05b163e69446c7f48ee" 184 | set "ControlNet_Kataragi_lineartXL-lora128_hash=bdc33b12ff20900a7fdea0b239c8ee66180d53b9a13f6f87a9d89d2aee9eac91" 185 | set "ControlNet_CL_am31_pose3D_V7_marged_rank256_hash=a34b7efd90e9820e6c065c66665409f3ce2324eee98237f89a40f41a6218a3ad" 186 | set "Lora_sdxl-testlora-plainmaterial_hash=24df34c2c3abf62c7c1b7ee5432935861b10c1cd38b6997b37743701df6cfe71" 187 | set "Lora_anime01_hash=14fc521897c6298272d1ba62dbe9a41e2c2ea3464b23760c7a956d50dd2b0fd5" 188 | set "Lora_anime02_hash=a6cb70645577e8e5e757dbb511dc913046c492f1b46932d891a684e59108b038" 189 | set "Lora_anime03_hash=5a4c1dedb42b469243c1201213e6e59d9bd0f01edb3a99ce93705200886fb842" 190 | set "Lora_animenuri_hash=afe115b55d2141f3ff39bdad2ea656555568f659b6ab34a8db2dc22ed2410441" 191 | set "Lora_atunuri02_hash=da22a0ed520b03368d2403ed35db6c3c2213c04ab236535133bf7c830fe91b36" 192 | set "Lora_sdxl-testlora-normalmap_04b_dim32_hash=9432dee2c0b9e1636e7c6e9a544571991fc22a455d575ffc1e281a57efee727a" 193 | set "Lora_SDXL_baketu2_hash=d3f935e50967dd7712afdccaa9cdbd115b47c1fb61950553f5b4a70f2d99b3c0" 194 | set "Lora_sdxl_BWLine_hash=07c59708361b3e2e4f0b0c0f232183f5f39c32c31b6b6981b4392ea30d49dd57" 195 | set "Lora_sdxl_BW_bold_Line_hash=eda02fe96a41c60fba6a885072837d24e51a83897eb5ca4ead24a5a248e840b7" 196 | set "Lora_suisai01_hash=f32045c2f8c824f783aebb86206e8dd004038ea9fef7b18b9f5aeff8c0b89d21" 197 | set "Lora_Fixhands_anime_bdsqlsz_V1_hash=7fad91117c8205b11b7e7d37b2820ffc68ff526caabee546d54906907e373ed3" 198 | set "Stable-diffusion_animagine-xl-3.1_hash=e3c47aedb06418c6c331443cd89f2b3b3b34b7ed2102a3d4c4408a8d35aad6b0" 199 | set "tagger_config_hash=ddcdd28facc40ee8d0ef4b16ee3e7c70e4d7b156aff7b0f2ccc180e617eda795" 200 | set "tagger_model_hash=e6774bff34d43bd49f75a47db4ef217dce701c9847b546523eb85ff6dbba1db1" 201 | set "tagger_selected_tags_hash=298633d94d0031d2081c0893f29c82eab7f0df00b08483ba8f29d1e979441217" 202 | set "tagger_sw_jax_cv_config_hash=4dda7ac5591de07f7444ca30f2f89971a21769f1db6279f92ca996d371b761c9" 203 | 204 | echo Downloading Tagger model: 205 | call :download_files_default "tagger" "SmilingWolf/wd-swinv2-tagger-v3" "config.json,model.onnx,selected_tags.csv,sw_jax_cv_config.json" 206 | 207 | echo Downloading Lora models: 208 | call :download_files_default "Lora" "tori29umai/lineart" "sdxl_BW_bold_Line.safetensors,sdxl_BWLine.safetensors" 209 | call :download_files_default "Lora" "tori29umai/SDXL_shadow" "sdxl-testlora-normalmap_04b_dim32.safetensors,anime01.safetensors,anime02.safetensors,anime03.safetensors" 210 | call :download_files_default "Lora" "tori29umai/flat_color" "suisai01.safetensors,atunuri02.safetensors,animenuri.safetensors,SDXL_baketu2.safetensors" 211 | call :download_files_default "Lora" "bdsqlsz/stable-diffusion-xl-anime-V5" "Fixhands_anime_bdsqlsz_V1.safetensors" 212 | call :download_files_custom "Lora" "2vXpSwA7/iroiro-lora" "sdxl-testlora-plainmaterial.safetensors" "test3" 213 | 214 | echo Downloading ControlNet models: 215 | call :download_files_custom "ControlNet" "2vXpSwA7/iroiro-lora" "CN-anytest_v3-50000_am_dim256.safetensors,CN-anytest_v4-marged_am_dim256.safetensors" "test_controlnet2" 216 | call :download_files_custom "ControlNet" "stabilityai/control-lora" "control-lora-canny-rank256.safetensors" "control-LoRAs-rank256" 217 | call :download_files_default "ControlNet" "kataragi/ControlNet-LineartXL" "Kataragi_lineartXL-lora128.safetensors" 218 | call :download_files_default "ControlNet" "tori29umai/CN_pose3D_V7" "CL_am31_pose3D_V7_marged_rank256.safetensors" 219 | 220 | echo Downloading Stable-diffusion model: 221 | call :download_files_default "Stable-diffusion" "cagliostrolab/animagine-xl-3.1" "animagine-xl-3.1.safetensors" 222 | 223 | echo Downloading from Civitai: 224 | set "MODEL_DIR=!dpath!\ControlNet" 225 | set "DOWNLOAD_URL=https://civitai.com/api/download/models/515749?type=Model&format=SafeTensor" 226 | set "FILES=controlnet852AClone_v10.safetensors" 227 | 228 | if not exist "!MODEL_DIR!" ( 229 | echo Creating directory !MODEL_DIR! 230 | mkdir "!MODEL_DIR!" 231 | ) 232 | 233 | for %%f in (%FILES%) do ( 234 | set "FILE_PATH=!MODEL_DIR!\%%f" 235 | set "EXPECTED_HASH=!ControlNet_controlnet852AClone_v10_hash!" 236 | set "RETRY_COUNT=0" 237 | :retry_download_civitai 238 | if not exist "!FILE_PATH!" ( 239 | echo Downloading %%f from Civitai... 240 | curl -J -L -o "!FILE_PATH!" "%DOWNLOAD_URL%" 241 | if !errorlevel! neq 0 ( 242 | echo Error downloading %%f from Civitai 243 | ) else ( 244 | echo Downloaded %%f 245 | call :verify_hash "!FILE_PATH!" "!EXPECTED_HASH!" 246 | if !errorlevel! neq 0 ( 247 | echo Hash verification failed for %%f 248 | echo Expected: !EXPECTED_HASH! 249 | echo Actual: !ACTUAL_HASH! 250 | set /a RETRY_COUNT+=1 251 | if !RETRY_COUNT! lss 3 ( 252 | echo Retry !RETRY_COUNT!/3 253 | del "!FILE_PATH!" 254 | goto :retry_download_civitai 255 | ) else ( 256 | echo Hash verification failed after 3 retries. Deleting %%f 257 | del "!FILE_PATH!" 258 | ) 259 | ) 260 | ) 261 | ) else ( 262 | echo %%f already exists. Verifying hash... 263 | call :verify_hash "!FILE_PATH!" "!EXPECTED_HASH!" 264 | if !errorlevel! neq 0 ( 265 | echo Hash verification failed for existing file %%f 266 | echo Expected: !EXPECTED_HASH! 267 | echo Actual: !ACTUAL_HASH! 268 | del "!FILE_PATH!" 269 | set "RETRY_COUNT=0" 270 | goto :retry_download_civitai 271 | ) 272 | ) 273 | ) 274 | 275 | echo Script execution completed 276 | echo Press Enter to close the script... 277 | pause > nul 278 | exit /b 279 | endlocal -------------------------------------------------------------------------------- /AI_Assistant_modules/sd1_clip.py: -------------------------------------------------------------------------------- 1 | import sys 2 | # 'frozen' 状態に応じて適切なファイルパスを取得する関数 3 | def get_appropriate_file_path(): 4 | if getattr(sys, 'frozen', False): 5 | return sys.executable + "/Line2Normalmap/" 6 | else: 7 | return __file__ 8 | appropriate_file_path = get_appropriate_file_path() 9 | import sys 10 | # 'frozen'状態に応じて適切なファイルパスを取得する関数 11 | def get_appropriate_file_path(): 12 | if getattr(sys, 'frozen', False): 13 | # ビルドされたアプリケーションの場合、sys.executableのパスを使用 14 | return sys.executable + "/Line2Normalmap/" 15 | else: 16 | # そうでない場合は、従来通りappropriate_file_pathを使用 17 | return appropriate_file_path 18 | 19 | # 適切なファイルパスを取得 20 | appropriate_file_path = get_appropriate_file_path() 21 | 22 | import sys 23 | import os 24 | 25 | # 'frozen'状態に応じて適切なファイルパスを取得する関数 26 | def get_appropriate_file_path(): 27 | if getattr(sys, 'frozen', False): 28 | # ビルドされたアプリケーションの場合、os.path.dirname(sys.executable)のパスを使用 29 | return os.path.dirname(sys.executable) + "/ldm_patched/modules" 30 | else: 31 | # そうでない場合は、従来通りappropriate_file_pathを使用 32 | return os.path.dirname(appropriate_file_path) 33 | 34 | # 適切なファイルパスを取得 35 | appropriate_file_path = get_appropriate_file_path() 36 | 37 | # Implementation of CLIPTextModel transformer 38 | 39 | # using https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py as reference 40 | # written by Forge 41 | 42 | 43 | import os 44 | 45 | from transformers import CLIPTokenizer 46 | import ldm_patched.modules.ops 47 | import torch 48 | import traceback 49 | import zipfile 50 | from . import model_management 51 | import ldm_patched.modules.clip_model 52 | import json 53 | from transformers import CLIPTextModel, CLIPTextConfig, modeling_utils 54 | 55 | 56 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 57 | # This function is only for reference, and not used in the backend or runtime. 58 | def gen_empty_tokens(special_tokens, length): 59 | start_token = special_tokens.get("start", None) 60 | end_token = special_tokens.get("end", None) 61 | pad_token = special_tokens.get("pad") 62 | output = [] 63 | if start_token is not None: 64 | output.append(start_token) 65 | if end_token is not None: 66 | output.append(end_token) 67 | output += [pad_token] * (length - len(output)) 68 | return output 69 | 70 | class ClipTokenWeightEncoder: 71 | 72 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 73 | # This function is only for reference, and not used in the backend or runtime. 74 | def encode_token_weights(self, token_weight_pairs): 75 | to_encode = list() 76 | max_token_len = 0 77 | has_weights = False 78 | for x in token_weight_pairs: 79 | tokens = list(map(lambda a: a[0], x)) 80 | max_token_len = max(len(tokens), max_token_len) 81 | has_weights = has_weights or not all(map(lambda a: a[1] == 1.0, x)) 82 | to_encode.append(tokens) 83 | 84 | sections = len(to_encode) 85 | if has_weights or sections == 0: 86 | to_encode.append(gen_empty_tokens(self.special_tokens, max_token_len)) 87 | 88 | out, pooled = self.encode(to_encode) 89 | if pooled is not None: 90 | first_pooled = pooled[0:1].to(model_management.intermediate_device()) 91 | else: 92 | first_pooled = pooled 93 | 94 | output = [] 95 | for k in range(0, sections): 96 | z = out[k:k+1] 97 | if has_weights: 98 | z_empty = out[-1] 99 | for i in range(len(z)): 100 | for j in range(len(z[i])): 101 | weight = token_weight_pairs[k][j][1] 102 | if weight != 1.0: 103 | z[i][j] = (z[i][j] - z_empty[j]) * weight + z_empty[j] 104 | output.append(z) 105 | 106 | if (len(output) == 0): 107 | return out[-1:].to(model_management.intermediate_device()), first_pooled 108 | return torch.cat(output, dim=-2).to(model_management.intermediate_device()), first_pooled 109 | 110 | class SDClipModel(torch.nn.Module, ClipTokenWeightEncoder): 111 | """Uses the CLIP transformer encoder for text (from huggingface)""" 112 | LAYERS = [ 113 | "last", 114 | "pooled", 115 | "hidden" 116 | ] 117 | def __init__(self, version="openai/clip-vit-large-patch14", device="cpu", max_length=77, 118 | freeze=True, layer="last", layer_idx=None, textmodel_json_config=None, dtype=None, model_class=ldm_patched.modules.clip_model.CLIPTextModel, 119 | special_tokens={"start": 49406, "end": 49407, "pad": 49407}, layer_norm_hidden_state=True): # clip-vit-base-patch32 120 | super().__init__() 121 | assert layer in self.LAYERS 122 | 123 | if textmodel_json_config is None: 124 | textmodel_json_config = os.path.join(appropriate_file_path, "sd1_clip_config.json") 125 | 126 | config = CLIPTextConfig.from_json_file(textmodel_json_config) 127 | self.num_layers = config.num_hidden_layers 128 | 129 | with ldm_patched.modules.ops.use_patched_ops(ldm_patched.modules.ops.manual_cast): 130 | with modeling_utils.no_init_weights(): 131 | self.transformer = CLIPTextModel(config) 132 | 133 | if dtype is not None: 134 | self.transformer.to(dtype) 135 | 136 | self.transformer.text_model.embeddings.to(torch.float32) 137 | 138 | self.max_length = max_length 139 | if freeze: 140 | self.freeze() 141 | self.layer = layer 142 | self.layer_idx = None 143 | self.special_tokens = special_tokens 144 | self.text_projection = torch.nn.Parameter(torch.eye(self.transformer.get_input_embeddings().weight.shape[1])) 145 | self.logit_scale = torch.nn.Parameter(torch.tensor(4.6055)) 146 | self.enable_attention_masks = False 147 | 148 | self.layer_norm_hidden_state = layer_norm_hidden_state 149 | if layer == "hidden": 150 | assert layer_idx is not None 151 | assert abs(layer_idx) < self.num_layers 152 | self.clip_layer(layer_idx) 153 | self.layer_default = (self.layer, self.layer_idx) 154 | 155 | def freeze(self): 156 | self.transformer = self.transformer.eval() 157 | #self.train = disabled_train 158 | for param in self.parameters(): 159 | param.requires_grad = False 160 | 161 | def clip_layer(self, layer_idx): 162 | if abs(layer_idx) > self.num_layers: 163 | self.layer = "last" 164 | else: 165 | self.layer = "hidden" 166 | self.layer_idx = layer_idx 167 | 168 | def reset_clip_layer(self): 169 | self.layer = self.layer_default[0] 170 | self.layer_idx = self.layer_default[1] 171 | 172 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 173 | # This function is only for reference, and not used in the backend or runtime. 174 | def set_up_textual_embeddings(self, tokens, current_embeds): 175 | out_tokens = [] 176 | next_new_token = token_dict_size = current_embeds.weight.shape[0] - 1 177 | embedding_weights = [] 178 | 179 | for x in tokens: 180 | tokens_temp = [] 181 | for y in x: 182 | if isinstance(y, int): 183 | if y == token_dict_size: #EOS token 184 | y = -1 185 | tokens_temp += [y] 186 | else: 187 | if y.shape[0] == current_embeds.weight.shape[1]: 188 | embedding_weights += [y] 189 | tokens_temp += [next_new_token] 190 | next_new_token += 1 191 | else: 192 | print("WARNING: shape mismatch when trying to apply embedding, embedding will be ignored", y.shape[0], current_embeds.weight.shape[1]) 193 | while len(tokens_temp) < len(x): 194 | tokens_temp += [self.special_tokens["pad"]] 195 | out_tokens += [tokens_temp] 196 | 197 | n = token_dict_size 198 | if len(embedding_weights) > 0: 199 | new_embedding = torch.nn.Embedding(next_new_token + 1, current_embeds.weight.shape[1], device=current_embeds.weight.device, dtype=current_embeds.weight.dtype) 200 | new_embedding.weight[:token_dict_size] = current_embeds.weight[:-1] 201 | for x in embedding_weights: 202 | new_embedding.weight[n] = x 203 | n += 1 204 | new_embedding.weight[n] = current_embeds.weight[-1] #EOS embedding 205 | self.transformer.set_input_embeddings(new_embedding) 206 | 207 | processed_tokens = [] 208 | for x in out_tokens: 209 | processed_tokens += [list(map(lambda a: n if a == -1 else a, x))] #The EOS token should always be the largest one 210 | 211 | return processed_tokens 212 | 213 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 214 | # This function is only for reference, and not used in the backend or runtime. 215 | def forward(self, tokens): 216 | backup_embeds = self.transformer.get_input_embeddings() 217 | device = backup_embeds.weight.device 218 | tokens = self.set_up_textual_embeddings(tokens, backup_embeds) 219 | tokens = torch.LongTensor(tokens).to(device) 220 | 221 | attention_mask = None 222 | if self.enable_attention_masks: 223 | attention_mask = torch.zeros_like(tokens) 224 | max_token = self.transformer.get_input_embeddings().weight.shape[0] - 1 225 | for x in range(attention_mask.shape[0]): 226 | for y in range(attention_mask.shape[1]): 227 | attention_mask[x, y] = 1 228 | if tokens[x, y] == max_token: 229 | break 230 | 231 | outputs = self.transformer(input_ids=tokens, attention_mask=attention_mask, 232 | output_hidden_states=self.layer == "hidden") 233 | self.transformer.set_input_embeddings(backup_embeds) 234 | 235 | if self.layer == "last": 236 | z = outputs.last_hidden_state 237 | elif self.layer == "pooled": 238 | z = outputs.pooler_output[:, None, :] 239 | else: 240 | z = outputs.hidden_states[self.layer_idx] 241 | if self.layer_norm_hidden_state: 242 | z = self.transformer.text_model.final_layer_norm(z) 243 | 244 | if hasattr(outputs, "pooler_output"): 245 | pooled_output = outputs.pooler_output.float() 246 | else: 247 | pooled_output = None 248 | 249 | if self.text_projection is not None and pooled_output is not None: 250 | pooled_output = pooled_output.float().to(self.text_projection.device) @ self.text_projection.float() 251 | return z.float(), pooled_output 252 | 253 | def encode(self, tokens): 254 | return self(tokens) 255 | 256 | def load_sd(self, sd): 257 | if "text_projection" in sd: 258 | self.text_projection[:] = sd.pop("text_projection") 259 | if "text_projection.weight" in sd: 260 | self.text_projection[:] = sd.pop("text_projection.weight").transpose(0, 1) 261 | return self.transformer.load_state_dict(sd, strict=False) 262 | 263 | 264 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 265 | # This function is only for reference, and not used in the backend or runtime. 266 | def parse_parentheses(string): 267 | result = [] 268 | current_item = "" 269 | nesting_level = 0 270 | for char in string: 271 | if char == "(": 272 | if nesting_level == 0: 273 | if current_item: 274 | result.append(current_item) 275 | current_item = "(" 276 | else: 277 | current_item = "(" 278 | else: 279 | current_item += char 280 | nesting_level += 1 281 | elif char == ")": 282 | nesting_level -= 1 283 | if nesting_level == 0: 284 | result.append(current_item + ")") 285 | current_item = "" 286 | else: 287 | current_item += char 288 | else: 289 | current_item += char 290 | if current_item: 291 | result.append(current_item) 292 | return result 293 | 294 | 295 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 296 | # This function is only for reference, and not used in the backend or runtime. 297 | def token_weights(string, current_weight): 298 | a = parse_parentheses(string) 299 | out = [] 300 | for x in a: 301 | weight = current_weight 302 | if len(x) >= 2 and x[-1] == ')' and x[0] == '(': 303 | x = x[1:-1] 304 | xx = x.rfind(":") 305 | weight *= 1.1 306 | if xx > 0: 307 | try: 308 | weight = float(x[xx+1:]) 309 | x = x[:xx] 310 | except: 311 | pass 312 | out += token_weights(x, weight) 313 | else: 314 | out += [(x, current_weight)] 315 | return out 316 | 317 | 318 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 319 | # This function is only for reference, and not used in the backend or runtime. 320 | def escape_important(text): 321 | text = text.replace("\\)", "\0\1") 322 | text = text.replace("\\(", "\0\2") 323 | return text 324 | 325 | 326 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 327 | # This function is only for reference, and not used in the backend or runtime. 328 | def unescape_important(text): 329 | text = text.replace("\0\1", ")") 330 | text = text.replace("\0\2", "(") 331 | return text 332 | 333 | 334 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 335 | # This function is only for reference, and not used in the backend or runtime. 336 | def safe_load_embed_zip(embed_path): 337 | with zipfile.ZipFile(embed_path) as myzip: 338 | names = list(filter(lambda a: "data/" in a, myzip.namelist())) 339 | names.reverse() 340 | for n in names: 341 | with myzip.open(n) as myfile: 342 | data = myfile.read() 343 | number = len(data) // 4 344 | length_embed = 1024 #sd2.x 345 | if number < 768: 346 | continue 347 | if number % 768 == 0: 348 | length_embed = 768 #sd1.x 349 | num_embeds = number // length_embed 350 | embed = torch.frombuffer(data, dtype=torch.float) 351 | out = embed.reshape((num_embeds, length_embed)).clone() 352 | del embed 353 | return out 354 | 355 | 356 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 357 | # This function is only for reference, and not used in the backend or runtime. 358 | def expand_directory_list(directories): 359 | dirs = set() 360 | for x in directories: 361 | dirs.add(x) 362 | for root, subdir, file in os.walk(x, followlinks=True): 363 | dirs.add(root) 364 | return list(dirs) 365 | 366 | 367 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 368 | # This function is only for reference, and not used in the backend or runtime. 369 | def load_embed(embedding_name, embedding_directory, embedding_size, embed_key=None): 370 | if isinstance(embedding_directory, str): 371 | embedding_directory = [embedding_directory] 372 | 373 | embedding_directory = expand_directory_list(embedding_directory) 374 | 375 | valid_file = None 376 | for embed_dir in embedding_directory: 377 | embed_path = os.path.abspath(os.path.join(embed_dir, embedding_name)) 378 | embed_dir = os.path.abspath(embed_dir) 379 | try: 380 | if os.path.commonpath((embed_dir, embed_path)) != embed_dir: 381 | continue 382 | except: 383 | continue 384 | if not os.path.isfile(embed_path): 385 | extensions = ['.safetensors', '.pt', '.bin'] 386 | for x in extensions: 387 | t = embed_path + x 388 | if os.path.isfile(t): 389 | valid_file = t 390 | break 391 | else: 392 | valid_file = embed_path 393 | if valid_file is not None: 394 | break 395 | 396 | if valid_file is None: 397 | return None 398 | 399 | embed_path = valid_file 400 | 401 | embed_out = None 402 | 403 | try: 404 | if embed_path.lower().endswith(".safetensors"): 405 | import safetensors.torch 406 | embed = safetensors.torch.load_file(embed_path, device="cpu") 407 | else: 408 | if 'weights_only' in torch.load.__code__.co_varnames: 409 | try: 410 | embed = torch.load(embed_path, weights_only=True, map_location="cpu") 411 | except: 412 | embed_out = safe_load_embed_zip(embed_path) 413 | else: 414 | embed = torch.load(embed_path, map_location="cpu") 415 | except Exception as e: 416 | print(traceback.format_exc()) 417 | print() 418 | print("error loading embedding, skipping loading:", embedding_name) 419 | return None 420 | 421 | if embed_out is None: 422 | if 'string_to_param' in embed: 423 | values = embed['string_to_param'].values() 424 | embed_out = next(iter(values)) 425 | elif isinstance(embed, list): 426 | out_list = [] 427 | for x in range(len(embed)): 428 | for k in embed[x]: 429 | t = embed[x][k] 430 | if t.shape[-1] != embedding_size: 431 | continue 432 | out_list.append(t.reshape(-1, t.shape[-1])) 433 | embed_out = torch.cat(out_list, dim=0) 434 | elif embed_key is not None and embed_key in embed: 435 | embed_out = embed[embed_key] 436 | else: 437 | values = embed.values() 438 | embed_out = next(iter(values)) 439 | return embed_out 440 | 441 | class SDTokenizer: 442 | def __init__(self, tokenizer_path=None, max_length=77, pad_with_end=True, embedding_directory=None, embedding_size=768, embedding_key='clip_l', tokenizer_class=CLIPTokenizer, has_start_token=True, pad_to_max_length=True): 443 | if tokenizer_path is None: 444 | tokenizer_path = os.path.join(appropriate_file_path, "sd1_tokenizer") 445 | self.tokenizer = tokenizer_class.from_pretrained(tokenizer_path) 446 | self.max_length = max_length 447 | 448 | empty = self.tokenizer('')["input_ids"] 449 | if has_start_token: 450 | self.tokens_start = 1 451 | self.start_token = empty[0] 452 | self.end_token = empty[1] 453 | else: 454 | self.tokens_start = 0 455 | self.start_token = None 456 | self.end_token = empty[0] 457 | self.pad_with_end = pad_with_end 458 | self.pad_to_max_length = pad_to_max_length 459 | 460 | vocab = self.tokenizer.get_vocab() 461 | self.inv_vocab = {v: k for k, v in vocab.items()} 462 | self.embedding_directory = embedding_directory 463 | self.max_word_length = 8 464 | self.embedding_identifier = "embedding:" 465 | self.embedding_size = embedding_size 466 | self.embedding_key = embedding_key 467 | 468 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 469 | # This function is only for reference, and not used in the backend or runtime. 470 | def _try_get_embedding(self, embedding_name:str): 471 | ''' 472 | Takes a potential embedding name and tries to retrieve it. 473 | Returns a Tuple consisting of the embedding and any leftover string, embedding can be None. 474 | ''' 475 | embed = load_embed(embedding_name, self.embedding_directory, self.embedding_size, self.embedding_key) 476 | if embed is None: 477 | stripped = embedding_name.strip(',') 478 | if len(stripped) < len(embedding_name): 479 | embed = load_embed(stripped, self.embedding_directory, self.embedding_size, self.embedding_key) 480 | return (embed, embedding_name[len(stripped):]) 481 | return (embed, "") 482 | 483 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 484 | # This function is only for reference, and not used in the backend or runtime. 485 | def tokenize_with_weights(self, text:str, return_word_ids=False): 486 | ''' 487 | Takes a prompt and converts it to a list of (token, weight, word id) elements. 488 | Tokens can both be integer tokens and pre computed CLIP tensors. 489 | Word id values are unique per word and embedding, where the id 0 is reserved for non word tokens. 490 | Returned list has the dimensions NxM where M is the input size of CLIP 491 | ''' 492 | if self.pad_with_end: 493 | pad_token = self.end_token 494 | else: 495 | pad_token = 0 496 | 497 | text = escape_important(text) 498 | parsed_weights = token_weights(text, 1.0) 499 | 500 | #tokenize words 501 | tokens = [] 502 | for weighted_segment, weight in parsed_weights: 503 | to_tokenize = unescape_important(weighted_segment).replace("\n", " ").split(' ') 504 | to_tokenize = [x for x in to_tokenize if x != ""] 505 | for word in to_tokenize: 506 | #if we find an embedding, deal with the embedding 507 | if word.startswith(self.embedding_identifier) and self.embedding_directory is not None: 508 | embedding_name = word[len(self.embedding_identifier):].strip('\n') 509 | embed, leftover = self._try_get_embedding(embedding_name) 510 | if embed is None: 511 | print(f"warning, embedding:{embedding_name} does not exist, ignoring") 512 | else: 513 | if len(embed.shape) == 1: 514 | tokens.append([(embed, weight)]) 515 | else: 516 | tokens.append([(embed[x], weight) for x in range(embed.shape[0])]) 517 | #if we accidentally have leftover text, continue parsing using leftover, else move on to next word 518 | if leftover != "": 519 | word = leftover 520 | else: 521 | continue 522 | #parse word 523 | tokens.append([(t, weight) for t in self.tokenizer(word)["input_ids"][self.tokens_start:-1]]) 524 | 525 | #reshape token array to CLIP input size 526 | batched_tokens = [] 527 | batch = [] 528 | if self.start_token is not None: 529 | batch.append((self.start_token, 1.0, 0)) 530 | batched_tokens.append(batch) 531 | for i, t_group in enumerate(tokens): 532 | #determine if we're going to try and keep the tokens in a single batch 533 | is_large = len(t_group) >= self.max_word_length 534 | 535 | while len(t_group) > 0: 536 | if len(t_group) + len(batch) > self.max_length - 1: 537 | remaining_length = self.max_length - len(batch) - 1 538 | #break word in two and add end token 539 | if is_large: 540 | batch.extend([(t,w,i+1) for t,w in t_group[:remaining_length]]) 541 | batch.append((self.end_token, 1.0, 0)) 542 | t_group = t_group[remaining_length:] 543 | #add end token and pad 544 | else: 545 | batch.append((self.end_token, 1.0, 0)) 546 | if self.pad_to_max_length: 547 | batch.extend([(pad_token, 1.0, 0)] * (remaining_length)) 548 | #start new batch 549 | batch = [] 550 | if self.start_token is not None: 551 | batch.append((self.start_token, 1.0, 0)) 552 | batched_tokens.append(batch) 553 | else: 554 | batch.extend([(t,w,i+1) for t,w in t_group]) 555 | t_group = [] 556 | 557 | #fill last batch 558 | batch.append((self.end_token, 1.0, 0)) 559 | if self.pad_to_max_length: 560 | batch.extend([(pad_token, 1.0, 0)] * (self.max_length - len(batch))) 561 | 562 | if not return_word_ids: 563 | batched_tokens = [[(t, w) for t, w,_ in x] for x in batched_tokens] 564 | 565 | return batched_tokens 566 | 567 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 568 | # This function is only for reference, and not used in the backend or runtime. 569 | def untokenize(self, token_weight_pair): 570 | return list(map(lambda a: (a, self.inv_vocab[a[0]]), token_weight_pair)) 571 | 572 | 573 | class SD1Tokenizer: 574 | def __init__(self, embedding_directory=None, clip_name="l", tokenizer=SDTokenizer): 575 | self.clip_name = clip_name 576 | self.clip = "clip_{}".format(self.clip_name) 577 | setattr(self, self.clip, tokenizer(embedding_directory=embedding_directory)) 578 | 579 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 580 | # This function is only for reference, and not used in the backend or runtime. 581 | def tokenize_with_weights(self, text:str, return_word_ids=False): 582 | out = {} 583 | out[self.clip_name] = getattr(self, self.clip).tokenize_with_weights(text, return_word_ids) 584 | return out 585 | 586 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 587 | # This function is only for reference, and not used in the backend or runtime. 588 | def untokenize(self, token_weight_pair): 589 | return getattr(self, self.clip).untokenize(token_weight_pair) 590 | 591 | 592 | class SD1ClipModel(torch.nn.Module): 593 | def __init__(self, device="cpu", dtype=None, clip_name="l", clip_model=SDClipModel, **kwargs): 594 | super().__init__() 595 | self.clip_name = clip_name 596 | self.clip = "clip_{}".format(self.clip_name) 597 | setattr(self, self.clip, clip_model(device=device, dtype=dtype, **kwargs)) 598 | 599 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 600 | # This function is only for reference, and not used in the backend or runtime. 601 | def clip_layer(self, layer_idx): 602 | getattr(self, self.clip).clip_layer(layer_idx) 603 | 604 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 605 | # This function is only for reference, and not used in the backend or runtime. 606 | def reset_clip_layer(self): 607 | getattr(self, self.clip).reset_clip_layer() 608 | 609 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 610 | # This function is only for reference, and not used in the backend or runtime. 611 | def encode_token_weights(self, token_weight_pairs): 612 | token_weight_pairs = token_weight_pairs[self.clip_name] 613 | out, pooled = getattr(self, self.clip).encode_token_weights(token_weight_pairs) 614 | return out, pooled 615 | 616 | # Taken from https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/sd1_clip.py 617 | # This function is only for reference, and not used in the backend or runtime. 618 | def load_sd(self, sd): 619 | return getattr(self, self.clip).load_sd(sd) 620 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (c) 2023 AUTOMATIC1111 5 | Copyright (c) 2024 tori29umai 6 | 7 | Copyright (C) 2007 Free Software Foundation, Inc. 8 | Everyone is permitted to copy and distribute verbatim copies 9 | of this license document, but changing it is not allowed. 10 | 11 | Preamble 12 | 13 | The GNU Affero General Public License is a free, copyleft license for 14 | software and other kinds of works, specifically designed to ensure 15 | cooperation with the community in the case of network server software. 16 | 17 | The licenses for most software and other practical works are designed 18 | to take away your freedom to share and change the works. By contrast, 19 | our General Public Licenses are intended to guarantee your freedom to 20 | share and change all versions of a program--to make sure it remains free 21 | software for all its users. 22 | 23 | When we speak of free software, we are referring to freedom, not 24 | price. Our General Public Licenses are designed to make sure that you 25 | have the freedom to distribute copies of free software (and charge for 26 | them if you wish), that you receive source code or can get it if you 27 | want it, that you can change the software or use pieces of it in new 28 | free programs, and that you know you can do these things. 29 | 30 | Developers that use our General Public Licenses protect your rights 31 | with two steps: (1) assert copyright on the software, and (2) offer 32 | you this License which gives you legal permission to copy, distribute 33 | and/or modify the software. 34 | 35 | A secondary benefit of defending all users' freedom is that 36 | improvements made in alternate versions of the program, if they 37 | receive widespread use, become available for other developers to 38 | incorporate. Many developers of free software are heartened and 39 | encouraged by the resulting cooperation. However, in the case of 40 | software used on network servers, this result may fail to come about. 41 | The GNU General Public License permits making a modified version and 42 | letting the public access it on a server without ever releasing its 43 | source code to the public. 44 | 45 | The GNU Affero General Public License is designed specifically to 46 | ensure that, in such cases, the modified source code becomes available 47 | to the community. It requires the operator of a network server to 48 | provide the source code of the modified version running there to the 49 | users of that server. Therefore, public use of a modified version, on 50 | a publicly accessible server, gives the public access to the source 51 | code of the modified version. 52 | 53 | An older license, called the Affero General Public License and 54 | published by Affero, was designed to accomplish similar goals. This is 55 | a different license, not a version of the Affero GPL, but Affero has 56 | released a new version of the Affero GPL which permits relicensing under 57 | this license. 58 | 59 | The precise terms and conditions for copying, distribution and 60 | modification follow. 61 | 62 | TERMS AND CONDITIONS 63 | 64 | 0. Definitions. 65 | 66 | "This License" refers to version 3 of the GNU Affero General Public License. 67 | 68 | "Copyright" also means copyright-like laws that apply to other kinds of 69 | works, such as semiconductor masks. 70 | 71 | "The Program" refers to any copyrightable work licensed under this 72 | License. Each licensee is addressed as "you". "Licensees" and 73 | "recipients" may be individuals or organizations. 74 | 75 | To "modify" a work means to copy from or adapt all or part of the work 76 | in a fashion requiring copyright permission, other than the making of an 77 | exact copy. The resulting work is called a "modified version" of the 78 | earlier work or a work "based on" the earlier work. 79 | 80 | A "covered work" means either the unmodified Program or a work based 81 | on the Program. 82 | 83 | To "propagate" a work means to do anything with it that, without 84 | permission, would make you directly or secondarily liable for 85 | infringement under applicable copyright law, except executing it on a 86 | computer or modifying a private copy. Propagation includes copying, 87 | distribution (with or without modification), making available to the 88 | public, and in some countries other activities as well. 89 | 90 | To "convey" a work means any kind of propagation that enables other 91 | parties to make or receive copies. Mere interaction with a user through 92 | a computer network, with no transfer of a copy, is not conveying. 93 | 94 | An interactive user interface displays "Appropriate Legal Notices" 95 | to the extent that it includes a convenient and prominently visible 96 | feature that (1) displays an appropriate copyright notice, and (2) 97 | tells the user that there is no warranty for the work (except to the 98 | extent that warranties are provided), that licensees may convey the 99 | work under this License, and how to view a copy of this License. If 100 | the interface presents a list of user commands or options, such as a 101 | menu, a prominent item in the list meets this criterion. 102 | 103 | 1. Source Code. 104 | 105 | The "source code" for a work means the preferred form of the work 106 | for making modifications to it. "Object code" means any non-source 107 | form of a work. 108 | 109 | A "Standard Interface" means an interface that either is an official 110 | standard defined by a recognized standards body, or, in the case of 111 | interfaces specified for a particular programming language, one that 112 | is widely used among developers working in that language. 113 | 114 | The "System Libraries" of an executable work include anything, other 115 | than the work as a whole, that (a) is included in the normal form of 116 | packaging a Major Component, but which is not part of that Major 117 | Component, and (b) serves only to enable use of the work with that 118 | Major Component, or to implement a Standard Interface for which an 119 | implementation is available to the public in source code form. A 120 | "Major Component", in this context, means a major essential component 121 | (kernel, window system, and so on) of the specific operating system 122 | (if any) on which the executable work runs, or a compiler used to 123 | produce the work, or an object code interpreter used to run it. 124 | 125 | The "Corresponding Source" for a work in object code form means all 126 | the source code needed to generate, install, and (for an executable 127 | work) run the object code and to modify the work, including scripts to 128 | control those activities. However, it does not include the work's 129 | System Libraries, or general-purpose tools or generally available free 130 | programs which are used unmodified in performing those activities but 131 | which are not part of the work. For example, Corresponding Source 132 | includes interface definition files associated with source files for 133 | the work, and the source code for shared libraries and dynamically 134 | linked subprograms that the work is specifically designed to require, 135 | such as by intimate data communication or control flow between those 136 | subprograms and other parts of the work. 137 | 138 | The Corresponding Source need not include anything that users 139 | can regenerate automatically from other parts of the Corresponding 140 | Source. 141 | 142 | The Corresponding Source for a work in source code form is that 143 | same work. 144 | 145 | 2. Basic Permissions. 146 | 147 | All rights granted under this License are granted for the term of 148 | copyright on the Program, and are irrevocable provided the stated 149 | conditions are met. This License explicitly affirms your unlimited 150 | permission to run the unmodified Program. The output from running a 151 | covered work is covered by this License only if the output, given its 152 | content, constitutes a covered work. This License acknowledges your 153 | rights of fair use or other equivalent, as provided by copyright law. 154 | 155 | You may make, run and propagate covered works that you do not 156 | convey, without conditions so long as your license otherwise remains 157 | in force. You may convey covered works to others for the sole purpose 158 | of having them make modifications exclusively for you, or provide you 159 | with facilities for running those works, provided that you comply with 160 | the terms of this License in conveying all material for which you do 161 | not control copyright. Those thus making or running the covered works 162 | for you must do so exclusively on your behalf, under your direction 163 | and control, on terms that prohibit them from making any copies of 164 | your copyrighted material outside their relationship with you. 165 | 166 | Conveying under any other circumstances is permitted solely under 167 | the conditions stated below. Sublicensing is not allowed; section 10 168 | makes it unnecessary. 169 | 170 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 171 | 172 | No covered work shall be deemed part of an effective technological 173 | measure under any applicable law fulfilling obligations under article 174 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 175 | similar laws prohibiting or restricting circumvention of such 176 | measures. 177 | 178 | When you convey a covered work, you waive any legal power to forbid 179 | circumvention of technological measures to the extent such circumvention 180 | is effected by exercising rights under this License with respect to 181 | the covered work, and you disclaim any intention to limit operation or 182 | modification of the work as a means of enforcing, against the work's 183 | users, your or third parties' legal rights to forbid circumvention of 184 | technological measures. 185 | 186 | 4. Conveying Verbatim Copies. 187 | 188 | You may convey verbatim copies of the Program's source code as you 189 | receive it, in any medium, provided that you conspicuously and 190 | appropriately publish on each copy an appropriate copyright notice; 191 | keep intact all notices stating that this License and any 192 | non-permissive terms added in accord with section 7 apply to the code; 193 | keep intact all notices of the absence of any warranty; and give all 194 | recipients a copy of this License along with the Program. 195 | 196 | You may charge any price or no price for each copy that you convey, 197 | and you may offer support or warranty protection for a fee. 198 | 199 | 5. Conveying Modified Source Versions. 200 | 201 | You may convey a work based on the Program, or the modifications to 202 | produce it from the Program, in the form of source code under the 203 | terms of section 4, provided that you also meet all of these conditions: 204 | 205 | a) The work must carry prominent notices stating that you modified 206 | it, and giving a relevant date. 207 | 208 | b) The work must carry prominent notices stating that it is 209 | released under this License and any conditions added under section 210 | 7. This requirement modifies the requirement in section 4 to 211 | "keep intact all notices". 212 | 213 | c) You must license the entire work, as a whole, under this 214 | License to anyone who comes into possession of a copy. This 215 | License will therefore apply, along with any applicable section 7 216 | additional terms, to the whole of the work, and all its parts, 217 | regardless of how they are packaged. This License gives no 218 | permission to license the work in any other way, but it does not 219 | invalidate such permission if you have separately received it. 220 | 221 | d) If the work has interactive user interfaces, each must display 222 | Appropriate Legal Notices; however, if the Program has interactive 223 | interfaces that do not display Appropriate Legal Notices, your 224 | work need not make them do so. 225 | 226 | A compilation of a covered work with other separate and independent 227 | works, which are not by their nature extensions of the covered work, 228 | and which are not combined with it such as to form a larger program, 229 | in or on a volume of a storage or distribution medium, is called an 230 | "aggregate" if the compilation and its resulting copyright are not 231 | used to limit the access or legal rights of the compilation's users 232 | beyond what the individual works permit. Inclusion of a covered work 233 | in an aggregate does not cause this License to apply to the other 234 | parts of the aggregate. 235 | 236 | 6. Conveying Non-Source Forms. 237 | 238 | You may convey a covered work in object code form under the terms 239 | of sections 4 and 5, provided that you also convey the 240 | machine-readable Corresponding Source under the terms of this License, 241 | in one of these ways: 242 | 243 | a) Convey the object code in, or embodied in, a physical product 244 | (including a physical distribution medium), accompanied by the 245 | Corresponding Source fixed on a durable physical medium 246 | customarily used for software interchange. 247 | 248 | b) Convey the object code in, or embodied in, a physical product 249 | (including a physical distribution medium), accompanied by a 250 | written offer, valid for at least three years and valid for as 251 | long as you offer spare parts or customer support for that product 252 | model, to give anyone who possesses the object code either (1) a 253 | copy of the Corresponding Source for all the software in the 254 | product that is covered by this License, on a durable physical 255 | medium customarily used for software interchange, for a price no 256 | more than your reasonable cost of physically performing this 257 | conveying of source, or (2) access to copy the 258 | Corresponding Source from a network server at no charge. 259 | 260 | c) Convey individual copies of the object code with a copy of the 261 | written offer to provide the Corresponding Source. This 262 | alternative is allowed only occasionally and noncommercially, and 263 | only if you received the object code with such an offer, in accord 264 | with subsection 6b. 265 | 266 | d) Convey the object code by offering access from a designated 267 | place (gratis or for a charge), and offer equivalent access to the 268 | Corresponding Source in the same way through the same place at no 269 | further charge. You need not require recipients to copy the 270 | Corresponding Source along with the object code. If the place to 271 | copy the object code is a network server, the Corresponding Source 272 | may be on a different server (operated by you or a third party) 273 | that supports equivalent copying facilities, provided you maintain 274 | clear directions next to the object code saying where to find the 275 | Corresponding Source. Regardless of what server hosts the 276 | Corresponding Source, you remain obligated to ensure that it is 277 | available for as long as needed to satisfy these requirements. 278 | 279 | e) Convey the object code using peer-to-peer transmission, provided 280 | you inform other peers where the object code and Corresponding 281 | Source of the work are being offered to the general public at no 282 | charge under subsection 6d. 283 | 284 | A separable portion of the object code, whose source code is excluded 285 | from the Corresponding Source as a System Library, need not be 286 | included in conveying the object code work. 287 | 288 | A "User Product" is either (1) a "consumer product", which means any 289 | tangible personal property which is normally used for personal, family, 290 | or household purposes, or (2) anything designed or sold for incorporation 291 | into a dwelling. In determining whether a product is a consumer product, 292 | doubtful cases shall be resolved in favor of coverage. For a particular 293 | product received by a particular user, "normally used" refers to a 294 | typical or common use of that class of product, regardless of the status 295 | of the particular user or of the way in which the particular user 296 | actually uses, or expects or is expected to use, the product. A product 297 | is a consumer product regardless of whether the product has substantial 298 | commercial, industrial or non-consumer uses, unless such uses represent 299 | the only significant mode of use of the product. 300 | 301 | "Installation Information" for a User Product means any methods, 302 | procedures, authorization keys, or other information required to install 303 | and execute modified versions of a covered work in that User Product from 304 | a modified version of its Corresponding Source. The information must 305 | suffice to ensure that the continued functioning of the modified object 306 | code is in no case prevented or interfered with solely because 307 | modification has been made. 308 | 309 | If you convey an object code work under this section in, or with, or 310 | specifically for use in, a User Product, and the conveying occurs as 311 | part of a transaction in which the right of possession and use of the 312 | User Product is transferred to the recipient in perpetuity or for a 313 | fixed term (regardless of how the transaction is characterized), the 314 | Corresponding Source conveyed under this section must be accompanied 315 | by the Installation Information. But this requirement does not apply 316 | if neither you nor any third party retains the ability to install 317 | modified object code on the User Product (for example, the work has 318 | been installed in ROM). 319 | 320 | The requirement to provide Installation Information does not include a 321 | requirement to continue to provide support service, warranty, or updates 322 | for a work that has been modified or installed by the recipient, or for 323 | the User Product in which it has been modified or installed. Access to a 324 | network may be denied when the modification itself materially and 325 | adversely affects the operation of the network or violates the rules and 326 | protocols for communication across the network. 327 | 328 | Corresponding Source conveyed, and Installation Information provided, 329 | in accord with this section must be in a format that is publicly 330 | documented (and with an implementation available to the public in 331 | source code form), and must require no special password or key for 332 | unpacking, reading or copying. 333 | 334 | 7. Additional Terms. 335 | 336 | "Additional permissions" are terms that supplement the terms of this 337 | License by making exceptions from one or more of its conditions. 338 | Additional permissions that are applicable to the entire Program shall 339 | be treated as though they were included in this License, to the extent 340 | that they are valid under applicable law. If additional permissions 341 | apply only to part of the Program, that part may be used separately 342 | under those permissions, but the entire Program remains governed by 343 | this License without regard to the additional permissions. 344 | 345 | When you convey a copy of a covered work, you may at your option 346 | remove any additional permissions from that copy, or from any part of 347 | it. (Additional permissions may be written to require their own 348 | removal in certain cases when you modify the work.) You may place 349 | additional permissions on material, added by you to a covered work, 350 | for which you have or can give appropriate copyright permission. 351 | 352 | Notwithstanding any other provision of this License, for material you 353 | add to a covered work, you may (if authorized by the copyright holders of 354 | that material) supplement the terms of this License with terms: 355 | 356 | a) Disclaiming warranty or limiting liability differently from the 357 | terms of sections 15 and 16 of this License; or 358 | 359 | b) Requiring preservation of specified reasonable legal notices or 360 | author attributions in that material or in the Appropriate Legal 361 | Notices displayed by works containing it; or 362 | 363 | c) Prohibiting misrepresentation of the origin of that material, or 364 | requiring that modified versions of such material be marked in 365 | reasonable ways as different from the original version; or 366 | 367 | d) Limiting the use for publicity purposes of names of licensors or 368 | authors of the material; or 369 | 370 | e) Declining to grant rights under trademark law for use of some 371 | trade names, trademarks, or service marks; or 372 | 373 | f) Requiring indemnification of licensors and authors of that 374 | material by anyone who conveys the material (or modified versions of 375 | it) with contractual assumptions of liability to the recipient, for 376 | any liability that these contractual assumptions directly impose on 377 | those licensors and authors. 378 | 379 | All other non-permissive additional terms are considered "further 380 | restrictions" within the meaning of section 10. If the Program as you 381 | received it, or any part of it, contains a notice stating that it is 382 | governed by this License along with a term that is a further 383 | restriction, you may remove that term. If a license document contains 384 | a further restriction but permits relicensing or conveying under this 385 | License, you may add to a covered work material governed by the terms 386 | of that license document, provided that the further restriction does 387 | not survive such relicensing or conveying. 388 | 389 | If you add terms to a covered work in accord with this section, you 390 | must place, in the relevant source files, a statement of the 391 | additional terms that apply to those files, or a notice indicating 392 | where to find the applicable terms. 393 | 394 | Additional terms, permissive or non-permissive, may be stated in the 395 | form of a separately written license, or stated as exceptions; 396 | the above requirements apply either way. 397 | 398 | 8. Termination. 399 | 400 | You may not propagate or modify a covered work except as expressly 401 | provided under this License. Any attempt otherwise to propagate or 402 | modify it is void, and will automatically terminate your rights under 403 | this License (including any patent licenses granted under the third 404 | paragraph of section 11). 405 | 406 | However, if you cease all violation of this License, then your 407 | license from a particular copyright holder is reinstated (a) 408 | provisionally, unless and until the copyright holder explicitly and 409 | finally terminates your license, and (b) permanently, if the copyright 410 | holder fails to notify you of the violation by some reasonable means 411 | prior to 60 days after the cessation. 412 | 413 | Moreover, your license from a particular copyright holder is 414 | reinstated permanently if the copyright holder notifies you of the 415 | violation by some reasonable means, this is the first time you have 416 | received notice of violation of this License (for any work) from that 417 | copyright holder, and you cure the violation prior to 30 days after 418 | your receipt of the notice. 419 | 420 | Termination of your rights under this section does not terminate the 421 | licenses of parties who have received copies or rights from you under 422 | this License. If your rights have been terminated and not permanently 423 | reinstated, you do not qualify to receive new licenses for the same 424 | material under section 10. 425 | 426 | 9. Acceptance Not Required for Having Copies. 427 | 428 | You are not required to accept this License in order to receive or 429 | run a copy of the Program. Ancillary propagation of a covered work 430 | occurring solely as a consequence of using peer-to-peer transmission 431 | to receive a copy likewise does not require acceptance. However, 432 | nothing other than this License grants you permission to propagate or 433 | modify any covered work. These actions infringe copyright if you do 434 | not accept this License. Therefore, by modifying or propagating a 435 | covered work, you indicate your acceptance of this License to do so. 436 | 437 | 10. Automatic Licensing of Downstream Recipients. 438 | 439 | Each time you convey a covered work, the recipient automatically 440 | receives a license from the original licensors, to run, modify and 441 | propagate that work, subject to this License. You are not responsible 442 | for enforcing compliance by third parties with this License. 443 | 444 | An "entity transaction" is a transaction transferring control of an 445 | organization, or substantially all assets of one, or subdividing an 446 | organization, or merging organizations. If propagation of a covered 447 | work results from an entity transaction, each party to that 448 | transaction who receives a copy of the work also receives whatever 449 | licenses to the work the party's predecessor in interest had or could 450 | give under the previous paragraph, plus a right to possession of the 451 | Corresponding Source of the work from the predecessor in interest, if 452 | the predecessor has it or can get it with reasonable efforts. 453 | 454 | You may not impose any further restrictions on the exercise of the 455 | rights granted or affirmed under this License. For example, you may 456 | not impose a license fee, royalty, or other charge for exercise of 457 | rights granted under this License, and you may not initiate litigation 458 | (including a cross-claim or counterclaim in a lawsuit) alleging that 459 | any patent claim is infringed by making, using, selling, offering for 460 | sale, or importing the Program or any portion of it. 461 | 462 | 11. Patents. 463 | 464 | A "contributor" is a copyright holder who authorizes use under this 465 | License of the Program or a work on which the Program is based. The 466 | work thus licensed is called the contributor's "contributor version". 467 | 468 | A contributor's "essential patent claims" are all patent claims 469 | owned or controlled by the contributor, whether already acquired or 470 | hereafter acquired, that would be infringed by some manner, permitted 471 | by this License, of making, using, or selling its contributor version, 472 | but do not include claims that would be infringed only as a 473 | consequence of further modification of the contributor version. For 474 | purposes of this definition, "control" includes the right to grant 475 | patent sublicenses in a manner consistent with the requirements of 476 | this License. 477 | 478 | Each contributor grants you a non-exclusive, worldwide, royalty-free 479 | patent license under the contributor's essential patent claims, to 480 | make, use, sell, offer for sale, import and otherwise run, modify and 481 | propagate the contents of its contributor version. 482 | 483 | In the following three paragraphs, a "patent license" is any express 484 | agreement or commitment, however denominated, not to enforce a patent 485 | (such as an express permission to practice a patent or covenant not to 486 | sue for patent infringement). To "grant" such a patent license to a 487 | party means to make such an agreement or commitment not to enforce a 488 | patent against the party. 489 | 490 | If you convey a covered work, knowingly relying on a patent license, 491 | and the Corresponding Source of the work is not available for anyone 492 | to copy, free of charge and under the terms of this License, through a 493 | publicly available network server or other readily accessible means, 494 | then you must either (1) cause the Corresponding Source to be so 495 | available, or (2) arrange to deprive yourself of the benefit of the 496 | patent license for this particular work, or (3) arrange, in a manner 497 | consistent with the requirements of this License, to extend the patent 498 | license to downstream recipients. "Knowingly relying" means you have 499 | actual knowledge that, but for the patent license, your conveying the 500 | covered work in a country, or your recipient's use of the covered work 501 | in a country, would infringe one or more identifiable patents in that 502 | country that you have reason to believe are valid. 503 | 504 | If, pursuant to or in connection with a single transaction or 505 | arrangement, you convey, or propagate by procuring conveyance of, a 506 | covered work, and grant a patent license to some of the parties 507 | receiving the covered work authorizing them to use, propagate, modify 508 | or convey a specific copy of the covered work, then the patent license 509 | you grant is automatically extended to all recipients of the covered 510 | work and works based on it. 511 | 512 | A patent license is "discriminatory" if it does not include within 513 | the scope of its coverage, prohibits the exercise of, or is 514 | conditioned on the non-exercise of one or more of the rights that are 515 | specifically granted under this License. You may not convey a covered 516 | work if you are a party to an arrangement with a third party that is 517 | in the business of distributing software, under which you make payment 518 | to the third party based on the extent of your activity of conveying 519 | the work, and under which the third party grants, to any of the 520 | parties who would receive the covered work from you, a discriminatory 521 | patent license (a) in connection with copies of the covered work 522 | conveyed by you (or copies made from those copies), or (b) primarily 523 | for and in connection with specific products or compilations that 524 | contain the covered work, unless you entered into that arrangement, 525 | or that patent license was granted, prior to 28 March 2007. 526 | 527 | Nothing in this License shall be construed as excluding or limiting 528 | any implied license or other defenses to infringement that may 529 | otherwise be available to you under applicable patent law. 530 | 531 | 12. No Surrender of Others' Freedom. 532 | 533 | If conditions are imposed on you (whether by court order, agreement or 534 | otherwise) that contradict the conditions of this License, they do not 535 | excuse you from the conditions of this License. If you cannot convey a 536 | covered work so as to satisfy simultaneously your obligations under this 537 | License and any other pertinent obligations, then as a consequence you may 538 | not convey it at all. For example, if you agree to terms that obligate you 539 | to collect a royalty for further conveying from those to whom you convey 540 | the Program, the only way you could satisfy both those terms and this 541 | License would be to refrain entirely from conveying the Program. 542 | 543 | 13. Remote Network Interaction; Use with the GNU General Public License. 544 | 545 | Notwithstanding any other provision of this License, if you modify the 546 | Program, your modified version must prominently offer all users 547 | interacting with it remotely through a computer network (if your version 548 | supports such interaction) an opportunity to receive the Corresponding 549 | Source of your version by providing access to the Corresponding Source 550 | from a network server at no charge, through some standard or customary 551 | means of facilitating copying of software. This Corresponding Source 552 | shall include the Corresponding Source for any work covered by version 3 553 | of the GNU General Public License that is incorporated pursuant to the 554 | following paragraph. 555 | 556 | Notwithstanding any other provision of this License, you have 557 | permission to link or combine any covered work with a work licensed 558 | under version 3 of the GNU General Public License into a single 559 | combined work, and to convey the resulting work. The terms of this 560 | License will continue to apply to the part which is the covered work, 561 | but the work with which it is combined will remain governed by version 562 | 3 of the GNU General Public License. 563 | 564 | 14. Revised Versions of this License. 565 | 566 | The Free Software Foundation may publish revised and/or new versions of 567 | the GNU Affero General Public License from time to time. Such new versions 568 | will be similar in spirit to the present version, but may differ in detail to 569 | address new problems or concerns. 570 | 571 | Each version is given a distinguishing version number. If the 572 | Program specifies that a certain numbered version of the GNU Affero General 573 | Public License "or any later version" applies to it, you have the 574 | option of following the terms and conditions either of that numbered 575 | version or of any later version published by the Free Software 576 | Foundation. If the Program does not specify a version number of the 577 | GNU Affero General Public License, you may choose any version ever published 578 | by the Free Software Foundation. 579 | 580 | If the Program specifies that a proxy can decide which future 581 | versions of the GNU Affero General Public License can be used, that proxy's 582 | public statement of acceptance of a version permanently authorizes you 583 | to choose that version for the Program. 584 | 585 | Later license versions may give you additional or different 586 | permissions. However, no additional obligations are imposed on any 587 | author or copyright holder as a result of your choosing to follow a 588 | later version. 589 | 590 | 15. Disclaimer of Warranty. 591 | 592 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 593 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 594 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 595 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 596 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 597 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 598 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 599 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 600 | 601 | 16. Limitation of Liability. 602 | 603 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 604 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 605 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 606 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 607 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 608 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 609 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 610 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 611 | SUCH DAMAGES. 612 | 613 | 17. Interpretation of Sections 15 and 16. 614 | 615 | If the disclaimer of warranty and limitation of liability provided 616 | above cannot be given local legal effect according to their terms, 617 | reviewing courts shall apply local law that most closely approximates 618 | an absolute waiver of all civil liability in connection with the 619 | Program, unless a warranty or assumption of liability accompanies a 620 | copy of the Program in return for a fee. 621 | 622 | END OF TERMS AND CONDITIONS 623 | 624 | How to Apply These Terms to Your New Programs 625 | 626 | If you develop a new program, and you want it to be of the greatest 627 | possible use to the public, the best way to achieve this is to make it 628 | free software which everyone can redistribute and change under these terms. 629 | 630 | To do so, attach the following notices to the program. It is safest 631 | to attach them to the start of each source file to most effectively 632 | state the exclusion of warranty; and each file should have at least 633 | the "copyright" line and a pointer to where the full notice is found. 634 | 635 | 636 | Copyright (C) 637 | 638 | This program is free software: you can redistribute it and/or modify 639 | it under the terms of the GNU Affero General Public License as published by 640 | the Free Software Foundation, either version 3 of the License, or 641 | (at your option) any later version. 642 | 643 | This program is distributed in the hope that it will be useful, 644 | but WITHOUT ANY WARRANTY; without even the implied warranty of 645 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 646 | GNU Affero General Public License for more details. 647 | 648 | You should have received a copy of the GNU Affero General Public License 649 | along with this program. If not, see . 650 | 651 | Also add information on how to contact you by electronic and paper mail. 652 | 653 | If your software can interact with users remotely through a computer 654 | network, you should also make sure that it provides a way for users to 655 | get its source. For example, if your program is a web application, its 656 | interface could display a "Source" link that leads users to an archive 657 | of the code. There are many ways you could offer source, and different 658 | solutions will be better for different programs; see section 13 for the 659 | specific requirements. 660 | 661 | You should also get your employer (if you work as a programmer) or school, 662 | if any, to sign a "copyright disclaimer" for the program, if necessary. 663 | For more information on this, and how to apply and follow the GNU AGPL, see 664 | . 665 | --------------------------------------------------------------------------------