Spaces:
Running on Zero
Running on Zero
more agent and mcp friendly
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
import subprocess
|
|
|
|
| 4 |
import torch
|
| 5 |
|
| 6 |
|
|
@@ -12,7 +13,6 @@ def clone_if_missing(path, repo, branch=None):
|
|
| 12 |
if branch:
|
| 13 |
cmd += ["-b", branch]
|
| 14 |
cmd += [repo, path]
|
| 15 |
-
|
| 16 |
subprocess.run(cmd, check=True)
|
| 17 |
|
| 18 |
|
|
@@ -21,7 +21,6 @@ clone_if_missing(
|
|
| 21 |
"https://github.com/pharmapsychotic/BLIP.git",
|
| 22 |
branch="lib",
|
| 23 |
)
|
| 24 |
-
|
| 25 |
clone_if_missing(
|
| 26 |
"clip-interrogator",
|
| 27 |
"https://github.com/pharmapsychotic/clip-interrogator.git",
|
|
@@ -31,9 +30,7 @@ clone_if_missing(
|
|
| 31 |
sys.path.append("src/blip")
|
| 32 |
sys.path.append("clip-interrogator")
|
| 33 |
|
| 34 |
-
|
| 35 |
print("Download preprocessed cache files...")
|
| 36 |
-
|
| 37 |
CACHE_URLS = [
|
| 38 |
"https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_artists.pkl",
|
| 39 |
"https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_flavors.pkl",
|
|
@@ -41,13 +38,10 @@ CACHE_URLS = [
|
|
| 41 |
"https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_movements.pkl",
|
| 42 |
"https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_trendings.pkl",
|
| 43 |
]
|
| 44 |
-
|
| 45 |
os.makedirs("cache", exist_ok=True)
|
| 46 |
-
|
| 47 |
for url in CACHE_URLS:
|
| 48 |
subprocess.run(["wget", "-nc", url, "-P", "cache"], check=False)
|
| 49 |
|
| 50 |
-
|
| 51 |
import gradio as gr
|
| 52 |
from clip_interrogator import Config, Interrogator
|
| 53 |
|
|
@@ -62,47 +56,50 @@ config.blip_num_beams = 64
|
|
| 62 |
ci = Interrogator(config)
|
| 63 |
|
| 64 |
|
| 65 |
-
def
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
else:
|
| 76 |
-
result = ci.interrogate_fast(
|
| 77 |
|
| 78 |
-
print(f"mode {
|
| 79 |
return result
|
| 80 |
|
| 81 |
|
| 82 |
title = """
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
</p>
|
| 90 |
-
</div>
|
| 91 |
"""
|
| 92 |
|
| 93 |
article = """
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
<p>
|
| 100 |
-
Follow Pharma on twitter
|
| 101 |
-
<a href="https://twitter.com/pharmapsychotic">@pharmapsychotic</a>
|
| 102 |
-
and check out more tools at
|
| 103 |
-
<a href="https://pharmapsychotic.com/tools.html">Ai generative art tools list</a>
|
| 104 |
-
</p>
|
| 105 |
-
</div>
|
| 106 |
"""
|
| 107 |
|
| 108 |
css = """
|
|
@@ -111,27 +108,23 @@ css = """
|
|
| 111 |
margin-left: auto;
|
| 112 |
margin-right: auto;
|
| 113 |
}
|
| 114 |
-
|
| 115 |
a {
|
| 116 |
text-decoration-line: underline;
|
| 117 |
font-weight: 600;
|
| 118 |
}
|
| 119 |
"""
|
| 120 |
|
| 121 |
-
|
| 122 |
-
with gr.Blocks(css=css) as demo:
|
| 123 |
with gr.Column(elem_id="col-container"):
|
| 124 |
gr.HTML(title)
|
| 125 |
|
| 126 |
input_image = gr.Image(type="pil", elem_id="input-img")
|
| 127 |
-
|
| 128 |
with gr.Row():
|
| 129 |
mode_input = gr.Radio(
|
| 130 |
["best", "classic", "fast"],
|
| 131 |
label="Select mode",
|
| 132 |
value="best",
|
| 133 |
)
|
| 134 |
-
|
| 135 |
flavor_input = gr.Slider(
|
| 136 |
minimum=2,
|
| 137 |
maximum=24,
|
|
@@ -141,7 +134,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 141 |
)
|
| 142 |
|
| 143 |
submit_btn = gr.Button("Submit")
|
| 144 |
-
|
| 145 |
output_text = gr.Textbox(
|
| 146 |
label="Description Output",
|
| 147 |
elem_id="output-txt",
|
|
@@ -151,10 +143,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 151 |
["27E894C4-9375-48A1-A95D-CB2425416B4B.png", "best", 4],
|
| 152 |
["DB362F56-BA98-4CA1-A999-A25AA94B723B.png", "fast", 4],
|
| 153 |
]
|
| 154 |
-
|
| 155 |
gr.Examples(
|
| 156 |
examples=examples,
|
| 157 |
-
fn=
|
| 158 |
inputs=[input_image, mode_input, flavor_input],
|
| 159 |
outputs=[output_text],
|
| 160 |
cache_examples=False,
|
|
@@ -163,16 +154,16 @@ with gr.Blocks(css=css) as demo:
|
|
| 163 |
|
| 164 |
gr.HTML(article)
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
|
| 174 |
demo.queue(max_size=32).launch(
|
| 175 |
footer_links=["api"],
|
| 176 |
ssr_mode=False,
|
| 177 |
mcp_server=True,
|
|
|
|
| 178 |
)
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
import subprocess
|
| 4 |
+
|
| 5 |
import torch
|
| 6 |
|
| 7 |
|
|
|
|
| 13 |
if branch:
|
| 14 |
cmd += ["-b", branch]
|
| 15 |
cmd += [repo, path]
|
|
|
|
| 16 |
subprocess.run(cmd, check=True)
|
| 17 |
|
| 18 |
|
|
|
|
| 21 |
"https://github.com/pharmapsychotic/BLIP.git",
|
| 22 |
branch="lib",
|
| 23 |
)
|
|
|
|
| 24 |
clone_if_missing(
|
| 25 |
"clip-interrogator",
|
| 26 |
"https://github.com/pharmapsychotic/clip-interrogator.git",
|
|
|
|
| 30 |
sys.path.append("src/blip")
|
| 31 |
sys.path.append("clip-interrogator")
|
| 32 |
|
|
|
|
| 33 |
print("Download preprocessed cache files...")
|
|
|
|
| 34 |
CACHE_URLS = [
|
| 35 |
"https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_artists.pkl",
|
| 36 |
"https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_flavors.pkl",
|
|
|
|
| 38 |
"https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_movements.pkl",
|
| 39 |
"https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_trendings.pkl",
|
| 40 |
]
|
|
|
|
| 41 |
os.makedirs("cache", exist_ok=True)
|
|
|
|
| 42 |
for url in CACHE_URLS:
|
| 43 |
subprocess.run(["wget", "-nc", url, "-P", "cache"], check=False)
|
| 44 |
|
|
|
|
| 45 |
import gradio as gr
|
| 46 |
from clip_interrogator import Config, Interrogator
|
| 47 |
|
|
|
|
| 56 |
ci = Interrogator(config)
|
| 57 |
|
| 58 |
|
| 59 |
+
def generate_image_prompt(input_image, interrogation_mode: str, best_mode_max_flavors):
|
| 60 |
+
"""
|
| 61 |
+
Generate a Stable Diffusion 2.0 prompt description from an input image.
|
| 62 |
+
|
| 63 |
+
Use this tool when you need to interrogate an image with CLIP Interrogator 2.1 and produce a text prompt.
|
| 64 |
|
| 65 |
+
Args:
|
| 66 |
+
input_image: PIL image to describe.
|
| 67 |
+
interrogation_mode (str): Interrogation mode to use: best, classic, or fast.
|
| 68 |
+
best_mode_max_flavors: Maximum number of flavors used only when interrogation_mode is best.
|
| 69 |
|
| 70 |
+
Returns:
|
| 71 |
+
str: Generated prompt description, or an upload prompt when no image is provided.
|
| 72 |
+
"""
|
| 73 |
+
if input_image is None:
|
| 74 |
+
return "Please upload an image."
|
| 75 |
+
|
| 76 |
+
input_image = input_image.convert("RGB")
|
| 77 |
+
if interrogation_mode == "best":
|
| 78 |
+
result = ci.interrogate(input_image, max_flavors=int(best_mode_max_flavors))
|
| 79 |
+
elif interrogation_mode == "classic":
|
| 80 |
+
result = ci.interrogate_classic(input_image)
|
| 81 |
else:
|
| 82 |
+
result = ci.interrogate_fast(input_image)
|
| 83 |
|
| 84 |
+
print(f"mode {interrogation_mode}: {result}")
|
| 85 |
return result
|
| 86 |
|
| 87 |
|
| 88 |
title = """
|
| 89 |
+
# CLIP Interrogator 2.1
|
| 90 |
+
|
| 91 |
+
Want to figure out what a good prompt might be to create new images like an existing one?
|
| 92 |
+
The CLIP Interrogator is here to get you answers!
|
| 93 |
+
This version is specialized for Stable Diffusion 2.0 using the ViT-H-14 OpenCLIP model.
|
| 94 |
+
|
|
|
|
|
|
|
| 95 |
"""
|
| 96 |
|
| 97 |
article = """
|
| 98 |
+
|
| 99 |
+
Server busy? You can also run on [Google Colab](https://colab.research.google.com/github/pharmapsychotic/clip-interrogator/blob/open-clip/clip_interrogator.ipynb)
|
| 100 |
+
|
| 101 |
+
Follow Pharma on twitter [@pharmapsychotic](https://twitter.com/pharmapsychotic) and check out more tools at [Ai generative art tools list](https://pharmapsychotic.com/tools.html)
|
| 102 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
"""
|
| 104 |
|
| 105 |
css = """
|
|
|
|
| 108 |
margin-left: auto;
|
| 109 |
margin-right: auto;
|
| 110 |
}
|
|
|
|
| 111 |
a {
|
| 112 |
text-decoration-line: underline;
|
| 113 |
font-weight: 600;
|
| 114 |
}
|
| 115 |
"""
|
| 116 |
|
| 117 |
+
with gr.Blocks() as demo:
|
|
|
|
| 118 |
with gr.Column(elem_id="col-container"):
|
| 119 |
gr.HTML(title)
|
| 120 |
|
| 121 |
input_image = gr.Image(type="pil", elem_id="input-img")
|
|
|
|
| 122 |
with gr.Row():
|
| 123 |
mode_input = gr.Radio(
|
| 124 |
["best", "classic", "fast"],
|
| 125 |
label="Select mode",
|
| 126 |
value="best",
|
| 127 |
)
|
|
|
|
| 128 |
flavor_input = gr.Slider(
|
| 129 |
minimum=2,
|
| 130 |
maximum=24,
|
|
|
|
| 134 |
)
|
| 135 |
|
| 136 |
submit_btn = gr.Button("Submit")
|
|
|
|
| 137 |
output_text = gr.Textbox(
|
| 138 |
label="Description Output",
|
| 139 |
elem_id="output-txt",
|
|
|
|
| 143 |
["27E894C4-9375-48A1-A95D-CB2425416B4B.png", "best", 4],
|
| 144 |
["DB362F56-BA98-4CA1-A999-A25AA94B723B.png", "fast", 4],
|
| 145 |
]
|
|
|
|
| 146 |
gr.Examples(
|
| 147 |
examples=examples,
|
| 148 |
+
fn=generate_image_prompt,
|
| 149 |
inputs=[input_image, mode_input, flavor_input],
|
| 150 |
outputs=[output_text],
|
| 151 |
cache_examples=False,
|
|
|
|
| 154 |
|
| 155 |
gr.HTML(article)
|
| 156 |
|
| 157 |
+
submit_btn.click(
|
| 158 |
+
fn=generate_image_prompt,
|
| 159 |
+
inputs=[input_image, mode_input, flavor_input],
|
| 160 |
+
outputs=output_text,
|
| 161 |
+
api_name="clipi2",
|
| 162 |
+
)
|
|
|
|
| 163 |
|
| 164 |
demo.queue(max_size=32).launch(
|
| 165 |
footer_links=["api"],
|
| 166 |
ssr_mode=False,
|
| 167 |
mcp_server=True,
|
| 168 |
+
css=css,
|
| 169 |
)
|