Spaces:
Sleeping
Sleeping
File size: 5,045 Bytes
e8994f0 12ec423 e8994f0 e8de545 e8994f0 02e0d06 e8994f0 02e0d06 fe957af 12ec423 829a688 e8994f0 fe957af e8994f0 e8de545 02e0d06 12ec423 e8994f0 e8de545 829a688 e8994f0 12ec423 e8994f0 829a688 | 1 2 3 4 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | import gradio as gr
from PIL import Image as PILImage
import tempfile
from modules.ddg_search import search_images
from modules.embeddings import get_embedding, load_image_from_url
from modules.similarity import compute_similarity
from modules.pdf_report import create_pdf_report
def analyze_image_stream(prompt, uploaded_img, num_results):
if not uploaded_img or not prompt:
yield None, None, None, "β Please provide both a prompt and an image.", *[gr.update(visible=False)] * 6
return
user_img = uploaded_img.convert("RGB")
urls = search_images(prompt, max_results=num_results)
retrieved_images = [(url, "") for url in urls]
yield retrieved_images, None, None, "π Computing similarity...", \
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), \
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
user_emb = get_embedding(user_img)
results = []
for url in urls:
try:
img = load_image_from_url(url)
emb = get_embedding(img)
sim = compute_similarity(user_emb, emb)
results.append((url, sim))
except:
continue
results = sorted(results, key=lambda x: x[1], reverse=True)
top_results = results[:5]
top_images = [(url, f"Similarity: {sim:.3f}") for url, sim in top_results]
pdf_bytes = create_pdf_report(prompt, user_img, top_results)
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as f:
f.write(pdf_bytes)
pdf_path = f.name
yield retrieved_images, top_images, pdf_path, "β
Done!", \
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), \
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
with gr.Blocks(title="Influence Analyzer Demo", css="""
#uploaded_image img {
object-fit: contain;
height: 250px;
width: auto;
border: 1px solid #ccc;
margin-top: 5px;
}
""") as demo:
gr.Markdown("""
### π Search-Based Influence Analyzer Demo
This tool supports the **interpretability of generative black-box models** by identifying potentially influential training data.
**How it works:**
- π Retrieves public images using DuckDuckGo based on your prompt.
- π§ Computes embeddings and compares them to your uploaded image to find the most similar results.
The goal is to provide a proxy for understanding which real-world images may have contributed to a generation β useful for **interpretability**, **data attribution**, and **copyright assessment**.
""")
gr.Markdown("### π Provide Your Prompt and Image")
with gr.Row():
with gr.Column():
prompt = gr.Textbox(label="Prompt", placeholder="Describe your image prompt...", lines=1)
num_results = gr.Slider(5, 50, value=30, step=1, label="Images to retrieve")
gr.Markdown("**βοΈ Try an example**")
examples = [
("a horse in the moon", "examples/moon_horse.png"),
("a greek philosopher drinking beer", "examples/greek_beer.png"),
]
example_btns = []
for i, (text, path) in enumerate(examples):
btn = gr.Button(text)
example_btns.append((btn, text, path))
with gr.Column():
uploaded_img = gr.Image(type="pil", label="Upload your generated image", elem_id="uploaded_image")
run_btn = gr.Button("π Run Search & Compare", variant="primary")
for btn, prompt_text, image_path in example_btns:
def make_loader(p=prompt_text, img=image_path):
def _load():
return p, PILImage.open(img)
return _load
btn.click(fn=make_loader(), inputs=[], outputs=[prompt, uploaded_img])
gr.Markdown("---")
retrieved_heading = gr.Markdown("### π Retrieved Images", visible=False)
retrieved_gallery = gr.Gallery(label="", columns=5, height="auto", visible=False)
similar_heading = gr.Markdown("### β
Top 5 Most Similar Images", visible=False)
similar_gallery = gr.Gallery(label="", columns=5, height="auto", visible=False)
output_heading = gr.Markdown("### π Output Report", visible=False)
pdf_out = gr.File(label="Download Similarity Report", visible=False)
status = gr.Textbox(label="Status", interactive=False)
run_btn.click(
fn=analyze_image_stream,
inputs=[prompt, uploaded_img, num_results],
outputs=[
retrieved_gallery,
similar_gallery,
pdf_out,
status,
retrieved_heading,
similar_heading,
output_heading,
retrieved_gallery,
similar_gallery,
pdf_out,
],
)
gr.Markdown("---")
gr.Markdown(
"π§ *This demo is part of ongoing research in interpretability for generative models. "
"If you're interested in collaboration, feel free to get in touch.*"
)
demo.launch()
|