Skip to content

Commit

Permalink
Release v1.4
Browse files Browse the repository at this point in the history
- Add support for FaceID feature of SD.Next
- Add support for IP Adapters feature of SD.Next
- Add hires.fix related params
- Update default generation params to what RealisiticVision v6.B recommends
- Rename "enhace_face" related params to "restore_face"
- Fix some params not updating via UI, especially params set to default value and params of type boolean or enum
- Fix UI wrongly setting password as endpoint
- Fix UI wrongly setting username as endpoint
- Fix some AUTOMATIC1111 related API incompatibilities
- Fix trigger on output message, fixes #11
- Fix multithreading issues related to context, fixes #13
- Fix full_quality param not set for SD.Next
- Fix missing trace log when errors are logged
  • Loading branch information
Trojaner committed Jan 7, 2024
1 parent d4667f8 commit 638a7a3
Show file tree
Hide file tree
Showing 9 changed files with 614 additions and 177 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ Demo:
## Features
- Generate images using stable-diffusion-webui.
- Well documented [settings](https://github.com/Trojaner/text-generation-webui-stable_diffusion/blob/main/settings.debug.yaml) file for easy configuration.
- Supports face swapping for generating consistent character images without needing loras. See [Ethical Guidelines](#ethical-guidelines) for more information.
- Multi-threading support - can handle concurrent chat sessions and requests.
- Supports face swapping for generating consistent character images. See [Ethical Guidelines](#ethical-guidelines) for more information.
- Supports generation rules for defining when and how to generate images. Can be used for character specific parameters, triggerwords for LoRA or for generating images based on the sentiment of the generated text, etc.

## Supported Stable Diffusion WebUI Extensions
- [FaceSwapLab](https://github.com/glucauze/sd-webui-faceswaplab)
- [ReActor](https://github.com/Gourieff/sd-webui-reactor)
- [FaceID](https://github.com/vladmandic/automatic) (SD.Next feature)
- [IP Adapter](https://github.com/vladmandic/automatic) (SD.Next feature)

## Installation
- Open a shell with cmd_linux.sh/cmd_macos.sh/cmd_windows.bat inside text-generation-webui folder.
Expand Down Expand Up @@ -53,7 +55,7 @@ text-generation-webui, Visual Studio Code and Python 3.10 are required for devel
- Last but not least, ensure that you do not accidentally commit changes you might have made to the `settings.debug.yaml` or `launch.json` files unless intentional.

## Ethical Guidelines
This extension integrates with various face swap extensions for stable-diffusion-webui and hence allows to swap faces in the generated images. This extension is not intended to for the creation of non-consensual deepfake content. Please use this extension responsibly and do not use it to create such content. The main purpose of the face swapping functionality is to allow the creation of consistent images of text-generation-webui characters. If you are unsure whether your use case is ethical, please refrain from using this extension.
This extension integrates with various face swap extensions for stable-diffusion-webui and hence allows to swap faces in the generated images. This extension is not intended for the creation of non-consensual deepfake content. Please use this extension responsibly and do not use it to create such content. The main purpose of the face swapping functionality is to allow the creation of consistent images for text-generation-webui characters. If you are unsure whether your use case is ethical, please refrain from using this extension.

The maintainers and contributors of this extension cannot be held liable for any misuse of this extension but will try to prevent such misuse by all means.

Expand Down
12 changes: 4 additions & 8 deletions context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import threading
from dataclasses import dataclass
from typing import cast
from .params import StableDiffusionWebUiExtensionParams
from .sd_client import SdWebUIApi

Expand All @@ -17,23 +15,21 @@ class GenerationContext(object):

# Create a thread-local state for multi-threading support in case
# multiple sessions run concurrently at the same time.
_local_state = threading.local()
_current_context: GenerationContext | None = None


def get_current_context() -> GenerationContext | None:
"""
Gets the current generation context (thread-safe).
"""

global _local_state
_local_state.current_context = getattr(_local_state, "current_context", None)
return cast(GenerationContext | None, _local_state.current_context)
return _current_context


def set_current_context(context: GenerationContext | None) -> None:
"""
Sets the current generation context (thread-safe).
"""

global _local_state
_local_state.current_context = context
global _current_context
_current_context = context
60 changes: 41 additions & 19 deletions ext_modules/image_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def normalize_regex(regex: str) -> str:
return regex


def normalize_prompt(prompt: str, do_additional_normalization: bool = False) -> str:
def normalize_prompt(prompt: str) -> str:
if prompt is None:
return ""

Expand Down Expand Up @@ -192,7 +192,9 @@ def generate_html_images_for_context(

except Exception as e:
logger.error(
f"[SD WebUI Integration] Failed to apply rule: {rule['regex']}: {e}"
f"[SD WebUI Integration] Failed to apply rule: {rule['regex']}: %s",
e,
exc_info=True,
)

context_prompt = ""
Expand Down Expand Up @@ -223,10 +225,8 @@ def generate_html_images_for_context(
.lower()
)

generated_prompt = _combine_prompts(
normalize_prompt(rules_prompt), normalize_prompt(context_prompt)
)
generated_negative_prompt = normalize_prompt(rules_negative_prompt)
generated_prompt = _combine_prompts(rules_prompt, normalize_prompt(context_prompt))
generated_negative_prompt = rules_negative_prompt

full_prompt = _combine_prompts(generated_prompt, context.params.base_prompt)

Expand All @@ -238,12 +238,14 @@ def generate_html_images_for_context(
(
"[SD WebUI Integration] Using stable-diffusion-webui to generate images."
+ (
f"\n"
f" Prompt: {full_prompt}\n"
f" Negative Prompt: {full_negative_prompt}"
(
f"\n"
f" Prompt: {full_prompt}\n"
f" Negative Prompt: {full_negative_prompt}"
)
if context.params.debug_mode_enabled
else ""
)
if context.params.debug_mode_enabled
else ""
)

try:
Expand All @@ -252,16 +254,29 @@ def generate_html_images_for_context(
negative_prompt=full_negative_prompt,
seed=context.params.seed,
sampler_name=context.params.sampler_name,
enable_hr=context.params.upscaling_enabled,
full_quality=True,
enable_hr=context.params.upscaling_enabled
or context.params.hires_fix_enabled,
hr_scale=context.params.upscaling_scale,
hr_upscaler=context.params.upscaling_upscaler,
denoising_strength=context.params.denoising_strength,
denoising_strength=context.params.hires_fix_denoising_strength,
hr_sampler=context.params.hires_fix_sampler,
hr_force=context.params.hires_fix_enabled,
hr_second_pass_steps=context.params.hires_fix_sampling_steps
if context.params.hires_fix_enabled
else 0,
steps=context.params.sampling_steps,
cfg_scale=context.params.cfg_scale,
width=context.params.width,
height=context.params.height,
restore_faces=context.params.enhance_faces_enabled,
override_settings_restore_afterwards=True,
restore_faces=context.params.restore_faces_enabled,
faceid_enabled=context.params.faceid_enabled,
faceid_scale=context.params.faceid_scale,
faceid_image=context.params.faceid_source_face,
ipadapter_enabled=context.params.ipadapter_enabled,
ipadapter_adapter=context.params.ipadapter_adapter,
ipadapter_scale=context.params.ipadapter_scale,
ipadapter_image=context.params.ipadapter_reference_image,
use_async=False,
)

Expand Down Expand Up @@ -310,7 +325,9 @@ def generate_html_images_for_context(
image = response.image
except Exception as e:
logger.error(
f"[SD WebUI Integration] FaceSwapLab failed to swap faces: {e}"
"[SD WebUI Integration] FaceSwapLab failed to swap faces: %s",
e,
exc_info=True,
)

if reactor_force_enabled or (
Expand Down Expand Up @@ -338,7 +355,9 @@ def generate_html_images_for_context(
image = response.image
except Exception as e:
logger.error(
f"[SD WebUI Integration] ReActor failed to swap faces: {e}"
"[SD WebUI Integration] ReActor failed to swap faces: %s",
e,
exc_info=True,
)

if context.params.save_images:
Expand Down Expand Up @@ -386,10 +405,13 @@ def generate_html_images_for_context(


def _combine_prompts(prompt1: str, prompt2: str) -> str:
if not prompt1 or prompt1 == "":
if prompt1 is None and prompt2 is None:
return ""

if prompt1 is None or prompt1 == "":
return prompt2.strip(",").strip()

if not prompt2 or prompt2 == "":
if prompt2 is None or prompt2 == "":
return prompt1.strip(",").strip()

return prompt1.strip(",").strip() + ", " + prompt2.strip(",").strip()
Loading

0 comments on commit 638a7a3

Please sign in to comment.