Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from modules.embeddings import get_embedding, load_image_from_url
|
|
| 7 |
from modules.similarity import compute_similarity
|
| 8 |
from modules.pdf_report import create_pdf_report
|
| 9 |
|
| 10 |
-
|
| 11 |
def analyze_image_stream(prompt, uploaded_img, num_results):
|
| 12 |
if not uploaded_img or not prompt:
|
| 13 |
yield None, None, None, "β Please provide both a prompt and an image.", *[gr.update(visible=False)] * 6
|
|
@@ -17,12 +17,10 @@ def analyze_image_stream(prompt, uploaded_img, num_results):
|
|
| 17 |
urls = search_images(prompt, max_results=num_results)
|
| 18 |
retrieved_images = [(url, "") for url in urls]
|
| 19 |
|
| 20 |
-
# Step 1: Show retrieved gallery
|
| 21 |
yield retrieved_images, None, None, "π Computing similarity...", \
|
| 22 |
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), \
|
| 23 |
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
| 24 |
|
| 25 |
-
# Step 2: Embedding + Similarity
|
| 26 |
user_emb = get_embedding(user_img)
|
| 27 |
results = []
|
| 28 |
for url in urls:
|
|
@@ -38,18 +36,16 @@ def analyze_image_stream(prompt, uploaded_img, num_results):
|
|
| 38 |
top_results = results[:5]
|
| 39 |
top_images = [(url, f"Similarity: {sim:.3f}") for url, sim in top_results]
|
| 40 |
|
| 41 |
-
# Step 3: Generate PDF
|
| 42 |
pdf_bytes = create_pdf_report(prompt, user_img, top_results)
|
| 43 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as f:
|
| 44 |
f.write(pdf_bytes)
|
| 45 |
pdf_path = f.name
|
| 46 |
|
| 47 |
-
# Step 4: Show everything
|
| 48 |
yield retrieved_images, top_images, pdf_path, "β
Done!", \
|
| 49 |
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), \
|
| 50 |
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
| 51 |
|
| 52 |
-
|
| 53 |
with gr.Blocks(title="Influence Analyzer Demo") as demo:
|
| 54 |
gr.Markdown("""
|
| 55 |
### π Search-Based Influence Analyzer Demo
|
|
@@ -70,27 +66,29 @@ The goal is to provide a proxy for understanding which real-world images may hav
|
|
| 70 |
num_results = gr.Slider(5, 50, value=30, step=1, label="Images to retrieve")
|
| 71 |
|
| 72 |
gr.Markdown("**βοΈ Try an example**")
|
| 73 |
-
uploaded_img = gr.Image(type="pil", label="Upload your generated image")
|
| 74 |
-
|
| 75 |
examples = [
|
| 76 |
("a horse in the moon", "examples/moon_horse.png"),
|
| 77 |
("a greek philosopher drinking beer", "examples/greek_beer.png"),
|
| 78 |
]
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
return fn
|
| 85 |
-
|
| 86 |
-
gr.Button(text).click(fn=make_fn(), inputs=[], outputs=[prompt, uploaded_img])
|
| 87 |
|
| 88 |
with gr.Column():
|
| 89 |
-
|
| 90 |
|
| 91 |
run_btn = gr.Button("π Run Search & Compare", variant="primary")
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
gr.Markdown("---")
|
| 95 |
retrieved_heading = gr.Markdown("### π Retrieved Images", visible=False)
|
| 96 |
retrieved_gallery = gr.Gallery(label="", columns=5, height="auto", visible=False)
|
|
|
|
| 7 |
from modules.similarity import compute_similarity
|
| 8 |
from modules.pdf_report import create_pdf_report
|
| 9 |
|
| 10 |
+
|
| 11 |
def analyze_image_stream(prompt, uploaded_img, num_results):
|
| 12 |
if not uploaded_img or not prompt:
|
| 13 |
yield None, None, None, "β Please provide both a prompt and an image.", *[gr.update(visible=False)] * 6
|
|
|
|
| 17 |
urls = search_images(prompt, max_results=num_results)
|
| 18 |
retrieved_images = [(url, "") for url in urls]
|
| 19 |
|
|
|
|
| 20 |
yield retrieved_images, None, None, "π Computing similarity...", \
|
| 21 |
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), \
|
| 22 |
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
| 23 |
|
|
|
|
| 24 |
user_emb = get_embedding(user_img)
|
| 25 |
results = []
|
| 26 |
for url in urls:
|
|
|
|
| 36 |
top_results = results[:5]
|
| 37 |
top_images = [(url, f"Similarity: {sim:.3f}") for url, sim in top_results]
|
| 38 |
|
|
|
|
| 39 |
pdf_bytes = create_pdf_report(prompt, user_img, top_results)
|
| 40 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as f:
|
| 41 |
f.write(pdf_bytes)
|
| 42 |
pdf_path = f.name
|
| 43 |
|
|
|
|
| 44 |
yield retrieved_images, top_images, pdf_path, "β
Done!", \
|
| 45 |
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), \
|
| 46 |
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
| 47 |
|
| 48 |
+
|
| 49 |
with gr.Blocks(title="Influence Analyzer Demo") as demo:
|
| 50 |
gr.Markdown("""
|
| 51 |
### π Search-Based Influence Analyzer Demo
|
|
|
|
| 66 |
num_results = gr.Slider(5, 50, value=30, step=1, label="Images to retrieve")
|
| 67 |
|
| 68 |
gr.Markdown("**βοΈ Try an example**")
|
|
|
|
|
|
|
| 69 |
examples = [
|
| 70 |
("a horse in the moon", "examples/moon_horse.png"),
|
| 71 |
("a greek philosopher drinking beer", "examples/greek_beer.png"),
|
| 72 |
]
|
| 73 |
|
| 74 |
+
example_btns = []
|
| 75 |
+
for i, (text, path) in enumerate(examples):
|
| 76 |
+
btn = gr.Button(text)
|
| 77 |
+
example_btns.append((btn, text, path))
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
with gr.Column():
|
| 80 |
+
uploaded_img = gr.Image(type="pil", label="Upload your generated image")
|
| 81 |
|
| 82 |
run_btn = gr.Button("π Run Search & Compare", variant="primary")
|
| 83 |
|
| 84 |
+
for btn, prompt_text, image_path in example_btns:
|
| 85 |
+
def make_loader(p=prompt_text, img=image_path):
|
| 86 |
+
def _load():
|
| 87 |
+
return p, PILImage.open(img)
|
| 88 |
+
return _load
|
| 89 |
+
btn.click(fn=make_loader(), inputs=[], outputs=[prompt, uploaded_img])
|
| 90 |
+
|
| 91 |
+
# Results
|
| 92 |
gr.Markdown("---")
|
| 93 |
retrieved_heading = gr.Markdown("### π Retrieved Images", visible=False)
|
| 94 |
retrieved_gallery = gr.Gallery(label="", columns=5, height="auto", visible=False)
|