import gradio as gr from datasets import load_dataset from sentence_transformers import SentenceTransformer import os import requests os.environ['NO_PROXY'] = 'huggingface.co' model = SentenceTransformer('clip-ViT-B-32') # Candidate images. dataset = load_dataset("sasha/pedro-embeddings") ds = dataset["train"] ds.add_faiss_index(column='embeddings') def query(image, number_to_retrieve=1): prompt = model.encode(image) scores, retrieved_examples = ds.get_nearest_examples('embeddings', image, k=number_to_retrieve) return retrieved_examples with gr.Blocks() as demo: gr.Markdown("# Find my Pedro Pascal") gr.Markdown("## Use this Space to find the Pedro Pascal most similar to your input image!") with gr.Row(): with gr.Column(min_width= 900): inputs = gr.Image(shape=(800, 1600)) btn = gr.Button("Find my Pedro!") description = gr.Markdown() with gr.Column(): outputs=gr.Gallery().style(grid=[1], height="auto") gr.Markdown("### Image Examples") gr.Examples( examples=["elton.jpg", "ken.jpg", "gaga.jpg", "taylor.jpg"], inputs=inputs, outputs=[outputs], fn=query, cache_examples=True, ) btn.click(query, inputs, [outputs]) demo.launch()