Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,9 @@
|
|
| 1 |
-
|
| 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 |
-
|
| 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
|
|
@@ -46,7 +14,6 @@ config.blip_num_beams = 64
|
|
| 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')
|
|
@@ -56,25 +23,14 @@ def inference(input_images, mode, best_max_flavors):
|
|
| 56 |
prompt_result = ci.interrogate_classic(image)
|
| 57 |
else:
|
| 58 |
prompt_result = ci.interrogate_fast(image)
|
| 59 |
-
prompt_results.append((image, prompt_result))
|
| 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!
|
|
@@ -85,7 +41,6 @@ title = """
|
|
| 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>
|
|
@@ -99,7 +54,7 @@ article = """
|
|
| 99 |
"""
|
| 100 |
|
| 101 |
css = '''
|
| 102 |
-
#col-container {width:
|
| 103 |
a {text-decoration-line: underline; font-weight: 600;}
|
| 104 |
.animate-spin {
|
| 105 |
animation: spin 1s linear infinite;
|
|
@@ -141,37 +96,33 @@ a {text-decoration-line: underline; font-weight: 600;}
|
|
| 141 |
#gallery .caption {
|
| 142 |
padding: var(--size-2) var(--size-3) !important;
|
| 143 |
text-overflow: clip !important;
|
| 144 |
-
white-space: normal !important;
|
| 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
|
| 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='
|
| 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 |
-
|
| 172 |
|
| 173 |
-
|
| 174 |
-
submit_btn.click(fn=inference, inputs=[input_image,mode_input,flavor_input], outputs=[gallery], api_name="clipi2")
|
| 175 |
|
| 176 |
-
|
| 177 |
block.queue().launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import io
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
from clip_interrogator import Config, Interrogator
|
| 6 |
+
|
| 7 |
config = Config()
|
| 8 |
config.device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 9 |
config.blip_offload = False if torch.cuda.is_available() else True
|
|
|
|
| 14 |
ci = Interrogator(config)
|
| 15 |
|
| 16 |
def inference(input_images, mode, best_max_flavors):
|
|
|
|
| 17 |
prompt_results = []
|
| 18 |
for image_bytes in input_images:
|
| 19 |
image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
|
|
|
|
| 23 |
prompt_result = ci.interrogate_classic(image)
|
| 24 |
else:
|
| 25 |
prompt_result = ci.interrogate_fast(image)
|
| 26 |
+
prompt_results.append((image, prompt_result))
|
| 27 |
return prompt_results
|
| 28 |
|
|
|
|
| 29 |
title = """
|
| 30 |
<div style="text-align: center; max-width: 500px; margin: 0 auto;">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
<h1 style="font-weight: 600; margin-bottom: 7px;">
|
| 32 |
CLIP Interrogator 2.1
|
| 33 |
</h1>
|
|
|
|
| 34 |
<p style="margin-bottom: 10px;font-size: 94%;font-weight: 100;line-height: 1.5em;">
|
| 35 |
Want to figure out what a good prompt might be to create new images like an existing one?
|
| 36 |
<br />The CLIP Interrogator is here to get you answers!
|
|
|
|
| 41 |
|
| 42 |
article = """
|
| 43 |
<div style="text-align: center; max-width: 500px; margin: 0 auto;font-size: 94%;">
|
|
|
|
| 44 |
<p>
|
| 45 |
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>
|
| 46 |
</p>
|
|
|
|
| 54 |
"""
|
| 55 |
|
| 56 |
css = '''
|
| 57 |
+
#col-container {width: 80%; margin-left: auto; margin-right: auto;}
|
| 58 |
a {text-decoration-line: underline; font-weight: 600;}
|
| 59 |
.animate-spin {
|
| 60 |
animation: spin 1s linear infinite;
|
|
|
|
| 96 |
#gallery .caption {
|
| 97 |
padding: var(--size-2) var(--size-3) !important;
|
| 98 |
text-overflow: clip !important;
|
| 99 |
+
white-space: normal !important;
|
| 100 |
color: var(--block-label-text-color) !important;
|
| 101 |
font-weight: var(--weight-semibold) !important;
|
| 102 |
text-align: center !important;
|
| 103 |
height: 100% !important;
|
| 104 |
font-size: 17px !important;
|
| 105 |
}
|
|
|
|
| 106 |
'''
|
| 107 |
|
| 108 |
with gr.Blocks(css=css) as block:
|
| 109 |
with gr.Column(elem_id="col-container"):
|
| 110 |
gr.HTML(title)
|
| 111 |
|
| 112 |
+
input_image = gr.Files(label="Inputs", file_count="multiple", type='file', elem_id='inputs')
|
| 113 |
with gr.Row():
|
| 114 |
mode_input = gr.Radio(['best', 'classic', 'fast'], label='Select mode', value='best')
|
| 115 |
+
flavor_input = gr.Slider(minimum=2, maximum=24, step=2, value=4, label='Best mode max flavors')
|
| 116 |
|
| 117 |
submit_btn = gr.Button("Submit")
|
| 118 |
|
|
|
|
| 119 |
gallery = gr.Gallery(
|
| 120 |
label="Outputs", show_label=True, elem_id="gallery", object_fit="contain", height="auto"
|
| 121 |
)
|
| 122 |
|
| 123 |
with gr.Group(elem_id="share-btn-container"):
|
| 124 |
+
gr.HTML(article)
|
| 125 |
|
| 126 |
+
submit_btn.click(fn=inference, inputs=[input_image, mode_input, flavor_input], outputs=[gallery], api_name="clipi2")
|
|
|
|
| 127 |
|
|
|
|
| 128 |
block.queue().launch()
|