pull/27/head
unknown 2023-01-15 16:24:01 +08:00
parent c681814c7e
commit 96649112d5
3 changed files with 81 additions and 50 deletions

View File

@ -119,9 +119,13 @@ Keep your pace. Let's start from building your prompt library. <a href="#build-l
Make sure you cloned the [stable-diffusion-webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui) before. If not check out [stable-diffusion-webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui) to install it first.
**Make sure you add "--api --cors-allow-origins http://localhost:5173" as "COMMANDLINE_ARGS" in webui-user.bat (where you start webui) like this:**
-> Below is very important, otherwise "send to webui button will be not functional"
**If using remotly add parmas like this: --cors-allow-origins http://remote-ip:5173 --api --listen --server-name remote-ip**
**Make sure you add "--api --listen --cors-allow-origins http://localhost:5173" as "COMMANDLINE_ARGS" in webui-user.bat like this**
**If you start webui manually please also make sure you add these parameters to launch.py**
**If using customized webui/ ip and port please refer to cuatomized ip and port**
![install](./images/0-1.JPG)
@ -140,6 +144,34 @@ Goto Tab extensions in the webui and paste:
<p align="right">(<a href="#readme-top">back to top</a>)</p>
## Customized extension ip and port
If you need to customized the ip of this extension.
Search %extension-path%\assets\index.*.js, change "127.0.0.1" into your extension ip
Search %extension-path%\scripts\prompt_gallery.py change
"""python
pg_ip = "%your_extension_ip%" if shared.cmd_opts.listen else 'localhost'
pg_port = %your_extension_port%
"""
Change webui param to --cors-allow-origins http://%extension-ip%:%extension-port% --api --server-name %webui-ip%
### Customized webui IP and port
If your webui executed on customized ip and port. The ip and port should be detected automatically. If not, please try:
Search %extension-path%\scripts\prompt_gallery.py change
"""python
remote_webui = '%webui_ip%'
if shared.cmd_opts.server_name:
remote_webui = str(shared.cmd_opts.server_name)
port = "%webui_port%"
"""
## Usage

View File

@ -17,6 +17,8 @@ import base64
import io
from PIL import Image
import mimetypes
from stat import S_IREAD, S_IRGRP, S_IROTH, S_IRWXO, S_IWRITE, S_IWOTH, S_IWRITE
mimetypes.init()
mimetypes.add_type('application/javascript', '.js')
@ -34,13 +36,16 @@ if '__file__' in locals().keys():
else:
root_path = os.path.abspath(shared.script_path)
rela_path = os.path.join( 'extensions')
try:
with open("./extensions/prompt_gallery_name.json") as fd:
with open( os.path.join( rela_path, 'prompt_gallery_name.json') ) as fd:
extension_name = json.load(fd)['name']
except:
extension_name = "Prompt Gallery"
OUTPATH_SAMPLES = os.path.join(root_path, 'extensions', extension_name, 'assets', 'preview')
OUTPATH_GRIDS = os.path.join(root_path, 'extensions', extension_name, 'assets', 'grid')
OUTPATH_SAMPLES = os.path.join(rela_path, extension_name, 'assets', 'preview')
OUTPATH_GRIDS = os.path.join(rela_path, extension_name, 'assets', 'grid')
BATCH_SIZE = 2
N_ITER = 2
@ -77,7 +82,7 @@ pg_templates = {}
pg_templates.update(options_section_def(('saving-images', "Saving images/grids"), {
"samples_save": OptionInfo(True, "Always save all generated images"),
"samples_format": OptionInfo('png', 'File format for images'),
"samples_filename_pattern": OptionInfo("", "Images filename pattern", component_args=hide_dirs),
"samples_filename_pattern": OptionInfo("[seed]", "Images filename pattern", component_args=hide_dirs),
"save_images_add_number": OptionInfo(True, "Add number to filename when saving", component_args=hide_dirs),
"grid_save": OptionInfo(True, "Always save all generated image grids"),
@ -89,9 +94,6 @@ pg_templates.update(options_section_def(('saving-images', "Saving images/grids")
"enable_pnginfo": OptionInfo(True, "Save text information about generation parameters as chunks to png files"),
"save_txt": OptionInfo(False, "Create a text file next to every image with generation parameters."),
"save_images_before_face_restoration": OptionInfo(False, "Save a copy of image before doing face restoration."),
"save_images_before_highres_fix": OptionInfo(False, "Save a copy of image before applying highres fix."),
"save_images_before_color_correction": OptionInfo(False, "Save a copy of image before applying color correction to img2img results"),
"jpeg_quality": OptionInfo(80, "Quality for saved jpeg images", gr.Slider, {"minimum": 1, "maximum": 100, "step": 1}),
"export_for_4chan": OptionInfo(True, "If PNG image is larger than 4MB or any dimension is larger than 4000, downscale and save copy as JPG"),
@ -99,26 +101,20 @@ pg_templates.update(options_section_def(('saving-images', "Saving images/grids")
"save_selected_only": OptionInfo(True, "When using 'Save' button, only save a single selected image"),
"do_not_add_watermark": OptionInfo(False, "Do not add watermark to images"),
"temp_dir": OptionInfo("", "Directory for temporary images; leave empty for default"),
"clean_temp_dir_at_start": OptionInfo(False, "Cleanup non-default temporary directory when starting webui"),
}))
pg_templates.update(options_section_def(('saving-paths', "Paths for saving"), {
"outdir_samples": OptionInfo("", "Output directory for images; if empty, defaults to three directories below", component_args=hide_dirs),
"outdir_txt2img_samples": OptionInfo("outputs/txt2img-images", 'Output directory for txt2img images', component_args=hide_dirs),
"outdir_img2img_samples": OptionInfo("outputs/img2img-images", 'Output directory for img2img images', component_args=hide_dirs),
"outdir_extras_samples": OptionInfo("outputs/extras-images", 'Output directory for images from extras tab', component_args=hide_dirs),
"outdir_grids": OptionInfo("", "Output directory for grids; if empty, defaults to two directories below", component_args=hide_dirs),
"outdir_txt2img_grids": OptionInfo("outputs/txt2img-grids", 'Output directory for txt2img grids', component_args=hide_dirs),
"outdir_img2img_grids": OptionInfo("outputs/img2img-grids", 'Output directory for img2img grids', component_args=hide_dirs),
"outdir_save": OptionInfo("log/images", "Directory for saving images using the Save button", component_args=hide_dirs),
}))
# pg_templates.update(options_section_def(('saving-paths', "Paths for saving"), {
# "outdir_samples": OptionInfo("", "Output directory for images; if empty, defaults to three directories below", component_args=hide_dirs),
# "outdir_txt2img_samples": OptionInfo("outputs/txt2img-images", 'Output directory for txt2img images', component_args=hide_dirs),
# "outdir_img2img_samples": OptionInfo("outputs/img2img-images", 'Output directory for img2img images', component_args=hide_dirs),
# "outdir_extras_samples": OptionInfo("outputs/extras-images", 'Output directory for images from extras tab', component_args=hide_dirs),
# "outdir_grids": OptionInfo("", "Output directory for grids; if empty, defaults to two directories below", component_args=hide_dirs),
# "outdir_txt2img_grids": OptionInfo("outputs/txt2img-grids", 'Output directory for txt2img grids', component_args=hide_dirs),
# "outdir_img2img_grids": OptionInfo("outputs/img2img-grids", 'Output directory for img2img grids', component_args=hide_dirs),
# "outdir_save": OptionInfo("log/images", "Directory for saving images using the Save button", component_args=hide_dirs),
# }))
pg_templates.update(options_section_def(('saving-to-dirs', "Saving to a directory"), {
"save_to_dirs": OptionInfo(False, "Save images to a subdirectory"),
"grid_save_to_dirs": OptionInfo(False, "Save grids to a subdirectory"),
"use_save_to_dirs_for_ui": OptionInfo(False, "When using \"Save\" button, save images to a subdirectory"),
"directories_filename_pattern": OptionInfo("", "Directory name pattern", component_args=hide_dirs),
"directories_max_prompt_words": OptionInfo(8, "Max prompt words for [prompt_words] pattern", gr.Slider, {"minimum": 1, "maximum": 20, "step": 1, **hide_dirs}),
}))
@ -137,15 +133,12 @@ pg_templates.update(options_section_def(('ui', "User interface"), {
"show_progress_grid": OptionInfo(True, "Show previews of all images generated in a batch as a grid"),
"return_grid": OptionInfo(True, "Show grid in results for web"),
"do_not_show_images": OptionInfo(False, "Do not show any images in results for web"),
"add_model_hash_to_info": OptionInfo(True, "Add model hash to generation information"),
"add_model_name_to_info": OptionInfo(False, "Add model name to generation information"),
"disable_weights_auto_swap": OptionInfo(False, "When reading generation parameters from text into UI (from PNG info or pasted text), do not change the selected model/checkpoint."),
"send_seed": OptionInfo(True, "Send seed when sending prompt or image to other interface"),
"font": OptionInfo("", "Font for image grids that have text"),
"js_modal_lightbox": OptionInfo(True, "Enable full page image viewer"),
"js_modal_lightbox_initially_zoomed": OptionInfo(True, "Show images zoomed in by default in full page image viewer"),
"show_progress_in_title": OptionInfo(True, "Show generation progress in window title."),
'quicksettings': OptionInfo("sd_model_checkpoint", "Quicksettings list"),
'localization': OptionInfo("None", "Localization (requires restart)", gr.Dropdown, lambda: {"choices": ["None"] + list(localization.localizations.keys())}, refresh=lambda: localization.list_localizations(cmd_opts.localizations_dir)),
}))
@ -242,7 +235,6 @@ def cmdargs(line):
args = shlex.split(line)
args[args.index('--outpath_samples')+1] = args[args.index('--outpath_samples')+1][:-1]
args[args.index('--outpath_grids')+1] = args[args.index('--outpath_grids')+1][:-1]
print(args)
pos = 0
res = {}
@ -439,7 +431,7 @@ class PromptStyle(typing.NamedTuple):
def save_styles() -> None:
if len(OUTPUTS.keys()) == 0:
return
path = os.path.join(root_path, 'styles.csv')
path = os.path.join('./', 'styles.csv')
# Write to temporary file first, so we don't nuke the file if something goes wrong
fd, temp_path = tempfile.mkstemp(".csv")
with os.fdopen(fd, "a", encoding="utf-8-sig", newline='') as file:
@ -452,7 +444,7 @@ def save_styles() -> None:
# writer.writerows(style._asdict() for k, style in self.styles.items())
# Always keep a backup file around
if os.path.exists(path):
if os.path.exists(path) and not os.path.exists(path + ".bak"):
shutil.move(path, path + ".bak")
shutil.move(temp_path, path)
@ -512,9 +504,9 @@ def scan_outputs(avatar_name):
files.remove(each_avatar + '.png')
if len(files) == 0:
continue
print("Insert file:")
for file in files:
print(os.path.join(root, folder, file))
# print("Insert file:")
# for file in files:
# print(os.path.join(root, folder, file))
qc_dict[folder] = [os.path.join(root, folder, file) for file in files]
if len(qc_dict.keys()) == 0:
@ -526,12 +518,12 @@ def update_gallery(dropdown, avatar):
global trg_img, current_folder
current_folder = os.path.join(root, dropdown)
trg_img = os.path.join(root, dropdown, avatar + '.png')
print("Detected folders:")
print(qc_dict)
print("Selected folder:")
print(dropdown)
print("Detected files:")
print(qc_dict[dropdown])
# print("Detected folders:")
# print(qc_dict)
# print("Selected folder:")
# print(dropdown)
# print("Detected files:")
# print(qc_dict[dropdown])
return qc_dict[dropdown]
def clean_select_picture(filename):
@ -544,7 +536,7 @@ def clean_select_picture(filename):
if each_avatar + '.png' == file:
is_avatar = True
break
if '-' in file and file.split('-')[1] in filename:
if '-' in file and file.split('.')[0] in filename:
print("rename", os.path.join(current_folder, file), trg_img)
os.rename(os.path.join(current_folder, file), trg_img)
elif is_avatar == False:
@ -647,6 +639,8 @@ class Script(scripts.Script):
def run(self, p, checkbox_iterate, avatar_dict, prompt_dict, default_negative, default_positive, dropdown, prompt_display, rename_button, label_avatar, open_button, export_button, skip_exist, label_presets, label_preview, preview_dropdown, preview_gallery, qc_select, qc_refresh, qc_show, selected_img):
global pg_templates
backup = copy.deepcopy(shared.opts)
print("Auth " + str(os.path.join(rela_path, extension_name)))
os.chmod(os.path.join(rela_path, extension_name, 'assets'), S_IWOTH)
shared.opts.data.update(pg_templates)
lines = [x.strip() for x in prompt_display.splitlines()]
lines = [x for x in lines if len(x) > 0]
@ -675,7 +669,7 @@ class Script(scripts.Script):
jobs.append(args)
print(f"Will process {len(lines)} lines in {job_count} jobs.")
# print(f"Will process {len(lines)} lines in {job_count} jobs.")
if (checkbox_iterate and p.seed == -1):
p.seed = int(random.randrange(4294967294))
@ -697,5 +691,6 @@ class Script(scripts.Script):
OUTPUTS = {}
rawDict = {}
shared.opts = backup
return Processed(p, images, p.seed, "")

View File

@ -22,17 +22,21 @@ import json
from modules import shared
from fastapi import APIRouter
pg_ip = "127.0.0.1"
#pg_ip = "127.0.0.1" if shared.cmd_opts.listen else 'localhost'
pg_port = 5173
pg_ip = "0.0.0.0" if shared.cmd_opts.listen else 'localhost'
def on_ui_settings():
global pg_ip
with open("./extensions/prompt_gallery_name.json") as fd:
name = json.load(fd)['name']
os.chmod('./extensions/'+name, stat.S_IRWXO)
app = FastAPI()
app.mount('/', StaticFiles(directory='./extensions/'+name,html=True))
config = Config(app=app, host=pg_ip,port=5173, log_level="info", loop="asyncio", limit_max_requests=1)
config = Config(app=app, host=pg_ip,port=pg_port, log_level="info", loop="asyncio", limit_max_requests=1)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
@ -41,7 +45,7 @@ def on_ui_settings():
allow_headers=["*"]
)
thread = threading.Thread(target= uvicorn.run, kwargs={'app':app, 'host': pg_ip, 'port':5173})
thread = threading.Thread(target= uvicorn.run, kwargs={'app':app, 'host': pg_ip, 'port':pg_port})
thread.start()
wait_time = 0
@ -49,11 +53,11 @@ def on_ui_settings():
while if_connect == False and wait_time<=6:
try:
tmp = requests.get("http://localhost:5173")
tmp = requests.get("http://{}:{}".format(pg_ip, str(pg_port)))
if_connect = True if int(tmp.status_code) /100 == 2. or int(tmp.status_code) /100 == 2 else False
except:
print(".")
time.sleep(10)
time.sleep(1)
wait_time+=1
@ -62,13 +66,13 @@ def on_ui_tabs():
extension_theme = 'white'
else:
extension_theme = 'black'
remote_webui = 'localhost'
remote_webui = '127.0.0.1'
if shared.cmd_opts.server_name:
remote_webui = str(shared.cmd_opts.server_name)
port = str(shared.cmd_opts.port) if shared.cmd_opts.port is not None else "7860"
html = """<script>var ip = window.location.hostname; </script>
<iframe id="tab_iframe" allow="clipboard-read; clipboard-write" style="width: 100%; min-height: 1080px; padding: 0;margin: 0;border: none;" src="http://{remote_webui:s}:5173/?theme={theme:s}&port={port:s}&ip={remote_webui:s}" frameborder="0" marginwidth="0" marginheight="0"></iframe>""".format(remote_webui=remote_webui, theme=extension_theme, port=port)
<iframe id="tab_iframe" allow="clipboard-read; clipboard-write" style="width: 100%; min-height: 1080px; padding: 0;margin: 0;border: none;" src="http://{pg_ip:s}:{pg_port:s}/?theme={theme:s}&port={port:s}&ip={remote_webui:s}" frameborder="0" marginwidth="0" marginheight="0"></iframe>""".format(pg_ip=pg_ip, pg_port=str(pg_port), remote_webui=remote_webui, theme=extension_theme, port=port)
with gr.Blocks(analytics_enabled=False, elem_id="prompt_gallery") as prompt_gallery:
prompt_gallery = gr.HTML(html)