File size: 1,513 Bytes
a86cf7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# This is a Gradio app for similarity image search.
# The app takes a text input and returns the top_k images based on similarity.

import gradio as gr
import numpy as np
import pandas as pd

# Dummy function to simulate image search based on text input
def similarity_image_search(text, top_k):
    # Simulate a database of images with their similarity scores
    images = [
        ("image1.jpg", 0.95),
        ("image2.jpg", 0.85),
        ("image3.jpg", 0.75),
        ("image4.jpg", 0.65),
        ("image5.jpg", 0.55),
        ("image6.jpg", 0.45),
        ("image7.jpg", 0.35),
        ("image8.jpg", 0.25),
        ("image9.jpg", 0.15),
        ("image10.jpg", 0.05),
    ]
    
    # Sort images by similarity score in descending order
    images.sort(key=lambda x: x[1], reverse=True)
    
    # Return the top_k images
    return [img[0] for img in images[:top_k]]

# Create a Gradio interface
with gr.Blocks() as demo:
    # Text input for the search query
    text_input = gr.Textbox(label="Search Query")
    
    # Slider to select the number of top images to display
    top_k_slider = gr.Slider(1, 10, value=5, label="Number of Top Images")
    
    # Gallery to display the top_k images
    image_gallery = gr.Gallery(label="Top Images", columns=3)
    
    # Define the event listener for the text input
    text_input.submit(similarity_image_search, inputs=[text_input, top_k_slider], outputs=image_gallery)

# Launch the Gradio app
if __name__ == "__main__":
    demo.launch(show_error=True)