Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat:adjust generated image box size; allow customize prompt #101

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 37 additions & 26 deletions gradio_demo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
sys.path.append('./')

import os
import logging
import cv2
import math
import torch
Expand All @@ -27,6 +28,11 @@

import gradio as gr

logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s [%(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
# global variable
MAX_SEED = np.iinfo(np.int32).max
device = get_torch_device()
Expand All @@ -45,7 +51,7 @@
# Load pipeline
controlnet = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=dtype)

def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8"):
def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", server_name="127.0.0.1", server_port=7860):

if pretrained_model_name_or_path.endswith(
".ckpt"
Expand Down Expand Up @@ -196,21 +202,14 @@ def resize_img(input_image, max_side=1280, min_side=1024, size=None,
input_image = Image.fromarray(res)
return input_image

def apply_style(style_name: str, positive: str, negative: str = "") -> tuple[str, str]:
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
return p.replace("{prompt}", positive), n + ' ' + negative

def generate_image(face_image, pose_image, prompt, negative_prompt, style_name, num_steps, identitynet_strength_ratio, adapter_strength_ratio, guidance_scale, seed, progress=gr.Progress(track_tqdm=True)):
def generate_image(face_image, pose_image, prompt, negative_prompt, num_steps, identitynet_strength_ratio, adapter_strength_ratio, guidance_scale, seed, progress=gr.Progress(track_tqdm=True)):

if face_image is None:
raise gr.Error(f"Cannot find any input face image! Please upload the face image")

if prompt is None:
prompt = "a person"

# apply the style template
prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)

face_image = load_image(face_image[0])
face_image = resize_img(face_image)
face_image_cv2 = convert_from_image_to_cv2(face_image)
Expand Down Expand Up @@ -243,8 +242,9 @@ def generate_image(face_image, pose_image, prompt, negative_prompt, style_name,

generator = torch.Generator(device=device).manual_seed(seed)

print("Start inference...")
print(f"[Debug] Prompt: {prompt}, \n[Debug] Neg Prompt: {negative_prompt}")
logging.debug("Start inference...")
logging.debug(f"Prompt: {prompt}")
logging.debug(f"Neg Prompt: {negative_prompt}")

pipe.set_ip_adapter_scale(adapter_strength_ratio)
images = pipe(
Expand Down Expand Up @@ -336,17 +336,25 @@ def generate_image(face_image, pose_image, prompt, negative_prompt, style_name,

# prompt
prompt = gr.Textbox(label="Prompt",
info="Give simple prompt is enough to achieve good face fidelity",
info="Give simple prompt is enough to achieve good face fedility",
placeholder="A photo of a person",
value="")
value=styles[DEFAULT_STYLE_NAME][0],
interactive=True)

negative_prompt = gr.Textbox(
label="Negative Prompt",
placeholder="low quality",
value=styles[DEFAULT_STYLE_NAME][1],
interactive=True,
)

submit = gr.Button("Submit", variant="primary")

style = gr.Dropdown(label="Style template", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME)

# strength
identitynet_strength_ratio = gr.Slider(
label="IdentityNet strength (for fidelity)",
label="IdentityNet strength (for fedility)",
minimum=0,
maximum=1.5,
step=0.05,
Expand All @@ -357,15 +365,10 @@ def generate_image(face_image, pose_image, prompt, negative_prompt, style_name,
minimum=0,
maximum=1.5,
step=0.05,
value=0.80,
value=0.40,
)

with gr.Accordion(open=False, label="Advanced Options"):
negative_prompt = gr.Textbox(
label="Negative Prompt",
placeholder="low quality",
value="(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
)
num_steps = gr.Slider(
label="Number of sample steps",
minimum=20,
Expand All @@ -390,7 +393,7 @@ def generate_image(face_image, pose_image, prompt, negative_prompt, style_name,
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)

with gr.Column():
gallery = gr.Gallery(label="Generated Images")
gallery = gr.Gallery(label="Generated Images", columns=1, rows=1, height=512)
usage_tips = gr.Markdown(label="Usage tips of InstantID", value=tips ,visible=False)

face_files.upload(fn=swap_to_gallery, inputs=face_files, outputs=[uploaded_faces, clear_button_face, face_files])
Expand All @@ -399,9 +402,11 @@ def generate_image(face_image, pose_image, prompt, negative_prompt, style_name,
remove_and_reupload_faces.click(fn=remove_back_to_files, outputs=[uploaded_faces, clear_button_face, face_files])
remove_and_reupload_poses.click(fn=remove_back_to_files, outputs=[uploaded_poses, clear_button_pose, pose_files])

change_style = lambda style_name: styles.get(style_name, styles[DEFAULT_STYLE_NAME])
style.change(change_style, inputs=[style], outputs=[prompt, negative_prompt])
submit.click(
fn=remove_tips,
outputs=usage_tips,
outputs=usage_tips,
).then(
fn=randomize_seed_fn,
inputs=[seed, randomize_seed],
Expand All @@ -410,7 +415,7 @@ def generate_image(face_image, pose_image, prompt, negative_prompt, style_name,
api_name=False,
).then(
fn=generate_image,
inputs=[face_files, pose_files, prompt, negative_prompt, style, num_steps, identitynet_strength_ratio, adapter_strength_ratio, guidance_scale, seed],
inputs=[face_files, pose_files, prompt, negative_prompt, num_steps, identitynet_strength_ratio, adapter_strength_ratio, guidance_scale, seed],
outputs=[gallery, usage_tips]
)

Expand All @@ -424,13 +429,19 @@ def generate_image(face_image, pose_image, prompt, negative_prompt, style_name,

gr.Markdown(article)

demo.launch()
demo.launch(server_name=server_name, server_port=server_port)

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--pretrained_model_name_or_path", type=str, default="wangqixun/YamerMIX_v8"
"--pretrained_model_name_or_path", type=str, default="wangqixun/YamerMIX_v8",
)
parser.add_argument(
"--server_name", type=str, default="127.0.0.1",
)
parser.add_argument(
"--server_port", type=int, default="7860",
)
args = parser.parse_args()

main(args.pretrained_model_name_or_path)
main(args.pretrained_model_name_or_path, args.server_name, args.server_port)
18 changes: 9 additions & 9 deletions gradio_demo/style_template.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
style_list = [
{
"name": "(No style)",
"prompt": "{prompt}",
"negative_prompt": "",
"prompt": "",
"negative_prompt": "NSFW,realistic, (worst quality, low quality:1.3),(bad and mutated hands:1.3),(worst quality:2),(low quality:2,),(blurry:2),horror,geometry,(bad hands),(missing fingers),multiple limbs,bad anatomy,(interlocked fingers:1.2),ugly fingers,(extra digit and hands and fingers and legs and arms:1.4),crown braid,(deformed fingers:1.2),(long fingers:1.2),(bad-artist-anime),bad-artist,bad hand",
},
{
"name": "Watercolor",
"prompt": "watercolor painting, {prompt}. vibrant, beautiful, painterly, detailed, textural, artistic",
"prompt": "watercolor painting, vibrant, beautiful, painterly, detailed, textural, artistic",
"negative_prompt": "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, anime, photorealistic, 35mm film, deformed, glitch, low contrast, noisy",
},
{
"name": "Film Noir",
"prompt": "film noir style, ink sketch|vector, {prompt} highly detailed, sharp focus, ultra sharpness, monochrome, high contrast, dramatic shadows, 1940s style, mysterious, cinematic",
"prompt": "film noir style, ink sketch|vector, highly detailed, sharp focus, ultra sharpness, monochrome, high contrast, dramatic shadows, 1940s style, mysterious, cinematic",
"negative_prompt": "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, photo, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
},
{
Expand All @@ -21,27 +21,27 @@
},
{
"name": "Jungle",
"prompt": 'waist-up "{prompt} in a Jungle" by Syd Mead, tangerine cold color palette, muted colors, detailed, 8k,photo r3al,dripping paint,3d toon style,3d style,Movie Still',
"prompt": 'waist-up "in a Jungle" by Syd Mead, tangerine cold color palette, muted colors, detailed, 8k,photo r3al,dripping paint,3d toon style,3d style,Movie Still',
"negative_prompt": "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, photo, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
},
{
"name": "Mars",
"prompt": "{prompt}, Post-apocalyptic. Mars Colony, Scavengers roam the wastelands searching for valuable resources, rovers, bright morning sunlight shining, (detailed) (intricate) (8k) (HDR) (cinematic lighting) (sharp focus)",
"prompt": "Post-apocalyptic. Mars Colony, Scavengers roam the wastelands searching for valuable resources, rovers, bright morning sunlight shining, (detailed) (intricate) (8k) (HDR) (cinematic lighting) (sharp focus)",
"negative_prompt": "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, photo, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
},
{
"name": "Vibrant Color",
"prompt": "vibrant colorful, ink sketch|vector|2d colors, at nightfall, sharp focus, {prompt}, highly detailed, sharp focus, the clouds,colorful,ultra sharpness",
"prompt": "vibrant colorful, ink sketch|vector|2d colors, at nightfall, sharp focus, highly detailed, sharp focus, the clouds,colorful,ultra sharpness",
"negative_prompt": "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, photo, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
},
{
"name": "Snow",
"prompt": "cinema 4d render, {prompt}, high contrast, vibrant and saturated, sico style, surrounded by magical glow,floating ice shards, snow crystals, cold, windy background, frozen natural landscape in background cinematic atmosphere,highly detailed, sharp focus, intricate design, 3d, unreal engine, octane render, CG best quality, highres, photorealistic, dramatic lighting, artstation, concept art, cinematic, epic Steven Spielberg movie still, sharp focus, smoke, sparks, art by pascal blanche and greg rutkowski and repin, trending on artstation, hyperrealism painting, matte painting, 4k resolution",
"prompt": "cinema 4d render, high contrast, vibrant and saturated, sico style, surrounded by magical glow,floating ice shards, snow crystals, cold, windy background, frozen natural landscape in background cinematic atmosphere,highly detailed, sharp focus, intricate design, 3d, unreal engine, octane render, CG best quality, highres, photorealistic, dramatic lighting, artstation, concept art, cinematic, epic Steven Spielberg movie still, sharp focus, smoke, sparks, art by pascal blanche and greg rutkowski and repin, trending on artstation, hyperrealism painting, matte painting, 4k resolution",
"negative_prompt": "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, photo, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
},
{
"name": "Line art",
"prompt": "line art drawing {prompt} . professional, sleek, modern, minimalist, graphic, line art, vector graphics",
"prompt": "line art drawing professional, sleek, modern, minimalist, graphic, line art, vector graphics",
"negative_prompt": "anime, photorealistic, 35mm film, deformed, glitch, blurry, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, mutated, realism, realistic, impressionism, expressionism, oil, acrylic",
},
]
Expand Down