Anime-Pack / app.py
shellypeng's picture
Upload app.py
5b9202b
raw
history blame
7.49 kB
import gradio as gr
import numpy as np
from PIL import Image
from transformers import pipeline
from diffusers import StableDiffusionDepth2ImgPipeline, StableDiffusionPipeline, StableDiffusionControlNetPipeline, StableDiffusionUpscalePipeline, StableDiffusionImg2ImgPipeline, AutoPipelineForImage2Image
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler, DPMSolverMultistepScheduler
import torch
from controlnet_aux import HEDdetector
from diffusers.utils import load_image
from huggingface_hub import notebook_login, login
import concurrent.futures
from threading import Thread
hidden_booster_text = "beautiful face, beautiful hand, small boobs, a cup"
hidden_negative = "big boobs, huge boobs, sexy, dirty, d cup, e cup, g cup, slutty, badhandv4, ng_deepnegative_v1_75t, worst quality, low quality, extra digits, text, signature, bad anatomy, mutated hand, error, missing finger, cropped, worse quality, bad quality, lowres, floating limbs, bad hands, anatomical nonsense"
hed = HEDdetector.from_pretrained('lllyasviel/ControlNet')
controlnet_scribble = ControlNetModel.from_pretrained(
"lllyasviel/sd-controlnet-scribble", torch_dtype=torch.float16
)
pipe_scribble = StableDiffusionControlNetPipeline.from_single_file(
"https://huggingface.co/shellypeng/anime-god/blob/main/animeGod_v10.safetensors", controlnet=controlnet_scribble,
torch_dtype=torch.float16,
)
pipe_scribble.load_textual_inversion("shellypeng/bad-prompt")
pipe_scribble.load_textual_inversion("shellypeng/badhandv4")
# pipe.load_textual_inversion("shellypeng/easynegative")
pipe_scribble.load_textual_inversion("shellypeng/deepnegative")
pipe_scribble.load_textual_inversion("shellypeng/verybadimagenegative")
pipe_scribble.scheduler = DPMSolverMultistepScheduler.from_config(pipe_scribble.scheduler.config, use_karras_sigmas=True)
# pipe.enable_model_cpu_offload()
def dummy(images, **kwargs):
return images, False
pipe_scribble.safety_checker = lambda images, **kwargs: (images, [False] * len(images))
pipe_scribble.to("cuda")
def scribble_to_image(text, input_img):
"""
pass the sd model and do scribble to image
include Adetailer, detail tweaker lora, prompt backend include: beautiful eyes, beautiful face, beautiful hand, (maybe infer from user's prompt for gesture and facial
expression to improve hand)
"""
# change param "bag" below to text, image param below to input_img
input_img = Image.fromarray(input_img)
input_img = hed(input_img, scribble=True)
input_img = load_image(input_img)
# global prompt
prompt = text + hidden_booster_text
res_image0 = pipe_scribble(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
res_image1 = pipe_scribble(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
res_image2 = pipe_scribble(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
res_image3 = pipe_scribble(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
return res_image0, res_image1, res_image2, res_image3
theme = gr.themes.Soft(
primary_hue="orange",
secondary_hue="orange",
).set(
block_background_fill='*primary_50'
)
pipe_img2img = StableDiffusionImg2ImgPipeline.from_single_file("https://huggingface.co/shellypeng/anime-god/blob/main/animeGod_v10.safetensors",
torch_dtype=torch.float16)
pipe_img2img.load_lora_weights("shellypeng/detail-tweaker")
pipe_img2img.fuse_lora(lora_scale=0.1)
pipe_img2img.load_textual_inversion("shellypeng/bad-prompt")
pipe_img2img.load_textual_inversion("shellypeng/badhandv4")
# pipe.load_textual_inversion("shellypeng/easynegative")
pipe_img2img.load_textual_inversion("shellypeng/deepnegative")
pipe_img2img.load_textual_inversion("shellypeng/verybadimagenegative")
pipe_img2img.scheduler = DPMSolverMultistepScheduler.from_config(pipe_img2img.scheduler.config, use_karras_sigmas=True)
# pipe.enable_model_cpu_offload()
def dummy(images, **kwargs):
return images, False
pipe_img2img.safety_checker = lambda images, **kwargs: (images, [False] * len(images))
pipe_img2img = pipe_img2img.to("cuda")
def real_img2img_to_anime(text, input_img):
"""
pass the sd model and do scribble to image
include Adetailer, detail tweaker lora, prompt backend include: beautiful eyes, beautiful face, beautiful hand, (maybe infer from user's prompt for gesture and facial
expression to improve hand)
"""
input_img = Image.fromarray(input_img)
input_img = load_image(input_img)
prompt = text + hidden_booster_text
# input_img = depth_estimator(input_img)['depth']
res_image0 = pipe_img2img(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
res_image1 = pipe_img2img(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
res_image2 = pipe_img2img(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
res_image3 = pipe_img2img(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
return res_image0, res_image1, res_image2, res_image3
theme = gr.themes.Soft(
primary_hue="orange",
secondary_hue="orange",
).set(
block_background_fill='*primary_50'
)
generator = torch.manual_seed(33)
with gr.Blocks(theme=theme, css="footer {visibility: hidden}", title="ShellAI Apps") as iface:
with gr.Tab("Animefier"):
with gr.Row(equal_height=True):
with gr.Column():
prompt_box = gr.Textbox(label="Prompt", placeholder="Enter a prompt")
image_box = gr.Image(label="Input Image", height=350)
gen_btn = gr.Button(value="Generate")
with gr.Row(equal_height=True):
global image1
global image2
global image3
global image4
image1 = gr.Image()
image2 = gr.Image()
image3 = gr.Image()
image4 = gr.Image()
def mult_thread(prompt_box, image_box):
with concurrent.futures.ThreadPoolExecutor(max_workers=12000) as executor:
future = executor.submit(real_img2img_to_anime, prompt_box, image_box)
image1, image2, image3, image4 = future.result()
return image1, image2, image3, image4
gen_btn.click(mult_thread, [prompt_box, image_box], [image1, image2, image3, image4])
with gr.Tab("AniSketch"):
with gr.Row(equal_height=True):
with gr.Column():
prompt_box = gr.Textbox(label="Prompt", placeholder="Enter a prompt")
image_box = gr.Image(label="Input Image", height=350)
gen_btn = gr.Button(value="Generate")
with gr.Row(equal_height=True):
image1 = gr.Image()
image2 = gr.Image()
image3 = gr.Image()
image4 = gr.Image()
def mult_thread(prompt_box, image_box):
with concurrent.futures.ThreadPoolExecutor(max_workers=12000) as executor:
future = executor.submit(scribble_to_image, prompt_box, image_box)
image1, image2, image3, image4 = future.result()
return image1, image2, image3, image4
gen_btn.click(mult_thread, [prompt_box, image_box], [image1, image2, image3, image4])
iface.launch(inline=False)