CiaraRowles's picture
Update app.py
0f33a11 verified
raw
history blame
3.32 kB
import torch
import spaces
from diffusers import StableDiffusionPipeline, DDIMScheduler, AutoencoderKL
from transformers import AutoFeatureExtractor
from controlnet.callable_functions import process_single_image_both_ways,make_stylecode,process_single_image
from huggingface_hub import hf_hub_download
import gradio as gr
import cv2
base_model_path = "SG161222/Realistic_Vision_V4.0_noVAE"
vae_model_path = "stabilityai/sd-vae-ft-mse"
image_encoder_path = "laion/CLIP-ViT-H-14-laion2B-s32B-b79K"
ip_ckpt = hf_hub_download(repo_id="CiaraRowles/stylecodes", filename="stylecodes_sd15_v1.bin", repo_type="model")
device = "cuda"
cv2.setNumThreads(1)
@spaces.GPU(enable_queue=True)
def generate_image(images, prompt, negative_prompt,stylecode, progress=gr.Progress(track_tqdm=True)):
image = images
yield None
base_size = 512
# Calculate new width and height
image = process_single_image(ip_ckpt,image_path="",prompt=prompt,num_inference_steps=20,image=image,stylecode=stylecode)
yield image
@spaces.GPU(enable_queue=True)
def make_stylecode_gui (images, progress=gr.Progress(track_tqdm=True)):
code = make_stylecode(ip_ckpt,images)
return code
def swap_to_gallery(images):
return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
def remove_back_to_files():
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
css = '''
h1{margin-bottom: 0 !important}
'''
with gr.Blocks(css=css) as demo:
gr.Markdown("# IP-Adapter-Instruct demo")
gr.Markdown("Demo for the [CiaraRowles/IP-Adapter-Instruct model](https://huggingface.co/CiaraRowles/IP-Adapter-Instruct)")
with gr.Row():
with gr.Column():
files = gr.Image(
label="Input image",
type="pil"
)
uploaded_files = gr.Gallery(label="Your image", visible=False, columns=5, rows=1, height=125)
with gr.Column(visible=False) as clear_button:
remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
prompt = gr.Textbox(label="Prompt",
info="Try something like 'a photo of a man/woman/person'",
placeholder="A photo of a [man/woman/person]...")
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="low quality")
stylecode_gen = gr.Button("Make stylecode")
stylecode = gr.Textbox(label="stylecode")
submit = gr.Button("Use stylecode")
with gr.Column():
gallery = gr.Gallery(label="Generated Images")
stylecode_gen.click(fn=make_stylecode_gui,inputs=[files],outputs=stylecode)
submit.click(fn=generate_image,
inputs=[files, prompt, negative_prompt,stylecode],
outputs=gallery)
gr.Markdown("This demo includes extra features to mitigate the implicit bias of the model and prevent explicit usage of it to generate content with faces of people, including third parties, that is not safe for all audiences, including naked or semi-naked people.")
gr.Markdown("based on: https://huggingface.co/spaces/multimodalart/Ip-Adapter-FaceID")
demo.launch()