Commit 00b2ff19 authored by chenpangpang's avatar chenpangpang
Browse files

feat: Dockerfile改进,显示界面改成标题和提示为中文、可生成公开链接的方式。

parent c90df435
...@@ -12,7 +12,7 @@ RUN pip install -r gradio_demo/requirements.txt && \ ...@@ -12,7 +12,7 @@ RUN pip install -r gradio_demo/requirements.txt && \
# Prod # # Prod #
######### #########
FROM image.sourcefind.cn:5000/gpu/admin/base/jupyterlab-pytorch:2.2.0-python3.10-cuda12.1-ubuntu22.04 FROM image.sourcefind.cn:5000/gpu/admin/base/jupyterlab-pytorch:2.2.0-python3.10-cuda12.1-ubuntu22.04
RUN apt-get update && apt-get install -y libglib2.0-0 libgl1-mesa-glx
COPY --from=base /opt/conda/lib/python3.10/site-packages /opt/conda/lib/python3.10/site-packages COPY --from=base /opt/conda/lib/python3.10/site-packages /opt/conda/lib/python3.10/site-packages
COPY --from=base /root/instantid/InstantID /root/InstantID COPY --from=base /root/instantid/InstantID /root/InstantID
COPY --from=base /root/instantid/启动器.ipynb /root/instantid/run.sh /root/ COPY --from=base /root/instantid/启动器.ipynb /root/instantid/run.sh /root/
# RUN apt-get update && apt-get install -y libglib2.0-0 libgl1-mesa-glx
\ No newline at end of file
import sys import sys
sys.path.append('./') sys.path.append('./')
from typing import Tuple from typing import Tuple
...@@ -48,8 +49,8 @@ controlnet_path = f'./checkpoints/ControlNetModel' ...@@ -48,8 +49,8 @@ controlnet_path = f'./checkpoints/ControlNetModel'
# Load pipeline # Load pipeline
controlnet = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=dtype) controlnet = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=dtype)
def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=False):
def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=False):
if pretrained_model_name_or_path.endswith( if pretrained_model_name_or_path.endswith(
".ckpt" ".ckpt"
) or pretrained_model_name_or_path.endswith(".safetensors"): ) or pretrained_model_name_or_path.endswith(".safetensors"):
...@@ -92,6 +93,7 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F ...@@ -92,6 +93,7 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
# load and disable LCM # load and disable LCM
pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl") pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl")
pipe.disable_lora() pipe.disable_lora()
def toggle_lcm_ui(value): def toggle_lcm_ui(value):
if value: if value:
return ( return (
...@@ -156,7 +158,7 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F ...@@ -156,7 +158,7 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
def convert_from_image_to_cv2(img: Image) -> np.ndarray: def convert_from_image_to_cv2(img: Image) -> np.ndarray:
return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
def draw_kps(image_pil, kps, color_list=[(255,0,0), (0,255,0), (0,0,255), (255,255,0), (255,0,255)]): def draw_kps(image_pil, kps, color_list=[(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255)]):
stickwidth = 4 stickwidth = 4
limbSeq = np.array([[0, 2], [1, 2], [3, 2], [4, 2]]) limbSeq = np.array([[0, 2], [1, 2], [3, 2], [4, 2]])
kps = np.array(kps) kps = np.array(kps)
...@@ -172,7 +174,8 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F ...@@ -172,7 +174,8 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
y = kps[index][:, 1] y = kps[index][:, 1]
length = ((x[0] - x[1]) ** 2 + (y[0] - y[1]) ** 2) ** 0.5 length = ((x[0] - x[1]) ** 2 + (y[0] - y[1]) ** 2) ** 0.5
angle = math.degrees(math.atan2(y[0] - y[1], x[0] - x[1])) angle = math.degrees(math.atan2(y[0] - y[1], x[0] - x[1]))
polygon = cv2.ellipse2Poly((int(np.mean(x)), int(np.mean(y))), (int(length / 2), stickwidth), int(angle), 0, 360, 1) polygon = cv2.ellipse2Poly((int(np.mean(x)), int(np.mean(y))), (int(length / 2), stickwidth), int(angle), 0,
360, 1)
out_img = cv2.fillConvexPoly(out_img.copy(), polygon, color) out_img = cv2.fillConvexPoly(out_img.copy(), polygon, color)
out_img = (out_img * 0.6).astype(np.uint8) out_img = (out_img * 0.6).astype(np.uint8)
...@@ -192,9 +195,9 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F ...@@ -192,9 +195,9 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
w_resize_new, h_resize_new = size w_resize_new, h_resize_new = size
else: else:
ratio = min_side / min(h, w) ratio = min_side / min(h, w)
w, h = round(ratio*w), round(ratio*h) w, h = round(ratio * w), round(ratio * h)
ratio = max_side / max(h, w) ratio = max_side / max(h, w)
input_image = input_image.resize([round(ratio*w), round(ratio*h)], mode) input_image = input_image.resize([round(ratio * w), round(ratio * h)], mode)
w_resize_new = (round(ratio * w) // base_pixel_number) * base_pixel_number w_resize_new = (round(ratio * w) // base_pixel_number) * base_pixel_number
h_resize_new = (round(ratio * h) // base_pixel_number) * base_pixel_number h_resize_new = (round(ratio * h) // base_pixel_number) * base_pixel_number
input_image = input_image.resize([w_resize_new, h_resize_new], mode) input_image = input_image.resize([w_resize_new, h_resize_new], mode)
...@@ -203,7 +206,7 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F ...@@ -203,7 +206,7 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
res = np.ones([max_side, max_side, 3], dtype=np.uint8) * 255 res = np.ones([max_side, max_side, 3], dtype=np.uint8) * 255
offset_x = (max_side - w_resize_new) // 2 offset_x = (max_side - w_resize_new) // 2
offset_y = (max_side - h_resize_new) // 2 offset_y = (max_side - h_resize_new) // 2
res[offset_y:offset_y+h_resize_new, offset_x:offset_x+w_resize_new] = np.array(input_image) res[offset_y:offset_y + h_resize_new, offset_x:offset_x + w_resize_new] = np.array(input_image)
input_image = Image.fromarray(res) input_image = Image.fromarray(res)
return input_image return input_image
...@@ -211,7 +214,9 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F ...@@ -211,7 +214,9 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME]) p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
return p.replace("{prompt}", positive), n + ' ' + negative return p.replace("{prompt}", positive), n + ' ' + negative
def generate_image(face_image_path, pose_image_path, prompt, negative_prompt, style_name, num_steps, identitynet_strength_ratio, adapter_strength_ratio, guidance_scale, seed, enable_LCM, enhance_face_region, progress=gr.Progress(track_tqdm=True)): def generate_image(face_image_path, pose_image_path, prompt, negative_prompt, style_name, num_steps,
identitynet_strength_ratio, adapter_strength_ratio, guidance_scale, seed, enable_LCM,
enhance_face_region, progress=gr.Progress(track_tqdm=True)):
if enable_LCM: if enable_LCM:
pipe.enable_lora() pipe.enable_lora()
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config) pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
...@@ -239,7 +244,8 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F ...@@ -239,7 +244,8 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
if len(face_info) == 0: if len(face_info) == 0:
raise gr.Error(f"Cannot find any face in the image! Please upload another person image") raise gr.Error(f"Cannot find any face in the image! Please upload another person image")
face_info = sorted(face_info, key=lambda x:(x['bbox'][2]-x['bbox'][0])*(x['bbox'][3]-x['bbox'][1]))[-1] # only use the maximum face face_info = sorted(face_info, key=lambda x: (x['bbox'][2] - x['bbox'][0]) * (x['bbox'][3] - x['bbox'][1]))[
-1] # only use the maximum face
face_emb = face_info['embedding'] face_emb = face_info['embedding']
face_kps = draw_kps(convert_from_cv2_to_image(face_image_cv2), face_info['kps']) face_kps = draw_kps(convert_from_cv2_to_image(face_image_cv2), face_info['kps'])
...@@ -291,18 +297,17 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F ...@@ -291,18 +297,17 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
### Description ### Description
title = r""" title = r"""
<h1 align="center">InstantID: Zero-shot Identity-Preserving Generation in Seconds</h1> <h1 align="center">InstantID: 1张照片,无需训练,秒级生成个人写真</h1>
""" """
description = r""" description = r"""
<b>Official 🤗 Gradio demo</b> for <a href='https://github.com/InstantID/InstantID' target='_blank'><b>InstantID: Zero-shot Identity-Preserving Generation in Seconds</b></a>.<br> <b>官方Gradio demo</b> for <a href='https://github.com/InstantID/InstantID' target='_blank'><b>InstantID: 1张照片,无需训练,秒级生成个人写真</b></a>.<br>
用户指南:<br>
How to use:<br> 1. 上传人物图片。 对于多人图像,我们只会检测最大的脸部。 确保脸部不要太小,并且没有明显遮挡或模糊。
1. Upload an image with a face. For images with multiple faces, we will only detect the largest face. Ensure the face is not too small and is clearly visible without significant obstructions or blurring. 2. (可选)上传另一个人的图像作为参考姿势。 如果没有上传,我们将使用第一张图像来提取姿势。 如果您在步骤1中使用了裁剪后的脸部,建议上传它以提取新的姿势。
2. (Optional) You can upload another image as a reference for the face pose. If you don't, we will use the first detected face image to extract facial landmarks. If you use a cropped face at step 1, it is recommended to upload it to define a new face pose. 3. (可选)输入文本prompt提示词,就像所有文生图应用中所做的那样.
3. Enter a text prompt, as done in normal text-to-image models. 4. 点击 <b>Submit</b> 按钮开始定制.
4. Click the <b>Submit</b> button to begin customization. 5. 分享美图给你的好友吧, enjoy😊!
5. Share your customized photo with your friends and enjoy! 😊
""" """
article = r""" article = r"""
...@@ -342,7 +347,6 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F ...@@ -342,7 +347,6 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
with gr.Row(): with gr.Row():
with gr.Column(): with gr.Column():
# upload face image # upload face image
face_file = gr.Image(label="Upload a photo of your face", type="filepath") face_file = gr.Image(label="Upload a photo of your face", type="filepath")
...@@ -411,7 +415,7 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F ...@@ -411,7 +415,7 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
with gr.Column(): with gr.Column():
gallery = gr.Image(label="Generated Images") gallery = gr.Image(label="Generated Images")
usage_tips = gr.Markdown(label="Usage tips of InstantID", value=tips ,visible=False) usage_tips = gr.Markdown(label="Usage tips of InstantID", value=tips, visible=False)
submit.click( submit.click(
fn=remove_tips, fn=remove_tips,
...@@ -424,7 +428,8 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F ...@@ -424,7 +428,8 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
api_name=False, api_name=False,
).then( ).then(
fn=generate_image, fn=generate_image,
inputs=[face_file, pose_file, prompt, negative_prompt, style, num_steps, identitynet_strength_ratio, adapter_strength_ratio, guidance_scale, seed, enable_LCM, enhance_face_region], inputs=[face_file, pose_file, prompt, negative_prompt, style, num_steps, identitynet_strength_ratio,
adapter_strength_ratio, guidance_scale, seed, enable_LCM, enhance_face_region],
outputs=[gallery, usage_tips] outputs=[gallery, usage_tips]
) )
...@@ -441,7 +446,8 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F ...@@ -441,7 +446,8 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
gr.Markdown(article) gr.Markdown(article)
demo.launch(server_name='0.0.0.0') demo.launch(server_name='0.0.0.0', share=True)
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment