Spaces:
Build error
Build error
Update gradio_ui.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import gradio as grfrom PIL import Imagefrom backend import query_pinecone_with_filters# Gradio Query Handlerdef search_pinecone(text_query, image_query, mode):"""Handle search queries for text-to-image or image-to-image using Gradio UI.Args:text_query: Text string for text-to-image search.image_query: File path to the uploaded image for image-to-image search.mode: 'text-to-image' or 'image-to-image'.Returns:List of images similar to the query and their captions."""filter_metadata = {"type": "image"} # Only return image resultsif mode == "text-to-image":if not text_query:return [], "Error: Text query cannot be empty for text-to-image mode."# Handle text queryresults = query_pinecone_with_filters(query=text_query, mode="text-to-image", top_k=5, filter_metadata=filter_metadata)elif mode == "image-to-image":if not image_query:return [], "Error: Please upload an image for image-to-image mode."# Handle image queryquery_image = Image.open(image_query).convert("RGB") # Convert uploaded imageresults = query_pinecone_with_filters(query=image_query, mode="image-to-image", top_k=5, filter_metadata=filter_metadata)else:return [], "Error: Invalid mode selected."# Extract images and their captionsimages = []captions = []for match in results:image_path = match["metadata"]["image_path"]caption = ", ".join(match["metadata"].get("captions", []))images.append(image_path)captions.append(f"Score: {match['score']}\nCaption: {caption}")return images, "\n\n".join(captions)# Gradio Interfacewith gr.Blocks() as demo:gr.Markdown("## Multimodal Search Engine with CLIP")with gr.Row():mode = gr.Radio(choices=["text-to-image", "image-to-image"], value="text-to-image", label="Search Mode")with gr.Row():text_query = gr.Textbox(label="Enter Text Query", visible=True)image_query = gr.Image(type="filepath", label="Upload an Image Query", visible=False)with gr.Row():search_btn = gr.Button("Search")with gr.Row():result_gallery = gr.Gallery(label="Search Results", show_label=True, elem_id="result-gallery")with gr.Row():captions_box = gr.Textbox(label="Result Captions", lines=10, interactive=False)# Handle mode change to toggle visibilitydef toggle_inputs(mode):if mode == "text-to-image":return gr.update(visible=True), gr.update(visible=False)elif mode == "image-to-image":return gr.update(visible=False), gr.update(visible=True)mode.change(toggle_inputs, inputs=mode, outputs=[text_query, image_query])# Search on button clicksearch_btn.click(search_pinecone, inputs=[text_query, image_query, mode],outputs=[result_gallery, captions_box])if __name__ == "__main__":demo.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import gradio as grfrom PIL import Imagefrom backend import query_pinecone_with_filters# Gradio Query Handlerdef search_pinecone(text_query, image_query, mode):"""Handle search queries for text-to-image or image-to-image using Gradio UI.Args:text_query: Text string for text-to-image search.image_query: File path to the uploaded image for image-to-image search.mode: 'text-to-image' or 'image-to-image'.Returns:List of images similar to the query and their captions."""filter_metadata = {"type": "image"} # Only return image resultsif mode == "text-to-image":if not text_query:return [], "Error: Text query cannot be empty for text-to-image mode."# Handle text queryresults = query_pinecone_with_filters(query=text_query, mode="text-to-image", top_k=5, filter_metadata=filter_metadata)elif mode == "image-to-image":if not image_query:return [], "Error: Please upload an image for image-to-image mode."# Handle image queryquery_image = Image.open(image_query).convert("RGB") # Convert uploaded imageresults = query_pinecone_with_filters(query=image_query, mode="image-to-image", top_k=5, filter_metadata=filter_metadata)else:return [], "Error: Invalid mode selected."# Extract images and their captionsimages = []captions = []for match in results:image_path = match["metadata"]["image_path"]caption = ", ".join(match["metadata"].get("captions", []))images.append(image_path)captions.append(f"Score: {match['score']}\nCaption: {caption}")return images, "\n\n".join(captions)# Gradio Interfacewith gr.Blocks() as demo:gr.Markdown("## Multimodal Search Engine with CLIP")with gr.Row():mode = gr.Radio(choices=["text-to-image", "image-to-image"], value="text-to-image", label="Search Mode")with gr.Row():text_query = gr.Textbox(label="Enter Text Query", visible=True)image_query = gr.Image(type="filepath", label="Upload an Image Query", visible=False)with gr.Row():search_btn = gr.Button("Search")with gr.Row():result_gallery = gr.Gallery(label="Search Results", show_label=True, elem_id="result-gallery")with gr.Row():captions_box = gr.Textbox(label="Result Captions", lines=10, interactive=False)# Handle mode change to toggle visibilitydef toggle_inputs(mode):if mode == "text-to-image":return gr.update(visible=True), gr.update(visible=False)elif mode == "image-to-image":return gr.update(visible=False), gr.update(visible=True)mode.change(toggle_inputs, inputs=mode, outputs=[text_query, image_query])# Search on button clicksearch_btn.click(search_pinecone, inputs=[text_query, image_query, mode],outputs=[result_gallery, captions_box])if __name__ == "__main__":demo.launch(share=True)
gradio_ui.py+77-77