Flyxion/CLIP-Interrogator
0
1#!/usr/bin/env python32import gradio as gr3import os4from clip_interrogator import Config, Interrogator5from huggingface_hub import hf_hub_download6from share_btn import community_icon_html, loading_icon_html, share_js7 8MODELS = ['ViT-L (best for Stable Diffusion 1.*)', 'ViT-H (best for Stable Diffusion 2.*)']9 10# download preprocessed files11PREPROCESS_FILES = [12 'ViT-H-14_laion2b_s32b_b79k_artists.pkl',13 'ViT-H-14_laion2b_s32b_b79k_flavors.pkl',14 'ViT-H-14_laion2b_s32b_b79k_mediums.pkl',15 'ViT-H-14_laion2b_s32b_b79k_movements.pkl',16 'ViT-H-14_laion2b_s32b_b79k_trendings.pkl',17 'ViT-L-14_openai_artists.pkl',18 'ViT-L-14_openai_flavors.pkl',19 'ViT-L-14_openai_mediums.pkl',20 'ViT-L-14_openai_movements.pkl',21 'ViT-L-14_openai_trendings.pkl',22]23print("Download preprocessed cache files...")24for file in PREPROCESS_FILES:25 path = hf_hub_download(repo_id="pharma/ci-preprocess", filename=file, cache_dir="cache")26 cache_path = os.path.dirname(path)27 28 29# load BLIP and ViT-L https://huggingface.co/openai/clip-vit-large-patch1430config = Config(cache_path=cache_path, clip_model_path="cache", clip_model_name="ViT-L-14/openai")31ci_vitl = Interrogator(config)32ci_vitl.clip_model = ci_vitl.clip_model.to("cpu")33 34# load ViT-H https://huggingface.co/laion/CLIP-ViT-H-14-laion2B-s32B-b79K35config.blip_model = ci_vitl.blip_model36config.clip_model_name = "ViT-H-14/laion2b_s32b_b79k"37ci_vith = Interrogator(config)38ci_vith.clip_model = ci_vith.clip_model.to("cpu")39 40 41def inference(image, clip_model_name, mode):42 43 # move selected model to GPU and other model to CPU44 if clip_model_name == MODELS[0]:45 ci_vith.clip_model = ci_vith.clip_model.to("cpu")46 ci_vitl.clip_model = ci_vitl.clip_model.to(ci_vitl.device)47 ci = ci_vitl48 else:49 ci_vitl.clip_model = ci_vitl.clip_model.to("cpu")50 ci_vith.clip_model = ci_vith.clip_model.to(ci_vith.device)51 ci = ci_vith52 53 ci.config.blip_num_beams = 6454 ci.config.chunk_size = 204855 ci.config.flavor_intermediate_count = 2048 if clip_model_name == MODELS[0] else 102456 57 image = image.convert('RGB')58 if mode == 'best':59 prompt = ci.interrogate(image)60 elif mode == 'classic':61 prompt = ci.interrogate_classic(image)62 else:63 prompt = ci.interrogate_fast(image)64 65 return prompt, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)66 67 68TITLE = """69 <div style="text-align: center; max-width: 650px; margin: 0 auto;">70 <div71 style="72 display: inline-flex;73 align-items: center;74 gap: 0.8rem;75 font-size: 1.75rem;76 "77 >78 <h1 style="font-weight: 900; margin-bottom: 7px;">79 CLIP Interrogator80 </h1>81 </div>82 <p style="margin-bottom: 10px; font-size: 94%">83 Want to figure out what a good prompt might be to create new images like an existing one?<br>The CLIP Interrogator is here to get you answers!84 </p>85 <p>You can skip the queue by duplicating this space and upgrading to gpu in settings: <a style='display:inline-block' href='https://huggingface.co/spaces/pharma/CLIP-Interrogator?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14' alt='Duplicate Space'></a></p>86 </div>87"""88 89ARTICLE = """90<div style="text-align: center; max-width: 650px; margin: 0 auto;">91 <p>92 Example art by <a href="https://pixabay.com/illustrations/watercolour-painting-art-effect-4799014/">Layers</a> 93 and <a href="https://pixabay.com/illustrations/animal-painting-cat-feline-pet-7154059/">Lin Tong</a> 94 from pixabay.com95 </p>96 97 <p>98 Server busy? You can also run on <a href="https://colab.research.google.com/github/pharmapsychotic/clip-interrogator/blob/main/clip_interrogator.ipynb">Google Colab</a>99 </p>100 101 <p>102 Has this been helpful to you? Follow me on twitter 103 <a href="https://twitter.com/pharmapsychotic">@pharmapsychotic</a><br>104 and check out more tools at my105 <a href="https://pharmapsychotic.com/tools.html">Ai generative art tools list</a>106 </p>107</div>108"""109 110CSS = '''111#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}112a {text-decoration-line: underline; font-weight: 600;}113.animate-spin {114 animation: spin 1s linear infinite;115}116@keyframes spin {117 from { transform: rotate(0deg); }118 to { transform: rotate(360deg); }119}120#share-btn-container {121 display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;122}123#share-btn {124 all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;125}126#share-btn * {127 all: unset;128}129#share-btn-container div:nth-child(-n+2){130 width: auto !important;131 min-height: 0px !important;132}133#share-btn-container .wrap {134 display: none !important;135}136'''137 138with gr.Blocks(css=CSS) as block:139 with gr.Column(elem_id="col-container"):140 gr.HTML(TITLE)141 142 input_image = gr.Image(type='pil', elem_id="input-img")143 input_model = gr.Dropdown(MODELS, value=MODELS[0], label='CLIP Model')144 input_mode = gr.Radio(['best', 'fast'], value='best', label='Mode')145 submit_btn = gr.Button("Submit")146 output_text = gr.Textbox(label="Output", elem_id="output-txt")147 148 with gr.Group(elem_id="share-btn-container"):149 community_icon = gr.HTML(community_icon_html, visible=False)150 loading_icon = gr.HTML(loading_icon_html, visible=False)151 share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)152 153 examples=[['example01.jpg', MODELS[0], 'best'], ['example02.jpg', MODELS[0], 'best']]154 ex = gr.Examples(155 examples=examples, 156 fn=inference, 157 inputs=[input_image, input_model, input_mode], 158 outputs=[output_text, share_button, community_icon, loading_icon], 159 cache_examples=True, 160 run_on_click=True161 )162 ex.dataset.headers = [""]163 164 gr.HTML(ARTICLE)165 166 submit_btn.click(167 fn=inference, 168 inputs=[input_image, input_model, input_mode], 169 outputs=[output_text, share_button, community_icon, loading_icon]170 )171 share_button.click(None, [], [], _js=share_js)172 173block.queue(max_size=32).launch(show_api=False)174 