Spaces:
Sleeping
Sleeping
Update app.py
Browse files6 unmodified lines789101112133 unmodified lines171819202122232425269 unmodified lines363738394041424344454647484950515253545514 unmodified lines7071727374757677787980818283848586878889909192939495969798996 unmodified linesfrom modules.similarity import compute_similarityfrom modules.pdf_report import create_pdf_reportdef 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)] * 63 unmodified linesurls = 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:9 unmodified linestop_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.nameyield 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)def load_example(prompt_val, img_path):return prompt_val, PILImage.open(img_path)with gr.Blocks(title="Influence Analyzer Demo") as demo:gr.Markdown("""### π Search-Based Influence Analyzer Demo14 unmodified linesnum_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"),]for prompt_text, image_path in examples:btn = gr.Button(prompt_text)# Closure workarounddef make_click_fn(p=prompt_text, i=image_path):return lambda: load_example(p, i)btn.click(fn=make_click_fn(),inputs=[],outputs=[prompt, gr.State()],).then(fn=lambda _, __: PILImage.open(image_path),inputs=[prompt, gr.State()],outputs=[uploaded_img := gr.Image(type="pil", visible=False)])with gr.Column():uploaded_img = gr.Image(type="pil", label="Upload your generated image")run_btn = gr.Button("π Run Search & Compare", variant="primary")gr.Markdown("---")retrieved_heading = gr.Markdown("### π Retrieved Images", visible=False)retrieved_gallery = gr.Gallery(label="", columns=5, height="auto", visible=False)6 unmodified lines789101112133 unmodified lines1718192021222324252627289 unmodified lines38394041424344454647484950515253545514 unmodified lines7071727374757677787980818283848586878889909192939495966 unmodified linesfrom modules.similarity import compute_similarityfrom modules.pdf_report import create_pdf_report# --- Analysis Function ---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)] * 63 unmodified linesurls = search_images(prompt, max_results=num_results)retrieved_images = [(url, "") for url in urls]# Step 1: Show retrieved galleryyield 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)# Step 2: Embedding + Similarityuser_emb = get_embedding(user_img)results = []for url in urls:9 unmodified linestop_results = results[:5]top_images = [(url, f"Similarity: {sim:.3f}") for url, sim in top_results]# Step 3: Generate PDFpdf_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# Step 4: Show everythingyield 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)# --- UI Layout ---with gr.Blocks(title="Influence Analyzer Demo") as demo:gr.Markdown("""### π Search-Based Influence Analyzer Demo14 unmodified linesnum_results = gr.Slider(5, 50, value=30, step=1, label="Images to retrieve")gr.Markdown("**βοΈ Try an example**")uploaded_img = gr.Image(type="pil", label="Upload your generated image")examples = [("a horse in the moon", "examples/moon_horse.png"),("a greek philosopher drinking beer", "examples/greek_beer.png"),]for text, path in examples:def make_fn(prompt_val=text, image_path=path):def fn():return prompt_val, PILImage.open(image_path)return fngr.Button(text).click(fn=make_fn(), inputs=[], outputs=[prompt, uploaded_img])with gr.Column():pass # uploaded_img already placed above for alignmentrun_btn = gr.Button("π Run Search & Compare", variant="primary")# --- Results Section ---gr.Markdown("---")retrieved_heading = gr.Markdown("### π Retrieved Images", visible=False)retrieved_gallery = gr.Gallery(label="", columns=5, height="auto", visible=False)
app.py+22-25