Update app.py
Browse files
app.py
CHANGED
|
@@ -1,217 +1,177 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from clip_interrogator import Config, Interrogator
|
| 4 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
#
|
| 15 |
-
#
|
| 16 |
-
#
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
prompt = ci.interrogate(image)
|
| 69 |
-
elif mode == 'classic':
|
| 70 |
-
prompt = ci.interrogate_classic(image)
|
| 71 |
-
elif mode == 'fast':
|
| 72 |
-
prompt = ci.interrogate_fast(image)
|
| 73 |
-
elif mode == 'negative':
|
| 74 |
-
prompt = ci.interrogate_negative(image)
|
| 75 |
-
|
| 76 |
-
return prompt, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
TITLE = """
|
| 80 |
-
<div style="text-align: center; max-width: 650px; margin: 0 auto;">
|
| 81 |
<div
|
| 82 |
style="
|
| 83 |
display: inline-flex;
|
| 84 |
align-items: center;
|
| 85 |
gap: 0.8rem;
|
| 86 |
font-size: 1.75rem;
|
|
|
|
| 87 |
"
|
| 88 |
>
|
| 89 |
-
<h1 style="font-weight:
|
| 90 |
-
CLIP Interrogator
|
| 91 |
</h1>
|
| 92 |
</div>
|
| 93 |
-
<p style="margin-bottom: 10px;
|
| 94 |
-
Want to figure out what a good prompt might be to create new images like an existing one?
|
|
|
|
|
|
|
| 95 |
</p>
|
| 96 |
-
<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/pharmapsychotic/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>
|
| 97 |
</div>
|
| 98 |
"""
|
| 99 |
|
| 100 |
-
|
| 101 |
-
<div style="text-align: center; max-width:
|
| 102 |
-
|
| 103 |
-
Example art by <a href="https://pixabay.com/illustrations/watercolour-painting-art-effect-4799014/">Layers</a>
|
| 104 |
-
and <a href="https://pixabay.com/illustrations/animal-painting-cat-feline-pet-7154059/">Lin Tong</a>
|
| 105 |
-
from pixabay.com
|
| 106 |
-
</p>
|
| 107 |
-
|
| 108 |
<p>
|
| 109 |
-
Server busy? You can also run on <a href="https://colab.research.google.com/github/pharmapsychotic/clip-interrogator/blob/
|
| 110 |
</p>
|
| 111 |
-
|
| 112 |
<p>
|
| 113 |
-
Has this been helpful to you? Follow
|
| 114 |
-
<a href="https://twitter.com/pharmapsychotic">@pharmapsychotic</a>
|
| 115 |
-
and check out more tools at
|
| 116 |
<a href="https://pharmapsychotic.com/tools.html">Ai generative art tools list</a>
|
| 117 |
</p>
|
| 118 |
</div>
|
| 119 |
"""
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
}
|
| 131 |
-
|
| 132 |
-
|
| 133 |
}
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
-
with gr.Column():
|
| 151 |
with gr.Row():
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
artist = gr.Label(label="Artist", num_top_classes=5)
|
| 157 |
-
movement = gr.Label(label="Movement", num_top_classes=5)
|
| 158 |
-
trending = gr.Label(label="Trending", num_top_classes=5)
|
| 159 |
-
flavor = gr.Label(label="Flavor", num_top_classes=5)
|
| 160 |
-
|
| 161 |
-
button = gr.Button("Analyze", api_name="image-analysis")
|
| 162 |
-
button.click(image_analysis, inputs=[image, model], outputs=[medium, artist, movement, trending, flavor])
|
| 163 |
-
|
| 164 |
-
examples=[['example01.jpg', MODELS[0]], ['example02.jpg', MODELS[0]]]
|
| 165 |
-
ex = gr.Examples(
|
| 166 |
-
examples=examples,
|
| 167 |
-
fn=image_analysis,
|
| 168 |
-
inputs=[input_image, input_model],
|
| 169 |
-
outputs=[medium, artist, movement, trending, flavor],
|
| 170 |
-
cache_examples=True,
|
| 171 |
-
run_on_click=True
|
| 172 |
-
)
|
| 173 |
-
ex.dataset.headers = [""]
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
with gr.Blocks(css=CSS) as block:
|
| 177 |
-
with gr.Column(elem_id="col-container"):
|
| 178 |
-
gr.HTML(TITLE)
|
| 179 |
-
|
| 180 |
-
with gr.Tab("Prompt"):
|
| 181 |
-
with gr.Row():
|
| 182 |
-
input_image = gr.Image(type='pil', elem_id="input-img")
|
| 183 |
-
with gr.Column():
|
| 184 |
-
input_model = gr.Dropdown(MODELS, value=MODELS[0], label='CLIP Model')
|
| 185 |
-
input_mode = gr.Radio(['best', 'fast', 'classic', 'negative'], value='best', label='Mode')
|
| 186 |
-
submit_btn = gr.Button("Submit", api_name="image-to-prompt")
|
| 187 |
-
output_text = gr.Textbox(label="Output", elem_id="output-txt")
|
| 188 |
-
|
| 189 |
-
with gr.Group(elem_id="share-btn-container"):
|
| 190 |
-
community_icon = gr.HTML(community_icon_html, visible=False)
|
| 191 |
-
loading_icon = gr.HTML(loading_icon_html, visible=False)
|
| 192 |
-
share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
|
| 193 |
-
|
| 194 |
-
examples=[['example01.jpg', MODELS[0], 'best'], ['example02.jpg', MODELS[0], 'best']]
|
| 195 |
-
ex = gr.Examples(
|
| 196 |
-
examples=examples,
|
| 197 |
-
fn=image_to_prompt,
|
| 198 |
-
inputs=[input_image, input_model, input_mode],
|
| 199 |
-
outputs=[output_text, share_button, community_icon, loading_icon],
|
| 200 |
-
cache_examples=True,
|
| 201 |
-
run_on_click=True
|
| 202 |
-
)
|
| 203 |
-
ex.dataset.headers = [""]
|
| 204 |
-
|
| 205 |
-
with gr.Tab("Analyze"):
|
| 206 |
-
analyze_tab()
|
| 207 |
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
inputs=[input_image, input_model, input_mode],
|
| 213 |
-
outputs=[output_text, share_button, community_icon, loading_icon]
|
| 214 |
-
)
|
| 215 |
-
share_button.click(None, [], [], _js=share_js)
|
| 216 |
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
| 2 |
|
| 3 |
+
import os, subprocess
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# def setup():
|
| 7 |
+
# install_cmds = [
|
| 8 |
+
# ['pip', 'install', 'ftfy', 'gradio', 'regex', 'tqdm', 'transformers==4.21.2', 'timm', 'fairscale', 'requests'],
|
| 9 |
+
# ['pip', 'install', 'open_clip_torch'],
|
| 10 |
+
# ['pip', 'install', '-e', 'git+https://github.com/pharmapsychotic/BLIP.git@lib#egg=blip'],
|
| 11 |
+
# ['git', 'clone', '-b', 'open-clip', 'https://github.com/pharmapsychotic/clip-interrogator.git']
|
| 12 |
+
# ]
|
| 13 |
+
# for cmd in install_cmds:
|
| 14 |
+
# print(subprocess.run(cmd, stdout=subprocess.PIPE).stdout.decode('utf-8'))
|
| 15 |
+
|
| 16 |
+
# setup()
|
| 17 |
+
|
| 18 |
+
# download cache files
|
| 19 |
+
# print("Download preprocessed cache files...")
|
| 20 |
+
# CACHE_URLS = [
|
| 21 |
+
# 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_artists.pkl',
|
| 22 |
+
# 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_flavors.pkl',
|
| 23 |
+
# 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_mediums.pkl',
|
| 24 |
+
# 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_movements.pkl',
|
| 25 |
+
# 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_trendings.pkl',
|
| 26 |
+
# ]
|
| 27 |
+
# os.makedirs('cache', exist_ok=True)
|
| 28 |
+
# for url in CACHE_URLS:
|
| 29 |
+
# print(subprocess.run(['wget', url, '-P', 'cache'], stdout=subprocess.PIPE).stdout.decode('utf-8'))
|
| 30 |
+
|
| 31 |
+
import sys
|
| 32 |
+
sys.path.append('src/blip')
|
| 33 |
+
sys.path.append('clip-interrogator')
|
| 34 |
+
|
| 35 |
+
import gradio as gr
|
| 36 |
+
from clip_interrogator import Config, Interrogator
|
| 37 |
+
import io
|
| 38 |
+
from PIL import Image
|
| 39 |
+
config = Config()
|
| 40 |
+
config.device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 41 |
+
config.blip_offload = False if torch.cuda.is_available() else True
|
| 42 |
+
config.chunk_size = 2048
|
| 43 |
+
config.flavor_intermediate_count = 512
|
| 44 |
+
config.blip_num_beams = 64
|
| 45 |
+
|
| 46 |
+
ci = Interrogator(config)
|
| 47 |
+
|
| 48 |
+
def inference(input_images, mode, best_max_flavors):
|
| 49 |
+
# Process each image in the list and generate prompt results
|
| 50 |
+
prompt_results = []
|
| 51 |
+
for image_bytes in input_images:
|
| 52 |
+
image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
|
| 53 |
+
if mode == 'best':
|
| 54 |
+
prompt_result = ci.interrogate(image, max_flavors=int(best_max_flavors))
|
| 55 |
+
elif mode == 'classic':
|
| 56 |
+
prompt_result = ci.interrogate_classic(image)
|
| 57 |
+
else:
|
| 58 |
+
prompt_result = ci.interrogate_fast(image)
|
| 59 |
+
prompt_results.append((image, prompt_result)) # Use dictionary to set image labels
|
| 60 |
+
return prompt_results
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
title = """
|
| 64 |
+
<div style="text-align: center; max-width: 500px; margin: 0 auto;">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
<div
|
| 66 |
style="
|
| 67 |
display: inline-flex;
|
| 68 |
align-items: center;
|
| 69 |
gap: 0.8rem;
|
| 70 |
font-size: 1.75rem;
|
| 71 |
+
margin-bottom: 10px;
|
| 72 |
"
|
| 73 |
>
|
| 74 |
+
<h1 style="font-weight: 600; margin-bottom: 7px;">
|
| 75 |
+
CLIP Interrogator 2.1
|
| 76 |
</h1>
|
| 77 |
</div>
|
| 78 |
+
<p style="margin-bottom: 10px;font-size: 94%;font-weight: 100;line-height: 1.5em;">
|
| 79 |
+
Want to figure out what a good prompt might be to create new images like an existing one?
|
| 80 |
+
<br />The CLIP Interrogator is here to get you answers!
|
| 81 |
+
<br />This version is specialized for producing nice prompts for use with Stable Diffusion 2.0 using the ViT-H-14 OpenCLIP model!
|
| 82 |
</p>
|
|
|
|
| 83 |
</div>
|
| 84 |
"""
|
| 85 |
|
| 86 |
+
article = """
|
| 87 |
+
<div style="text-align: center; max-width: 500px; margin: 0 auto;font-size: 94%;">
|
| 88 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
<p>
|
| 90 |
+
Server busy? You can also run on <a href="https://colab.research.google.com/github/pharmapsychotic/clip-interrogator/blob/open-clip/clip_interrogator.ipynb">Google Colab</a>
|
| 91 |
</p>
|
|
|
|
| 92 |
<p>
|
| 93 |
+
Has this been helpful to you? Follow Pharma on twitter
|
| 94 |
+
<a href="https://twitter.com/pharmapsychotic">@pharmapsychotic</a>
|
| 95 |
+
and check out more tools at his
|
| 96 |
<a href="https://pharmapsychotic.com/tools.html">Ai generative art tools list</a>
|
| 97 |
</p>
|
| 98 |
</div>
|
| 99 |
"""
|
| 100 |
|
| 101 |
+
css = '''
|
| 102 |
+
#col-container {width: width: 80%;; margin-left: auto; margin-right: auto;}
|
| 103 |
+
a {text-decoration-line: underline; font-weight: 600;}
|
| 104 |
+
.animate-spin {
|
| 105 |
+
animation: spin 1s linear infinite;
|
| 106 |
+
}
|
| 107 |
+
@keyframes spin {
|
| 108 |
+
from {
|
| 109 |
+
transform: rotate(0deg);
|
| 110 |
}
|
| 111 |
+
to {
|
| 112 |
+
transform: rotate(360deg);
|
| 113 |
}
|
| 114 |
+
}
|
| 115 |
+
#share-btn-container {
|
| 116 |
+
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;
|
| 117 |
+
}
|
| 118 |
+
#share-btn {
|
| 119 |
+
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;
|
| 120 |
+
}
|
| 121 |
+
#share-btn * {
|
| 122 |
+
all: unset;
|
| 123 |
+
}
|
| 124 |
+
#share-btn-container div:nth-child(-n+2){
|
| 125 |
+
width: auto !important;
|
| 126 |
+
min-height: 0px !important;
|
| 127 |
+
}
|
| 128 |
+
#share-btn-container .wrap {
|
| 129 |
+
display: none !important;
|
| 130 |
+
}
|
| 131 |
+
#gallery .caption-label {
|
| 132 |
+
font-size: 15px !important;
|
| 133 |
+
right: 0 !important;
|
| 134 |
+
max-width: 100% !important;
|
| 135 |
+
text-overflow: clip !important;
|
| 136 |
+
white-space: normal !important;
|
| 137 |
+
overflow: auto !important;
|
| 138 |
+
height: 20% !important;
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
#gallery .caption {
|
| 142 |
+
padding: var(--size-2) var(--size-3) !important;
|
| 143 |
+
text-overflow: clip !important;
|
| 144 |
+
white-space: normal !important; /* Allows the text to wrap */
|
| 145 |
+
color: var(--block-label-text-color) !important;
|
| 146 |
+
font-weight: var(--weight-semibold) !important;
|
| 147 |
+
text-align: center !important;
|
| 148 |
+
height: 100% !important;
|
| 149 |
+
font-size: 17px !important;
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
'''
|
| 153 |
+
|
| 154 |
+
with gr.Blocks(css=css) as block:
|
| 155 |
+
with gr.Column(elem_id="col-container"):
|
| 156 |
+
gr.HTML(title)
|
| 157 |
|
| 158 |
+
input_image = gr.Files(label = "Inputs", file_count="multiple", type='bytes', elem_id='inputs')
|
|
|
|
| 159 |
with gr.Row():
|
| 160 |
+
mode_input = gr.Radio(['best', 'classic', 'fast'], label='Select mode', value='best')
|
| 161 |
+
flavor_input = gr.Slider(minimum=2, maximum=24, step=2, value=4, label='best mode max flavors')
|
| 162 |
+
|
| 163 |
+
submit_btn = gr.Button("Submit")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
+
# rows, cols = NUM_IMAGES //3,
|
| 166 |
+
gallery = gr.Gallery(
|
| 167 |
+
label="Outputs", show_label=True, elem_id="gallery", object_fit="contain", height="auto"
|
| 168 |
+
)
|
| 169 |
|
| 170 |
+
with gr.Group(elem_id="share-btn-container"):
|
| 171 |
+
loading_icon = gr.HTML(loading_icon_html, visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
+
gr.HTML(article)
|
| 174 |
+
submit_btn.click(fn=inference, inputs=[input_image,mode_input,flavor_input], outputs=[gallery], api_name="clipi2")
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
block.queue(max_size=32,concurrency_count=10).launch(show_api=False)
|