Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,34 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
import gradio as gr
|
| 3 |
from clip_interrogator import Config, Interrogator
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
# MODELS = ['ViT-L (best for Stable Diffusion 1.*)',]
|
| 7 |
|
| 8 |
# load BLIP and ViT-L https://huggingface.co/openai/clip-vit-large-patch14
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
def image_analysis(image):
|
| 15 |
image = image.convert('RGB')
|
| 16 |
image_features = ci.image_to_features(image)
|
| 17 |
|
|
@@ -30,7 +47,22 @@ def image_analysis(image):
|
|
| 30 |
return medium_ranks, artist_ranks, movement_ranks, trending_ranks, flavor_ranks
|
| 31 |
|
| 32 |
|
| 33 |
-
def image_to_prompt(image, mode):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
image = image.convert('RGB')
|
| 35 |
if mode == 'best':
|
| 36 |
prompt = ci.interrogate(image)
|
|
@@ -41,7 +73,7 @@ def image_to_prompt(image, mode):
|
|
| 41 |
elif mode == 'negative':
|
| 42 |
prompt = ci.interrogate_negative(image)
|
| 43 |
|
| 44 |
-
return prompt
|
| 45 |
|
| 46 |
|
| 47 |
TITLE = """
|
|
@@ -61,7 +93,7 @@ TITLE = """
|
|
| 61 |
<p style="margin-bottom: 10px; font-size: 94%">
|
| 62 |
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!
|
| 63 |
</p>
|
| 64 |
-
<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/
|
| 65 |
</div>
|
| 66 |
"""
|
| 67 |
|
|
@@ -72,11 +104,9 @@ ARTICLE = """
|
|
| 72 |
and <a href="https://pixabay.com/illustrations/animal-painting-cat-feline-pet-7154059/">Lin Tong</a>
|
| 73 |
from pixabay.com
|
| 74 |
</p>
|
| 75 |
-
|
| 76 |
<p>
|
| 77 |
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>
|
| 78 |
</p>
|
| 79 |
-
|
| 80 |
<p>
|
| 81 |
Has this been helpful to you? Follow me on twitter
|
| 82 |
<a href="https://twitter.com/pharmapsychotic">@pharmapsychotic</a><br>
|
|
@@ -126,14 +156,14 @@ def analyze_tab():
|
|
| 126 |
trending = gr.Label(label="Trending", num_top_classes=5)
|
| 127 |
flavor = gr.Label(label="Flavor", num_top_classes=5)
|
| 128 |
|
| 129 |
-
button = gr.Button("Analyze")
|
| 130 |
button.click(image_analysis, inputs=[image, model], outputs=[medium, artist, movement, trending, flavor])
|
| 131 |
|
| 132 |
examples=[['example01.jpg', MODELS[0]], ['example02.jpg', MODELS[0]]]
|
| 133 |
ex = gr.Examples(
|
| 134 |
examples=examples,
|
| 135 |
fn=image_analysis,
|
| 136 |
-
inputs=[input_image],
|
| 137 |
outputs=[medium, artist, movement, trending, flavor],
|
| 138 |
cache_examples=True,
|
| 139 |
run_on_click=True
|
|
@@ -149,16 +179,22 @@ with gr.Blocks(css=CSS) as block:
|
|
| 149 |
with gr.Row():
|
| 150 |
input_image = gr.Image(type='pil', elem_id="input-img")
|
| 151 |
with gr.Column():
|
|
|
|
| 152 |
input_mode = gr.Radio(['best', 'fast', 'classic', 'negative'], value='best', label='Mode')
|
| 153 |
-
submit_btn = gr.Button("Submit")
|
| 154 |
output_text = gr.Textbox(label="Output", elem_id="output-txt")
|
| 155 |
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
ex = gr.Examples(
|
| 158 |
examples=examples,
|
| 159 |
fn=image_to_prompt,
|
| 160 |
-
inputs=[input_image, input_mode],
|
| 161 |
-
outputs=[output_text],
|
| 162 |
cache_examples=True,
|
| 163 |
run_on_click=True
|
| 164 |
)
|
|
@@ -171,8 +207,9 @@ with gr.Blocks(css=CSS) as block:
|
|
| 171 |
|
| 172 |
submit_btn.click(
|
| 173 |
fn=image_to_prompt,
|
| 174 |
-
inputs=[input_image, input_mode],
|
| 175 |
-
outputs=[output_text]
|
| 176 |
)
|
|
|
|
| 177 |
|
| 178 |
-
block.queue(max_size=64).launch(show_api=False)
|
|
|
|
| 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 |
+
MODELS = ['ViT-L (best for Stable Diffusion 1.*)']#, 'ViT-H (best for Stable Diffusion 2.*)']
|
|
|
|
| 7 |
|
| 8 |
# load BLIP and ViT-L https://huggingface.co/openai/clip-vit-large-patch14
|
| 9 |
+
config = Config(clip_model_name="ViT-L-14/openai")
|
| 10 |
+
ci_vitl = Interrogator(config)
|
| 11 |
+
# ci_vitl.clip_model = ci_vitl.clip_model.to("cpu")
|
| 12 |
+
|
| 13 |
+
# load ViT-H https://huggingface.co/laion/CLIP-ViT-H-14-laion2B-s32B-b79K
|
| 14 |
+
# config.blip_model = ci_vitl.blip_model
|
| 15 |
+
# config.clip_model_name = "ViT-H-14/laion2b_s32b_b79k"
|
| 16 |
+
# ci_vith = Interrogator(config)
|
| 17 |
+
# ci_vith.clip_model = ci_vith.clip_model.to("cpu")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def image_analysis(image, clip_model_name):
|
| 21 |
+
# move selected model to GPU and other model to CPU
|
| 22 |
+
# if clip_model_name == MODELS[0]:
|
| 23 |
+
# ci_vith.clip_model = ci_vith.clip_model.to("cpu")
|
| 24 |
+
# ci_vitl.clip_model = ci_vitl.clip_model.to(ci_vitl.device)
|
| 25 |
+
# ci = ci_vitl
|
| 26 |
+
# else:
|
| 27 |
+
# ci_vitl.clip_model = ci_vitl.clip_model.to("cpu")
|
| 28 |
+
# ci_vith.clip_model = ci_vith.clip_model.to(ci_vith.device)
|
| 29 |
+
# ci = ci_vith
|
| 30 |
+
ci = ci_vitl
|
| 31 |
|
|
|
|
| 32 |
image = image.convert('RGB')
|
| 33 |
image_features = ci.image_to_features(image)
|
| 34 |
|
|
|
|
| 47 |
return medium_ranks, artist_ranks, movement_ranks, trending_ranks, flavor_ranks
|
| 48 |
|
| 49 |
|
| 50 |
+
def image_to_prompt(image, clip_model_name, mode):
|
| 51 |
+
# move selected model to GPU and other model to CPU
|
| 52 |
+
# if clip_model_name == MODELS[0]:
|
| 53 |
+
# ci_vith.clip_model = ci_vith.clip_model.to("cpu")
|
| 54 |
+
# ci_vitl.clip_model = ci_vitl.clip_model.to(ci_vitl.device)
|
| 55 |
+
# ci = ci_vitl
|
| 56 |
+
# else:
|
| 57 |
+
# ci_vitl.clip_model = ci_vitl.clip_model.to("cpu")
|
| 58 |
+
# ci_vith.clip_model = ci_vith.clip_model.to(ci_vith.device)
|
| 59 |
+
# ci = ci_vith
|
| 60 |
+
ci = ci_vitl
|
| 61 |
+
|
| 62 |
+
ci.config.blip_num_beams = 64
|
| 63 |
+
ci.config.chunk_size = 2048
|
| 64 |
+
ci.config.flavor_intermediate_count = 2048 if clip_model_name == MODELS[0] else 1024
|
| 65 |
+
|
| 66 |
image = image.convert('RGB')
|
| 67 |
if mode == 'best':
|
| 68 |
prompt = ci.interrogate(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 = """
|
|
|
|
| 93 |
<p style="margin-bottom: 10px; font-size: 94%">
|
| 94 |
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!
|
| 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 |
|
|
|
|
| 104 |
and <a href="https://pixabay.com/illustrations/animal-painting-cat-feline-pet-7154059/">Lin Tong</a>
|
| 105 |
from pixabay.com
|
| 106 |
</p>
|
|
|
|
| 107 |
<p>
|
| 108 |
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>
|
| 109 |
</p>
|
|
|
|
| 110 |
<p>
|
| 111 |
Has this been helpful to you? Follow me on twitter
|
| 112 |
<a href="https://twitter.com/pharmapsychotic">@pharmapsychotic</a><br>
|
|
|
|
| 156 |
trending = gr.Label(label="Trending", num_top_classes=5)
|
| 157 |
flavor = gr.Label(label="Flavor", num_top_classes=5)
|
| 158 |
|
| 159 |
+
button = gr.Button("Analyze", api_name="image-analysis")
|
| 160 |
button.click(image_analysis, inputs=[image, model], outputs=[medium, artist, movement, trending, flavor])
|
| 161 |
|
| 162 |
examples=[['example01.jpg', MODELS[0]], ['example02.jpg', MODELS[0]]]
|
| 163 |
ex = gr.Examples(
|
| 164 |
examples=examples,
|
| 165 |
fn=image_analysis,
|
| 166 |
+
inputs=[input_image, input_model],
|
| 167 |
outputs=[medium, artist, movement, trending, flavor],
|
| 168 |
cache_examples=True,
|
| 169 |
run_on_click=True
|
|
|
|
| 179 |
with gr.Row():
|
| 180 |
input_image = gr.Image(type='pil', elem_id="input-img")
|
| 181 |
with gr.Column():
|
| 182 |
+
input_model = gr.Dropdown(MODELS, value=MODELS[0], label='CLIP Model')
|
| 183 |
input_mode = gr.Radio(['best', 'fast', 'classic', 'negative'], value='best', label='Mode')
|
| 184 |
+
submit_btn = gr.Button("Submit", api_name="image-to-prompt")
|
| 185 |
output_text = gr.Textbox(label="Output", elem_id="output-txt")
|
| 186 |
|
| 187 |
+
with gr.Group(elem_id="share-btn-container"):
|
| 188 |
+
community_icon = gr.HTML(community_icon_html, visible=False)
|
| 189 |
+
loading_icon = gr.HTML(loading_icon_html, visible=False)
|
| 190 |
+
share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
|
| 191 |
+
|
| 192 |
+
examples=[['example01.jpg', MODELS[0], 'best'], ['example02.jpg', MODELS[0], 'best']]
|
| 193 |
ex = gr.Examples(
|
| 194 |
examples=examples,
|
| 195 |
fn=image_to_prompt,
|
| 196 |
+
inputs=[input_image, input_model, input_mode],
|
| 197 |
+
outputs=[output_text, share_button, community_icon, loading_icon],
|
| 198 |
cache_examples=True,
|
| 199 |
run_on_click=True
|
| 200 |
)
|
|
|
|
| 207 |
|
| 208 |
submit_btn.click(
|
| 209 |
fn=image_to_prompt,
|
| 210 |
+
inputs=[input_image, input_model, input_mode],
|
| 211 |
+
outputs=[output_text, share_button, community_icon, loading_icon]
|
| 212 |
)
|
| 213 |
+
share_button.click(None, [], [], _js=share_js)
|
| 214 |
|
| 215 |
+
block.queue(max_size=64).launch(show_api=False)
|